# HG changeset patch # User andrew # Date 1371561349 18000 # Node ID e28599cbc0d414127805ef1a0a912fa23dc30cdf # Parent 0ab2712a035ac1898f8499bc89dbab91e85ff30c 8009424: Adapt Nashorn to JSR-292 implementation change Reviewed-by: jrose, twisti, jdn, mchung diff -r 0ab2712a035a -r e28599cbc0d4 src/share/classes/java/lang/invoke/MethodHandles.java --- a/src/share/classes/java/lang/invoke/MethodHandles.java Tue Jun 18 08:09:59 2013 -0500 +++ b/src/share/classes/java/lang/invoke/MethodHandles.java Tue Jun 18 08:15:49 2013 -0500 @@ -35,7 +35,7 @@ import sun.reflect.CallerSensitive; import sun.reflect.Reflection; import sun.reflect.misc.ReflectUtil; -import sun.security.util.SecurityConstants; + import static java.lang.invoke.MethodHandleStatics.*; import static java.lang.invoke.MethodHandleNatives.Constants.*; import sun.security.util.SecurityConstants; @@ -565,13 +565,11 @@ * refuses access * @throws NullPointerException if any argument is null */ - @CallerSensitive public MethodHandle findStatic(Class refc, String name, MethodType type) throws NoSuchMethodException, IllegalAccessException { MemberName method = resolveOrFail(refc, name, type, true); - Class callerClass = Reflection.getCallerClass(); - checkSecurityManager(refc, method, callerClass); - return accessStatic(refc, method, findBoundCallerClass(method, callerClass)); + checkSecurityManager(refc, method); + return accessStatic(refc, method, findBoundCallerClass(method)); } private MethodHandle accessStatic(Class refc, MemberName method, Class callerClass) throws IllegalAccessException { @@ -624,12 +622,10 @@ * refuses access * @throws NullPointerException if any argument is null */ - @CallerSensitive public MethodHandle findVirtual(Class refc, String name, MethodType type) throws NoSuchMethodException, IllegalAccessException { MemberName method = resolveOrFail(refc, name, type, false); - Class callerClass = Reflection.getCallerClass(); - checkSecurityManager(refc, method, callerClass); - return accessVirtual(refc, method, findBoundCallerClass(method, callerClass)); + checkSecurityManager(refc, method); + return accessVirtual(refc, method, findBoundCallerClass(method)); } private MethodHandle resolveVirtual(Class refc, String name, MethodType type) throws NoSuchMethodException, IllegalAccessException { MemberName method = resolveOrFail(refc, name, type, false); @@ -638,8 +634,7 @@ private MethodHandle accessVirtual(Class refc, MemberName method, Class callerClass) throws IllegalAccessException { checkMethod(refc, method, false); MethodHandle mh = MethodHandleImpl.findMethod(method, true, lookupClassOrNull()); - Class callerClass = Reflection.getCallerClass(); - mh = maybeBindCaller(method, mh, findBoundCallerClass(method, callerClass)); + mh = maybeBindCaller(method, mh, callerClass); return restrictProtectedReceiver(method, mh); } @@ -669,11 +664,10 @@ * refuses access * @throws NullPointerException if any argument is null */ - @CallerSensitive public MethodHandle findConstructor(Class refc, MethodType type) throws NoSuchMethodException, IllegalAccessException { String name = ""; MemberName ctor = resolveOrFail(refc, name, type, false, false, lookupClassOrNull()); - checkSecurityManager(refc, ctor, Reflection.getCallerClass()); + checkSecurityManager(refc, ctor); return accessConstructor(refc, ctor); } private MethodHandle accessConstructor(Class refc, MemberName ctor) throws IllegalAccessException { @@ -739,14 +733,12 @@ * refuses access * @throws NullPointerException if any argument is null */ - @CallerSensitive public MethodHandle findSpecial(Class refc, String name, MethodType type, Class specialCaller) throws NoSuchMethodException, IllegalAccessException { checkSpecialCaller(specialCaller); MemberName method = resolveOrFail(refc, name, type, false, false, specialCaller); - Class callerClass = Reflection.getCallerClass(); - checkSecurityManager(refc, method, callerClass); - return accessSpecial(refc, method, findBoundCallerClass(method, callerClass), specialCaller); + checkSecurityManager(refc, method); + return accessSpecial(refc, method, findBoundCallerClass(method), specialCaller); } private MethodHandle accessSpecial(Class refc, MemberName method, Class callerClass, @@ -803,10 +795,9 @@ * refuses access * @throws NullPointerException if any argument is null */ - @CallerSensitive public MethodHandle findGetter(Class refc, String name, Class type) throws NoSuchFieldException, IllegalAccessException { MemberName field = resolveOrFail(refc, name, type, false); - checkSecurityManager(refc, field, Reflection.getCallerClass()); + checkSecurityManager(refc, field); return makeAccessor(refc, field, false, false, 0); } private MethodHandle resolveGetter(Class refc, String name, Class type) throws NoSuchFieldException, IllegalAccessException { @@ -831,10 +822,9 @@ * refuses access * @throws NullPointerException if any argument is null */ - @CallerSensitive public MethodHandle findSetter(Class refc, String name, Class type) throws NoSuchFieldException, IllegalAccessException { MemberName field = resolveOrFail(refc, name, type, false); - checkSecurityManager(refc, field, Reflection.getCallerClass()); + checkSecurityManager(refc, field); return makeAccessor(refc, field, false, true, 0); } private MethodHandle resolveSetter(Class refc, String name, Class type) throws NoSuchFieldException, IllegalAccessException { @@ -858,10 +848,9 @@ * refuses access * @throws NullPointerException if any argument is null */ - @CallerSensitive public MethodHandle findStaticGetter(Class refc, String name, Class type) throws NoSuchFieldException, IllegalAccessException { MemberName field = resolveOrFail(refc, name, type, true); - checkSecurityManager(refc, field, Reflection.getCallerClass()); + checkSecurityManager(refc, field); return makeAccessor(refc, field, false, false, 1); } private MethodHandle resolveStaticGetter(Class refc, String name, Class type) throws NoSuchFieldException, IllegalAccessException { @@ -885,10 +874,9 @@ * refuses access * @throws NullPointerException if any argument is null */ - @CallerSensitive public MethodHandle findStaticSetter(Class refc, String name, Class type) throws NoSuchFieldException, IllegalAccessException { MemberName field = resolveOrFail(refc, name, type, true); - checkSecurityManager(refc, field, Reflection.getCallerClass()); + checkSecurityManager(refc, field); return makeAccessor(refc, field, false, true, 1); } private MethodHandle resolveStaticSetter(Class refc, String name, Class type) throws NoSuchFieldException, IllegalAccessException { @@ -942,15 +930,13 @@ * refuses access * @throws NullPointerException if any argument is null */ - @CallerSensitive public MethodHandle bind(Object receiver, String name, MethodType type) throws NoSuchMethodException, IllegalAccessException { Class refc = receiver.getClass(); // may get NPE MemberName method = resolveOrFail(refc, name, type, false); - Class callerClass = Reflection.getCallerClass(); - checkSecurityManager(refc, method, callerClass); + checkSecurityManager(refc, method); checkMethod(refc, method, false); MethodHandle dmh = MethodHandleImpl.findMethod(method, true, lookupClassOrNull()); - MethodHandle bcmh = maybeBindCaller(method, dmh, findBoundCallerClass(method, callerClass)); + MethodHandle bcmh = maybeBindCaller(method, dmh, findBoundCallerClass(method)); if (bcmh != dmh) return fixVarargs(bcmh.bindTo(receiver), dmh); MethodHandle bmh = MethodHandleImpl.bindReceiver(dmh, receiver); if (bmh == null) @@ -979,16 +965,13 @@ * is set and {@code asVarargsCollector} fails * @throws NullPointerException if the argument is null */ - @CallerSensitive public MethodHandle unreflect(Method m) throws IllegalAccessException { MemberName method = new MemberName(m); assert(method.isMethod()); if (!m.isAccessible()) checkMethod(method.getDeclaringClass(), method, method.isStatic()); MethodHandle mh = MethodHandleImpl.findMethod(method, true, lookupClassOrNull()); - Class callerClass = findBoundCallerClass(method); // stack walk magic: do not refactor - mh = maybeBindCaller(method, mh, callerClass); - if (!m.isAccessible()) mh = restrictProtectedReceiver(method, mh); - return mh; + mh = maybeBindCaller(method, mh, findBoundCallerClass(method)); + return restrictProtectedReceiver(method, mh); } /** @@ -1012,7 +995,6 @@ * is set and {@code asVarargsCollector} fails * @throws NullPointerException if any argument is null */ - @CallerSensitive public MethodHandle unreflectSpecial(Method m, Class specialCaller) throws IllegalAccessException { checkSpecialCaller(specialCaller); MemberName method = new MemberName(m); @@ -1020,7 +1002,7 @@ // ignore m.isAccessible: this is a new kind of access checkMethod(m.getDeclaringClass(), method, false); MethodHandle mh = MethodHandleImpl.findMethod(method, false, lookupClassOrNull()); - mh = maybeBindCaller(method, mh, findBoundCallerClass(method, callerClass)); + mh = maybeBindCaller(method, mh, findBoundCallerClass(method)); return restrictReceiver(method, mh, specialCaller); } @@ -1129,7 +1111,7 @@ * If this lookup object has private access, then the caller class is the lookupClass. * Otherwise, if m is caller-sensitive, throw IllegalAccessException. */ - Class findBoundCallerClass(MemberName m, Class caller) { + Class findBoundCallerClass(MemberName m) throws IllegalAccessException { Class callerClass = null; if (MethodHandleNatives.isCallerSensitive(m)) { // Only full-power lookup is allowed to resolve caller-sensitive methods @@ -1166,7 +1148,7 @@ * Perform necessary access checks. * This function performs stack walk magic: do not refactor it. */ - void checkSecurityManager(Class refc, MemberName m, Class caller) { + void checkSecurityManager(Class refc, MemberName m) { SecurityManager smgr = System.getSecurityManager(); if (smgr == null) return; if (allowedModes == TRUSTED) return; @@ -1198,23 +1180,22 @@ // Inline SecurityManager.checkMemberAccess final int which = Member.DECLARED; final Class clazz = defc; - if (!overridden) { - if (!isFullPowerLookup() || - (lookupClass.getClassLoader() != defc.getClassLoader())) { - smgr.checkPermission(SecurityConstants.CHECK_MEMBER_ACCESS_PERMISSION); - } - } else { - // Don't refactor; otherwise break the stack depth for - // checkMemberAccess of subclasses of SecurityManager as specified. - smgr.checkMemberAccess(defc, Member.DECLARED); - } - } + if (!overridden) { + if (!isFullPowerLookup() || + (lookupClass.getClassLoader() != defc.getClassLoader())) { + smgr.checkPermission(SecurityConstants.CHECK_MEMBER_ACCESS_PERMISSION); + } + } else { + // Don't refactor; otherwise break the stack depth for + // checkMemberAccess of subclasses of SecurityManager as specified. + smgr.checkMemberAccess(clazz, which); + } + } // Step 4: if (defc != refc) { ReflectUtil.checkPackageAccess(defc); } - } void checkMethod(Class refc, MemberName m, boolean wantStatic) throws IllegalAccessException {