test/java/lang/invoke/MethodHandles/TestCatchException.java
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File jdk Sdiff test/java/lang/invoke/MethodHandles

test/java/lang/invoke/MethodHandles/TestCatchException.java

Print this page
rev 9089 : [mq]: 8027827.catchException.custom.webrev01
rev 9092 : [mq]: 8027827.caching


  55         MethodHandle target = LOOKUP.findStatic(TestCatchException.class, "noThrow", M_TYPE);
  56         MethodHandle handler = LOOKUP.findStatic(TestCatchException.class, "handler", MethodType.methodType(int.class, Exception.class));
  57 
  58         MethodHandle h = MethodHandles.catchException(target, Exception.class, handler);
  59 
  60         int x = (int)h.invokeExact(new Object(), new Object(), 1, 2, 3, 4, 5, 6, 7);
  61         assertEquals(x, 42);
  62     }
  63 
  64     @Test
  65     public void testThrowPath() throws Throwable {
  66         MethodHandle target = LOOKUP.findStatic(TestCatchException.class, "throwEx", M_TYPE);
  67         MethodHandle handler = LOOKUP.findStatic(TestCatchException.class, "handler", MethodType.methodType(int.class, Exception.class));
  68 
  69         MethodHandle h = MethodHandles.catchException(target, Exception.class, handler);
  70 
  71         int x = (int)h.invokeExact(new Object(), new Object(), 1, 2, 3, 4, 5, 6, 7);
  72         assertEquals(x, 17);
  73     }
  74 


































  75     public static void main(String[] args) throws Throwable {
  76         TestCatchException test = new TestCatchException();
  77         test.testNoThrowPath();
  78         test.testThrowPath();

  79         System.out.println("TEST PASSED");
  80     }
  81 }


  55         MethodHandle target = LOOKUP.findStatic(TestCatchException.class, "noThrow", M_TYPE);
  56         MethodHandle handler = LOOKUP.findStatic(TestCatchException.class, "handler", MethodType.methodType(int.class, Exception.class));
  57 
  58         MethodHandle h = MethodHandles.catchException(target, Exception.class, handler);
  59 
  60         int x = (int)h.invokeExact(new Object(), new Object(), 1, 2, 3, 4, 5, 6, 7);
  61         assertEquals(x, 42);
  62     }
  63 
  64     @Test
  65     public void testThrowPath() throws Throwable {
  66         MethodHandle target = LOOKUP.findStatic(TestCatchException.class, "throwEx", M_TYPE);
  67         MethodHandle handler = LOOKUP.findStatic(TestCatchException.class, "handler", MethodType.methodType(int.class, Exception.class));
  68 
  69         MethodHandle h = MethodHandles.catchException(target, Exception.class, handler);
  70 
  71         int x = (int)h.invokeExact(new Object(), new Object(), 1, 2, 3, 4, 5, 6, 7);
  72         assertEquals(x, 17);
  73     }
  74 
  75 
  76     public static Object m1(Object o1, Object o2, Object o3, Object o4, Object o5,
  77                             Object o6, Object o7, Object o8, Object... tail) {
  78         return tail;
  79     }
  80 
  81     public static Object m2(Exception e, Object o1, Object o2, Object o3, Object o4,
  82                             Object o5, Object o6, Object o7, Object o8, Object... tail) {
  83         return tail;
  84     }
  85 
  86     @Test
  87     public void testVarargsCollector() throws Throwable {
  88         MethodType t1 = MethodType.methodType(Object.class, Object.class, Object.class, Object.class, Object.class,
  89                 Object.class, Object.class, Object.class, Object.class, Object[].class);
  90 
  91         MethodType t2 = t1.insertParameterTypes(0, Exception.class);
  92 
  93         MethodHandle target = LOOKUP.findStatic(TestCatchException.class, "m1", t1)
  94                                     .asVarargsCollector(Object[].class);
  95 
  96         MethodHandle catcher = LOOKUP.findStatic(TestCatchException.class, "m2", t2);
  97 
  98         MethodHandle gwc = MethodHandles.catchException(target, Exception.class, catcher);
  99 
 100         Object o = new Object();
 101         Object[] obj1 = new Object[] { "str" };
 102 
 103         Object r1 = target.invokeExact(o, o, o, o, o, o, o, o, obj1);
 104         Object r2 = gwc.invokeExact(o, o, o, o, o, o, o, o, obj1);
 105         assertEquals(r1, obj1);
 106         assertEquals(r2, obj1);
 107     }
 108 
 109     public static void main(String[] args) throws Throwable {
 110         TestCatchException test = new TestCatchException();
 111         test.testNoThrowPath();
 112         test.testThrowPath();
 113         test.testVarargsCollector();
 114         System.out.println("TEST PASSED");
 115     }
 116 }
test/java/lang/invoke/MethodHandles/TestCatchException.java
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File