测试反射调用私有方法/私有静态方法
2017-10-17 09:39
483 查看
测试反射调用方法
import java.lang.reflect.Method;
class T{
public void me2(String s){
System.out.println("public method ");
}
private static void me1(String s,Integer i){
System.out.println("this is a private static method and the parameters is: "+s+" "+i);
}
private void me(String s){
System.out.println("this is a private method and the parameters is: "+s);
}
}
public class Test1 {
public static void main(String[] args){
T t=new T();
try{
Method method=Class.forName("T").getDeclaredMethod("me", new Class[]{String.class});
method.setAccessible(true);
method.invoke(t, "test private");
Method method1=Class.forName("T").getDeclaredMethod("me1", new Class[]{String.class,Integer.class});
method1.setAccessible(true);
method1.invoke(T.class, "test static private",1296699761);
Method method2=t.getClass().getDeclaredMethod("me2",new Class[]{String.class});
//method2.setAccessible(true);
method2.invoke(t, "test public");
}catch(Exception e){
e.printStackTrace();
}
System.out.println("end!");
}
}
getDeclaredMethod方法第一个参数是方法名,第二个是参数类型的数组
invoke方法第一个参数是类或者对象实例,后面的参数是方法形参
setAccessible要设置成true的,否则无法调用private方法
运行结果:
this is a private method and the parameters is: test private
this is a private static method and the parameters is: test static private 1296699761
public method
end!
import java.lang.reflect.Method;
class T{
public void me2(String s){
System.out.println("public method ");
}
private static void me1(String s,Integer i){
System.out.println("this is a private static method and the parameters is: "+s+" "+i);
}
private void me(String s){
System.out.println("this is a private method and the parameters is: "+s);
}
}
public class Test1 {
public static void main(String[] args){
T t=new T();
try{
Method method=Class.forName("T").getDeclaredMethod("me", new Class[]{String.class});
method.setAccessible(true);
method.invoke(t, "test private");
Method method1=Class.forName("T").getDeclaredMethod("me1", new Class[]{String.class,Integer.class});
method1.setAccessible(true);
method1.invoke(T.class, "test static private",1296699761);
Method method2=t.getClass().getDeclaredMethod("me2",new Class[]{String.class});
//method2.setAccessible(true);
method2.invoke(t, "test public");
}catch(Exception e){
e.printStackTrace();
}
System.out.println("end!");
}
}
getDeclaredMethod方法第一个参数是方法名,第二个是参数类型的数组
invoke方法第一个参数是类或者对象实例,后面的参数是方法形参
setAccessible要设置成true的,否则无法调用private方法
运行结果:
this is a private method and the parameters is: test private
this is a private static method and the parameters is: test static private 1296699761
public method
end!
相关文章推荐
- private(私有)方法单元测试无法覆盖?那就用反射调用来测试private(私有)方法
- C# 反射调用私有 静态 方法
- 静态方法,实例方法,反射调用性能测试
- 首页文章分类 关于 Search private(私有)方法单元测试无法覆盖?那就用反射调用来测试private(私有)方法
- 测试 java反射Reflection中 获取 类属性、方法、构造方法及 调用类方法改变 对象属性
- java反射中两种方法的不同(反射能否实现对类的私有方法的调用)
- JavaSE第六十四讲:使用反射机制调用对象的私有方法、访问对象的私有成员变量
- (转载,但不知道谁原创)获取SPRING 代理对象的真实实例,可以反射私有方法,便于测试
- Java反射学习总结二(用反射调用对象的私有属性和方法)
- JAVA 反射 动态调用不同类的静态方法
- 使用反射机制实现对类中私有方法的调用
- java利用反射来调用一个类的私有方法
- 通过反射测试私有方法
- JAVA反射获取私有值和调用私有方法
- 利用反射访问私有属性、调用私有方法
- 通过反射创建私有化类的对象,调用类的私有方法,改变私有属性
- java 反射机制调用私有构造方法并执行类内方法
- JUnit 3.8 通过反射测试私有方法
- JUnit 3.8 通过反射测试私有方法
- 利用反射调用私有方法