微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

方法抛出“java.lang.IllegalArgumentException”异常如何使用java反射设置私有最终值

如何解决方法抛出“java.lang.IllegalArgumentException”异常如何使用java反射设置私有最终值

我正在为某种方法编写junit 代码。并且在此方法中需要设置私有最终值以实现完整的代码覆盖。但我得到 Method throw '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);
    }

这是我的异常日志

java.lang.IllegalArgumentException: 无法将 java.lang.Long 字段 com.azure.messaging.eventhubs.EventData$SystemProperties.sequenceNumber 设置为 (long)10 在 sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167) 在 sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(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(Native Method) 在 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.proceed(MethodInvocation.java:60) 在 org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(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.proceed(InvocationInterceptorChain.java:104) 在 org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(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.hierarchical.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.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:135) 在 org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) 在 org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125) 在 org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135) 在 org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123) 在 org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) 在 org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively (NodeTestTask.java:122) 在 org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80) 在 java.util.ArrayList.forEach(ArrayList.java:1249) 在 org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38) 在 org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139) 在 org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) 在 org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125) 在 org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135) 在 org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123) 在 org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) 在 org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively (NodeTestTask.java:122) 在 org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80) 在 java.util.ArrayList.forEach(ArrayList.java:1249) 在 org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38) 在 org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139) 在 org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) 在 org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125) 在 org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135) 在 org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123) 在 org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) 在 org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively (NodeTestTask.java:122) 在 org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80) 在 org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32) 在 org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57) 在 org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51) 在 org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:170) 在 org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:154) 在 org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:90) 在 org.gradle.api.internal.tasks.testing.junitplatform.JUnitPlatformTestClassprocessor$CollectAllTestClassesExecutor.processAllTestClasses(JUnitPlatformTestClassprocessor.java:102) 在 org.gradle.api.internal.tasks.testing.junitplatform.JUnitPlatformTestClassprocessor$CollectAllTestClassesExecutor.access$000(JUnitPlatformTestClassprocessor.java:82) 在 org.gradle.api.internal.tasks.testing.junitplatform.JUnitPlatformTestClassprocessor.stop(JUnitPlatformTestClassprocessor.java:78) 在 org.gradle.api.internal.tasks.testing.SuiteTestClassprocessor.stop(SuiteTestClassprocessor.java:61) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在 java.lang.reflect.Method.invoke(Method.java:498) 在 org.gradle.internal.dispatch.Reflectiondispatch.dispatch(Reflectiondispatch.java:35) 在 org.gradle.internal.dispatch.Reflectiondispatch.dispatch(Reflectiondispatch.java:24) 在 org.gradle.internal.dispatch.ContextClassLoaderdispatch.dispatch(ContextClassLoaderdispatch.java:32) 在 org.gradle.internal.dispatch.ProxydispatchAdapter$dispatchingInvocationHandler.invoke(ProxydispatchAdapter.java:93) 在 com.sun.proxy.$Proxy2.stop(来源不明) 在 org.gradle.api.internal.tasks.testing.worker.TestWorker.stop(TestWorker.java:132) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在 java.lang.reflect.Method.invoke(Method.java:498) 在 org.gradle.internal.dispatch.Reflectiondispatch.dispatch(Reflectiondispatch.java:35) 在 org.gradle.internal.dispatch.Reflectiondispatch.dispatch(Reflectiondispatch.java:24) 在 org.gradle.internal.remote.internal.hub.MessageHubBackedobjectConnection$dispatchWrapper.dispatch(MessageHubBackedobjectConnection.java:175) 在 org.gradle.internal.remote.internal.hub.MessageHubBackedobjectConnection$dispatchWrapper.dispatch(MessageHubBackedobjectConnection.java:157) 在 org.gradle.internal.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:404) 在 org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:63) 在 org.gradle.internal.concurrent.ManagedExecutorImpl$1.run(ManagedExecutorImpl.java:46)

================================================ ============================

java.lang.NullPointerException 在 com.azure.messaging.eventhubs.models.EventContext.updateCheckpoint(EventContext.java:101) 在 com.tdchannels.sdk.ms.channel.services.EventHubConsumerThread.lambda$new$0(EventHubConsumerThread.java:106) 在 com.tdchannels.sdk.ms.channel.services.impl.EventHubConsumerServiceImpltest.partitionProcessorTest(EventHubConsumerServiceImpltest.java:99)

解决方法

无法将 java.lang.Long 字段 com.azure.messaging.eventhubs.EventData$SystemProperties.sequenceNumber 设置为 (long)10 at

根据跟踪,您将 long 原语设置为 Long object 尝试设置 Object Long 而不是 long

像这样

Long aLong = Long.valueOf(100);
try{
    f.setLong(eventData,aLong);
}
catch(Throwable e)
{
    e.printStackTrace();
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。