52 import java.security.AllPermission; |
52 import java.security.AllPermission; |
53 import java.security.CodeSigner; |
53 import java.security.CodeSigner; |
54 import java.security.CodeSource; |
54 import java.security.CodeSource; |
55 import java.security.Permissions; |
55 import java.security.Permissions; |
56 import java.security.PrivilegedAction; |
56 import java.security.PrivilegedAction; |
|
57 import java.security.PrivilegedExceptionAction; |
57 import java.security.ProtectionDomain; |
58 import java.security.ProtectionDomain; |
58 import java.security.SecureClassLoader; |
59 import java.security.SecureClassLoader; |
59 import java.security.SecureRandom; |
60 import java.security.SecureRandom; |
60 import java.util.ArrayList; |
61 import java.util.ArrayList; |
61 import java.util.Arrays; |
62 import java.util.Arrays; |
408 * @return the constructor method handle. |
409 * @return the constructor method handle. |
409 * @throws Exception if anything goes wrong |
410 * @throws Exception if anything goes wrong |
410 */ |
411 */ |
411 public static MethodHandle getConstructor(final Class<?> sourceType, final Class<?> targetType) throws Exception { |
412 public static MethodHandle getConstructor(final Class<?> sourceType, final Class<?> targetType) throws Exception { |
412 final StaticClass adapterClass = getAdapterClassFor(new Class<?>[] { targetType }); |
413 final StaticClass adapterClass = getAdapterClassFor(new Class<?>[] { targetType }); |
413 return MH.bindTo(Bootstrap.getLinkerServices().getGuardedInvocation(new LinkRequestImpl(NashornCallSiteDescriptor.get( |
414 return AccessController.doPrivileged(new PrivilegedExceptionAction<MethodHandle>() { |
414 "dyn:new", MethodType.methodType(targetType, StaticClass.class, sourceType), 0), false, |
415 public MethodHandle run() throws Exception { |
415 adapterClass, null)).getInvocation(), adapterClass); |
416 return MH.bindTo(Bootstrap.getLinkerServices().getGuardedInvocation(new LinkRequestImpl(NashornCallSiteDescriptor.get( |
|
417 "dyn:new", MethodType.methodType(targetType, StaticClass.class, sourceType), 0), false, |
|
418 adapterClass, null)).getInvocation(), adapterClass); |
|
419 } |
|
420 }); |
416 } |
421 } |
417 |
422 |
418 /** |
423 /** |
419 * Finishes the bytecode generation for the adapter class that was started in the constructor, and loads the |
424 * Finishes the bytecode generation for the adapter class that was started in the constructor, and loads the |
420 * bytecode as a new class into the JVM. |
425 * bytecode as a new class into the JVM. |
454 // with ability to introspect on the class and use setAccessible(true) on it could invoke the method. It's a |
459 // with ability to introspect on the class and use setAccessible(true) on it could invoke the method. It's a |
455 // security tradeoff... |
460 // security tradeoff... |
456 private static ClassLoader createClassLoader(final ClassLoader parentLoader, final String className, |
461 private static ClassLoader createClassLoader(final ClassLoader parentLoader, final String className, |
457 final byte[] classBytes, final String privilegedActionClassName) { |
462 final byte[] classBytes, final String privilegedActionClassName) { |
458 return new AdapterLoader(parentLoader) { |
463 return new AdapterLoader(parentLoader) { |
|
464 private final ClassLoader myLoader = getClass().getClassLoader(); |
459 private final ProtectionDomain myProtectionDomain = getClass().getProtectionDomain(); |
465 private final ProtectionDomain myProtectionDomain = getClass().getProtectionDomain(); |
|
466 |
|
467 @Override |
|
468 public Class<?> loadClass(final String name, final boolean resolve) throws ClassNotFoundException { |
|
469 try { |
|
470 return super.loadClass(name, resolve); |
|
471 } catch (final SecurityException se) { |
|
472 // we may be implementing an interface or extending a class that was |
|
473 // loaded by a loader that prevents package.access. If so, it'd throw |
|
474 // SecurityException for nashorn's classes!. For adapter's to work, we |
|
475 // should be able to refer to nashorn classes. |
|
476 if (name.startsWith("jdk.nashorn.internal.")) { |
|
477 return myLoader.loadClass(name); |
|
478 } |
|
479 throw se; |
|
480 } |
|
481 } |
460 |
482 |
461 @Override |
483 @Override |
462 protected Class<?> findClass(final String name) throws ClassNotFoundException { |
484 protected Class<?> findClass(final String name) throws ClassNotFoundException { |
463 if(name.equals(className)) { |
485 if(name.equals(className)) { |
464 final byte[] bytes = classBytes; |
486 final byte[] bytes = classBytes; |