如何从java类外部调用私有方法
我有一个如何从java类外部调用私有方法,java,reflection,call,private,Java,Reflection,Call,Private,我有一个Dummy类,它有一个名为sayHello的私有方法。我想在Dummy外部呼叫sayHello。我认为反射应该是可能的,但我得到了一个IllegalAccessException。有什么想法吗?在使用方法对象的调用方法之前,在方法对象上使用setAccessible(true) import java.lang.reflect.*; class Dummy{ private void foo(){ System.out.println("hello foo()")
Dummy
类,它有一个名为sayHello
的私有方法。我想在Dummy
外部呼叫sayHello
。我认为反射应该是可能的,但我得到了一个IllegalAccessException
。有什么想法吗?在使用方法对象的调用方法之前,在方法对象上使用setAccessible(true)
import java.lang.reflect.*;
class Dummy{
private void foo(){
System.out.println("hello foo()");
}
}
class Test{
public static void main(String[] args) throws Exception {
Dummy d = new Dummy();
Method m = Dummy.class.getDeclaredMethod("foo");
//m.invoke(d);// throws java.lang.IllegalAccessException
m.setAccessible(true);// Abracadabra
m.invoke(d);// now its OK
}
}
首先,您必须获取类,这非常简单,然后使用getDeclaredMethod
按名称获取方法,然后您需要在method
对象上将该方法设置为可通过setAccessible
方法访问
Class<?> clazz = Class.forName("test.Dummy");
Method m = clazz.getDeclaredMethod("sayHello");
m.setAccessible(true);
m.invoke(new Dummy());
Class clazz=Class.forName(“test.Dummy”);
方法m=clazz.getDeclaredMethod(“sayHello”);
m、 setAccessible(true);
m、 调用(新的Dummy());
如果要将任何参数传递给私有函数,可以将其作为第二个、第三个。。。。。调用函数的参数。下面是示例代码
Method meth = obj.getClass().getDeclaredMethod("getMyName", String.class);
meth.setAccessible(true);
String name = (String) meth.invoke(obj, "Green Goblin");
完整示例您可以看到使用java反射访问私有方法(带参数)的示例,如下所示:
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
class Test
{
private void call(int n) //private method
{
System.out.println("in call() n: "+ n);
}
}
public class Sample
{
public static void main(String args[]) throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchFieldException
{
Class c=Class.forName("Test"); //specify class name in quotes
Object obj=c.newInstance();
//----Accessing private Method
Method m=c.getDeclaredMethod("call",new Class[]{int.class}); //getting method with parameters
m.setAccessible(true);
m.invoke(obj,7);
}
}
private的概念不就是不能从外部调用它吗?是的,反射是可能的,但是private的目的是使从外部调用方法变得更困难。也许它不应该是私有的?@robert它在同一个程序(模块)中@HamedRajabi:你是说调用私有方法的类和你的伪类在同一个包中?如果是这样的话,你可能想使用包私有
(省略修饰符)。@PriestVallon是的,我知道我不应该在真正的程序中这样做,我只是想知道!!!因为getMethod
只返回public方法,所以如果方法有参数,则需要getDeclaredMethod
查看下面的Keyur答案。
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
class Test
{
private void call(int n) //private method
{
System.out.println("in call() n: "+ n);
}
}
public class Sample
{
public static void main(String args[]) throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchFieldException
{
Class c=Class.forName("Test"); //specify class name in quotes
Object obj=c.newInstance();
//----Accessing private Method
Method m=c.getDeclaredMethod("call",new Class[]{int.class}); //getting method with parameters
m.setAccessible(true);
m.invoke(obj,7);
}
}