AnsweredAssumed Answered

ReflectUtil.loadClass() and custom classloader

Question asked by pgadecki on Nov 17, 2011
Latest reply on Jan 19, 2015 by oefimov
I'm implementing custom classloader which loads classes from BAR archives and I'm wondering why it is not directly called in ReflectUtil.loadClass()

public static Class<?> loadClass(String className) {
   Class<?> clazz = null;
   ClassLoader classLoader = getCustomClassLoader();
  
   // First exception in chain of classloaders will be used as cause when no class is found in any of them
   Throwable throwable = null;
  
   if(classLoader != null) {
     try {
       LOG.finest("Trying to load class with custom classloader: " + className);
            clazz = Class.forName(className, true, classLoader);
     } catch(Throwable t) {
       throwable = t;
     }
   }


Is there any particular reason to load class using custom classloader in this way:
clazz = Class.forName(className, true, classLoader);
instead of directly calling classloader method:
clazz = classLoader.loadClass(className);
?

My goal is to have possibility to load two versions of the same class during runtime depending on deployment's id during runtime.
It seems that call with Class.forName() my classloader is not called but some cached class is loaded which is not what I (and probably other custom classloader developers) want to achieve.

Outcomes