反射机制
一、什么是反射Reflect
- 反射是在运行时动态访问类和对象的技术
- 反射是JDK1.2版本后的高级特性,隶属于java.lang.reflect
- 大多数java框架都基于反射实现参数配置,动态注入等特性
二、反射的核心类
2.1 Class类
- class是JVM中代表“类和接口"的类
- class对象具体包含了某个特定类的结构信息
- class核心方法
方法 | 用途 |
---|
Class.forName() | 静态方法,用于获取指定Class对象 |
classObj.newInstance() | 通过默认构造方法创建新的对象 |
classObj.getConstructor() | 获得指定的public修饰构造方法Constructor对象 |
classObj.getMethod() | 获得指定的public修饰方法Method对象 |
classObj.getField() | 获取指定的public修饰成员变量Field对象 |
2.2 Constuctor构造方法类
@Test
public void test01(){
try {
Class employeeClass = Class.forName("com.futureport.reflect.entity.Employee");
Constructor constructor =employeeClass.getConstructor(new Class[]{
int.class,String.class,float.class,String.class
});
Employee employee = (Employee) constructor.newInstance(new Object[]{
100,"李磊",3000f,"研发部"
});
} catch (ClassNotFoundException | NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
2.3 Method方法类
方法 | 用途 |
---|
classObj.getMethod() | 获取指定public修饰的方法对象 |
methodObj.invoke() | 调用指定对象的对应方法 |
@Test
public void methodClass(){
try {
Class employeeClass = Class.forName("com.futureport.reflect.entity.Employee");
Constructor constructor = employeeClass.getConstructor(new Class[]{
int.class,String.class,float.class,String.class
});
Employee employee = (Employee) constructor.newInstance(new Object[]{
100,"李磊",30000f,"开发部"
});
Method updateSalary = employeeClass.getMethod("updateSalary", float.class);
Employee employee1 = (Employee) updateSalary.invoke(employee,new Object[]{
1000f
});
} catch (ClassNotFoundException | NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
2.4 Field成员变量类
方法 | 用途 |
---|
classObj.getField() | 获取指定public修饰的成员变量对象 |
fieldObj.set() | 为某对象指定成员变量赋值 |
fieldObj.get() | 获取某对象指定成员变量数值 |
@Test
public void field(){
try {
Class employeeClass =Class.forName("com.futureport.reflect.entity.Employee");
Constructor employeeClassConstructor = employeeClass.getConstructor(new Class[]{
int.class,String.class,float.class,String.class
});
Employee employee = (Employee) employeeClassConstructor.newInstance(new Object[]{
100,"李磊",30000f,"开发部"
});
Field employeeClassField = employeeClass.getField("ename");
employeeClassField.set(employee,"李雷");
String ename = (String) employeeClassField.get(employee);
System.out.println(ename);
} catch (ClassNotFoundException | NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
}
三、getDeclared系列方法
- getDeclaredConstructor(s)|Method(s)|Field(s)获取对应对象
- getConstructor(s)|Method(s)|Field(s)只能获取public对象
- 访问非作用域内构造方法,方法,成员变量会抛出异常