我知道如何从private
内的method
访问field
Class
或Test class
:
从名为MyClass
的{{1}}访问私有方法:
void doSomething()
要访问名为import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
...
try {
Method method = MyClass.class.getDeclaredMethod("doSomething", (Class[])null); // (Class[])null is for parameterless methods
method.setAccessible(true);
method.invoke(localInstanceOfMyClass);
}
catch (NoSuchMethodException ex){
ex.printStackTrace();
}
catch (IllegalAccessException ex){
ex.printStackTrace();
}
catch (IllegalArgumentException ex){
ex.printStackTrace();
}
catch (InvocationTargetException ex) {
ex.printStackTrace();
}
的{{1}} {/ 1}}的私人字段:
MyClass
(来源:https://stackoverflow.com/a/34658/1682559)
正如您所看到的,只需将boolean myField
放在import java.lang.reflect.Field;
...
try {
Field field = MyClass.class.getDeclaredField("myField");
field.setAccessible(true);
field.set(localInstanceOfMyClass, true); // true is the value I want to assign to myField
}
catch (NoSuchFieldException ex){
ex.printStackTrace();
}
catch (IllegalAccessException ex){
ex.printStackTrace();
}
catch (IllegalArgumentException ex){
ex.printStackTrace();
}
或private boolean
上即可。
所以,我的问题:是否可以以某种方式制作两个true
,两个用于false
,一个用于public static methods
,我可以在我的所有Method
中使用?澄清:
Field
基于以上示例Test Classes
en TestMethodsClass.setPrivateField(... some parameters ...);
TestMethodsClass.runPrivateVoidMethod(... some parameters ...);
TestMethodsClass.runPrivateReturnMethod(... some parameters ...);
的参数示例:
void doSomething()
提前感谢您的回复。
答案 0 :(得分:0)
好的,实际上很容易..
TestMethodsClass:
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class TestMethodsClass
{
public static void runPrivateVoidMethod(Object ob, String methodName, Class<?>[] parameters){
try {
Method method = null;
if(parameters == null){
Class<?>[] nullParameter = (Class[])null;
method = ob.getClass().getDeclaredMethod(methodName, nullParameter);
}
else
method = ob.getClass().getDeclaredMethod(methodName, parameters);
if(method != null){
method.setAccessible(true);
method.invoke(ob);
}
}
catch (NoSuchMethodException ex){
ex.printStackTrace();
}
catch (IllegalAccessException ex){
ex.printStackTrace();
}
catch (IllegalArgumentException ex){
ex.printStackTrace();
}
catch (InvocationTargetException ex) {
ex.printStackTrace();
}
}
public static Object runPrivateReturnMethod(Object ob, String methodName, Class<?>[] parameters){
Object returnObject = null;
try {
if(parameters == null){
Class<?>[] nullParameter = (Class[])null;
Method method = ob.getClass().getDeclaredMethod(methodName, nullParameter);
method.setAccessible(true);
returnObject = method.invoke(ob);
}
else{
Method method = ob.getClass().getDeclaredMethod(methodName, parameters);
method.setAccessible(true);
method.invoke(ob);
}
}
catch (NoSuchMethodException ex){
ex.printStackTrace();
}
catch (IllegalAccessException ex){
ex.printStackTrace();
}
catch (IllegalArgumentException ex){
ex.printStackTrace();
}
catch (InvocationTargetException ex) {
ex.printStackTrace();
}
return returnObject;
}
public static void setPrivateField(Object ob, String fieldName, Object value){
try {
Field field = ob.getClass().getDeclaredField(fieldName);
field.setAccessible(true);
field.set(ob, value);
}
catch (NoSuchFieldException ex){
ex.printStackTrace();
}
catch (IllegalAccessException ex){
ex.printStackTrace();
}
catch (IllegalArgumentException ex){
ex.printStackTrace();
}
}
}
如何称呼它:
// Set private field "boolean myField" from MyClass to true
TestMethodsClass.setPrivateField(localInstanceOfMyClass, "myField", true);
// Run private method "doSomething()" from MyClass
TestMethodsClass.runPrivateVoidMethod(localInstanceOfMyClass, "doSomething", null);
// Run private method "doSomething()" from MyClass and assign the return value to a local field
boolean test = (boolean)TestMethodsClass.runPrivateReturnMethod(localInstanceOfMyClass, "doSomething", null);
我还没有完全测试过,所以我不知道它是否适用于所有类型。但就我所知,弦乐和布里琴的效果非常好。当我从我的项目中获得一些空余时间时,稍后会做更多的测试。