< prev index next >

test/jdk/java/lang/invoke/MethodHandlesTest.java

Print this page




  21  * questions.
  22  */
  23 
  24 package test.java.lang.invoke;
  25 
  26 import org.junit.*;
  27 import test.java.lang.invoke.remote.RemoteExample;
  28 
  29 import java.lang.invoke.MethodHandle;
  30 import java.lang.invoke.MethodHandles;
  31 import java.lang.invoke.MethodHandles.Lookup;
  32 import java.lang.invoke.MethodType;
  33 import java.lang.reflect.Array;
  34 import java.lang.reflect.Field;
  35 import java.lang.reflect.Modifier;
  36 import java.util.ArrayList;
  37 import java.util.Arrays;
  38 import java.util.Collection;
  39 import java.util.Collections;
  40 import java.util.List;

  41 
  42 import static org.junit.Assert.*;
  43 
  44 /**
  45  *
  46  * @author jrose
  47  */
  48 public abstract class MethodHandlesTest {
  49 
  50     static final Class<?> THIS_CLASS = MethodHandlesTest.class;
  51     // How much output?
  52     static int verbosity = 0;
  53 
  54     static {
  55         String vstr = System.getProperty(THIS_CLASS.getSimpleName()+".verbosity");
  56         if (vstr == null)
  57             vstr = System.getProperty(THIS_CLASS.getName()+".verbosity");
  58         if (vstr != null)  verbosity = Integer.parseInt(vstr);
  59     }
  60 


 550         short fS = (short)'S';
 551         char fC = 'C';
 552         int fI = 'I';
 553         long fJ = 'J';
 554         float fF = 'F';
 555         double fD = 'D';
 556         static boolean sZ = true;
 557         static byte sB = 1+(byte)'B';
 558         static short sS = 1+(short)'S';
 559         static char sC = 1+'C';
 560         static int sI = 1+'I';
 561         static long sJ = 1+'J';
 562         static float sF = 1+'F';
 563         static double sD = 1+'D';
 564 
 565         Object fL = 'L';
 566         String fR = "R";
 567         static Object sL = 'M';
 568         static String sR = "S";
 569 
 570         static final Object[][] CASES;
























 571         static {
 572             ArrayList<Object[]> cases = new ArrayList<>();
 573             Object types[][] = {
 574                 {'L',Object.class}, {'R',String.class},
 575                 {'I',int.class}, {'J',long.class},
 576                 {'F',float.class}, {'D',double.class},
 577                 {'Z',boolean.class}, {'B',byte.class},
 578                 {'S',short.class}, {'C',char.class},
 579             };
 580             HasFields fields = new HasFields();
 581             for (Object[] t : types) {
 582                 for (int kind = 0; kind <= 1; kind++) {
 583                     boolean isStatic = (kind != 0);

 584                     char btc = (Character)t[0];
 585                     String name = (isStatic ? "s" : "f") + btc;

 586                     Class<?> type = (Class<?>) t[1];
 587                     Object value;
 588                     Field field;
 589                         try {
 590                         field = HasFields.class.getDeclaredField(name);
 591                     } catch (NoSuchFieldException | SecurityException ex) {
 592                         throw new InternalError("no field HasFields."+name);
 593                     }
 594                     try {
 595                         value = field.get(fields);
 596                     } catch (IllegalArgumentException | IllegalAccessException ex) {
 597                         throw new InternalError("cannot fetch field HasFields."+name);
 598                     }
 599                     if (type == float.class) {
 600                         float v = 'F';
 601                         if (isStatic)  v++;
 602                         assertTrue(value.equals(v));
 603                     }

















 604                     assertTrue(name.equals(field.getName()));
 605                     assertTrue(type.equals(field.getType()));
 606                     assertTrue(isStatic == (Modifier.isStatic(field.getModifiers())));
 607                     cases.add(new Object[]{ field, value });










 608                 }








 609             }
 610             cases.add(new Object[]{ new Object[]{ false, HasFields.class, "bogus_fD", double.class }, Error.class });
 611             cases.add(new Object[]{ new Object[]{ true,  HasFields.class, "bogus_sL", Object.class }, Error.class });
 612             CASES = cases.toArray(new Object[0][]);
 613         }
 614     }
 615 
 616     static final int TEST_UNREFLECT = 1, TEST_FIND_FIELD = 2, TEST_FIND_STATIC = 3, TEST_SETTER = 0x10, TEST_BOUND = 0x20, TEST_NPE = 0x40;
 617 
 618     static boolean testModeMatches(int testMode, boolean isStatic) {
 619         switch (testMode) {
 620         case TEST_FIND_STATIC:          return isStatic;
 621         case TEST_FIND_FIELD:           return !isStatic;
 622         case TEST_UNREFLECT:            return true;  // unreflect matches both
 623         }
 624         throw new InternalError("testMode="+testMode);
 625     }
 626 
 627     static class Callee {
 628         static Object id() { return called("id"); }
 629         static Object id(Object x) { return called("id", x); }
 630         static Object id(Object x, Object y) { return called("id", x, y); }
 631         static Object id(Object x, Object y, Object z) { return called("id", x, y, z); }
 632         static Object id(Object... vx) { return called("id", vx); }




  21  * questions.
  22  */
  23 
  24 package test.java.lang.invoke;
  25 
  26 import org.junit.*;
  27 import test.java.lang.invoke.remote.RemoteExample;
  28 
  29 import java.lang.invoke.MethodHandle;
  30 import java.lang.invoke.MethodHandles;
  31 import java.lang.invoke.MethodHandles.Lookup;
  32 import java.lang.invoke.MethodType;
  33 import java.lang.reflect.Array;
  34 import java.lang.reflect.Field;
  35 import java.lang.reflect.Modifier;
  36 import java.util.ArrayList;
  37 import java.util.Arrays;
  38 import java.util.Collection;
  39 import java.util.Collections;
  40 import java.util.List;
  41 import java.util.stream.Stream;
  42 
  43 import static org.junit.Assert.*;
  44 
  45 /**
  46  *
  47  * @author jrose
  48  */
  49 public abstract class MethodHandlesTest {
  50 
  51     static final Class<?> THIS_CLASS = MethodHandlesTest.class;
  52     // How much output?
  53     static int verbosity = 0;
  54 
  55     static {
  56         String vstr = System.getProperty(THIS_CLASS.getSimpleName()+".verbosity");
  57         if (vstr == null)
  58             vstr = System.getProperty(THIS_CLASS.getName()+".verbosity");
  59         if (vstr != null)  verbosity = Integer.parseInt(vstr);
  60     }
  61 


 551         short fS = (short)'S';
 552         char fC = 'C';
 553         int fI = 'I';
 554         long fJ = 'J';
 555         float fF = 'F';
 556         double fD = 'D';
 557         static boolean sZ = true;
 558         static byte sB = 1+(byte)'B';
 559         static short sS = 1+(short)'S';
 560         static char sC = 1+'C';
 561         static int sI = 1+'I';
 562         static long sJ = 1+'J';
 563         static float sF = 1+'F';
 564         static double sD = 1+'D';
 565 
 566         Object fL = 'L';
 567         String fR = "R";
 568         static Object sL = 'M';
 569         static String sR = "S";
 570 
 571         // final fields
 572         final boolean final_fZ = false;
 573         final byte final_fB = (byte)'B';
 574         final short final_fS = (short)'S';
 575         final char final_fC = 'C';
 576         final int final_fI = 'I';
 577         final long final_fJ = 'J';
 578         final float final_fF = 'F';
 579         final double final_fD = 'D';
 580         final static boolean final_sZ = true;
 581         final static byte final_sB = 1+(byte)'B';
 582         final static short final_sS = 1+(short)'S';
 583         final static char final_sC = 1+'C';
 584         final static int final_sI = 1+'I';
 585         final static long final_sJ = 1+'J';
 586         final static float final_sF = 1+'F';
 587         final static double final_sD = 1+'D';
 588 
 589         final Object final_fL = 'L';
 590         final String final_fR = "R";
 591         final static Object final_sL = 'M';
 592         final static String final_sR = "S";
 593 
 594         static final ArrayList<Object[]> STATIC_FIELD_CASES = new ArrayList<>();
 595         static final ArrayList<Object[]> INSTANCE_FIELD_CASES = new ArrayList<>();
 596         static {

 597             Object types[][] = {
 598                 {'L',Object.class}, {'R',String.class},
 599                 {'I',int.class}, {'J',long.class},
 600                 {'F',float.class}, {'D',double.class},
 601                 {'Z',boolean.class}, {'B',byte.class},
 602                 {'S',short.class}, {'C',char.class},
 603             };
 604             HasFields fields = new HasFields();
 605             for (Object[] t : types) {
 606                 for (int kind = 0; kind <= 1; kind++) {
 607                     boolean isStatic = (kind != 0);
 608                     ArrayList<Object[]> cases = isStatic ? STATIC_FIELD_CASES : INSTANCE_FIELD_CASES;
 609                     char btc = (Character)t[0];
 610                     String fname = (isStatic ? "s" : "f") + btc;
 611                     String finalFname = (isStatic ? "final_s" : "final_f") + btc;
 612                     Class<?> type = (Class<?>) t[1];
 613                     // non-final field
 614                     Field nonFinalField = getField(fname, type);
 615                     Object value = getValue(fields, nonFinalField);









 616                     if (type == float.class) {
 617                         float v = 'F';
 618                         if (isStatic)  v++;
 619                         assertTrue(value.equals(v));
 620                     }
 621                     assertTrue(isStatic == (Modifier.isStatic(nonFinalField.getModifiers())));
 622                     cases.add(new Object[]{ nonFinalField, value });
 623 
 624                     // setAccessible(true) on final field but static final field only has read access
 625                     Field finalField = getField(finalFname, type);
 626                     finalField.setAccessible(true);
 627                     assertTrue(isStatic == (Modifier.isStatic(finalField.getModifiers())));
 628                     cases.add(new Object[]{ finalField, value, Error.class});
 629                 }
 630             }
 631             INSTANCE_FIELD_CASES.add(new Object[]{ new Object[]{ false, HasFields.class, "bogus_fD", double.class }, Error.class });
 632             STATIC_FIELD_CASES.add(new Object[]{ new Object[]{ true,  HasFields.class, "bogus_sL", Object.class }, Error.class });
 633         }
 634 
 635         static Field getField(String name, Class<?> type) {
 636             try {
 637                 Field field = HasFields.class.getDeclaredField(name);
 638                 assertTrue(name.equals(field.getName()));
 639                 assertTrue(type.equals(field.getType()));
 640                 return field;
 641             } catch (NoSuchFieldException | SecurityException ex) {
 642                 throw new InternalError("no field HasFields."+name);
 643             }
 644         }
 645 
 646         static Object getValue(Object o, Field field) {
 647             try {
 648                 return field.get(o);
 649             } catch (IllegalArgumentException | IllegalAccessException ex) {
 650                 throw new InternalError("cannot fetch field HasFields."+field.getName());
 651             }
 652         }
 653         static Object[][] cases(int testMode) {
 654             if ((testMode & TEST_UNREFLECT) != 0) {
 655                 return Stream.concat(STATIC_FIELD_CASES.stream(), INSTANCE_FIELD_CASES.stream())
 656                     .toArray(Object[][]::new);
 657             } else if ((testMode & TEST_FIND_STATIC) != 0) {
 658                 return STATIC_FIELD_CASES.stream().toArray(Object[][]::new);
 659             } else if ((testMode & TEST_FIND_FIELD) != 0) {
 660                 return INSTANCE_FIELD_CASES.stream().toArray(Object[][]::new);
 661             }
 662             throw new InternalError("unexpected test mode: " + testMode);


 663         }
 664     }
 665 
 666     static final int TEST_UNREFLECT = 1, TEST_FIND_FIELD = 2, TEST_FIND_STATIC = 3, TEST_SETTER = 0x10, TEST_BOUND = 0x20, TEST_NPE = 0x40;
 667 
 668     static boolean testModeMatches(int testMode, boolean isStatic) {
 669         switch (testMode) {
 670         case TEST_FIND_STATIC:          return isStatic;
 671         case TEST_FIND_FIELD:           return !isStatic;
 672         case TEST_UNREFLECT:            return true;  // unreflect matches both
 673         }
 674         throw new InternalError("testMode="+testMode);
 675     }
 676 
 677     static class Callee {
 678         static Object id() { return called("id"); }
 679         static Object id(Object x) { return called("id", x); }
 680         static Object id(Object x, Object y) { return called("id", x, y); }
 681         static Object id(Object x, Object y, Object z) { return called("id", x, y, z); }
 682         static Object id(Object... vx) { return called("id", vx); }


< prev index next >