# HG changeset patch # User mchung # Date 1366175493 25200 # Node ID 7e7191c30eb5cf1e9419c1e87a164fa554b10e1c # Parent b181ec03eeb5f8033e43c17a85ba45fe5d40a4ba 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive Reviewed-by: jrose, alanb, twisti, sundar diff -r b181ec03eeb5 -r 7e7191c30eb5 nashorn/src/jdk/nashorn/api/scripting/NashornScriptEngineFactory.java --- a/nashorn/src/jdk/nashorn/api/scripting/NashornScriptEngineFactory.java Mon Apr 15 08:39:48 2013 -0300 +++ b/nashorn/src/jdk/nashorn/api/scripting/NashornScriptEngineFactory.java Tue Apr 16 22:11:33 2013 -0700 @@ -210,42 +210,10 @@ } private static ClassLoader getAppClassLoader() { - if (System.getSecurityManager() == null) { - return Thread.currentThread().getContextClassLoader(); - } - - // Try to determine the caller class loader. Use that if it can be - // found. If not, use the class loader of nashorn itself as the - // "application" class loader for scripts. - - // User could have called ScriptEngineFactory.getScriptEngine() - // - // - // - // - // - // - // or used one of the getEngineByABC methods of ScriptEngineManager. - // - // - // - // - // - // - - // So, stack depth is 3 or 4 (recall it is zero based). We try - // stack depths 3, 4 and look for non-bootstrap caller. - Class caller = null; - for (int depth = 3; depth < 5; depth++) { - caller = Reflection.getCallerClass(depth); - if (caller != null && caller.getClassLoader() != null) { - // found a non-bootstrap caller - break; - } - } - - final ClassLoader ccl = (caller == null)? null : caller.getClassLoader(); - // if caller loader is null, then use nashorn's own loader + // Revisit: script engine implementation needs the capability to + // find the class loader of the context in which the script engine + // is running so that classes will be found and loaded properly + ClassLoader ccl = Thread.currentThread().getContextClassLoader(); return (ccl == null)? NashornScriptEngineFactory.class.getClassLoader() : ccl; } } diff -r b181ec03eeb5 -r 7e7191c30eb5 nashorn/src/jdk/nashorn/internal/runtime/Context.java --- a/nashorn/src/jdk/nashorn/internal/runtime/Context.java Mon Apr 15 08:39:48 2013 -0300 +++ b/nashorn/src/jdk/nashorn/internal/runtime/Context.java Tue Apr 16 22:11:33 2013 -0700 @@ -56,6 +56,7 @@ import jdk.nashorn.internal.parser.Parser; import jdk.nashorn.internal.runtime.linker.JavaAdapterFactory; import jdk.nashorn.internal.runtime.options.Options; +import sun.reflect.CallerSensitive; import sun.reflect.Reflection; /** @@ -113,11 +114,12 @@ * Get the current global scope * @return the current global scope */ + @CallerSensitive public static ScriptObject getGlobal() { final SecurityManager sm = System.getSecurityManager(); if (sm != null) { // skip getCallerClass and getGlobal and get to the real caller - Class caller = Reflection.getCallerClass(2); + Class caller = Reflection.getCallerClass(); ClassLoader callerLoader = caller.getClassLoader(); // Allow this method only for nashorn's own classes, objects