diff --git a/js/rhino/src/org/mozilla/classfile/DefiningClassLoader.java b/js/rhino/src/org/mozilla/classfile/DefiningClassLoader.java index 8504469f4b27..80423fe562a4 100644 --- a/js/rhino/src/org/mozilla/classfile/DefiningClassLoader.java +++ b/js/rhino/src/org/mozilla/classfile/DefiningClassLoader.java @@ -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. + } + } + } diff --git a/js/rhino/src/org/mozilla/javascript/FunctionObject.java b/js/rhino/src/org/mozilla/javascript/FunctionObject.java index bc973f0918ca..142d15663f32 100644 --- a/js/rhino/src/org/mozilla/javascript/FunctionObject.java +++ b/js/rhino/src/org/mozilla/javascript/FunctionObject.java @@ -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) {} diff --git a/js/rhino/src/org/mozilla/javascript/ScriptRuntime.java b/js/rhino/src/org/mozilla/javascript/ScriptRuntime.java index ec6af4b20f51..db8656f08736 100644 --- a/js/rhino/src/org/mozilla/javascript/ScriptRuntime.java +++ b/js/rhino/src/org/mozilla/javascript/ScriptRuntime.java @@ -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