< prev index next >

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

Print this page

        

@@ -1,7 +1,7 @@
 /*
- * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.

@@ -633,11 +633,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.testCasesFor(testMode)) {
+        for (Object[] c : HasFields.CASES) {
             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);
         }

@@ -663,23 +663,26 @@
         boolean isGetter = ((testMode0 & TEST_SETTER) == 0);
         boolean doBound  = ((testMode0 & TEST_BOUND) != 0);
         boolean testNPE  = ((testMode0 & TEST_NPE) != 0);
         int testMode = testMode0 & ~(TEST_SETTER | TEST_BOUND | TEST_NPE);
         boolean positive = positive0 && !testNPE;
+        boolean isFinal;
         boolean isStatic;
         Class<?> fclass;
         String   fname;
         Class<?> ftype;
         Field f = (fieldRef instanceof Field ? (Field)fieldRef : null);
         if (f != null) {
             isStatic = Modifier.isStatic(f.getModifiers());
+            isFinal  = Modifier.isFinal(f.getModifiers());
             fclass   = f.getDeclaringClass();
             fname    = f.getName();
             ftype    = f.getType();
         } else {
             Object[] scnt = (Object[]) fieldRef;
             isStatic = (Boolean)  scnt[0];
+            isFinal  = false;
             fclass   = (Class<?>) scnt[1];
             fname    = (String)   scnt[2];
             ftype    = (Class<?>) scnt[3];
             try {
                 f = fclass.getDeclaredField(fname);

@@ -719,25 +722,23 @@
             assertExceptionClass(
                 (fname.contains("bogus"))
                 ?   NoSuchFieldException.class
                 :   IllegalAccessException.class,
                 noAccess);
+            if (((testMode0 & TEST_SETTER) != 0) && (isFinal && isStatic)) return; // Final static field setter test failed as intended.
             if (verbosity >= 5)  ex.printStackTrace(System.out);
         }
         if (verbosity >= 3)
-            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));
-        // negative test case and expected noAccess, then done.
-        if (!positive && noAccess != null) return;
-        // positive test case but found noAccess, then error
+            System.out.println((((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 && !testNPE && noAccess != null)  throw new RuntimeException(noAccess);
         assertEquals(positive0 ? "positive test" : "negative test erroneously passed", positive0, mh != null);
+        assertFalse("Setter methods should throw an exception if passed a final static field.", ((testMode0 & TEST_SETTER) != 0) && (isFinal && isStatic));
         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();

@@ -759,13 +760,10 @@
         }
         if (f != null && f.getDeclaringClass() == HasFields.class) {
             assertEquals(f.get(fields), value);  // clean to start with
         }
         Throwable caughtEx = null;
-        // non-final field and setAccessible(true) on instance field will have write access
-        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 {

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

@@ -819,12 +817,12 @@
                 if (f != null && f.getDeclaringClass() == HasFields.class) {
                     assertEquals(f.get(fields), putValue);
                 }
             }
         }
-        if (f != null && f.getDeclaringClass() == HasFields.class && writeAccess) {
-            f.set(fields, value);  // put it back if it has write access
+        if ((f != null) && (f.getDeclaringClass() == HasFields.class) && !isFinal) {
+            f.set(fields, value);  // put it back if we changed it.
         }
         if (testNPE) {
             if (caughtEx == null || !(caughtEx instanceof NullPointerException))
                 throw new RuntimeException("failed to catch NPE exception"+(caughtEx == null ? " (caughtEx=null)" : ""), caughtEx);
             caughtEx = null;  // nullify expected exception

@@ -868,12 +866,11 @@
         testSetter(TEST_FIND_STATIC);
     }
 
     public void testSetter(int testMode) throws Throwable {
         Lookup lookup = PRIVATE;  // FIXME: test more lookups than this one
-        startTest("testSetter");
-        for (Object[] c : HasFields.testCasesFor(testMode|TEST_SETTER)) {
+        for (Object[] c : HasFields.CASES) {
             boolean positive = (c[1] != Error.class);
             testSetter(positive, lookup, c[0], c[1], testMode);
             if (positive)
                 testSetter(positive, lookup, c[0], c[1], testMode | TEST_NPE);
         }
< prev index next >