Hi all,
I'm having what is probably quite a generic classloader problem,
in my junit tests during gradle build of java.
The thing is, I don't have this trouble when junit testing from ANT,
and the code runs fine in practice,
just when testing from gradle, is there a problem.
However, the problem probably stems from my choice of class loader.
I have come across this kind of problem before, and found a work-around
(rather than fix the problem at the time).
This time there is no work-around for me, I must fix it.
I was wondering if I could get some advice regarding this, and something
which appears strange to me.
It could simply be a case of my own misunderstanding of how the ClassLoader
works.
The problem comes from how I choose my class loader.
It is programmatic.
In this particular case, I know that if i hard-code the use current-thread
context classloader,
Thread.currentThread ().getContextClassLoader()
the problem goes away.
However, I am worried that in certain circumstances, this is not the correct
choice.
So I have employed code (based on something I read some time ago regarding
classloaders) to make that choice for me.
After having debugged the test run, I can see that the classloader chosen
seems to actually be a child of the same classloader that does work for this
case.
i.e.,
ClassLoader 1 (Thread.currentThread ().getContextClassLoader())
is parent of (perhaps indirectly ... deep ancestor)
ClassLoader 2 ( gotten via myClass.getClassLoader() )
Now, given that ClassLoader 2 is a descendant of ClassLoader 1, and I know I
have no problems with ClassLoader 1,
why does my ClassLoader 2 not work?
the code I use to determine if classloader 2 is a child of classloader1 is:
public static boolean isChild (final ClassLoader loader1, ClassLoader
loader2)
{
if (loader2 == null || loader1 == null || loader2==loader1) return
false;
for ( ; loader2 != null; loader2 = loader2.getParent ())
{
if (loader2 == loader1) return true;
}
return false;
}
Anyone got any ideas?
Thanks very much in advance for any hints!
sean