src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/linker/JavaAdapterClassLoader.java

Print this page




  87     // of Nashorn itself. Also note that the creation and loading of the global setter is deferred until it is
  88     // required by JVM linker, which will only happen on first invocation of any of the adapted method. We could defer
  89     // it even more by separating its invocation into a separate static method on the adapter class, but then someone
  90     // with ability to introspect on the class and use setAccessible(true) on it could invoke the method. It's a
  91     // security tradeoff...
  92     private ClassLoader createClassLoader(final ClassLoader parentLoader, final ProtectionDomain protectionDomain) {
  93         return new SecureClassLoader(parentLoader) {
  94             private final ClassLoader myLoader = getClass().getClassLoader();
  95 
  96             @Override
  97             public Class<?> loadClass(final String name, final boolean resolve) throws ClassNotFoundException {
  98                 try {
  99                     Context.checkPackageAccess(name);
 100                     return super.loadClass(name, resolve);
 101                 } catch (final SecurityException se) {
 102                     // we may be implementing an interface or extending a class that was
 103                     // loaded by a loader that prevents package.access. If so, it'd throw
 104                     // SecurityException for nashorn's classes!. For adapter's to work, we
 105                     // should be able to refer to the few classes it needs in its implementation.
 106                     if(VISIBLE_INTERNAL_CLASS_NAMES.contains(name)) {
 107                         return myLoader.loadClass(name);
 108                     }
 109                     throw se;
 110                 }
 111             }
 112 
 113             @Override
 114             protected Class<?> findClass(final String name) throws ClassNotFoundException {
 115                 if(name.equals(className)) {
 116                     assert classBytes != null : "what? already cleared .class bytes!!";
 117 
 118                     final Context ctx = AccessController.doPrivileged(new PrivilegedAction<Context>() {
 119                         @Override
 120                         public Context run() {
 121                             return Context.getContext();
 122                         }
 123                     }, GET_CONTEXT_ACC_CTXT);
 124                     DumpBytecode.dumpBytecode(ctx.getEnv(), ctx.getLogger(jdk.nashorn.internal.codegen.Compiler.class), classBytes, name);
 125                     return defineClass(name, classBytes, 0, classBytes.length, protectionDomain);
 126                 }
 127                 throw new ClassNotFoundException(name);


  87     // of Nashorn itself. Also note that the creation and loading of the global setter is deferred until it is
  88     // required by JVM linker, which will only happen on first invocation of any of the adapted method. We could defer
  89     // it even more by separating its invocation into a separate static method on the adapter class, but then someone
  90     // with ability to introspect on the class and use setAccessible(true) on it could invoke the method. It's a
  91     // security tradeoff...
  92     private ClassLoader createClassLoader(final ClassLoader parentLoader, final ProtectionDomain protectionDomain) {
  93         return new SecureClassLoader(parentLoader) {
  94             private final ClassLoader myLoader = getClass().getClassLoader();
  95 
  96             @Override
  97             public Class<?> loadClass(final String name, final boolean resolve) throws ClassNotFoundException {
  98                 try {
  99                     Context.checkPackageAccess(name);
 100                     return super.loadClass(name, resolve);
 101                 } catch (final SecurityException se) {
 102                     // we may be implementing an interface or extending a class that was
 103                     // loaded by a loader that prevents package.access. If so, it'd throw
 104                     // SecurityException for nashorn's classes!. For adapter's to work, we
 105                     // should be able to refer to the few classes it needs in its implementation.
 106                     if(VISIBLE_INTERNAL_CLASS_NAMES.contains(name)) {
 107                         return myLoader != null? myLoader.loadClass(name) : Class.forName(name, false, myLoader);
 108                     }
 109                     throw se;
 110                 }
 111             }
 112 
 113             @Override
 114             protected Class<?> findClass(final String name) throws ClassNotFoundException {
 115                 if(name.equals(className)) {
 116                     assert classBytes != null : "what? already cleared .class bytes!!";
 117 
 118                     final Context ctx = AccessController.doPrivileged(new PrivilegedAction<Context>() {
 119                         @Override
 120                         public Context run() {
 121                             return Context.getContext();
 122                         }
 123                     }, GET_CONTEXT_ACC_CTXT);
 124                     DumpBytecode.dumpBytecode(ctx.getEnv(), ctx.getLogger(jdk.nashorn.internal.codegen.Compiler.class), classBytes, name);
 125                     return defineClass(name, classBytes, 0, classBytes.length, protectionDomain);
 126                 }
 127                 throw new ClassNotFoundException(name);