Javassist NotFoundException on getting java.io.Serializable using JDK9

I have the following code:

private static CtClass resolveCtClass(String clazz) throws NotFoundException { ClassPool pool = ClassPool.getDefault(); return pool.get( clazz ); } 

When launched under JDK8, if this method is called using java.io.Serializable , it works, but when working in the JDK9 environment, it throws a NotFoundException .

Is there something I have not noticed here?

+6
source share
2 answers

This no longer happens with current builds of EA Java 9. Class files are now always localized, even if they are encapsulated in a module.

This is a consequence of the encapsulation of the Java 9 module, where non-exportable resources are no longer available through the ClassLoader API. Under the covers Javassist calls

 ClassLoader.getSystemClassLoader().findResource("java/io/Serializable.class"); 

to get the class file for Serializable . He then parses this class file and presents the information similar to the Java reflection API, but without loading the class so that it can be edited before it is loaded.

Prior to Java 8, this class file was available since most class loaders rely on finding the class file before loading it, so the above call returned a URL pointing to the file. Since Java 9, the resources of named modules are only available through the new findResource(String, String) API method, where the second argument names the module of this class.

Short answer: Javassist no longer works with Java 9, and none of its dependent projects will be. This is a known issue with the current implementation of Java 9 and will hopefully be fixed before release.

+6
source

(I never used a Javassist, so I'm just shooting in the dark, here ...)

ClassPool documentation says:

If get() is called on this object, it searches for the various sources represented by ClassPath to find the class file, and then creates a CtClass object representing this class file.

This seems to be related to the concept of a class path. Looking at ClassPath and CtClass supports this assumption.

If so, then Javassist might just not be suitable to look into the new JDK 9 modules .

If my guess is correct, you cannot get any JDK class from the pool. This should be easily verifiable.

+1
source

All Articles