关于java:JAVA基础知识之反射

58次阅读

共计 4813 个字符,预计需要花费 13 分钟才能阅读完成。

以下是我总结的一些简略的 JAVA 反射相干的小例子,非常简单,一看您就明确了,有什么问题您评论,我解答。

根底 bean

public class Person {
    public int age;
    public String name;
    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;}
    public Person(String name,int age) {
        this.age = age;
        this.name = name;
    }
    public Person() {}
}

三种获取 Class 的形式

// 第一种
Class<?> helloClass = "hello".getClass();
Class<? extends String> helloClassNew = "hello".getClass();
Class<? extends Object> helloClassNew2 = "hello".getClass();
// 第二种
Class<?> stringClass = String.class;
// 第三种
String className ="java.lang.String";
Class<?> stringClassNew = Class.forName(className);

获取父类型以及实现的接口类型

Class<?> superClass = stringClass.getSuperclass();
Class<?>[] interfaces = stringClass.getInterfaces();

获取类的字段

Class<?> clss = Person.class;
Field field = clss.getField("age");
// 获取该类也同时包含父类型的全副 public 字段
Field[] declaredFields = clss.getDeclaredFields();
// 获取该类外部的全副字段,无论 private 还是 public
Field[] fields = clss.getFields();

获取办法

Method method =clss.getMethod("setName", String.class);
// 获取该类也同时包含父类型的全副 public 办法
Method[] declaredMethods = clss.getDeclaredMethods();
// 获取所有该类外部的 method,无论 private 还是 public
Method[] methods = clss.getMethods();

获取构造函数

Constructor<?> constructor = clss.getConstructor(Class<?> ..types);
// 获取该类外部的所有构造函数
Constructor<?>[] declaredConstructor = clss.getDeclaredConstructors();
// 获取该类外部的所有 public 构造函数
Constructor<?>[] constructors = clss.getConstructors();

获取润饰

int modifiers = constructor.getModifiers();
boolean isPublic = Modifier.isPublic(modifiers);
设置字段为 public 的值,字段 age 为 public
Person person = new Person();
Class<?> o = person.getClass();
Field field = o.getDeclaredField("age");
field.set(person,"d");
System.out.println(person.getAge());
// 输入
d

设置字段为 private 的值,字段 name 为 private,须要减少 setAccessible 为 true,如果不设置为 true 则会报 IllegalAccessException

Person person = new Person();
Class<?> o = person.getClass();
Field field = o.getDeclaredField("name");
field.setAccessible(true);
field.set(person,"d");
System.out.println(person.getName());

通过获取字段的注解,做操作。

Person person = new Person();
Class<?> o = person.getClass();
Field[] fields = o.getDeclaredFields();
for(Field field:fields){if(field.getAnnotatedType(PrimaryKey.class) !=null){//this is the primary key}
    if(field.getAnnotatedType(Column.class) !=null){//this is an elment to read / write}
}

通过 MethodHandles 获取类

MethodHandles.Lookup lookup = MethodHandles.lookup();
Class<?> personClass = MethodHandles.lookup().findClass(Person.class.getName()); 

创立四种 method

//getter 办法
MethodType getterType = MethodType.methodType(String.class);
MethodHandle getterHandle = lookup.findVirtual(Person.class,"getName",getterType);
//setter 办法
MethodType setterType = MethodType.methodType(void.class,String.class);
MethodHandle setterHandle = lookup.findVirtual(Person.class,"setName",setterType);
// 构造方法
MethodType constructorType = MethodType.methodType(void.class,String.class,int.class);
MethodHandle constructorHandle = lookup.findConstructor(Person.class, constructorType);
// 空构造方法
MethodType emptyConstructorType = MethodType.methodType(void.class);
MethodHandle emptyConstructorHandle = lookup.findConstructor(Person.class,emptyConstructorType);

通过 invoke 创立 bean

MethodType constructorType = MethodType.methodType(void.class,String.class,int.class);
MethodHandle constructorHandle = lookup.findConstructor(Person.class, constructorType);

Person p = (Person)constructorHandle.invoke("Xuesong",33);
System.out.println(p);
// 输入
Person{age=33, name='Xuesong'}

MethodType emptyConstructorType = MethodType.methodType(void.class);
MethodHandle emptyConstructorHandle = lookup.findConstructor(Person.class,emptyConstructorType);

Person p2 = (Person)emptyConstructorHandle.invoke();
System.out.println(p2);
// 输入
Person{age=0, name='null'}

通过 invoke 调用 getter 和 setter

MethodHandle nameReader = lookup.findGetter(Person.class, "name", String.class);
MethodHandle nameWriter = lookup.findSetter(Person.class,"name",String.class);
Person person = new Person("Xuesong",33);
String name = (String) nameReader.invoke(person);
// 或者
//String name = (String) getterHandle.invoke(person);
System.out.println(name);
// 输入
Xuesong
nameWriter.invoke(person,"John");
// 或者
//setterHandle.invoke(person,"John");
System.out.println(person.getName());
// 输入
John

针对 private 的字段须要非凡解决

Field nameField = Person.class.getDeclaredField("name");
nameField.setAccessible(true);
MethodHandle privateNameReader = lookup.unreflectGetter(nameField);
String name = (String) privateNameReader.invoke(person);
System.out.println(name);
MethodHandle privateNameWriter = lookup.unreflectSetter(nameField);
privateNameWriter.invoke(person,"John");
System.out.println(person.getName());
//JDK9 当前的形式
Lookup privateLookup = MethodHandles.privateLookupIn(Person.class, lookup);
MethodHandle privateNameWriter = privateLookup.findSetter(Person.class,"name",String.class);
privateNameWriter.invoke(person, "John");

JDK9 当前减少了线程平安的一种 VarHandle,也能够解决

VarHandle nameVarHandle = MethodHandles.privateLookupIn(Person.class,lookup)
        .findVarHandle(Person.class,"name",String.class);
String name = (String) nameVarHandle.get(person);
String name = (String) nameVarHandle.getVolatile(person);

正文完
 0