反射(Reflection)
Java 是一门静态语言,而由于反射的出现,从静态语言。变成了"准动态的语言"
- Reflection是Java被视为动态语言的关键
- 反射机制允许程序在执行期间,借助于Reflection API来取得任何类的内部信息,并能直接操作任意对象的内部属性以及方法。
- 在加载完类后,会在方法区中产生一个class类型的对象,这个class对象包含了完整的类的结构信息,反射就好像一面镜子一样,可以通过这个对象看到类的结构。
- 优点:可以实现动态创建对象和编译,体现出很大的灵活性。
- 缺点:会对性能产生影响,使用反射基本上是一种解释操作,我们告诉JVM,希望做什么并且它满足我们的需求,这类操作要慢于直接执行相同的操作。
- 反射就是一种创建对象的一个逆过程,正常是类new一个对象,而反射是通过对象来得到类。
获得Class相关对象和属性
//方式一:通过对象获得
Class c1 = person.getClass();
System.out.println(c1.hashCode());
//方式二:forname获得
Class c2 = Class.forName("linluo.reflection.Student");
System.out.println(c2.hashCode());
//方式三:通过类名.class获得
Class c3 = Student.class;
System.out.println(c3.hashCode());
//方式四:基本内置类型的包装类都有一个Type属性
Class c4 = Integer.TYPE;
System.out.println(c4);
//获得父类类型
Class c5 = c1.getSuperclass();
System.out.println(c5);
//获取系统类的加载器
ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
//获取类的名字
String name = c1.getName();
System.out.println(name); //获取包名+类名
System.out.println(c1.getSimpleName());//获得类名
//获得类的属性
Filed[] fileds = c1.getFields(); 这个只能找到public属性
Field[] fields= c1.getDeclaredFields(); //这个能找到全部的属性
for (Field field : fields) {
System.out.println(field);
}
//获取指定属性的值
Field name1 = c1.getDeclaredField("name");
System.out.println(name1);
Method[] methods = c1.getMethods(); //获取本类及其父类的全部public方法
for (Method method: methods
) {
System.out.println("正常的:"+method);
}
methods = c1.getDeclaredMethods();//获取本类的全部方法
for ( Method method: methods
) {
System.out.println("getDeclare:"+method);
}
//获得指定方法
Method getName = c1.getMethod("getName", null);
Method setName = c1.getMethod("setName", String.class);
System.out.println(getName);
System.out.println(setName);
//获取指定的构造器
Constructor[] constructors = c1.getConstructors();
for (Constructor constructor : constructors) {
System.out.println(constructor);
}
Constructor[] declaredConstructors = c1.getDeclaredConstructors();
for (Constructor declaredConstructor : declaredConstructors) {
System.out.println("#"+declaredConstructor);
}
Constructor declaredConstructor = c1.getDeclaredConstructor(String.class, int.class, int.class);
System.out.println("指定"+declaredConstructor);
}
类的加载
- 加载
- 链接
- 初始化
类的引用问题
public class Test06 {
static{
System.out.println("Main加载");
}
public static void main(String[] args) throws ClassNotFoundException {
//1.主动引用
Son son = new Son();
//反射也会产生主动引用
Class.forName("linluo.reflection.Son");
//不会产生类的引用的方法
System.out.println(Son.b);
//Son[] array = new Son[5];
System.out.println(Son.M);
}
}
class Father{
static int b = 2;
static {
System.out.println("父加载");
}
}
class Son extends Father {
static {
System.out.println("子加载");
m = 300;
}
static int m = 100;
static final int M =1;
}
安全监测问题
- xxx.setAccessible(true);//设置安全监测,可以访问private
Class c1 = Class.forName("linluo.reflection.User");
User user6 = (User) c1.getDeclaredConstructor(String.class,int.class,int.class).newInstance("123",1,1);
Method getprivate = c1.getDeclaredMethod("demoprivate",null);
Field name = c1.getDeclaredField("name");
name.setAccessible(true);
name.set(user6,"123456");
getprivate.setAccessible(true);
getprivate.invoke(user6);
System.out.println(user6.getName());
反射泛型
//获得参数中的泛型,并获取泛型中的类型
Method method = Test11.class.getMethod("test01", Map.class, List.class);
Type[] genericParameterTypes = method.getGenericParameterTypes();
for (Type genericParameterType : genericParameterTypes) {
System.out.println("#"+genericParameterType);
if(genericParameterType instanceof ParameterizedType){
Type[] actualTypeArguments = ((ParameterizedType) genericParameterType).getActualTypeArguments();
for (Type actualTypeArgument : actualTypeArguments) {
System.out.println(actualTypeArgument);
}
}
}
//获取返回值的泛型的类型
Method test02 = Test11.class.getMethod("test02");
Type genericReturnType = test02.getGenericReturnType();
if(genericReturnType instanceof ParameterizedType){
Type[] actualTypeArguments = ((ParameterizedType) genericReturnType).getActualTypeArguments();
for (Type actualTypeArgument : actualTypeArguments) {
System.out.println(actualTypeArgument);
}
}
通过反射获得注解
package linluo.reflection;
import java.lang.annotation.*;
import java.lang.reflect.Field;
//练习反射操作注解
public class Test12 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException {
Class c1 = Class.forName("linluo.reflection.Student2");
//通过反射获得注解
Annotation[] annotations = c1.getAnnotations();
for (Annotation annotation : annotations) {
System.out.println(annotation);
}
//获得注解的value的值
Table table = (Table) c1.getAnnotation(Table.class);
String value = table.value();
System.out.println(value);
//获得类指定的注解
Field f = c1.getDeclaredField("name");
Filed annotation = f.getAnnotation(Filed.class);
System.out.println(annotation.columnName());
System.out.println(annotation.length());
System.out.println(annotation.type());
}
}
@Table("db_student")
class Student2{
@Filed(columnName = "db_id",type = "int",length = 10)
private int id;
@Filed(columnName = "db_age",type = "int",length = 10)
private int age;
@Filed(columnName = "db_name",type = "varchar",length = 3)
private String name;
public Student2() {
}
public Student2(int id, int age, String name) {
this.id = id;
this.age = age;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Student2{" +
"id=" + id +
", age=" + age +
", name='" + name + '\'' +
'}';
}
}
//类名的注解
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface Table{
String value();
}
//属性的注解
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface Filed{
String columnName();
String type();
int length();
}
本文参考b站-遇见狂神说-视频