方法引发了“java.lang.illegalargumentexception”异常如何使用java反射设置私有最终值

gk7wooem  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(438)

我正在为某个方法编写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);
    }

这是我的异常日志
java.lang.illegalargumentexception:无法在sun.reflect.unsafefieldaccessorimpl.throwsetillegalargumentexception(unsafefieldaccessorimpl)上将java.lang.long field com.azure.messaging.eventhubs.eventdata$systemproperties.sequencenumber设置为(long)10。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(本机方法)在sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl)。java:62)在sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl。java:43)在java.lang.reflect.method.invoke(方法。java:498)位于org.junit.platform.commons.util.reflectionutils.invokemethod(reflectionutils。java:675)在org.junit.jupiter.engine.execution.methodinvocation.procedue(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)在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.procedue(invocationinterceptorchain。java:62)在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)在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$execute执行$5(nodetesttask。java:135)在org.junit.platform.engine.support.hierarchical.throwablecollector.execute(throwablecollector。java:73)在org.junit.platform.engine.support.hierarchy.nodetesttask.lambda$executecursively$7(nodetesttask。java:125)在org.junit.platform.engine.support.hierarchy.node.around(节点。java:135)在org.junit.platform.engine.support.hierarchy.nodetesttask.lambda$executecursively$8(nodetesttask。java:123)在org.junit.platform.engine.support.hierarchical.throwablecollector.execute(throwablecollector。java:73)在org.junit.platform.engine.support.hierarchical.nodetesttask.executeRecursive(nodetesttask。java:122)在org.junit.platform.engine.support.hierarchy.nodetesttask.execute(nodetesttask。java:80)在java.util.arraylist.foreach(arraylist。java:1249)在org.junit.platform.engine.support.hierarchical.samethreadherarchicaltestexecutorservice.invokeall(samethreadherarchicaltestexecutorservice)。java:38)在org.junit.platform.engine.support.hierarchy.nodetesttask.lambda$executecursively$5(nodetesttask。java:139)在org.junit.platform.engine.support.hierarchical.throwablecollector.execute(throwablecollector。java:73)在org.junit.platform.engine.support.hierarchy.nodetesttask.lambda$executecursively$7(nodetesttask。java:125)在org.junit.platform.engine.support.hierarchy.node.around(node。java:135)在org.junit.platform.engine.support.hierarchy.nodetesttask.lambda$execute执行$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.hierarchy.nodetesttask.execute(nodetesttask。java:80)在java.util.arraylist.foreach(arraylist。java:1249)位于org.junit.platform.engine.support.hierarchical.samethreadherarchicaltestexecutorservice.invokeall(samethreadherarchicaltestexecutorservice)。java:38)在org.junit.platform.engine.support.hierarchy.nodetesttask.lambda$execute执行$5(nodetesttask。java:139)在org.junit.platform.engine.support.hierarchical.throwablecollector.execute(throwablecollector。java:73)在org.junit.platform.engine.support.hierarchy.nodetesttask.lambda$executecursively$7(nodetesttask。java:125)在org.junit.platform.engine.support.hierarchy.node.around(节点。java:135)在org.junit.platform.engine.support.hierarchy.nodetesttask.lambda$executecursively$8(nodetesttask。java:123)在org.junit.platform.engine.support.hierarchical.throwablecollector.execute(throwablecollector。java:73)在org.junit.platform.engine.support.hierarchical.nodetesttask.executeRecursive(nodetesttask。java:122)在org.junit.platform.engine.support.hierarchy.nodetesttask.execute(nodetesttask。java:80)位于org.junit.platform.engine.support.hierarchical.samethreadherarchicaltestexecutorservice.submit(samethreadherarchicaltestexecutorservice)。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(本机方法)在sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl)。java:62)在sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl。java:43)在java.lang.reflect.method.invoke(方法。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(本机方法)sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl。java:62)在sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl。java:43)在java.lang.reflect.method.invoke(方法。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.eventhubcummerthread.lambda$new$0(eventhubcummerthread。java:106)在com.tdchannels.sdk.ms.channel.services.impl.eventhubconsumerserviceimpltest.partitionprocessortest(eventhubconsumerserviceimpltest)。java:99)

ha5z0ras

ha5z0ras1#

无法将java.lang.long字段com.azure.messaging.eventhubs.eventdata$systemproperties.sequencenumber设置为(long)10 at
根据跟踪,将long原语设置为long object,尝试将对象设置为long而不是long
这样地

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

相关问题