FileDocCategorySizeDatePackage
ClassLoaderResolver.javaAPI DocAndroid 1.5 API8223Wed May 06 22:41:16 BST 2009com.vladium.util

ClassLoaderResolver

public abstract class ClassLoaderResolver extends Object
This non-instantiable non-subclassable class acts as the global point for choosing a ClassLoader for dynamic class/resource loading at any point in an application.
see
ResourceLoader
see
IClassLoadStrategy
see
DefaultClassLoadStrategy
author
Vlad Roubtsov, (C) 2003

Fields Summary
private static IClassLoadStrategy
s_strategy
private static final int
CALL_CONTEXT_OFFSET
private static final CallerResolver
CALLER_RESOLVER
Constructors Summary
private ClassLoaderResolver()

Methods Summary
public static java.lang.ClassgetCallerClass(int callerOffset)

        if (CALLER_RESOLVER == null) return null; // only happens if <clinit> failed

        return CALLER_RESOLVER.getClassContext () [CALL_CONTEXT_OFFSET + callerOffset];
    
public static synchronized java.lang.ClassLoadergetClassLoader(java.lang.Class caller)
This method selects the "best" classloader instance to be used for class/resource loading by whoever calls this method. The decision typically involves choosing between the caller's current, thread context, system, and other classloaders in the JVM and is made by the {@link IClassLoadStrategy} instance established by the last call to {@link #setStrategy}.

This method does not throw.

param
caller [null input eliminates the caller's current classloader from consideration]
return
classloader to be used by the caller ['null' indicates the primordial loader]

        final ClassLoadContext ctx = new ClassLoadContext (caller);
        
        return s_strategy.getClassLoader (ctx); 
    
public static synchronized java.lang.ClassLoadergetClassLoader()
This method selects the "best" classloader instance to be used for class/resource loading by whoever calls this method. The decision typically involves choosing between the caller's current, thread context, system, and other classloaders in the JVM and is made by the {@link IClassLoadStrategy} instance established by the last call to {@link #setStrategy}.

This method uses its own caller to set the call context. To be able to override this decision explicitly, use {@link #getClassLoader(Class)}.

This method does not throw.

return
classloader to be used by the caller ['null' indicates the primordial loader]

        final Class caller = getCallerClass (1); // 'caller' can be set to null
        final ClassLoadContext ctx = new ClassLoadContext (caller);
        
        return s_strategy.getClassLoader (ctx); 
    
public static synchronized IClassLoadStrategygetStrategy()
Gets the current classloader selection strategy setting.

        return s_strategy;
    
public static booleanisChild(java.lang.ClassLoader loader1, java.lang.ClassLoader loader2)
Returns 'true' if 'loader2' is a delegation child of 'loader1' [or if 'loader1'=='loader2']. Of course, this works only for classloaders that set their parent pointers correctly. 'null' is interpreted as the primordial loader [i.e., everybody's parent].

        if (loader1 == loader2) return true; 
        if (loader2 == null) return false; 
        if (loader1 == null) return true;
        
        for ( ; loader2 != null; loader2 = loader2.getParent ())
        {
            if (loader2 == loader1) return true;
        }   

        return false;
    
public static synchronized IClassLoadStrategysetStrategy(IClassLoadStrategy strategy)
Sets the classloader selection strategy to be used by subsequent calls to {@link #getClassLoader()}. An instance of {@link ClassLoaderResolver.DefaultClassLoadStrategy} is in effect if this method is never called.

param
strategy new strategy [may not be null]
return
previous setting

        if (strategy == null) throw new IllegalArgumentException ("null input: strategy");
        
        final IClassLoadStrategy old = s_strategy;
        s_strategy = strategy;
        
        return old;