-
-
Notifications
You must be signed in to change notification settings - Fork 352
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
refactor(classloader): reduces to the maximum the use of classloaders #897
Conversation
throw new SpoonClassNotFoundException("cannot load class: " + getQualifiedName() + " with class loader " | ||
+ Thread.currentThread().getContextClassLoader(), cnfe); | ||
+ Thread.currentThread().getContextClassLoader(), e); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't it misleading to say "with class loader " Thread.currentThread().getContextClassLoader()? This is not this one that is in cause in here, but rather the URLClassLoader with class paths. Shouldn't you better say "in class paths ...".
@monperrus Are you aware that this PR will broke Nopol and NPEFix? |
Then a test case is missing because nothing is broken currently. |
How does a not merged pull request can currently broke two external projects? |
/** | ||
* Sets the class loader used to compile/process the input source code. | ||
*/ | ||
void setInputClassLoader(ClassLoader classLoader); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm a bit concerned about removing this method that I'm using. The idea is that when you embedded this Spoon as a third-party framework in another framework, you need a simple and direct way to instruct Spoon the class loader that you want to use and not bother about some complicated stuff with class paths and thread context class loader. So I would vote against removing this method.
Hi Lionel, Could you propose a test case showing the behavior you rely on? |
Because not all class loader are sourceClassPath oriented. You can build a customer class loader that does at all rely on class paths. Building a test case for that is obvious: I want to be able to call setCallItWhateverYouWant(myCustomClassLoader) where myCustomClassLoader is an instance of class that extends ClassLoader (not URLClassLoader, neither something which is in relation with class paths). |
I understand your use case. Bbut what the Spoon code does is then simply Thread.currentThread().setContextClassLoader(myCustomClassLoader); so to me, it looks more meaningful that you call this directly instead of an implicit, poorly documented and untested: *.getEnvironment().setInputClassLoader(myCustomClassLoader) Am I missing something? --Martin |
I agree that Thread.currentThread().setContextClassLoader(myCustomClassLoader) does the job. I just a bit concern about the fact that this "contract" (i.e. what to do when one wants to instruct Spoon to use a custom, non-classpath oriented, classloader) should be documented somewhere. setInputClassloader() was the easy obvious place for that: the developer that is embedding Spoon in her/his framework directly and easily bumps into the method when using code completion for instance. Now I have the feeling that this will be fuzzier to find this information. But when one knows... Just my €0.02. |
Basically, the contract with the Spoon-specific classloader is twofold:
With a classpath-based classloader (aka URLClassLoader) as supported by With a custom classloader, only 2) works easily. 1) is impossible So the only way to suport 1) and 2) with a custom class loader is to do That said, we can do this in Spoon (+ documentation + test). But we setClassLoader (instead of the current two setClassLoader and OK for you ? --Martin |
9932cde
to
f0b698d
Compare
4f795b5
to
bd6e987
Compare
should simplify debug when Maven classloaders, JDT classloaders, IDE classloaders interplay.
bd6e987
to
811f7a7
Compare
Dear all, I managed to get rid of setInputClassLoader in my projects that use Spoon. I no longer need to transmit any class loader to Spoon. So as far as I am concerned, this is ok for me to deprecate/remove this method. Lionel. |
just one note to the class loader usage in spoon. The class loader is set thread locally and therefore influences all the code which is called in that thread later after spoon.runWithClassLoader(()->{
// only code in scope of this function will use that class loader.
//When we leave the function, then it should use origin class loader again.
}) |
should simplify debug when Maven classloaders, JDT classloaders, IDE classloaders
interplay.