版权声明:代码自由使用 https://blog.csdn.net/qq_33745102/article/details/88774900
为了测试反射API的功能,需要一个基准集,应该具备Java语言的各种结构。
构建基准
Main方法用于测试,其他的类为基准集。
- Mark
public interface Mark {
}
- Node
Node
实现了Mark
接口是SpecialNode
的父类,拥有两个私有字段
import java.util.ArrayList;
import java.util.List;
public class Node implements Mark{
private int x=0;
private List<String> list=new ArrayList<>();
public int y=0;
public Object obj=new Object();
//constructor
public Node(){
list.add("hello node");
}
//private instance method
private String greet(String name){
return "hello"+name;
}
//private instance method with no args
private String greet(){
return "hello world";
}
//private static method
private static String staticGreet(String name){
return "Static method Greet:Hello "+name;
}
//private static method with no args
private static String staticGreet(){
return "Static Default greet:hello world";
}
private static class Bucket1{}
private class Bucket2{}
}
- SpecialNode
@SimpleAnnotation
public class SpecialNode extends Node{
public boolean special=false;
private static String greet="hello";
private class Bucket3{}
private static class StaticBucket3{}
//private constructor
private SpecialNode(){
System.out.println("SpecialNode's private Constructor method");
}
private String reverse(String greet1){
return ""+greet+""+greet1;
}
private int simplere(int i){
return i;
}
}
- SimpleAnnotation
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface SimpleAnnotation {
}
字段获取
获取public字段不需要任何特殊操作:
public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
System.out.println("------------获取共有实例属性---------------------");
Node node=new SpecialNode("RED");
Field field=node.getClass().getField("special");
System.out.println(field.get(node));
//如果是基本类型,自动转换基本类型的相应的包装类
System.out.println("field \'special\''s class:"+field.get(node).getClass());
System.out.println("------------获取私有静态属性---------------------");
//获取私有静态属性,getField只能获取public字段
Field staticGreet=node.getClass().getDeclaredField("greet");
//设置可访问
staticGreet.setAccessible(true);
//如果是静态字段,也需要传入一个对象引用,但是这个对象引用会被忽略
System.out.println("private static Field greet:"+staticGreet.get(node));
System.out.println("-------------获取父类的私有属性--------------------");
//获取父类的私有属性
/**
* 1.getSupperClass返回其父类类对象,如果这个类是Object类,接口,或者基本类型返回null
* 2.可以传入子类的引用
* **/
Field parentx=node.getClass().getSuperclass().getDeclaredField("x");
parentx.setAccessible(true);
System.out.println("parent private property \'x\'="+parentx.get(node));
System.out.println("-------------获取父类的静态属性--------------------");
//获取父类的静态属性
Field parentPrivateStaticProperty=node.getClass().getSuperclass().getDeclaredField("aDouble");
parentPrivateStaticProperty.setAccessible(true);
System.out.println("parent Private Static Property aDouble: "+parentPrivateStaticProperty.get(node));
}
测试结果:
------------获取共有实例属性---------------------
SpecialNode's private Constructor methodRED
false
field 'special''s class:class java.lang.Boolean
------------获取私有静态属性---------------------
private static Field greet:hello
-------------获取父类的私有属性--------------------
parent private property 'x'=0
-------------获取父类的静态属性--------------------
parent Private Static Property aDouble: 2.0
获取私有属性的时候,必须抑制JVM的访问权限检查(Java language access checks)