java反射测试方法_Java反射API测试

为了测试反射API的功能,需要一个基准集,应该具备Java语言的各种结构。

构建基准

?i=20190324104430287.png

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 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)

标签:node,反射,Java,System,private,API,static,println,public

来源: https://blog.csdn.net/qq_33745102/article/details/88774900

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值