反射操作对象和属性
eg:
class Person{
private Integer age;
public String name;
protected String addr;
Boolean is18;
}
class Student extends Person{
private Integer claaNum;
public String sex;
}
1.获取Class对象
//获取Class对象的三种方法
Class<?> clazz = Student.class;
Class<?> clazz = new Student().getClass();
Class<?> clazz = Class.forName("com.git.Student");
2.字段的操作
1.获取字段的四种方法源码(jdk1.8)
/**
* Returns an array containing {@code Field} objects reflecting all
* the accessible public fields of the class or interface represented by
* 返回对象的所有公开成员,包括该类和该类继承的成员
*/
@CallerSensitive
public Field[] getFields() throws SecurityException {
//Member.PUBLIC,该类或该接口的所有公共成员的集合,包括继承的成员
checkMemberAccess(Member.PUBLIC, Reflection.getCallerClass(), true);//这句代码表名了只能返回自身和继承类的共有属性成员
return copyFields(privateGetPublicFields(null));
}
/**
*Returns an array of {@code Field} objects reflecting all the fields
* declared by the class or interface represented by this
* {@code Class} object. This includes public, protected, default
* (package) access, and private fields, but excludes inherited fields.
* 返回该类自身的所有成员集,包括public,protected,default,private 但是不包括继承的
* /
@CallerSensitive
public Field[] getDeclaredFields() throws SecurityException {
//Member.DECLARED,该类或者该接口自己的成员集。不包括继承的成员。
checkMemberAccess(Member.DECLARED, Reflection.getCallerClass(), true);
return copyFields(privateGetDeclaredFields(false));
}
/**
* Returns a {@code Field} object that reflects the specified public member
* field of the class or interface represented by this {@code Class}
* object. The {@code name} parameter is a {@code String} specifying the
* simple name of the desired field.
* 返回该对象代表的类或者接口的公共成员的Field对象
* 参数 字段名
*/
@CallerSensitive
public Field getField(String name)
throws NoSuchFieldException, SecurityException {
checkMemberAccess(Member.PUBLIC, Reflection.getCallerClass(), true);
Field field = getField0(name);
if (field == null) {
throw new NoSuchFieldException(name);
}
return field;
}
/* * Returns a {@code Field} object that reflects the specified declared
* field of the class or interface represented by this {@code Class}
* object. The {@code name} parameter is a {@code String} that specifies
* the simple name of the desired field.
* 返回自定类或者接口的字段(Field)对象
* 参数 字段名,
*
* /
@CallerSensitive
public Field getDeclaredField(String name)
throws NoSuchFieldException, SecurityException {
checkMemberAccess(Member.DECLARED, Reflection.getCallerClass(), true);
Field field = searchFields(privateGetDeclaredFields(false), name);
if (field == null) {
throw new NoSuchFieldException(name);
}
return field;
}
测试一:
public static void main(String[] args) {
Class<?> clazz = Student.class;
Field[] fields1 = clazz.getDeclaredFields();//本类的所有属性
Field[] fields2 = clazz.getFields();//本类或者父类的共有属性
Arrays.stream(fields1).forEach(System.out::println);
System.out.println("========================分割线========================");
Arrays.stream(fields2).forEach(System.out::println);
}
//结果
private java.lang.Integer com.qdz.qdzmart.framework.utils.Student.claaNum
public java.lang.String com.qdz.qdzmart.framework.utils.Student.sex
========================分割线========================
public java.lang.String com.qdz.qdzmart.framework.utils.Student.sex
public java.lang.String com.qdz.qdzmart.framework.utils.Person.name
测试二:
Field field1 = clazz.getField("name");//可以获取
Field field7 = clazz.getField("sex");//可以获取
Field field3 = clazz.getField("claaNum");//报错
Field field4 = clazz.getField("age");//报错
Field field5 = clazz.getField("addr");//报错
Field field6 = clazz.getField("is18");//报错
测试三:
Field field7 = clazz.getDeclaredField("sex");//可以获取
Field field3 = clazz.getDeclaredField("claaNum");//可以获取
Field field1 = clazz.getDeclaredField("name");//报错
Field field4 = clazz.getDeclaredField("age");//报错
Field field5 = clazz.getDeclaredField("addr");//报错
Field field6 = clazz.getDeclaredField("is18");//报错
以上四种方法用于数组等时需要注意下。
2.给成员赋值
这里有点坑