Java获取运行时类的完整结构
1.通过反射获得运行时类的完整结构
Field,Method,Constructor,Superclass,Interface,Annotation
- 实现的全部接口
- 所继承的父类
- 全部的构造器
- 全部的方法
- 全部的Field
- 注解
- …
2.有了Class对象,能做什么?
1.创建类的对象:调用Class对象的newInstance()方法
- 类必须有一个无参数的构造器
- 类的构造器的访问权限需要足够
2.没有无参构造器如何创建对象
只要在操作的时候明确的调用类的构造器,并将参数传递进去之后,才可以实例化造作
- 通过class类的getDeclaredConstructor(class … parameterTypes)拿到本类指定的形参类型的构造器
- 向构造器的形参中传递一个对象数组进去, 里面包含了构造器中所需的各种参数
- 通过Constructor实例化对象
3.调用指定的方法
通过反射,调用类的方法,通过Method类完成
- 通过class类的getMethod(String name,Class…parameterTypes)方法取得一个Method对象,并设置此方法操作时所需要的参数类型
- 之后使用Object invoke(Object obj, Object[] args)进行调用,并向方法中传递要设置的obj对象的参数信息
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
//动态的创建对象,通过反射
public class Test09 {
public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {
//获得class对象
Class c1 = Class.forName("User");
//构造一个对象
User user = (User) c1.newInstance(); //本质时调用了类的无参构造对象
System.out.println(user);
//通过构造器创建对象
Constructor constructor = c1.getDeclaredConstructor(String.class, int.class, int.class);
User user2 = (User)constructor.newInstance("michael", 001, 18);
System.out.println(user2);
//通过反射调用普通方法
User user3 = (User)c1.newInstance();
//通过反射获取一个方法
Method setName = c1.getDeclaredMethod("setName", String.class);
//invoke:激活方法
//(对象,方法的值)
setName.invoke(user3, "michael09");
System.out.println(user3.getName());
//通过反射操作属性
User user4 = (User)c1.newInstance();
Field name = c1.getDeclaredField("name");
//不能直接操作私有属性,我们需要关闭程序的安全检测,属性或者方法的setAccessible(true)
name.setAccessible(true);
name.set(user4, "michael07");
System.out.println(user4.getName());
}
}
4.setAccessible使用
- Method和Field,Constructor对象都有setAccessible()方法
- setAccessible作用是启动和禁用访问安全检查的开关
- 参数值为true则指示反射的对象在使用时应该取消java语言访问检查
a.提高反射效率。如果代码中必须用反射,而该句代码需要频繁的被调用,请设置为true
b.使用原本无法访问的private成员也可以访问 - 参数值为false则指示反射的对象应该实施jaav语言访问检查
5.类的调用方式性能分析
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
//分析性能问题
public class Test10 {
//普通调用方式
public static void test01(){
User user = new User();
long startTime = System.currentTimeMillis();
for (int i = 0; i < 100000000; i++) {
user.getName();
}
long endTime = System.currentTimeMillis();
System.out.println("普通方式执行1亿次耗时:" + (endTime - startTime) + "ms");
}
//反射调用方式
public static void test02() throws NoSuchMethodException, InvocationTargetException,IllegalAccessException {
User user1 = new User();
Class c1 = user1.getClass();
Method getName = c1.getDeclaredMethod("getName", null);
long startTime = System.currentTimeMillis();
for (int i = 0; i < 100000000; i++) {
getName.invoke(user1,null);
}
long endTime = System.currentTimeMillis();
System.out.println("反射方式执行1亿次耗时:" + (endTime - startTime) + "ms");
}
//反射调用方式 关闭检测
public static void test03() throws NoSuchMethodException, InvocationTargetException,IllegalAccessException {
User user1 = new User();
Class c1 = user1.getClass();
Method getName = c1.getDeclaredMethod("getName", null);
getName.setAccessible(true);
long startTime = System.currentTimeMillis();
for (int i = 0; i < 100000000; i++) {
getName.invoke(user1,null);
}
long endTime = System.currentTimeMillis();
System.out.println("反射无检测方式执行1亿次耗时:" + (endTime - startTime) + "ms");
}
public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException,IllegalAccessException{
test01();
test02();
test03();
}
}