You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've been replacing calls to the java.util.reflect package with equivalent code from fest-reflect, and I had an issue with type("...").load(), which throws a ClassNotFoundException when Class.forName("...") did not.
I thought that type("...").load() was strictly equivalent to Class.forName("..."), minus the checked exceptions. In fact, it is not : Class.forName() uses ClassLoader.getCallerClassLoader to determine the class loader to use, while fest-reflect's Type.load() uses the classloader used to load itself.
In complex cases (maven + ant in my case), fest-reflect's jar is not loaded in the same class loader as the application classes, which causes the problem (and can be fixed with .withClassLoader(getClass().getClassLoader()) in the calling class).
A solution would be to use sun.reflect.Reflection.getCallerClass() to determine the right classloader, but it would add a dependency to a sun package (bad bad bad).
When JEP 176 is available, it should be possible to use this new mechanism to implement a reliable detection of the caller class loader.
In the meantime, the documentation should explicitly mention this classloading issue, as it can be tricky to debug.
The text was updated successfully, but these errors were encountered:
I've been replacing calls to the java.util.reflect package with equivalent code from fest-reflect, and I had an issue with type("...").load(), which throws a ClassNotFoundException when Class.forName("...") did not.
I thought that type("...").load() was strictly equivalent to Class.forName("..."), minus the checked exceptions. In fact, it is not : Class.forName() uses ClassLoader.getCallerClassLoader to determine the class loader to use, while fest-reflect's Type.load() uses the classloader used to load itself.
In complex cases (maven + ant in my case), fest-reflect's jar is not loaded in the same class loader as the application classes, which causes the problem (and can be fixed with .withClassLoader(getClass().getClassLoader()) in the calling class).
A solution would be to use sun.reflect.Reflection.getCallerClass() to determine the right classloader, but it would add a dependency to a sun package (bad bad bad).
When JEP 176 is available, it should be possible to use this new mechanism to implement a reliable detection of the caller class loader.
In the meantime, the documentation should explicitly mention this classloading issue, as it can be tricky to debug.
The text was updated successfully, but these errors were encountered: