|
| 1 | +package com.baeldung.java9.methodhandles; |
| 2 | + |
| 3 | +import static org.hamcrest.CoreMatchers.is; |
| 4 | +import static org.junit.Assert.*; |
| 5 | + |
| 6 | +import java.lang.invoke.MethodHandle; |
| 7 | +import java.lang.invoke.MethodHandles; |
| 8 | +import java.lang.invoke.MethodType; |
| 9 | +import java.lang.invoke.WrongMethodTypeException; |
| 10 | +import java.lang.reflect.InvocationTargetException; |
| 11 | +import java.lang.reflect.Method; |
| 12 | +import java.util.Arrays; |
| 13 | +import java.util.List; |
| 14 | + |
| 15 | +import org.junit.Test; |
| 16 | + |
| 17 | +/** |
| 18 | + * Test case for the {@link MethodHandles} API |
| 19 | + */ |
| 20 | +public class MethodHandlesTest { |
| 21 | + |
| 22 | + @Test |
| 23 | + public void givenConcatMethodHandle_whenInvoked_thenCorrectlyConcatenated() throws Throwable { |
| 24 | + |
| 25 | + MethodHandles.Lookup publicLookup = MethodHandles.publicLookup(); |
| 26 | + MethodType mt = MethodType.methodType(String.class, String.class); |
| 27 | + MethodHandle concatMH = publicLookup.findVirtual(String.class, "concat", mt); |
| 28 | + |
| 29 | + String output = (String) concatMH.invoke("Effective ", "Java"); |
| 30 | + |
| 31 | + assertEquals("Effective Java", output); |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + public void givenAsListMethodHandle_whenInvokingWithArguments_thenCorrectlyInvoked() throws Throwable { |
| 36 | + MethodHandles.Lookup publicLookup = MethodHandles.publicLookup(); |
| 37 | + MethodType mt = MethodType.methodType(List.class, Object[].class); |
| 38 | + MethodHandle asListMH = publicLookup.findStatic(Arrays.class, "asList", mt); |
| 39 | + |
| 40 | + List<Integer> list = (List<Integer>) asListMH.invokeWithArguments(1, 2); |
| 41 | + |
| 42 | + assertThat(Arrays.asList(1, 2), is(list)); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void givenConstructorMethodHandle_whenInvoked_thenObjectCreatedCorrectly() throws Throwable { |
| 47 | + MethodHandles.Lookup publicLookup = MethodHandles.publicLookup(); |
| 48 | + MethodType mt = MethodType.methodType(void.class, String.class); |
| 49 | + MethodHandle newIntegerMH = publicLookup.findConstructor(Integer.class, mt); |
| 50 | + |
| 51 | + Integer integer = (Integer) newIntegerMH.invoke("1"); |
| 52 | + |
| 53 | + assertEquals(1, integer.intValue()); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void givenAFieldWithoutGetter_whenCreatingAGetter_thenCorrectlyInvoked() throws Throwable { |
| 58 | + MethodHandles.Lookup lookup = MethodHandles.lookup(); |
| 59 | + MethodHandle getTitleMH = lookup.findGetter(Book.class, "title", String.class); |
| 60 | + |
| 61 | + Book book = new Book("ISBN-1234", "Effective Java"); |
| 62 | + |
| 63 | + assertEquals("Effective Java", getTitleMH.invoke(book)); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void givenPrivateMethod_whenCreatingItsMethodHandle_thenCorrectlyInvoked() throws Throwable { |
| 68 | + MethodHandles.Lookup lookup = MethodHandles.lookup(); |
| 69 | + Method formatBookMethod = Book.class.getDeclaredMethod("formatBook"); |
| 70 | + formatBookMethod.setAccessible(true); |
| 71 | + |
| 72 | + MethodHandle formatBookMH = lookup.unreflect(formatBookMethod); |
| 73 | + |
| 74 | + Book book = new Book("ISBN-123", "Java in Action"); |
| 75 | + |
| 76 | + assertEquals("ISBN-123 > Java in Action", formatBookMH.invoke(book)); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void givenReplaceMethod_whenUsingReflectionAndInvoked_thenCorrectlyReplaced() throws Throwable { |
| 81 | + Method replaceMethod = String.class.getMethod("replace", char.class, char.class); |
| 82 | + |
| 83 | + String string = (String) replaceMethod.invoke("jovo", 'o', 'a'); |
| 84 | + |
| 85 | + assertEquals("java", string); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + public void givenReplaceMethodHandle_whenInvoked_thenCorrectlyReplaced() throws Throwable { |
| 90 | + MethodHandles.Lookup publicLookup = MethodHandles.publicLookup(); |
| 91 | + MethodType mt = MethodType.methodType(String.class, char.class, char.class); |
| 92 | + MethodHandle replaceMH = publicLookup.findVirtual(String.class, "replace", mt); |
| 93 | + |
| 94 | + String replacedString = (String) replaceMH.invoke("jovo", Character.valueOf('o'), 'a'); |
| 95 | + |
| 96 | + assertEquals("java", replacedString); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + public void givenReplaceMethodHandle_whenInvokingExact_thenCorrectlyReplaced() throws Throwable { |
| 101 | + MethodHandles.Lookup lookup = MethodHandles.lookup(); |
| 102 | + MethodType mt = MethodType.methodType(String.class, char.class, char.class); |
| 103 | + MethodHandle replaceMH = lookup.findVirtual(String.class, "replace", mt); |
| 104 | + |
| 105 | + String s = (String) replaceMH.invokeExact("jovo", 'o', 'a'); |
| 106 | + |
| 107 | + assertEquals("java", s); |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + public void givenSumMethodHandle_whenInvokingExact_thenSumIsCorrect() throws Throwable { |
| 112 | + MethodHandles.Lookup lookup = MethodHandles.lookup(); |
| 113 | + MethodType mt = MethodType.methodType(int.class, int.class, int.class); |
| 114 | + MethodHandle sumMH = lookup.findStatic(Integer.class, "sum", mt); |
| 115 | + |
| 116 | + int sum = (int) sumMH.invokeExact(1, 11); |
| 117 | + |
| 118 | + assertEquals(12, sum); |
| 119 | + } |
| 120 | + |
| 121 | + @Test(expected = WrongMethodTypeException.class) |
| 122 | + public void givenSumMethodHandleAndIncompatibleArguments_whenInvokingExact_thenException() throws Throwable { |
| 123 | + MethodHandles.Lookup lookup = MethodHandles.lookup(); |
| 124 | + MethodType mt = MethodType.methodType(int.class, int.class, int.class); |
| 125 | + MethodHandle sumMH = lookup.findStatic(Integer.class, "sum", mt); |
| 126 | + |
| 127 | + sumMH.invokeExact(Integer.valueOf(1), 11); |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + public void givenSpreadedEqualsMethodHandle_whenInvokedOnArray_thenCorrectlyEvaluated() throws Throwable { |
| 132 | + MethodHandles.Lookup publicLookup = MethodHandles.publicLookup(); |
| 133 | + MethodType mt = MethodType.methodType(boolean.class, Object.class); |
| 134 | + MethodHandle equalsMH = publicLookup.findVirtual(String.class, "equals", mt); |
| 135 | + |
| 136 | + MethodHandle methodHandle = equalsMH.asSpreader(Object[].class, 2); |
| 137 | + |
| 138 | + assertTrue((boolean) methodHandle.invoke(new Object[] { "java", "java" })); |
| 139 | + assertFalse((boolean) methodHandle.invoke(new Object[] { "java", "jova" })); |
| 140 | + } |
| 141 | + |
| 142 | + @Test |
| 143 | + public void givenConcatMethodHandle_whenBindToAString_thenCorrectlyConcatenated() throws Throwable { |
| 144 | + MethodHandles.Lookup publicLookup = MethodHandles.publicLookup(); |
| 145 | + MethodType mt = MethodType.methodType(String.class, String.class); |
| 146 | + MethodHandle concatMH = publicLookup.findVirtual(String.class, "concat", mt); |
| 147 | + |
| 148 | + MethodHandle bindedConcatMH = concatMH.bindTo("Hello "); |
| 149 | + |
| 150 | + assertEquals("Hello World!", bindedConcatMH.invoke("World!")); |
| 151 | + } |
| 152 | +} |
0 commit comments