0

We have a plugin named mockito and it has cglib-nodep-2.2.2.jar, hamcrest-core-1.3.jar, mockito-core-1.10.19.jar, objenesis-2.4.jar, jmockit-1.49.jar in it's bundle-classpath and have exported all the packages from all the jar files for consumption by other bundles. The 'automatic module name' is set to mockito.

We are currently in plan to migrate from eclipse 4.16 and Java 11 to eclipse 4.27 and java 17 respectively.

In one of the test case plugin, mockito bundle is imported and Test Class creates a mocked IProject by IProject project = Mockito.mock(IProject.class);. While running the test it is failing with error java.lang.reflect.InaccessibleObjectException: Unable to make protected final java.lang.Class java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain) throws java.lang.ClassFormatError accessible: module java.base does not "opens java.lang" to unnamed module @738e057b at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:354) at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:297) at java.base/java.lang.reflect.Method.checkCanSetAccessible(Method.java:199) at java.base/java.lang.reflect.Method.setAccessible(Method.java:193) at org.mockito.cglib.core.ReflectUtils$2.run(ReflectUtils.java:57) at java.base/java.security.AccessController.doPrivileged(AccessController.java:318) at org.mockito.cglib.core.ReflectUtils.<clinit>(ReflectUtils.java:47) at org.mockito.cglib.core.KeyFactory$Generator.generateClass(KeyFactory.java:167) at org.mockito.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25) at org.mockito.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:217) at org.mockito.cglib.core.KeyFactory$Generator.create(KeyFactory.java:145) at org.mockito.cglib.core.KeyFactory.create(KeyFactory.java:117) at org.mockito.cglib.core.KeyFactory.create(KeyFactory.java:109) at org.mockito.cglib.core.KeyFactory.create(KeyFactory.java:105) at org.mockito.cglib.proxy.Enhancer.<clinit>(Enhancer.java:70) at org.mockito.internal.creation.cglib.ClassImposterizer.createProxyClass(ClassImposterizer.java:95) at org.mockito.internal.creation.cglib.ClassImposterizer.imposterise(ClassImposterizer.java:57) at org.mockito.internal.creation.cglib.ClassImposterizer.imposterise(ClassImposterizer.java:49) at org.mockito.internal.creation.cglib.CglibMockMaker.createMock(CglibMockMaker.java:24) at org.mockito.internal.util.MockUtil.createMock(MockUtil.java:33) at org.mockito.internal.MockitoCore.mock(MockitoCore.java:59) at org.mockito.Mockito.mock(Mockito.java:1285) at org.mockito.Mockito.mock(Mockito.java:1163)

Other options tried:

  1. I also moved all the jars from classpath to modulepath and edited the Manifest.MF of each jar and introduced Automatic-module-Name (eg.Automatic-Module-Name: cglib.nodep). But, still I get the same error.

Shouldn't the module be named because 'Automatic Module name' is set in Manifest.MF?

The consideration of --add-opens java.lang==ALL-UNNAMED is not possible, as it would open the packages for all unnamed modules.

How to resolve this issue and open the packages java.lang package for access to mockito bundle?

How to do it the right way?

Thanks, Pal

5
  • In Eclipse/OSGi plugins/bundles can be started and stopped at runtime which is not possible in the Java Platform Module System. The Automatic-Module-Name (which by the way exists since Java 9) is not used, nor required for an Eclipse-based application. Everything except the system library is in an Eclipse/OSGi application in the unnamed module. Why do you say that --add-opens java.lang==ALL-UNNAMED is not possible? Commented Jul 16, 2024 at 14:39
  • But, Won't we required to Automatic-Module-Name in the Manifest.mf? I thought it is a required for eclipse while using Java 11 or later. won't it open the java.lang package to all unnamed modules. Commented Jul 16, 2024 at 14:51
  • 1
    The Automatic-Module-Name is only used when using the plugin JAR in a modular plain (non-OSGi) Java application, but not in Eclipse/OSGi. Your issue is related to the fact that in Java 16 and 17 generics became more restrictive. So in order to migrate to Java 17, you should adapt your code to these changes. By the way, Eclipse 4.27 is already 5 releases behind. Commented Jul 16, 2024 at 18:41
  • See also stackoverflow.com/q/69177973/6505250 Commented Jul 17, 2024 at 6:04
  • 4
    Since this exception is caused by Mockito, pay attention to the version number of Mockito, 1.10 according to the jar name. The current version is 5.12. If you want to stay with ancient software, don’t update the Java version. Commented Jul 17, 2024 at 10:36

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.