< prev index next >

test/jdk/java/lang/invoke/MethodHandlesGeneralTest.java

Print this page

        

@@ -632,11 +632,11 @@
         testGetter(TEST_FIND_STATIC);
     }
 
     public void testGetter(int testMode) throws Throwable {
         Lookup lookup = PRIVATE;  // FIXME: test more lookups than this one
-        for (Object[] c : HasFields.CASES) {
+        for (Object[] c : HasFields.cases(testMode)) {
             boolean positive = (c[1] != Error.class);
             testGetter(positive, lookup, c[0], c[1], testMode);
             if (positive)
                 testGetter(positive, lookup, c[0], c[1], testMode | TEST_NPE);
         }

@@ -721,19 +721,23 @@
                 :   IllegalAccessException.class,
                 noAccess);
             if (verbosity >= 5)  ex.printStackTrace(System.out);
         }
         if (verbosity >= 3)
-            System.out.println("find"+(isStatic?"Static":"")+(isGetter?"Getter":"Setter")+" "+fclass.getName()+"."+fname+"/"+ftype
-                               +" => "+mh
-                               +(noAccess == null ? "" : " !! "+noAccess));
+            System.out.format("%s%s %s.%s/%s => %s %s%n",
+                (testMode0 & TEST_UNREFLECT) != 0
+                    ? "unreflect"
+                    : "find" + ((testMode0 & TEST_FIND_STATIC) != 0 ? "Static" : ""),
+                (isGetter ? "Getter" : "Setter"),
+                fclass.getName(), fname, ftype, mh,
+                (noAccess == null ? "" : " !! "+noAccess));
+        if (!positive && noAccess != null) return;
         if (positive && !testNPE && noAccess != null)  throw new RuntimeException(noAccess);
         assertEquals(positive0 ? "positive test" : "negative test erroneously passed", positive0, mh != null);
         if (!positive && !testNPE)  return; // negative access test failed as expected
         assertEquals((isStatic ? 0 : 1)+(isGetter ? 0 : 1), mh.type().parameterCount());
 
-
         assertSame(mh.type(), expType);
         //assertNameStringContains(mh, fname);  // This does not hold anymore with LFs
         HasFields fields = new HasFields();
         HasFields fieldsForMH = fields;
         if (testNPE)  fieldsForMH = null;  // perturb MH argument to elicit expected error

@@ -753,10 +757,12 @@
         }
         if (f != null && f.getDeclaringClass() == HasFields.class) {
             assertEquals(f.get(fields), value);  // clean to start with
         }
         Throwable caughtEx = null;
+        boolean writeAccess = !Modifier.isFinal(f.getModifiers()) ||
+            (!Modifier.isStatic(f.getModifiers()) && f.isAccessible());
         if (isGetter) {
             Object expValue = value;
             for (int i = 0; i <= 1; i++) {
                 sawValue = null;  // make DA rules happy under try/catch
                 try {

@@ -776,12 +782,11 @@
                         caughtEx = ex;
                         break;
                     }
                 }
                 assertEquals(sawValue, expValue);
-                if (f != null && f.getDeclaringClass() == HasFields.class
-                    && !Modifier.isFinal(f.getModifiers())) {
+                if (f != null && f.getDeclaringClass() == HasFields.class && writeAccess) {
                     Object random = randomArg(ftype);
                     f.set(fields, random);
                     expValue = random;
                 } else {
                     break;

@@ -811,11 +816,11 @@
                 if (f != null && f.getDeclaringClass() == HasFields.class) {
                     assertEquals(f.get(fields), putValue);
                 }
             }
         }
-        if (f != null && f.getDeclaringClass() == HasFields.class) {
+        if (f != null && f.getDeclaringClass() == HasFields.class && writeAccess) {
             f.set(fields, value);  // put it back
         }
         if (testNPE) {
             if (caughtEx == null || !(caughtEx instanceof NullPointerException))
                 throw new RuntimeException("failed to catch NPE exception"+(caughtEx == null ? " (caughtEx=null)" : ""), caughtEx);

@@ -860,13 +865,21 @@
         testSetter(TEST_FIND_STATIC);
     }
 
     public void testSetter(int testMode) throws Throwable {
         Lookup lookup = PRIVATE;  // FIXME: test more lookups than this one
-        startTest("unreflectSetter");
-        for (Object[] c : HasFields.CASES) {
-            boolean positive = (c[1] != Error.class);
+        startTest("testSetter");
+        for (Object[] c : HasFields.cases(testMode)) {
+            boolean positive = (c[1] != Error.class) && !(c.length == 3 && c[2] == Error.class);
+            if ((testMode & TEST_UNREFLECT) != 0 && c.length == 3) {
+                assertTrue(c[0] instanceof Field && c[2] == Error.class);
+                Field f = (Field)c[0];
+                int mods = f.getModifiers();
+                // unreflectSetter should only have write access on instance final field if accessible flag is true
+                positive = !Modifier.isFinal(mods) ||
+                           (!Modifier.isStatic(mods) && f.isAccessible());
+            }
             testSetter(positive, lookup, c[0], c[1], testMode);
             if (positive)
                 testSetter(positive, lookup, c[0], c[1], testMode | TEST_NPE);
         }
         for (int isStaticN = 0; isStaticN <= 1; isStaticN++) {
< prev index next >