1.反射相关概念

2.反射的API

java 反射方法(java反射原理)(1)

3.反射的使用

  1. 获取Class对象

// 1.通过字符串获取Class对象,这个字符串必须带上完整路径名Class studentClass = Class.forName("com.test.reflection.Student");// 2.通过类的class属性Class studentClass2 = Student.class;// 3.通过对象的getClass()函数Student studentObject = new Student();Class studentClass3 = studentObject.getClass();System.out.println("class1 = " studentClass "\n" "class2 = " studentClass2 "\n" "class3 = " studentClass3 "\n" "class1 == class2 ? " (studentClass == studentClass2) "\n" "class2 == class3 ? " (studentClass2 == studentClass3));class1 = class com.test.reflection.Studentclass2 = class com.test.reflection.Studentclass3 = class com.test.reflection.Studentclass1 == class2 ? trueclass2 == class3 ? true

  1. 获取成员变量

// 1.获取所有声明的字段Field[] declaredFieldList = studentClass.getDeclaredFields();for (Field declaredField : declaredFieldList) { System.out.println("declared Field: " declaredField);}// 2.获取所有公有的字段Field[] fieldList = studentClass.getFields();for (Field field : fieldList) { System.out.println("field: " field);}declared Field: private java.lang.String com.test.reflection.Student.studentNamedeclared Field: public int com.test.reflection.Student.studentAgefield: public int com.test.reflection.Student.studentAge

  1. 获取泛型类型

Field field=MyClass.class.getField("stringList");Type genericsFieldType=field.getGenericType();if(genericsFieldType instanceof ParameterizedType){ ParameterizedType parameterizedType=(ParameterizedType) genericsFieldType; Type[] fieldArgTypes=parameterizedType.getActualTypeArguments(); for (Type fieldArgType:fieldArgTypes){ Class fieldArgClass=(Class) fieldArgType; System.out.println("泛型字段的类型:" fieldArgClass); }}

Method method=MyClass.class.getMethod("setList",List.class);Type[] genericParameterTypes=method.getGenericParameterTypes();for (Type genericType:genericParameterTypes){ if(genericType instanceof ParameterizedType){ ParameterizedType parameterizedType=(ParameterizedType)genericType; Type[] types= parameterizedType.getActualTypeArguments(); for (Type type:types){ Class realType=(Class) type; System.out.println("方法参数的类型:" realType); } }}

Method method=MyClass.class.getMethod("getStringList",null);System.out.println(method.getReturnType());Type retrunType=method.getGenericReturnType();System.out.println(retrunType);if(retrunType instanceof ParameterizedType){ ParameterizedType type=(ParameterizedType)retrunType; Type[] typeArguments=type.getActualTypeArguments(); for(Type typeArgument:typeArguments){ Class typeArgClass=(Class)typeArgument; System.out.println("泛型类型:" typeArgClass); }}

  1. 获取构造方法

// 1.获取所有声明的构造方法Constructor[] declaredConstructorList = studentClass.getDeclaredConstructors();for (Constructor declaredConstructor : declaredConstructorList) { System.out.println("declared Constructor: " declaredConstructor);}// 2.获取所有公有的构造方法Constructor[] constructorList = studentClass.getConstructors();for (Constructor constructor : constructorList) { System.out.println("constructor: " constructor);}declared Constructor: public com.test.reflection.Student()declared Constructor: private com.test.reflection.Student(java.lang.String)constructor: public com.test.reflection.Student()

  1. 获取非构造方法

// 1.获取所有声明的函数Method[] declaredMethodList = studentClass.getDeclaredMethods();for (Method declaredMethod : declaredMethodList) { System.out.println("declared Method: " declaredMethod);}// 2.获取所有公有的函数Method[] methodList = studentClass.getMethods();for (Method method : methodList) { System.out.println("method: " method);}declared Method: public void com.test.reflection.Student.setStudentAge(int)declared Method: private java.lang.String com.test.reflection.Student.show(java.lang.String)method: public void com.test.reflection.Student.setStudentAge(int)method: public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedExceptionmethod: public final native void java.lang.Object.wait(long) throws java.lang.InterruptedExceptionmethod: public final void java.lang.Object.wait() throws java.lang.InterruptedExceptionmethod: public boolean java.lang.Object.equals(java.lang.Object)method: public java.lang.String java.lang.Object.toString()method: public native int java.lang.Object.hashCode()method: public final native java.lang.Class java.lang.Object.getClass()method: public final native void java.lang.Object.notify()method: public final native void java.lang.Object.notifyAll()

  1. 示例

// 1.通过字符串获取Class对象,这个字符串必须带上完整路径名Class studentClass = Class.forName("com.test.reflection.Student");// 2.获取声明的构造方法,传入所需参数的类名,如果有多个参数,用','连接即可Constructor studentConstructor = studentClass.getDeclaredConstructor(String.class);// 如果是私有的构造方法,需要调用下面这一行代码使其可使用,公有的构造方法则不需要下面这一行代码studentConstructor.setAccessible(true);// 使用构造方法的newInstance方法创建对象,传入构造方法所需参数,如果有多个参数,用','连接即可Object student = studentConstructor.newInstance("NameA");// 3.获取声明的字段,传入字段名Field studentAgeField = studentClass.getDeclaredField("studentAge");// 如果是私有的字段,需要调用下面这一行代码使其可使用,公有的字段则不需要下面这一行代码// studentAgeField.setAccessible(true);// 使用字段的set方法设置字段值,传入此对象以及参数值studentAgeField.set(student,10);// 4.获取声明的函数,传入所需参数的类名,如果有多个参数,用','连接即可Method studentShowMethod = studentClass.getDeclaredMethod("show",String.class);// 如果是私有的函数,需要调用下面这一行代码使其可使用,公有的函数则不需要下面这一行代码studentShowMethod.setAccessible(true);// 使用函数的invoke方法调用此函数,传入此对象以及函数所需参数,如果有多个参数,用','连接即可。函数会返回一个Object对象,使用强制类型转换转成实际类型即可Object result = studentShowMethod.invoke(student,"message");

4.反射机制原理

  1. 总体流程
  1. 通过Class获取Field/Method/Constructor

java 反射方法(java反射原理)(2)

java 反射方法(java反射原理)(3)

java 反射方法(java反射原理)(4)

,