Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve documentation of Reflection.type() for classloader issues #10

Open
clementdenis opened this issue Jul 10, 2013 · 0 comments
Open

Comments

@clementdenis
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant