反射机制概述
反射机制
,是在程序运行时
动态的加载类
并获取其详细信息
,从而操作类或类的属性与方法
,一般情况下,一个对象的类型都是在编译期确定的,而通过反射调用的对象其类型在编译期是未知的,所以我们可以通过反射机制动态的调用一个未知类型的方法/属性
反射机制的原理
- 在我们程序运行过程中,Java 编译器会在编译过程中,通过类加载器将编译完成的.class文件加载到JVM内存中,而反射机制便是通过这些.class进行反编译,从而动态的创建与调用对象
反射机制优缺点
- 优点:可以在运行时动态的获取类的相关信息,这样更加方便我们做一些扩展功能,更加贴切的面向对象思想
- 缺点:通过反编译创建的对象,会更加消耗一定的系统资源,另外反射机制可以无限制的调用类内部的私有方法,这样便破坏了类的安全机制
反射机制的使用
import lombok.Data;
@Data
public class UserEntity {
private String name;
private Integer age;
public UserEntity() {
System.out.println("调用无参构造函数");
}
public UserEntity(String name, Integer age) {
this.name = name;
this.age = age;
System.out.println("调用有参构造函数");
}
private void test(String name,Integer age){
System.out.println("调用私有方法"+"name:"+name+"age:"+age);
}
}
import com.liunian.demo.entity.UserEntity;
public class Test01 {
public static void main(String[] args) {
Class<UserEntity> userEntityClass = UserEntity.class;
UserEntity userEntity = userEntityClass.newInstance();
}
}
- Class.forName(“类的完整路径地址:包名+类名”)
import com.liunian.demo.entity.UserEntity;
public class Test02 {
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
Class<?> aClass = Class.forName("com.liunian.demo.entity.UserEntity");
UserEntity o = (UserEntity) aClass.newInstance();
}
}
import com.liunian.demo.entity.UserEntity;
public class Test03 {
public static void main(String[] args) {
UserEntity userEntity = new UserEntity();
Class aClass = userEntity.getClass();
}
}
import com.liunian.demo.entity.UserEntity;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
public class Test02 {
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
Class<?> aClass = Class.forName("com.liunian.demo.entity.UserEntity");
Constructor<?> declaredConstructor = aClass.getDeclaredConstructor(String.class, Integer.class);
UserEntity userEntity = (UserEntity) declaredConstructor.newInstance("流年", 123123);
System.out.println(userEntity);
}
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210706165126957.png)
import java.lang.reflect.Field;
public class Test02 {
public static void main(String[] args) throws ClassNotFoundException {
Class<?> aClass = Class.forName("com.liunian.demo.entity.UserEntity");
Field[] fields = aClass.getFields();
for (Field field : fields) {
System.out.println(field);
}
Field[] declaredFields = aClass.getDeclaredFields();
for (Field declaredField : declaredFields) {
System.out.println(declaredField);
}
}
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210706170054613.png)
import com.liunian.demo.entity.UserEntity;
import java.lang.reflect.Field;
public class Test02 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, InstantiationException, IllegalAccessException {
Class<?> aClass = Class.forName("com.liunian.demo.entity.UserEntity");
UserEntity userEntity = (UserEntity) aClass.newInstance();
Field name = aClass.getDeclaredField("name");
name.setAccessible(true);
name.set(userEntity,"测试");
System.out.println(userEntity);
}
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210706170853313.png)
import java.lang.reflect.Method;
public class Test02 {
public static void main(String[] args) throws ClassNotFoundException {
Class<?> aClass = Class.forName("com.liunian.demo.entity.UserEntity");
Method[] methods = aClass.getMethods();
for (Method method : methods) {
System.out.println(method);
}
System.out.println("-------------------");
Method[] declaredMethods = aClass.getDeclaredMethods();
for (Method declaredMethod : declaredMethods) {
System.out.println(declaredMethod);
}
}
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/202107061703291.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM1MDQ0OTI1,size_16,color_FFFFFF,t_70)
import com.liunian.demo.entity.UserEntity;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class Test02 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
Class<?> aClass = Class.forName("com.liunian.demo.entity.UserEntity");
UserEntity userEntity = (UserEntity) aClass.newInstance();
Method test = aClass.getDeclaredMethod("test",String.class,Integer.class);
test.setAccessible(true);
test.invoke(userEntity,"流年",123);
}
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210706172805832.png)
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
public class Test04 {
public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
ArrayList<String> arrayList = new ArrayList<>();
arrayList.add("测试");
Class aClass = arrayList.getClass();
Method add = aClass.getDeclaredMethod("add", Object.class);
add.invoke(arrayList,1);
System.out.println(arrayList);
}
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210706173345503.png)