Java-将私有静态字段复制到局部变量的反射

Java-将私有静态字段复制到局部变量的反射,java,reflection,static,field,private,Java,Reflection,Static,Field,Private,私有方法:我知道如何: 运行不带参数的私有void方法 使用任意数量和类型的参数运行私有void方法 使用任何类型的返回类型运行不带参数的私有返回方法 使用任意数量和类型的参数以及任意类型的返回类型运行私有返回方法 私有字段:我知道如何: 设置任何类型的私有字段 设置任何类型的私有静态字段 设置任何类型的专用最终字段 设置任何类型的私有静态最终字段 私有字段:我知道如何: 获取任何类型的私有字段 获取任何类型的私有最终字段 私人构造函数:我知道如何: (可用于在单个模式中创建私有构造

私有方法
:我知道如何:

  • 运行不带参数的私有void方法
  • 使用任意数量和类型的参数运行私有void方法
  • 使用任何类型的返回类型运行不带参数的私有返回方法
  • 使用任意数量和类型的参数以及任意类型的返回类型运行私有返回方法
私有字段
:我知道如何:

  • 设置任何类型的私有字段
  • 设置任何类型的私有静态字段
  • 设置任何类型的专用最终字段
  • 设置任何类型的私有静态最终字段
私有字段
:我知道如何:

  • 获取任何类型的私有字段
  • 获取任何类型的私有最终字段
私人构造函数
:我知道如何:

(可用于在单个模式中创建私有构造函数的新实例,同时保持实例字段为空)

  • 创建不带参数的私有构造函数的新实例
  • 使用任意数量和类型的参数创建私有构造函数的新实例

我不知道的和我想知道的:

  • 获取任何类型的私有静态字段,并将其设置为局部(非静态)变量
  • 获取任何类型的私有静态final字段,并将其设置为局部(非静态)变量
我应该在代码中更改什么以及如何更改

TestMethodsClass中的此部分:

if(Modifier.isStatic(field.getModifiers())){
    Field modifiersField = Field.class.getDeclaredField("modifiers");
    modifiersField.setAccessible(true);
    modifiersField.setInt(field, field.getModifiers() & ~Modifier.STATIC);
}
或者我在UnitTest类中执行的调用:

// Getting private STATIC String myString of the class MyClass and set it to a local variable
// public static call equivalent: String localString = myClassInstance.myString;
// public Getter call equivalent: String localString = myClassInstance.getMyString();
try {
    String localString = TestMethodsClass.getPrivateField(myClassInstance, "myString");
}
catch (MyUnitTestException ex) {
    Assert.fail("setPrivateField caused an Exception: " + ex.getThrownException());
}

我当前的课程和一些如何命名的示例:

TestMethodsClass.java:

package unittests;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

public class TestMethodsClass
{
    // Test method to run a private void Method from a class
    public static void runPrivateVoidMethod(Object ob, String methodName, Class<?>[] paramTypes, Object[] paramValues) throws MyUnitTestException{
        try {
            Method method = null;
            if(paramTypes == null){
                method = ob.getClass().getDeclaredMethod(methodName, (Class[])null);
                if(method != null){
                    method.setAccessible(true);
                    method.invoke(ob);
                }
            }
            else{
                if(paramValues != null && paramTypes.length == paramValues.length){
                    method = ob.getClass().getDeclaredMethod(methodName, paramTypes);
                    if(method != null){
                        method.setAccessible(true);
                        method.invoke(ob, paramValues);
                    }
                }
                else
                    runPrivateReturnMethod(ob, methodName, null, null);
            }
        }
        catch (NoSuchMethodException ex){
            throw new MyUnitTestException(ex);
        }
        catch (IllegalAccessException ex){
            throw new MyUnitTestException(ex);
        }
        catch (IllegalArgumentException ex){
            throw new MyUnitTestException(ex);
        }
        catch (InvocationTargetException ex) {
            throw new MyUnitTestException(ex);
        }
    }

    // Test method to run a private Method that returns something from a class
    public static Object runPrivateReturnMethod(Object ob, String methodName, Class<?>[] paramTypes, Object[] paramValues) throws MyUnitTestException{
        Object returnObject = null;
        try {
            Method method = null;
            if(paramTypes == null){
                method = ob.getClass().getDeclaredMethod(methodName, (Class[])null);
                if(method != null){
                    method.setAccessible(true);
                    returnObject = method.invoke(ob);
                }
            }
            else{
                if(paramValues != null && paramTypes.length == paramValues.length){
                    method = ob.getClass().getDeclaredMethod(methodName, paramTypes);
                    if(method != null){
                        method.setAccessible(true);
                        returnObject = method.invoke(ob, paramValues);
                    }
                }
                else
                    returnObject = runPrivateReturnMethod(ob, methodName, null, null);
            }
        }
        catch (NoSuchMethodException ex){
            throw new MyUnitTestException(ex);
        }
        catch (IllegalAccessException ex){
            throw new MyUnitTestException(ex);
        }
        catch (IllegalArgumentException ex){
            throw new MyUnitTestException(ex);
        }
        catch (InvocationTargetException ex) {
            throw new MyUnitTestException(ex);
        }
        return returnObject;
    }

    // Test method to set a private Field from a class
    public static void setPrivateField(Object ob, String fieldName, Object value) throws MyUnitTestException{
        try {
            Field field = ob.getClass().getDeclaredField(fieldName);
            field.setAccessible(true);
            if(Modifier.isStatic(field.getModifiers())){
                Field modifiersField = Field.class.getDeclaredField("modifiers");
                modifiersField.setAccessible(true);
                modifiersField.setInt(field, field.getModifiers() & ~Modifier.STATIC);
            }
            if(Modifier.isFinal(field.getModifiers())){
                Field modifiersField = Field.class.getDeclaredField("modifiers");
                modifiersField.setAccessible(true);
                modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
            }
            field.set(ob, value);
        }
        catch (NoSuchFieldException ex){
            throw new MyUnitTestException(ex);
        }
        catch (IllegalAccessException ex){
            throw new MyUnitTestException(ex);
        }
        catch (IllegalArgumentException ex){
            throw new MyUnitTestException(ex);
        }
    }

    // Test method to access a private Field from a class
    public static Object getPrivateField(Object ob, String fieldName) throws MyUnitTestException{
        Object returnObject = null;
        try {
            Field field = ob.getClass().getDeclaredField(fieldName);
            if(Modifier.isStatic(field.getModifiers())){
                Field modifiersField = Field.class.getDeclaredField("modifiers");
                modifiersField.setAccessible(true);
                modifiersField.setInt(field, field.getModifiers() & ~Modifier.STATIC);
            }
            returnObject = field.get(ob);
        }
        catch (NoSuchFieldException ex) {
            throw new MyUnitTestException(ex);
        }
        catch (IllegalAccessException ex) {
            throw new MyUnitTestException(ex);
        }
        catch (IllegalArgumentException ex) {
            throw new MyUnitTestException(ex);
        }
        return returnObject;
    }

    // test method to access a private Constructor (of a Singleton class)
    public static Object getPrivateConstuctor(Object ob, Class<?>[] paramTypes, Object[] paramValues) throws MyUnitTestException{
        Object returnObject = null;
        try {
            Constructor<?> constructor = null;
            if(paramTypes == null){
                constructor = ob.getClass().getDeclaredConstructor(paramTypes);
                if(constructor != null){
                    constructor.setAccessible(true);
                    returnObject = constructor.newInstance();
                }
            }
            else{
                if(paramValues != null && paramTypes.length == paramValues.length){
                    constructor = ob.getClass().getDeclaredConstructor(paramTypes);
                    if(constructor != null){
                        constructor.setAccessible(true);
                        returnObject = constructor.newInstance(paramValues);
                    }
                }
                else
                    getPrivateConstuctor(ob, null, null);
            }
        }
        catch (NoSuchMethodException ex) {
            throw new MyUnitTestException(ex);
        }
        catch (InstantiationException ex) {
            throw new MyUnitTestException(ex);
        }
        catch (IllegalAccessException ex) {
            throw new MyUnitTestException(ex);
        }
        catch (IllegalArgumentException ex) {
            throw new MyUnitTestException(ex);
        }
        catch (InvocationTargetException ex) {
            throw new MyUnitTestException(ex);
        }
        return returnObject;
    }
}
MyUnitTestException.java:

package unittests;

public class MyUnitTestException extends Exception
{
    private static final long serialVersionUID = 1L;

    private Throwable thrownException;

    public MyUnitTestException(Throwable ex){
        super(ex);
        thrownException = ex;
    }

    public String getThrownException(){
        if(thrownException != null)
            return thrownException.getClass().getName();
        else
            return null;
    }
}
用法示例:

将MyClass类的私有int myInteger设置为3:

// public static call equivalent: myClassInstance.myInteger = 3;
// public Setter call equivalent: myClassInstance.setMyInteger(3);
try {
    TestMethodsClass.setPrivateField(myClassInstance, "myInteger", 3);
}
catch (MyUnitTestException ex) {
    Assert.fail("setPrivateField caused an Exception: " + ex.getThrownException());
}
获取类MyClass的私有字符串myString并将其设置为局部变量:

// public static call equivalent: String localString = myClassInstance.myString;
// public Getter call equivalent: String localString = myClassInstance.getMyString();
try {
    String localString = TestMethodsClass.getPrivateField(myClassInstance, "myString");
}
catch (MyUnitTestException ex) {
    Assert.fail("setPrivateField caused an Exception: " + ex.getThrownException());
}
// public call equivalent: MyClass localMyClassInstance = new MyClass();
try {
    MyClass localMyClassInstance = TestMethodsClass.getPrivateConstructor(myClassInstance, null, null);
}
catch (MyUnitTestException ex) {
    Assert.fail("setPrivateField caused an Exception: " + ex.getThrownException());
}
获取MyClass()的私有(构造函数)并将其设置为局部变量:

// public static call equivalent: String localString = myClassInstance.myString;
// public Getter call equivalent: String localString = myClassInstance.getMyString();
try {
    String localString = TestMethodsClass.getPrivateField(myClassInstance, "myString");
}
catch (MyUnitTestException ex) {
    Assert.fail("setPrivateField caused an Exception: " + ex.getThrownException());
}
// public call equivalent: MyClass localMyClassInstance = new MyClass();
try {
    MyClass localMyClassInstance = TestMethodsClass.getPrivateConstructor(myClassInstance, null, null);
}
catch (MyUnitTestException ex) {
    Assert.fail("setPrivateField caused an Exception: " + ex.getThrownException());
}
使用MyOtherObject作为参数运行私有void Setter方法:

// public call equivalent: myObjectInstance.setMyOtherClass(myOtherClassInstance);
try {
    TestMethodsClass.runPrivateVoidMethod(MyClass, "setMyOtherClass", new Class<?>[]{ MyOtherClass.class }, new Object[]{ myOtherClassInstance });
}
catch (MyUnitTestException ex) {
    Assert.fail("setPrivateField caused an Exception: " + ex.getThrownException());
}
实例
-控制器字段:

// Singleton class where we store all lists of the Models
public class Controller
{
    // Default field used by the Singleton Design Pattern
    private static Controller instance;

    ...

    // This Constructor is private since this is a Singleton class
    private Controller() {
        ...
    }

    // Default method used by the Singleton Design Pattern
    public static Controller getInstance(){
        if(instance == null)
            instance = new Controller();

        return instance;
    }

    ...
}
这会导致catch中出现以下UnitTest.error

junit.framework.AssertionFailedError: getPrivateField caused an Exception: java.lang.IllegalAccessException
    at junit.framework.Assert.fail(Assert.java:47)
    at controllers.ControllerUnitTest.controllerTest(ControllerUnitTest.java:69)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.rules.ExpectedException$ExpectedExceptionStatement.evaluate(ExpectedException.java:110)
    at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
提前感谢您的回复

  • 获取任何类型的
    私有静态
    字段
    ,并将其设置为本地(非静态)变量
  • 获取任何类型的
    私有静态final
    字段
    ,并将其设置为本地(非静态)变量
类似地,对于
final
字段

如果您的意思是将字段的值设置为局部变量的值

Field privateStaticField = ...;
privateStaticField.setAccessible(true);
Object localVariable = ...;
privateStaticField.set(null, localVariable);

对于
final
成员,它稍微复杂一些,因为您必须使用修饰符。如果字段是常量表达式,情况会变得更糟。请参阅。

请参阅
如果
您可以查看此评论的
恼人之处
,并将
解决方案应用于
您的
问题:-)@Duncan我只是用它来更清楚地说明区别。例如“private”、“private static”、“private final”和“private static final”。啊,好吧,删除它,看看它是否像这样更好。嗨,我知道用final我需要字节减法final修饰符,我已经在我的代码中包含了它。与
.setAccessible相同(true)用于静态的。我把当前的
getPrivateField方法
单独放在一起,因为这是我问题中的方法。@KevinCruijssenn那么问题出在哪里?好吧,它不起作用了。。当我试图获取私有静态字段时,我得到了一个
IllegalAccessException
。@KevinCruijssen请将完整堆栈跟踪添加到您的问题中。我已经添加了堆栈跟踪和我的UnitTest/Controller(请参见编辑1)
Field privateStaticField = ...;
privateStaticField.setAccessible(true);
Object localVariable = ...;
privateStaticField.set(null, localVariable);