NoSuchMethodError on Decapsulation.setField with JDK9 build 118 #278

russgold opened this Issue May 18, 2016 · 5 comments


None yet

2 participants


We get:

java.lang.NoSuchMethodError: sun.reflect.ReflectionFactory.newFieldAccessor(Ljava/lang/reflect/Field;Z)Lsun/reflect/FieldAccessor;
at weblogic.server.ServerLifecycleRuntimeTest.testShutdownRequestTimeout(
at weblogic.server.ServerLifecycleRuntimeTest.testNewDefaultShutdownRequestTimeout(

when running with JMockit 1.23 on the latest version of JDK9, as JMockit is using internal Sun APIs to access internals. These were removed in build 115

@rliesenfeld rliesenfeld added the other label May 18, 2016

Is the test calling Deencapsulation.setField on a static final field? If so, it won't work on JDK 9 for the reason exposed, and the test should be changed. However, apart from that there is no problem in keeping this method for use on JDK 8 or older.

russgold commented May 18, 2016 edited

Yes, the call from which this stack trace was taken does set a static final field. It works in JDK8 just fine. It doesn't work in JDK9, as of build 115. Apparently, we have about a thousand such calls in our tests, so changing all of these tests isn't particularly feasible. Are you saying that you are unable to make it work in the latest JDK9?


As far as I know, there is no way to make it work in JDK 9, because they removed the "sun.reflect.ReflectionFactory" class, with no equivalent replacement. I did some google searches, but couldn't find anything useful on the topic.

I am inclined to remove this feature (the ability to set a static final field), as it has always been of dubious value anyway. And if it won't be possible anymore in Java 9+, people will have to change their tests sooner or later.


Take a look at the method setPrivateStaticField in this class

I have confirmed that this works in JDK9, other than for primitives and strings, which the compiler might optimize away.

It turns out to be quite useful in a lot of cases, at least with regard to test written with SimpleStub. With JMockit, you could probably do it a different way; unfortunately, a lot of our developers have chosen to do it this way.

@rliesenfeld rliesenfeld self-assigned this May 19, 2016
@rliesenfeld rliesenfeld added bug and removed other labels May 19, 2016

Yes, it does work. For some reason, I thought that setting the field through "ReflectionFactory#newFieldAccessor(...)" was needed, but it's not.

@russgold russgold added a commit to russgold/jmockit1 that referenced this issue May 20, 2016
@russgold russgold Fix issue #278 - NoSuchMethodError when setting static final private …
…field via Deencapsulate.setField
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment