反射
反射的作用:
在运行状态中,
- 对于任意一个类,都能够知道这个类的所有属性和方法
- 对于任意一个类,都能够调用它的任意一个属性和方法
反射还是比较重要的知识点,这里主要介绍反射的各个方法的使用,可以做工具参考。
实体类
import java.io.Serializable;
import java.util.List;
public class ReflectionEntity implements Cloneable, Serializable {
private String str;
private double d;
public boolean b;
public static short s;
public ReflectionEntity() {}public ReflectionEntity(String str) {
this.str = str;
}public ReflectionEntity(String str, double d, boolean b) {
this.str = str;
this.b = b;
this.d = d;
}private void privateMethod() {}public String publicMethod(int i) {
return null;
}public String publicMethod(int i, double d, List l) {
return "Reflection.publicMethod(int i, double d, List l), result: " + "i = " +
i + ", d = " + d;
}public static int returnOne() {
return 1;
}public String toString() {
return "Str = " + str + ", d = " + d + ", b = " + b;
}
}
测试类
import javafx.scene.effect.Reflection;
import java.awt.*;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
public class ReflectionTest {
public static void main(String[] args) throws Exception {
Class> c = Class.forName("ReflectionEntity");
ReflectionEntity re = (ReflectionEntity) c.newInstance();
ReflectionEntity[] res = new ReflectionEntity[2];
// -------------- Class and ClassLoader --------------
System.out.println("Class.getClass(): " + c.getClass());
System.out.println("Class.getClassLoader(): " + c.getClassLoader());
System.out.println("Class.getSuperclass(): " + c.getSuperclass());
// 获取类的接口列表,返回的是一个数组
System.out.println("Class.getInterfaces(): " + c.getInterfaces()[0] + ", " + c.getInterfaces()[1]);
// 获取该数组的 Class 对象
System.out.println("Class.getComponentType(): " + res.getClass().getComponentType());
// 默认调用无参构造方法实例化
System.out.println("Class.newInstance(): " + re);
// -------------- Method --------------
Method method1 = c.getMethod("publicMethod", int.class, double.class, List.class);
System.out.println("Method.getMethod(): " + method1);
Method method2 = c.getDeclaredMethod("privateMethod");
System.out.println("Method.getDeclareMethod(): " +method2);
// 获取此类包括其父类中所有的 public 方法
Method[] md1 = c.getMethods();
System.out.println("Method.getMethods(): ");
for (Method m : md1)
System.out.println(m + "\n");
// 返回此类中所有的方法(无访问权限限制),但不包括继承的方法
Method[] md2 = c.getDeclaredMethods();
System.out.println("Method.getDeclaredMethods(): ");
for (Method m : md2)
System.out.println(m + "\n");
System.out.println("getName: " + method1.getName());
System.out.println("isAccessible(): " + method1.isAccessible());
System.out.println("isVarArgs(): " + method1.isVarArgs());
System.out.println("getReturnType(): " + method1.getReturnType());
// 获取方法的参数类型,数组形式
System.out.println("getParameterTypes(): " + method1.getParameterTypes()[0] + ", " + method1.getParameterTypes()[1] + ", " + method1.getParameterTypes()[2]);
// 获取方法的参数化(带泛型)类型,数组形式
System.out.println("getGenericParameterTypes(): " + method1.getGenericParameterTypes()[0] + ", " + method1.getGenericParameterTypes()[1] + ", " + method1.getGenericParameterTypes()[2]);
System.out.println(method1.invoke(re, 233, 666, new ArrayList()));
// -------------- Field --------------
Field f1 = c.getField("b");
Field f2 = c.getDeclaredField("d");
System.out.println("getField(): " + f1);
System.out.println("getDeclareField(): " + f2);
Field[] fs1 = c.getFields();
Field[] fs2 = c.getDeclaredFields();
System.out.println("getFields(): ");
for (Field f : fs1)
System.out.println(f);
System.out.println("getDeclareFields(): ");
for (Field f : fs2)
System.out.println(f);
System.out.println("getName and getType: " + f1.getName() + ", " + f1.getType());
System.out.println("getBoolean(): " + f1.getBoolean(re));
// 由于 isAccessible 为 false 故会报异常
//System.out.println("getDouble(): " + f2.getDouble(re));
// 以整数形式返回此Field对象的Java语言修饰符,如public、static、final等
System.out.println("getModifiers(): " + f1.getModifiers());
// 返回Field的访问权限
System.out.println("isAccessible(): " + f1.isAccessible());
f2.setAccessible(true);
System.out.println("getDouble(): " + f2.getDouble(re));
System.out.println(re);
f2.setDouble(re, 233.666);
System.out.println(re);
f2.setAccessible(false);
// -------------- Constructor --------------
Constructor> constructor = c.getConstructor(String.class);
Constructor>[] constructors = c.getConstructors();
System.out.println("constructor: " + constructor);
System.out.println("constructors: ");
for (Constructor cc : constructors)
System.out.println(cc);
System.out.println("getName: " + constructor.getName());
System.out.println("isAccessible(): " + constructor.isAccessible());
System.out.println("isVarArgs(): " + constructor.isVarArgs());
System.out.println("getModifiers(): " + constructor.getModifiers());
// 获取方法的参数类型,数组形式
System.out.println("getParameterTypes(): " + constructor.getParameterTypes()[0]);
ReflectionEntity r = (ReflectionEntity) constructor.newInstance("AE86");
System.out.println("newInstance(): " + r);
Field f3 = c.getField("s");
System.out.println("f3.getModified :" + f3.getModifiers());
}
}
修饰符 | 值 |
---|---|
public | 1 |
private | 2 |
protected | 4 |
static | 8 |
final | 16 |
sychronized | 32 |
valatile | 64 |
transient | 128 |
native | 256 |
interface | 512 |
abstract | 1024 |
strict | 2048 |