changeset 190:222a72df2f42

8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive Reviewed-by: jrose, alanb, twisti, sundar
author mchung
date Tue, 16 Apr 2013 22:11:33 -0700
parents e70e6b38826b
children 44d8612e29b0 d4468316fe73
files src/jdk/nashorn/api/scripting/NashornScriptEngineFactory.java src/jdk/nashorn/internal/runtime/Context.java
diffstat 2 files changed, 7 insertions(+), 37 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk/nashorn/api/scripting/NashornScriptEngineFactory.java	Mon Apr 15 08:39:48 2013 -0300
+++ b/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()
-        //
-        // <caller>
-        //  <factory.getScriptEngine()>
-        //   <factory.getAppClassLoader()>
-        //    <Reflection.getCallerClass()>
-        //
-        // or used one of the getEngineByABC methods of ScriptEngineManager.
-        //
-        // <caller>
-        //  <ScriptEngineManager.getEngineByName()>
-        //   <factory.getScriptEngine()>
-        //    <factory.getAppClassLoader()>
-        //     <Reflection.getCallerClass()>
-
-        // 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;
     }
 }
--- a/src/jdk/nashorn/internal/runtime/Context.java	Mon Apr 15 08:39:48 2013 -0300
+++ b/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