java.lang.IllegalArgumentException: Can not set java.lang.String field xxx to java.lang.Class

IllegalArgumentException是设置错了,field.get()参数需要的是该类对象,而不是该类的class对象

        //Student student = new Student();
        Class<Student> cls = Student.class;
        Field hhh = cls.getField("hhh");
        System.out.println(hhh.get(cls));
        //System.out.println(hhh.get(student));
    protected void ensureObj(Object o) {
        // NOTE: will throw NullPointerException, as specified, if o is null
        if (!field.getDeclaringClass().isAssignableFrom(o.getClass())) {
            throwSetIllegalArgumentException(o);
        }
    }

你可能感兴趣的:(Java,java,class)