「java代理私有方法」java如何实现代理机制

博主:adminadmin 2022-12-18 17:45:06 61

本篇文章给大家谈谈java代理私有方法,以及java如何实现代理机制对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

java中,怎么调用别的类的私有方法

反射(reflection)

[java] view plain copy

public static void main(String[] args) throws Exception {

Constructor? constructor = SecretTool.class.getDeclaredConstructors()[0];

constructor.setAccessible(true);

SecretTool tool = (SecretTool) constructor.newInstance(); // 得到它的一个实例

for(Method method : SecretTool.class.getDeclaredMethods()) {

method.setAccessible(true);

if(method.getName().equals("myMotto")) {

method.invoke(tool); // 调用没有返回值,无参的私有方法

} else if(method.getName().equals("calculate")) {

Integer result = (Integer)method.invoke(tool, 1,2);

System.out.println("1 + 2 = " + result.toString()); // 调用返回值为整数,且带参的私有方法

}

}

}

输出结果:

[plain] view plain copy

I like potato

1 + 2 = 3

java反射访问私有方法的的问题

java的反射可以绕过访问权限,访问到类的私有方法和成员。可能这点会引起安全性的讨论。反射的使用帮助解决很多复杂的问题,其运行时的类型检查,动态调用,代理的实现等,反射为我们写程序带来了很大的灵活性,很多功能都是基于反射。

利用反射还可以访问内部类、匿名内部类的私有属性。

用java自带的java -private 类名 反编译命令可以查看类的完整定义。(参考think in java)

下面举例子说明。首先定义一个接口

Java代码

public interface Ref {

public void f();

}

public interface Ref {

public void f();

}

接口的实现类

Java代码

public class RefImpl implements Ref {

//实现接口方法

public void f() {

System.out.println("public method f()");

}

void g(String args){

System.out.println("package method g():" + args);

}

private void w(){

System.out.println("private method w()");

}

}

public class RefImpl implements Ref {

//实现接口方法

public void f() {

System.out.println("public method f()");

}

void g(String args){

System.out.println("package method g():" + args);

}

private void w(){

System.out.println("private method w()");

}

}

测试类

Java代码

public class TestRef {

public static void main(String[] args) {

Ref ref = new RefImpl();

System.out.println(ref.getClass().getSimpleName()); //RefImpl类型

ref.f(); //调用接口方法

// ref.g(); //向上转型后实现类添加的方法不能调用

if(ref instanceof RefImpl){

RefImpl ref1 = (RefImpl)ref; //类型识别后转型

ref1.g("zhouyang");

// ref1.w(); //私有方法不能访问

}

//通过反射调用方法

try {

Ref ref2 = new RefImpl();

Method m = ref2.getClass().getDeclaredMethod("f");

Method m1 = ref2.getClass().getDeclaredMethod("g", String.class);//有参的方法

Method m2 = ref2.getClass().getDeclaredMethod("w");

System.out.println("==============");

m.invoke(ref); //调用方法f()

m1.invoke(ref, "yangzhou");

m2.setAccessible(true);///调用private方法的关键一句话

m2.invoke(ref);

} catch (Exception e) {

e.printStackTrace();

}

//java的javap反编译能够查看类的信息,-private 开关能够打开所有信息

//javap -private 类名 类必须是编译成.calss 文件

//利用反射访问私有成员,改变私有成员值,但是final域可以访问不可改变

PrivateField pf = new PrivateField();

// ps.ss; //私有成员不能访问

//打印原来的成员值

pf.print();

try {

//反射访问和改变原来值

Field[] f = pf.getClass().getDeclaredFields();

for(int i=0;if.length;i++){

f[i].setAccessible(true);

System.out.println(f[i].getType());//打印字段类型

System.out.println(f[i].get(pf)); //打印值

if("ss".equals(f[i].getName())){

f[i].set(pf, "hehe"); //修改成员值

}else{

f[i].setInt(pf, 55);

}

}

//重新打印修改后的成员值,final域值不变

pf.print();

} catch (Exception e) {

e.printStackTrace();

}

/*打印输出的结果

* RefImpl

public method f()

nb

如何获取java类里的私有方法

利用java的反射机制,以下代码:

public class Main {

private void fun(){

System.out.println("this is my private method!");

}

private void fun2(String what) {

System.out.println(what);

}

private static void fun3(){

System.out.println("this is my private static method!");

}

}

import java.lang.reflect.InvocationTargetException;

import java.lang.reflect.Method;

public class Test {

public static void main(String[] args) {

try {

Class? _class = Class.forName("Main");//名字填完整的包名.类名  ,示例没有包因此只填类名

Object object = _class.newInstance();

Method method = _class.getDeclaredMethod("fun");//fun无参数,因此只要方法名

method.setAccessible(true);//私有方法设置可访问 

method.invoke(object, new Object[0]);

Method method2 = _class.getDeclaredMethod("fun2",String.class);//有参数,需要参数的类别

method2.setAccessible(true);

method2.invoke(object, new String("test"));//传参数的值

Method method3 = _class.getDeclaredMethod("fun3");

method3.setAccessible(true);

method3.invoke(object,new Object[0]);//静态方法,object传null

} catch (ClassNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

 catch (InstantiationException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IllegalAccessException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (SecurityException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (NoSuchMethodException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IllegalArgumentException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (InvocationTargetException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

关于java代理私有方法和java如何实现代理机制的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-12-18,除非注明,否则均为首码项目网原创文章,转载请注明出处。