Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 1 | // Copyright (c) 2017, the R8 project authors. Please see the AUTHORS file |
| 2 | // for details. All rights reserved. Use of this source code is governed by a |
| 3 | // BSD-style license that can be found in the LICENSE file. |
| 4 | package invokepolymorphic; |
| 5 | |
| 6 | import java.lang.invoke.MethodHandle; |
| 7 | import java.lang.invoke.MethodHandles; |
| 8 | import java.lang.invoke.MethodType; |
| 9 | |
| 10 | class Data { |
| 11 | } |
| 12 | |
| 13 | public class InvokePolymorphic { |
| 14 | |
| 15 | public String buildString(Integer i1, int i2, String s) { |
| 16 | return (i1 == null ? "N" : "!N") + "-" + i2 + "-" + s; |
| 17 | } |
| 18 | |
| 19 | public void testInvokePolymorphic() { |
| 20 | MethodType mt = MethodType.methodType(String.class, Integer.class, int.class, String.class); |
| 21 | MethodHandles.Lookup lk = MethodHandles.lookup(); |
| 22 | |
| 23 | try { |
| 24 | MethodHandle mh = lk.findVirtual(getClass(), "buildString", mt); |
| 25 | System.out.println(mh.invoke(this, null, 1, "string")); |
| 26 | } catch (Throwable t) { |
| 27 | t.printStackTrace(); |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | public String buildString( |
| 32 | byte b, char c, short s, float f, double d, long l, Integer i1, int i2, String str) { |
| 33 | return b + "-" + c + "-" + s + "-" + f + "-" + d + "-" + l + "-" + (i1 == null ? "N" : "!N") |
| 34 | + "-" + i2 + "-" + str; |
| 35 | } |
| 36 | |
| 37 | public void testInvokePolymorphicRange() { |
| 38 | MethodType mt = MethodType.methodType(String.class, byte.class, char.class, short.class, |
| 39 | float.class, double.class, long.class, Integer.class, int.class, String.class); |
| 40 | MethodHandles.Lookup lk = MethodHandles.lookup(); |
| 41 | |
| 42 | try { |
| 43 | MethodHandle mh = lk.findVirtual(getClass(), "buildString", mt); |
| 44 | System.out.println( |
| 45 | mh.invoke(this, (byte) 2, 'a', (short) 0xFFFF, 1.1f, 2.24d, 12345678L, null, |
| 46 | 1, "string")); |
| 47 | } catch (Throwable t) { |
| 48 | t.printStackTrace(); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | public static void testWithAllTypes( |
| 53 | boolean z, char a, short b, int c, long d, float e, double f, String g, Object h) { |
| 54 | System.out.println(z); |
| 55 | System.out.println(a); |
| 56 | System.out.println(b); |
| 57 | System.out.println(c); |
| 58 | System.out.println(d); |
| 59 | System.out.println(e); |
| 60 | System.out.println(f); |
| 61 | System.out.println(g); |
| 62 | System.out.println(h); |
| 63 | } |
| 64 | |
| 65 | public void testInvokePolymorphicWithAllTypes() { |
| 66 | try { |
| 67 | MethodHandle mth = |
| 68 | MethodHandles.lookup() |
| 69 | .findStatic( |
| 70 | InvokePolymorphic.class, |
| 71 | "testWithAllTypes", |
| 72 | MethodType.methodType( |
| 73 | void.class, boolean.class, char.class, short.class, int.class, long.class, |
| 74 | float.class, double.class, String.class, Object.class)); |
| 75 | mth.invokeExact(false,'h', (short) 56, 72, Integer.MAX_VALUE + 42l, |
| 76 | 0.56f, 100.0d, "hello", (Object) "goodbye"); |
| 77 | } catch (Throwable t) { |
| 78 | t.printStackTrace(); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | public MethodHandle testInvokePolymorphicWithConstructor() { |
| 83 | MethodHandle mh = null; |
| 84 | MethodType mt = MethodType.methodType(void.class); |
| 85 | MethodHandles.Lookup lk = MethodHandles.lookup(); |
| 86 | |
| 87 | try { |
| 88 | mh = lk.findConstructor(Data.class, mt); |
| 89 | System.out.println(mh.invoke().getClass() == Data.class); |
| 90 | } catch (Throwable t) { |
| 91 | t.printStackTrace(); |
| 92 | } |
| 93 | |
| 94 | return mh; |
| 95 | } |
| 96 | |
| 97 | public static void main(String[] args) { |
| 98 | InvokePolymorphic invokePolymorphic = new InvokePolymorphic(); |
| 99 | invokePolymorphic.testInvokePolymorphic(); |
| 100 | invokePolymorphic.testInvokePolymorphicRange(); |
| 101 | invokePolymorphic.testInvokePolymorphicWithAllTypes(); |
| 102 | invokePolymorphic.testInvokePolymorphicWithConstructor(); |
| 103 | } |
| 104 | } |