Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
方法抛出';java.lang.IllegalArgumentException';例外。如何使用java反射设置私有最终值_Java_Spring Boot_Junit_Junit5_Java Reflection - Fatal编程技术网

方法抛出';java.lang.IllegalArgumentException';例外。如何使用java反射设置私有最终值

方法抛出';java.lang.IllegalArgumentException';例外。如何使用java反射设置私有最终值,java,spring-boot,junit,junit5,java-reflection,Java,Spring Boot,Junit,Junit5,Java Reflection,我正在为某个方法编写junit代码。在这个方法中,需要为完整的代码覆盖率设置私有的最终值。但我得到的方法抛出了“java.lang.IllegalArgumentException”异常 @Test void partitionProcessorTest() throws Exception { EventData eventData = new EventData(body); // below line code fo

我正在为某个方法编写junit代码。在这个方法中,需要为完整的代码覆盖率设置私有的最终值。但我得到的方法抛出了“java.lang.IllegalArgumentException”异常

 @Test
    void partitionProcessorTest() throws Exception {
     
        EventData eventData = new EventData(body);
        
        //  below line code for finding nested class
        Class<?> []clazz = eventData.getClass().getDeclaredClasses();

        // and this is my inner class object 
        Class<?> inner = clazz[0];

        // this is my inner class constructer 
        Constructor<?> constructor = inner.getDeclaredConstructor();
        constructor.setAccessible(true);
        Object obj = constructor.newInstance();

        // this is my sequenceNumber field. which is private final Long type, in inner class 
        Field f = inner.getDeclaredField("sequenceNumber");
        f.setAccessible(true);

        //I am trying to remove final keyword from declared field in my inner class
        Field filed= Field.class.getDeclaredField("modifiers");
        filed.setAccessible(true);
        filed.setInt(f, f.getModifiers() & ~Modifier.FINAL);
       
        // but iam getting exception, where i am donig mistake please suggest me  
        // In this line i am getting exception --> f.setLong(eventData , 10l);
        try{
        f.setLong(eventData , 10l);
        }
        catch(Throwable e)
        {
        e.printStackTrace();
        }
        System.out.println(obj);
    }
@测试
void partitionProcessorTest()引发异常{
EventData EventData=新的EventData(正文);
//用于查找嵌套类的行下代码
类[]clazz=eventData.getClass().getDeclaredClasses();
//这是我的内部类对象
类内=clazz[0];
//这是我的内部类构造函数
构造函数=internal.getDeclaredConstructor();
constructor.setAccessible(true);
Object obj=constructor.newInstance();
//这是我的sequenceNumber字段。它是内部类中的私有最终长类型
字段f=内部.getDeclaredField(“sequenceNumber”);
f、 setAccessible(true);
//我试图从我的内部类中声明的字段中删除final关键字
Field Field=Field.class.getDeclaredField(“修饰符”);
已存档。设置为可访问(true);
setInt(f,f.getModifiers()&~Modifier.FINAL);
//但是我有个例外,我犯了一个错误,请建议我
//在这一行中,我得到异常-->f.setLong(eventData,10l);
试一试{
f、 setLong(事件数据,10l);
}
捕获(可丢弃的e)
{
e、 printStackTrace();
}
系统输出打印项次(obj);
}
这是我的异常日志

java.lang.IllegalArgumentException:无法将java.lang.Long字段com.azure.messaging.eventhubs.EventData$SystemProperties.sequenceNumber设置为(Long)10 在sun.reflect.UnsafeFieldAccessorImpl.throwsetilegalargumentexception(UnsafeFieldAccessorImpl.java:167)处 在sun.reflect.UnsafeFieldAccessorImpl.throwsetilegalargumentexception(UnsafeFieldAccessorImpl.java:195)中 位于sun.reflect.UnsafeObjectFieldAccessorImpl.setLong(UnsafeObjectFieldAccessorImpl.java:120) 位于java.lang.reflect.Field.setLong(Field.java:986) 在com.tdchannels.sdk.ms.channel.services.impl.EventHubConsumerServiceImplTest.partitionProcessorTest(EventHubConsumerServiceImplTest.java:69)上 在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处 位于sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)中 位于java.lang.reflect.Method.invoke(Method.java:498) 位于org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:675) 位于org.junit.jupiter.engine.execution.MethodInvocation.procedure(MethodInvocation.java:60) 位于org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.Procedue(InvocationInterceptorChain.java:125) 位于org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:132) 位于org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:124) 位于org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:74) 位于org.junit.jupiter.engine.execution.ExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(ExecutableInvoker.java:115) 位于org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke$0(ExecutableInvoker.java:105) 位于org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.Procedue(InvocationInterceptorChain.java:104) 在org.junit.jupiter.engine.execution.InvocationInterceptorChain.procedure(InvocationInterceptorChain.java:62)上 位于org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:43) 位于org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:35) 位于org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:104) 位于org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:98) 位于org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$6(TestMethodTestDescriptor.java:202) 位于org.junit.platform.engine.support.hierarchy.ThrowableCollector.execute(ThrowableCollector.java:73) 位于org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:198) 位于org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:135) 位于org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:69) 位于org.junit.platform.engine.support.hierarchy.NodeTestTask.lambda$ExecuteCursive$5(NodeTestTask.java:135) 位于org.junit.platform.engine.support.hierarchy.ThrowableCollector.execute(ThrowableCollector.java:73) 位于org.junit.platform.engine.support.hierarchy.NodeTestTask.lambda$executecurively$7(NodeTestTask.java:125) 位于org.junit.platform.engine.support.hierarchy.Node.around(Node.java:135) 位于org.junit.platform.engine.support.hierarchy.NodeTestTask.lambda$ExecuteCursive$8(NodeTestTask.java:123) 位于org.junit.platform.engine.support.hierarchy.ThrowableCollector.execute(ThrowableCollector.java:73) 在org.junit.platform.engine.support.hierarchy.NodeTestTask.ExecuteCursive上(NodeTestTask.java:122) 位于org.junit.platform.engine.support.hierarchy.NodeTestTask.execute(NodeTestTask.java:80) forEach(ArrayList.java:1249) 位于org.junit.platform.engine.support.hierarchy.samethreadherarchycalTestExecutorService.invokeAll(samethreadherarchycalTestExecutorService.java:38) 位于org.junit.platform.engine.support.hierarchy.NodeTestTask.lambda$ExecuteCursive$5(NodeTestTask.java:139) 位于org.junit.platform.engine.support.hierarchy.ThrowableCollector.execute(ThrowableCollector.java:73) 位于org.junit.platform.engine.support.hierarchy.NodeTestTask.lambda$execute
Long aLong = Long.valueOf(100);
try{
    f.setLong(eventData , aLong);
}
catch(Throwable e)
{
    e.printStackTrace();
}