Clean up classloader usage to use the thread's context class loader.

This commit is contained in:
nboyd%atg.com 2001-06-07 16:04:33 +00:00
parent 523249b37d
commit 1e71ac75ba
3 changed files with 46 additions and 40 deletions

View File

@ -35,9 +35,11 @@
* file under either the NPL or the GPL.
*/
package org.mozilla.classfile;
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;
/**
* Load generated classes.
*
@ -45,6 +47,21 @@ package org.mozilla.classfile;
*/
public class DefiningClassLoader extends ClassLoader {
public static ClassLoader getContextClassLoader() {
try {
if (getContextClassLoaderMethod != null) {
return (ClassLoader) getContextClassLoaderMethod.invoke(
Thread.currentThread(),
new Object[0]);
}
} catch (IllegalAccessException e) {
// fall through...
} catch (InvocationTargetException e) {
// fall through...
}
return DefiningClassLoader.class.getClassLoader();
}
public Class defineClass(String name, byte data[]) {
return super.defineClass(name, data, 0, data.length);
}
@ -54,7 +71,7 @@ public class DefiningClassLoader extends ClassLoader {
{
Class clazz = findLoadedClass(name);
if (clazz == null) {
ClassLoader loader = getClass().getClassLoader();
ClassLoader loader = getContextClassLoader();
if (loader != null) {
clazz = loader.loadClass(name);
} else {
@ -65,4 +82,26 @@ public class DefiningClassLoader extends ClassLoader {
resolveClass(clazz);
return clazz;
}
private static Method getContextClassLoaderMethod;
static {
try {
// Don't use "Thread.class": that performs the lookup
// in the class initializer, which doesn't allow us to
// catch possible security exceptions.
Class threadClass = Class.forName("java.lang.Thread");
// We'd like to use "getContextClassLoader", but
// that's only available on Java2.
getContextClassLoaderMethod =
threadClass.getDeclaredMethod("getContextClassLoader",
new Class[0]);
} catch (ClassNotFoundException e) {
// ignore exceptions; we'll use Class.forName instead.
} catch (NoSuchMethodException e) {
// ignore exceptions; we'll use Class.forName instead.
} catch (SecurityException e) {
// ignore exceptions; we'll use Class.forName instead.
}
}
}

View File

@ -576,7 +576,7 @@ public class FunctionObject extends NativeFunction {
/** Get default master implementation or null if not available */
private static Invoker newInvokerMaster() {
try {
Class cl = Class.forName(INVOKER_MASTER_CLASS);
Class cl = ScriptRuntime.loadClassName(INVOKER_MASTER_CLASS);
return (Invoker)cl.newInstance();
}
catch (ClassNotFoundException ex) {}

View File

@ -42,6 +42,7 @@ package org.mozilla.javascript;
import java.util.*;
import java.lang.reflect.*;
import org.mozilla.classfile.DefiningClassLoader;
/**
* This is the class that implements the runtime.
@ -2029,49 +2030,15 @@ public class ScriptRuntime {
cx.currentActivation = activation;
}
private static Method getContextClassLoaderMethod;
static {
try {
// Don't use "Thread.class": that performs the lookup
// in the class initializer, which doesn't allow us to
// catch possible security exceptions.
Class threadClass = Class.forName("java.lang.Thread");
// We'd like to use "getContextClassLoader", but
// that's only available on Java2.
getContextClassLoaderMethod =
threadClass.getDeclaredMethod("getContextClassLoader",
new Class[0]);
} catch (ClassNotFoundException e) {
// ignore exceptions; we'll use Class.forName instead.
} catch (NoSuchMethodException e) {
// ignore exceptions; we'll use Class.forName instead.
} catch (SecurityException e) {
// ignore exceptions; we'll use Class.forName instead.
}
}
public static Class loadClassName(String className)
throws ClassNotFoundException
{
try {
if (getContextClassLoaderMethod != null) {
ClassLoader cl = (ClassLoader)
getContextClassLoaderMethod.invoke(
Thread.currentThread(),
new Object[0]);
if (cl != null)
return cl.loadClass(className);
} else {
ClassLoader cl = ScriptRuntime.class.getClassLoader();
if (cl != null)
return cl.loadClass(className);
}
ClassLoader cl = DefiningClassLoader.getContextClassLoader();
if (cl != null)
return cl.loadClass(className);
} catch (SecurityException e) {
// fall through...
} catch (IllegalAccessException e) {
// fall through...
} catch (InvocationTargetException e) {
// fall through...
} catch (ClassNotFoundException e) {
// Rather than just letting the exception propagate
// we'll try Class.forName as well. The results could be