Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Class.isAssignableFrom ALWAYS returning false... only outside IDE

I've tested on three windows machines, and two linux VPSes, on different versions of Java, both on the OpenJDK & Oracle JDK. It functioned perfectly, and then all of a sudden, it only works in my IDE, though I haven't changed any relevant code, and I can't imagine what can cause this.

Prevalent code in system:

Class<?> cls = (session == null ? secjlcl : session.getJLCL()).loadClass(name);
Logger.log(JavaLoader.class.isAssignableFrom(cls) + " - " + cls + " - " + cls.getSuperclass().getName());
if (JavaLoader.class.isAssignableFrom(cls)) {

And my ClassLoader:

public class JavaLoaderClassLoader extends URLClassLoader {
public JavaLoaderClassLoader(URL[] url, ClassLoader parent) {
    super(url);
}

private HashMap<String, Class<?>> javaLoaders = new HashMap<String, Class<?>>();

public String addClass(byte[] data) throws LinkageError {
    Class<?> cls = defineClass(null, data, 0, data.length);
    javaLoaders.put(cls.getName(), cls);
    return cls.getName();
}

public Class<?> loadClass(String name, boolean resolve) {
    if (javaLoaders.containsKey(name)) return javaLoaders.get(name);
    try {
        Class<?> see = super.loadClass(name, resolve);
        if (see != null) return see;
    }catch (ClassNotFoundException e) {
        Logger.logError(e);
    }
    return null;
}

public void finalize() throws Throwable {
    super.finalize();
    javaLoaders = null;
}
}

One note, I expect many classloaders to load a different file in the same name/package, so I use separate classloaders to keep them separate, however in testing, that was NOT tested.

Now, this had worked flawlessly in the past, and I have zero clue why it stopped. I would assume I broke something, but the code still works in my IDE?

like image 639
JavaProphet Avatar asked Apr 26 '15 10:04

JavaProphet


1 Answers

This appears to be your bug:

public JavaLoaderClassLoader(URL[] url, ClassLoader parent) {
    super(url);
}

You aren't installing parent as the parent class loader through the super constructor.

like image 98
Tom Hawtin - tackline Avatar answered Oct 11 '22 10:10

Tom Hawtin - tackline