< prev index next >

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

Print this page




 617     public void testFindGetter0() throws Throwable {
 618         if (CAN_SKIP_WORKING)  return;
 619         startTest("findGetter");
 620         testGetter(TEST_FIND_FIELD);
 621         testGetter(TEST_FIND_FIELD | TEST_BOUND);
 622     }
 623 
 624     @Test
 625     public void testFindStaticGetter() throws Throwable {
 626         CodeCacheOverflowProcessor.runMHTest(this::testFindStaticGetter0);
 627     }
 628 
 629     public void testFindStaticGetter0() throws Throwable {
 630         if (CAN_SKIP_WORKING)  return;
 631         startTest("findStaticGetter");
 632         testGetter(TEST_FIND_STATIC);
 633     }
 634 
 635     public void testGetter(int testMode) throws Throwable {
 636         Lookup lookup = PRIVATE;  // FIXME: test more lookups than this one
 637         for (Object[] c : HasFields.CASES) {
 638             boolean positive = (c[1] != Error.class);
 639             testGetter(positive, lookup, c[0], c[1], testMode);
 640             if (positive)
 641                 testGetter(positive, lookup, c[0], c[1], testMode | TEST_NPE);
 642         }
 643         testGetter(true, lookup,
 644                    new Object[]{ true,  System.class, "out", java.io.PrintStream.class },
 645                    System.out, testMode);
 646         for (int isStaticN = 0; isStaticN <= 1; isStaticN++) {
 647             testGetter(false, lookup,
 648                        new Object[]{ (isStaticN != 0), System.class, "bogus", char.class },
 649                        null, testMode);
 650         }
 651     }
 652 
 653     public void testGetter(boolean positive, MethodHandles.Lookup lookup,
 654                            Object fieldRef, Object value, int testMode) throws Throwable {
 655         testAccessor(positive, lookup, fieldRef, value, testMode);
 656     }
 657 


 706             case TEST_SETTER|
 707                  TEST_UNREFLECT:   mh = lookup.unreflectSetter(f);                      break;
 708             case TEST_SETTER|
 709                  TEST_FIND_FIELD:  mh = lookup.findSetter(fclass, fname, ftype);        break;
 710             case TEST_SETTER|
 711                  TEST_FIND_STATIC: mh = lookup.findStaticSetter(fclass, fname, ftype);  break;
 712             default:
 713                 throw new InternalError("testMode="+testMode);
 714             }
 715         } catch (ReflectiveOperationException ex) {
 716             mh = null;
 717             noAccess = ex;
 718             assertExceptionClass(
 719                 (fname.contains("bogus"))
 720                 ?   NoSuchFieldException.class
 721                 :   IllegalAccessException.class,
 722                 noAccess);
 723             if (verbosity >= 5)  ex.printStackTrace(System.out);
 724         }
 725         if (verbosity >= 3)
 726             System.out.println("find"+(isStatic?"Static":"")+(isGetter?"Getter":"Setter")+" "+fclass.getName()+"."+fname+"/"+ftype
 727                                +" => "+mh
 728                                +(noAccess == null ? "" : " !! "+noAccess));





 729         if (positive && !testNPE && noAccess != null)  throw new RuntimeException(noAccess);
 730         assertEquals(positive0 ? "positive test" : "negative test erroneously passed", positive0, mh != null);
 731         if (!positive && !testNPE)  return; // negative access test failed as expected
 732         assertEquals((isStatic ? 0 : 1)+(isGetter ? 0 : 1), mh.type().parameterCount());
 733 
 734 
 735         assertSame(mh.type(), expType);
 736         //assertNameStringContains(mh, fname);  // This does not hold anymore with LFs
 737         HasFields fields = new HasFields();
 738         HasFields fieldsForMH = fields;
 739         if (testNPE)  fieldsForMH = null;  // perturb MH argument to elicit expected error
 740         if (doBound)
 741             mh = mh.bindTo(fieldsForMH);
 742         Object sawValue;
 743         Class<?> vtype = ftype;
 744         if (ftype != int.class)  vtype = Object.class;
 745         if (isGetter) {
 746             mh = mh.asType(mh.type().generic()
 747                            .changeReturnType(vtype));
 748         } else {
 749             int last = mh.type().parameterCount() - 1;
 750             mh = mh.asType(mh.type().generic()
 751                            .changeReturnType(void.class)
 752                            .changeParameterType(last, vtype));
 753         }
 754         if (f != null && f.getDeclaringClass() == HasFields.class) {
 755             assertEquals(f.get(fields), value);  // clean to start with
 756         }
 757         Throwable caughtEx = null;


 758         if (isGetter) {
 759             Object expValue = value;
 760             for (int i = 0; i <= 1; i++) {
 761                 sawValue = null;  // make DA rules happy under try/catch
 762                 try {
 763                     if (isStatic || doBound) {
 764                         if (ftype == int.class)
 765                             sawValue = (int) mh.invokeExact();  // do these exactly
 766                         else
 767                             sawValue = mh.invokeExact();
 768                     } else {
 769                         if (ftype == int.class)
 770                             sawValue = (int) mh.invokeExact((Object) fieldsForMH);
 771                         else
 772                             sawValue = mh.invokeExact((Object) fieldsForMH);
 773                     }
 774                 } catch (RuntimeException ex) {
 775                     if (ex instanceof NullPointerException && testNPE) {
 776                         caughtEx = ex;
 777                         break;
 778                     }
 779                 }
 780                 assertEquals(sawValue, expValue);
 781                 if (f != null && f.getDeclaringClass() == HasFields.class
 782                     && !Modifier.isFinal(f.getModifiers())) {
 783                     Object random = randomArg(ftype);
 784                     f.set(fields, random);
 785                     expValue = random;
 786                 } else {
 787                     break;
 788                 }
 789             }
 790         } else {
 791             for (int i = 0; i <= 1; i++) {
 792                 Object putValue = randomArg(ftype);
 793                 try {
 794                     if (isStatic || doBound) {
 795                         if (ftype == int.class)
 796                             mh.invokeExact((int)putValue);  // do these exactly
 797                         else
 798                             mh.invokeExact(putValue);
 799                     } else {
 800                         if (ftype == int.class)
 801                             mh.invokeExact((Object) fieldsForMH, (int)putValue);
 802                         else
 803                             mh.invokeExact((Object) fieldsForMH, putValue);
 804                     }
 805                 } catch (RuntimeException ex) {
 806                     if (ex instanceof NullPointerException && testNPE) {
 807                         caughtEx = ex;
 808                         break;
 809                     }
 810                 }
 811                 if (f != null && f.getDeclaringClass() == HasFields.class) {
 812                     assertEquals(f.get(fields), putValue);
 813                 }
 814             }
 815         }
 816         if (f != null && f.getDeclaringClass() == HasFields.class) {
 817             f.set(fields, value);  // put it back
 818         }
 819         if (testNPE) {
 820             if (caughtEx == null || !(caughtEx instanceof NullPointerException))
 821                 throw new RuntimeException("failed to catch NPE exception"+(caughtEx == null ? " (caughtEx=null)" : ""), caughtEx);
 822             caughtEx = null;  // nullify expected exception
 823         }
 824         if (caughtEx != null) {
 825             throw new RuntimeException("unexpected exception", caughtEx);
 826         }
 827     }
 828 
 829     @Test
 830     public void testUnreflectSetter() throws Throwable {
 831         CodeCacheOverflowProcessor.runMHTest(this::testUnreflectSetter0);
 832     }
 833 
 834     public void testUnreflectSetter0() throws Throwable {
 835         if (CAN_SKIP_WORKING)  return;
 836         startTest("unreflectSetter");


 845     public void testFindSetter0() throws Throwable {
 846         if (CAN_SKIP_WORKING)  return;
 847         startTest("findSetter");
 848         testSetter(TEST_FIND_FIELD);
 849         testSetter(TEST_FIND_FIELD | TEST_BOUND);
 850     }
 851 
 852     @Test
 853     public void testFindStaticSetter() throws Throwable {
 854         CodeCacheOverflowProcessor.runMHTest(this::testFindStaticSetter0);
 855     }
 856 
 857     public void testFindStaticSetter0() throws Throwable {
 858         if (CAN_SKIP_WORKING)  return;
 859         startTest("findStaticSetter");
 860         testSetter(TEST_FIND_STATIC);
 861     }
 862 
 863     public void testSetter(int testMode) throws Throwable {
 864         Lookup lookup = PRIVATE;  // FIXME: test more lookups than this one
 865         startTest("unreflectSetter");
 866         for (Object[] c : HasFields.CASES) {
 867             boolean positive = (c[1] != Error.class);








 868             testSetter(positive, lookup, c[0], c[1], testMode);
 869             if (positive)
 870                 testSetter(positive, lookup, c[0], c[1], testMode | TEST_NPE);
 871         }
 872         for (int isStaticN = 0; isStaticN <= 1; isStaticN++) {
 873             testSetter(false, lookup,
 874                        new Object[]{ (isStaticN != 0), System.class, "bogus", char.class },
 875                        null, testMode);
 876         }
 877     }
 878 
 879     public void testSetter(boolean positive, MethodHandles.Lookup lookup,
 880                            Object fieldRef, Object value, int testMode) throws Throwable {
 881         testAccessor(positive, lookup, fieldRef, value, testMode | TEST_SETTER);
 882     }
 883 
 884     @Test
 885     public void testArrayElementGetter() throws Throwable {
 886         CodeCacheOverflowProcessor.runMHTest(this::testArrayElementGetter0);
 887     }




 617     public void testFindGetter0() throws Throwable {
 618         if (CAN_SKIP_WORKING)  return;
 619         startTest("findGetter");
 620         testGetter(TEST_FIND_FIELD);
 621         testGetter(TEST_FIND_FIELD | TEST_BOUND);
 622     }
 623 
 624     @Test
 625     public void testFindStaticGetter() throws Throwable {
 626         CodeCacheOverflowProcessor.runMHTest(this::testFindStaticGetter0);
 627     }
 628 
 629     public void testFindStaticGetter0() throws Throwable {
 630         if (CAN_SKIP_WORKING)  return;
 631         startTest("findStaticGetter");
 632         testGetter(TEST_FIND_STATIC);
 633     }
 634 
 635     public void testGetter(int testMode) throws Throwable {
 636         Lookup lookup = PRIVATE;  // FIXME: test more lookups than this one
 637         for (Object[] c : HasFields.cases(testMode)) {
 638             boolean positive = (c[1] != Error.class);
 639             testGetter(positive, lookup, c[0], c[1], testMode);
 640             if (positive)
 641                 testGetter(positive, lookup, c[0], c[1], testMode | TEST_NPE);
 642         }
 643         testGetter(true, lookup,
 644                    new Object[]{ true,  System.class, "out", java.io.PrintStream.class },
 645                    System.out, testMode);
 646         for (int isStaticN = 0; isStaticN <= 1; isStaticN++) {
 647             testGetter(false, lookup,
 648                        new Object[]{ (isStaticN != 0), System.class, "bogus", char.class },
 649                        null, testMode);
 650         }
 651     }
 652 
 653     public void testGetter(boolean positive, MethodHandles.Lookup lookup,
 654                            Object fieldRef, Object value, int testMode) throws Throwable {
 655         testAccessor(positive, lookup, fieldRef, value, testMode);
 656     }
 657 


 706             case TEST_SETTER|
 707                  TEST_UNREFLECT:   mh = lookup.unreflectSetter(f);                      break;
 708             case TEST_SETTER|
 709                  TEST_FIND_FIELD:  mh = lookup.findSetter(fclass, fname, ftype);        break;
 710             case TEST_SETTER|
 711                  TEST_FIND_STATIC: mh = lookup.findStaticSetter(fclass, fname, ftype);  break;
 712             default:
 713                 throw new InternalError("testMode="+testMode);
 714             }
 715         } catch (ReflectiveOperationException ex) {
 716             mh = null;
 717             noAccess = ex;
 718             assertExceptionClass(
 719                 (fname.contains("bogus"))
 720                 ?   NoSuchFieldException.class
 721                 :   IllegalAccessException.class,
 722                 noAccess);
 723             if (verbosity >= 5)  ex.printStackTrace(System.out);
 724         }
 725         if (verbosity >= 3)
 726             System.out.format("%s%s %s.%s/%s => %s %s%n",
 727                 (testMode0 & TEST_UNREFLECT) != 0
 728                     ? "unreflect"
 729                     : "find" + ((testMode0 & TEST_FIND_STATIC) != 0 ? "Static" : ""),
 730                 (isGetter ? "Getter" : "Setter"),
 731                 fclass.getName(), fname, ftype, mh,
 732                 (noAccess == null ? "" : " !! "+noAccess));
 733         if (!positive && noAccess != null) return;
 734         if (positive && !testNPE && noAccess != null)  throw new RuntimeException(noAccess);
 735         assertEquals(positive0 ? "positive test" : "negative test erroneously passed", positive0, mh != null);
 736         if (!positive && !testNPE)  return; // negative access test failed as expected
 737         assertEquals((isStatic ? 0 : 1)+(isGetter ? 0 : 1), mh.type().parameterCount());
 738 

 739         assertSame(mh.type(), expType);
 740         //assertNameStringContains(mh, fname);  // This does not hold anymore with LFs
 741         HasFields fields = new HasFields();
 742         HasFields fieldsForMH = fields;
 743         if (testNPE)  fieldsForMH = null;  // perturb MH argument to elicit expected error
 744         if (doBound)
 745             mh = mh.bindTo(fieldsForMH);
 746         Object sawValue;
 747         Class<?> vtype = ftype;
 748         if (ftype != int.class)  vtype = Object.class;
 749         if (isGetter) {
 750             mh = mh.asType(mh.type().generic()
 751                            .changeReturnType(vtype));
 752         } else {
 753             int last = mh.type().parameterCount() - 1;
 754             mh = mh.asType(mh.type().generic()
 755                            .changeReturnType(void.class)
 756                            .changeParameterType(last, vtype));
 757         }
 758         if (f != null && f.getDeclaringClass() == HasFields.class) {
 759             assertEquals(f.get(fields), value);  // clean to start with
 760         }
 761         Throwable caughtEx = null;
 762         boolean writeAccess = !Modifier.isFinal(f.getModifiers()) ||
 763             (!Modifier.isStatic(f.getModifiers()) && f.isAccessible());
 764         if (isGetter) {
 765             Object expValue = value;
 766             for (int i = 0; i <= 1; i++) {
 767                 sawValue = null;  // make DA rules happy under try/catch
 768                 try {
 769                     if (isStatic || doBound) {
 770                         if (ftype == int.class)
 771                             sawValue = (int) mh.invokeExact();  // do these exactly
 772                         else
 773                             sawValue = mh.invokeExact();
 774                     } else {
 775                         if (ftype == int.class)
 776                             sawValue = (int) mh.invokeExact((Object) fieldsForMH);
 777                         else
 778                             sawValue = mh.invokeExact((Object) fieldsForMH);
 779                     }
 780                 } catch (RuntimeException ex) {
 781                     if (ex instanceof NullPointerException && testNPE) {
 782                         caughtEx = ex;
 783                         break;
 784                     }
 785                 }
 786                 assertEquals(sawValue, expValue);
 787                 if (f != null && f.getDeclaringClass() == HasFields.class && writeAccess) {

 788                     Object random = randomArg(ftype);
 789                     f.set(fields, random);
 790                     expValue = random;
 791                 } else {
 792                     break;
 793                 }
 794             }
 795         } else {
 796             for (int i = 0; i <= 1; i++) {
 797                 Object putValue = randomArg(ftype);
 798                 try {
 799                     if (isStatic || doBound) {
 800                         if (ftype == int.class)
 801                             mh.invokeExact((int)putValue);  // do these exactly
 802                         else
 803                             mh.invokeExact(putValue);
 804                     } else {
 805                         if (ftype == int.class)
 806                             mh.invokeExact((Object) fieldsForMH, (int)putValue);
 807                         else
 808                             mh.invokeExact((Object) fieldsForMH, putValue);
 809                     }
 810                 } catch (RuntimeException ex) {
 811                     if (ex instanceof NullPointerException && testNPE) {
 812                         caughtEx = ex;
 813                         break;
 814                     }
 815                 }
 816                 if (f != null && f.getDeclaringClass() == HasFields.class) {
 817                     assertEquals(f.get(fields), putValue);
 818                 }
 819             }
 820         }
 821         if (f != null && f.getDeclaringClass() == HasFields.class && writeAccess) {
 822             f.set(fields, value);  // put it back
 823         }
 824         if (testNPE) {
 825             if (caughtEx == null || !(caughtEx instanceof NullPointerException))
 826                 throw new RuntimeException("failed to catch NPE exception"+(caughtEx == null ? " (caughtEx=null)" : ""), caughtEx);
 827             caughtEx = null;  // nullify expected exception
 828         }
 829         if (caughtEx != null) {
 830             throw new RuntimeException("unexpected exception", caughtEx);
 831         }
 832     }
 833 
 834     @Test
 835     public void testUnreflectSetter() throws Throwable {
 836         CodeCacheOverflowProcessor.runMHTest(this::testUnreflectSetter0);
 837     }
 838 
 839     public void testUnreflectSetter0() throws Throwable {
 840         if (CAN_SKIP_WORKING)  return;
 841         startTest("unreflectSetter");


 850     public void testFindSetter0() throws Throwable {
 851         if (CAN_SKIP_WORKING)  return;
 852         startTest("findSetter");
 853         testSetter(TEST_FIND_FIELD);
 854         testSetter(TEST_FIND_FIELD | TEST_BOUND);
 855     }
 856 
 857     @Test
 858     public void testFindStaticSetter() throws Throwable {
 859         CodeCacheOverflowProcessor.runMHTest(this::testFindStaticSetter0);
 860     }
 861 
 862     public void testFindStaticSetter0() throws Throwable {
 863         if (CAN_SKIP_WORKING)  return;
 864         startTest("findStaticSetter");
 865         testSetter(TEST_FIND_STATIC);
 866     }
 867 
 868     public void testSetter(int testMode) throws Throwable {
 869         Lookup lookup = PRIVATE;  // FIXME: test more lookups than this one
 870         startTest("testSetter");
 871         for (Object[] c : HasFields.cases(testMode)) {
 872             boolean positive = (c[1] != Error.class) && !(c.length == 3 && c[2] == Error.class);
 873             if ((testMode & TEST_UNREFLECT) != 0 && c.length == 3) {
 874                 assertTrue(c[0] instanceof Field && c[2] == Error.class);
 875                 Field f = (Field)c[0];
 876                 int mods = f.getModifiers();
 877                 // unreflectSetter should only have write access on instance final field if accessible flag is true
 878                 positive = !Modifier.isFinal(mods) ||
 879                            (!Modifier.isStatic(mods) && f.isAccessible());
 880             }
 881             testSetter(positive, lookup, c[0], c[1], testMode);
 882             if (positive)
 883                 testSetter(positive, lookup, c[0], c[1], testMode | TEST_NPE);
 884         }
 885         for (int isStaticN = 0; isStaticN <= 1; isStaticN++) {
 886             testSetter(false, lookup,
 887                        new Object[]{ (isStaticN != 0), System.class, "bogus", char.class },
 888                        null, testMode);
 889         }
 890     }
 891 
 892     public void testSetter(boolean positive, MethodHandles.Lookup lookup,
 893                            Object fieldRef, Object value, int testMode) throws Throwable {
 894         testAccessor(positive, lookup, fieldRef, value, testMode | TEST_SETTER);
 895     }
 896 
 897     @Test
 898     public void testArrayElementGetter() throws Throwable {
 899         CodeCacheOverflowProcessor.runMHTest(this::testArrayElementGetter0);
 900     }


< prev index next >