javajavassistjava-17

Javassist with JDK 17


Javassist (version 3.28.0-GA) stopped working after upgrading from JDK 11 to 17.

Calling CtClass#toClass renders the following error:

  ...
  Caused by: 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 @63e31ee
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 javassist.util.proxy.SecurityActions.setAccessible(SecurityActions.java:159)
at javassist.util.proxy.DefineClassHelper$JavaOther.defineClass(DefineClassHelper.java:213)
at javassist.util.proxy.DefineClassHelper$Java11.defineClass(DefineClassHelper.java:52)
at javassist.util.proxy.DefineClassHelper.toClass(DefineClassHelper.java:260)
at javassist.ClassPool.toClass(ClassPool.java:1240)
at javassist.CtClass.toClass(CtClass.java:1392)

Anyone know of a workaround?


Solution

  • This seems to be a well known problem with Javassist and JDK 16 and later (https://github.com/jboss-javassist/javassist/issues/369).

    The solution mentioned in the ticket is to no longer use CtClass.toClass():

    Please do not use ctClass.toClass() anymore, try to [use] ctClass.toClass(neigborClass) as replacement.