changeset 6014:b453d9be6b3f jdk7u21-b07

8009677: Better setting of setters Reviewed-by: ahgross, jrose, twisti
author kvn
date Thu, 14 Mar 2013 08:55:04 -0700
parents a19614a3dabb
children 2899c3dbf5e8 d42b986caf14
files src/share/classes/java/lang/invoke/MethodHandles.java
diffstat 1 files changed, 8 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/lang/invoke/MethodHandles.java	Sat Mar 16 10:08:14 2013 -0400
+++ b/src/share/classes/java/lang/invoke/MethodHandles.java	Thu Mar 14 08:55:04 2013 -0700
@@ -689,7 +689,7 @@
         }
         private MethodHandle accessConstructor(Class<?> refc, MemberName ctor) throws IllegalAccessException {
             assert(ctor.isConstructor());
-            checkAccess(refc, ctor);
+            checkAccess(refc, ctor, false /* is_setter */);
             MethodHandle rawMH = MethodHandleImpl.findMethod(ctor, false, lookupClassOrNull());
             MethodHandle allocMH = MethodHandleImpl.makeAllocator(rawMH);
             assert(!MethodHandleNatives.isCallerSensitive(ctor));  // maybeBindCaller not relevant here
@@ -1057,7 +1057,7 @@
             if (c.isAccessible()) {
                 rawCtor = MethodHandleImpl.findMethod(ctor, false, /*no lookupClass*/ null);
             } else {
-                checkAccess(c.getDeclaringClass(), ctor);
+                checkAccess(c.getDeclaringClass(), ctor, false /* is_setter */);
                 rawCtor = MethodHandleImpl.findMethod(ctor, false, lookupClassOrNull());
             }
             assert(!MethodHandleNatives.isCallerSensitive(ctor));  // maybeBindCaller not relevant here
@@ -1211,14 +1211,17 @@
             else if (wantStatic != m.isStatic())
                 message = wantStatic ? "expected a static method" : "expected a non-static method";
             else
-                { checkAccess(refc, m); return; }
+                { checkAccess(refc, m, false /* is_setter */); return; }
             throw m.makeAccessException(message, this);
         }
 
-        void checkAccess(Class<?> refc, MemberName m) throws IllegalAccessException {
+        void checkAccess(Class<?> refc, MemberName m, boolean isSetter) throws IllegalAccessException {
             int allowedModes = this.allowedModes;
             if (allowedModes == TRUSTED)  return;
             int mods = m.getModifiers();
+            if (m.isField() && Modifier.isFinal(mods) && isSetter) {
+              throw m.makeAccessException("unexpected set of a final field", this);
+            }
             if (Modifier.isPublic(mods) && Modifier.isPublic(refc.getModifiers()) && allowedModes != 0)
                 return;  // common case
             int requestedModes = fixmods(mods);  // adjust 0 => PACKAGE
@@ -1317,7 +1320,7 @@
                                                 : "expected a non-static field", this);
             if (trusted)
                 return MethodHandleImpl.accessField(field, isSetter, /*no lookupClass*/ null);
-            checkAccess(refc, field);
+            checkAccess(refc, field, isSetter);
             MethodHandle mh = MethodHandleImpl.accessField(field, isSetter, lookupClassOrNull());
             return restrictProtectedReceiver(field, mh);
         }