Path: blob/master/test/jdk/java/lang/constant/MethodHandleDescTest.java
66644 views
/*1* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.lang.invoke.MethodHandle;24import java.lang.invoke.MethodHandleInfo;25import java.lang.invoke.MethodHandles;26import java.lang.invoke.MethodType;27import java.lang.invoke.WrongMethodTypeException;28import java.lang.constant.ClassDesc;29import java.lang.constant.ConstantDescs;30import java.lang.constant.DirectMethodHandleDesc;31import java.lang.constant.MethodHandleDesc;32import java.lang.reflect.Field;33import java.lang.reflect.Modifier;34import java.lang.constant.MethodTypeDesc;35import java.util.ArrayList;36import java.util.List;37import java.util.function.Supplier;3839import org.testng.annotations.Test;4041import static java.lang.constant.ConstantDescs.CD_Void;42import static java.lang.constant.ConstantDescs.CD_boolean;43import static java.lang.constant.DirectMethodHandleDesc.*;44import static java.lang.constant.DirectMethodHandleDesc.Kind.GETTER;45import static java.lang.constant.DirectMethodHandleDesc.Kind.SETTER;46import static java.lang.constant.DirectMethodHandleDesc.Kind.STATIC_GETTER;47import static java.lang.constant.DirectMethodHandleDesc.Kind.STATIC_SETTER;48import static java.lang.constant.DirectMethodHandleDesc.Kind.VIRTUAL;49import static java.lang.constant.ConstantDescs.CD_Integer;50import static java.lang.constant.ConstantDescs.CD_List;51import static java.lang.constant.ConstantDescs.CD_Object;52import static java.lang.constant.ConstantDescs.CD_String;53import static java.lang.constant.ConstantDescs.CD_int;54import static java.lang.constant.ConstantDescs.CD_void;55import static java.lang.invoke.MethodHandleInfo.*;56import static org.testng.Assert.assertEquals;57import static org.testng.Assert.assertNotEquals;58import static org.testng.Assert.assertNotSame;59import static org.testng.Assert.assertSame;60import static org.testng.Assert.assertTrue;61import static org.testng.Assert.fail;6263/**64* @test65* @compile MethodHandleDescTest.java66* @run testng MethodHandleDescTest67* @summary unit tests for java.lang.constant.MethodHandleDesc68*/69@Test70public class MethodHandleDescTest extends SymbolicDescTest {71private static ClassDesc helperHolderClass = ClassDesc.of("TestHelpers");72private static ClassDesc testClass = helperHolderClass.nested("TestClass");73private static ClassDesc testInterface = helperHolderClass.nested("TestInterface");74private static ClassDesc testSuperclass = helperHolderClass.nested("TestSuperclass");757677private static void assertMHEquals(MethodHandle a, MethodHandle b) {78MethodHandleInfo ia = LOOKUP.revealDirect(a);79MethodHandleInfo ib = LOOKUP.revealDirect(b);80assertEquals(ia.getDeclaringClass(), ib.getDeclaringClass());81assertEquals(ia.getName(), ib.getName());82assertEquals(ia.getMethodType(), ib.getMethodType());83assertEquals(ia.getReferenceKind(), ib.getReferenceKind());84}8586private void testMethodHandleDesc(MethodHandleDesc r) throws ReflectiveOperationException {87if (r instanceof DirectMethodHandleDesc) {88testSymbolicDesc(r);8990DirectMethodHandleDesc rr = (DirectMethodHandleDesc) r;91assertEquals(r, MethodHandleDesc.of(rr.kind(), rr.owner(), rr.methodName(), rr.lookupDescriptor()));92assertEquals(r.invocationType().resolveConstantDesc(LOOKUP), ((MethodHandle) r.resolveConstantDesc(LOOKUP)).type());93}94else {95testSymbolicDescForwardOnly(r);96}97}9899private String lookupDescriptor(DirectMethodHandleDesc rr) {100switch (rr.kind()) {101case VIRTUAL:102case SPECIAL:103case INTERFACE_VIRTUAL:104case INTERFACE_SPECIAL:105return rr.invocationType().dropParameterTypes(0, 1).descriptorString();106case CONSTRUCTOR:107return rr.invocationType().changeReturnType(CD_void).descriptorString();108default:109return rr.invocationType().descriptorString();110}111}112113private void testMethodHandleDesc(MethodHandleDesc r, MethodHandle mh) throws ReflectiveOperationException {114testMethodHandleDesc(r);115116assertMHEquals(((MethodHandle) r.resolveConstantDesc(LOOKUP)), mh);117assertEquals(mh.describeConstable().orElseThrow(), r);118119// compare extractable properties: refKind, owner, name, type120MethodHandleInfo mhi = LOOKUP.revealDirect(mh);121DirectMethodHandleDesc rr = (DirectMethodHandleDesc) r;122assertEquals(mhi.getDeclaringClass().descriptorString(), rr.owner().descriptorString());123assertEquals(mhi.getName(), rr.methodName());124assertEquals(mhi.getReferenceKind(), rr.kind().refKind);125MethodType type = mhi.getMethodType();126assertEquals(type.toMethodDescriptorString(), lookupDescriptor(rr));127}128129public void testSimpleMHs() throws ReflectiveOperationException {130MethodHandle MH_String_isEmpty = LOOKUP.findVirtual(String.class, "isEmpty", MethodType.fromMethodDescriptorString("()Z", null));131testMethodHandleDesc(MethodHandleDesc.of(Kind.VIRTUAL, CD_String, "isEmpty", "()Z"), MH_String_isEmpty);132testMethodHandleDesc(MethodHandleDesc.ofMethod(Kind.VIRTUAL, CD_String, "isEmpty", MethodTypeDesc.of(CD_boolean)), MH_String_isEmpty);133134MethodHandle MH_List_isEmpty = LOOKUP.findVirtual(List.class, "isEmpty", MethodType.fromMethodDescriptorString("()Z", null));135testMethodHandleDesc(MethodHandleDesc.of(Kind.INTERFACE_VIRTUAL, CD_List, "isEmpty", "()Z"), MH_List_isEmpty);136testMethodHandleDesc(MethodHandleDesc.ofMethod(Kind.INTERFACE_VIRTUAL, CD_List, "isEmpty", MethodTypeDesc.of(CD_boolean)), MH_List_isEmpty);137138MethodHandle MH_String_format = LOOKUP.findStatic(String.class, "format", MethodType.methodType(String.class, String.class, Object[].class));139testMethodHandleDesc(MethodHandleDesc.of(Kind.STATIC, CD_String, "format", MethodType.methodType(String.class, String.class, Object[].class).descriptorString()),140MH_String_format);141testMethodHandleDesc(MethodHandleDesc.ofMethod(Kind.STATIC, CD_String, "format", MethodTypeDesc.of(CD_String, CD_String, CD_Object.arrayType())),142MH_String_format);143144MethodHandle MH_ArrayList_new = LOOKUP.findConstructor(ArrayList.class, MethodType.methodType(void.class));145testMethodHandleDesc(MethodHandleDesc.ofMethod(Kind.CONSTRUCTOR, ClassDesc.of("java.util.ArrayList"), "<init>", MethodTypeDesc.of(CD_void)),146MH_ArrayList_new);147testMethodHandleDesc(MethodHandleDesc.ofConstructor(ClassDesc.of("java.util.ArrayList")), MH_ArrayList_new);148149// bad constructor non void return type150try {151MethodHandleDesc.of(Kind.CONSTRUCTOR, ClassDesc.of("java.util.ArrayList"), "<init>", "()I");152fail("should have failed: non void return type for constructor");153} catch (IllegalArgumentException ex) {154// good155}156157// null list of parameters158try {159MethodHandleDesc.ofConstructor(ClassDesc.of("java.util.ArrayList", null));160fail("should have failed: null list of parameters");161} catch (NullPointerException ex) {162// good163}164165// null elements in list of parameters166try {167ClassDesc[] paramList = new ClassDesc[1];168paramList[0] = null;169MethodHandleDesc.ofConstructor(ClassDesc.of("java.util.ArrayList"), paramList);170fail("should have failed: null content in list of parameters");171} catch (NullPointerException ex) {172// good173}174}175176public void testAsType() throws Throwable {177MethodHandleDesc mhr = MethodHandleDesc.ofMethod(Kind.STATIC, ClassDesc.of("java.lang.Integer"), "valueOf",178MethodTypeDesc.of(CD_Integer, CD_int));179MethodHandleDesc takesInteger = mhr.asType(MethodTypeDesc.of(CD_Integer, CD_Integer));180testMethodHandleDesc(takesInteger);181MethodHandle mh1 = (MethodHandle) takesInteger.resolveConstantDesc(LOOKUP);182assertEquals((Integer) 3, (Integer) mh1.invokeExact((Integer) 3));183assertEquals(takesInteger.toString(), "MethodHandleDesc[STATIC/Integer::valueOf(int)Integer].asType(Integer)Integer");184185try {186Integer i = (Integer) mh1.invokeExact(3);187fail("Expected WMTE");188}189catch (WrongMethodTypeException ignored) { }190191MethodHandleDesc takesInt = takesInteger.asType(MethodTypeDesc.of(CD_Integer, CD_int));192testMethodHandleDesc(takesInt);193MethodHandle mh2 = (MethodHandle) takesInt.resolveConstantDesc(LOOKUP);194assertEquals((Integer) 3, (Integer) mh2.invokeExact(3));195196try {197Integer i = (Integer) mh2.invokeExact((Integer) 3);198fail("Expected WMTE");199}200catch (WrongMethodTypeException ignored) { }201202// Short circuit optimization203MethodHandleDesc same = mhr.asType(mhr.invocationType());204assertSame(mhr, same);205206try {207mhr.asType(null);208fail("Expected NPE");209} catch (NullPointerException ex) {210// good211}212213// @@@ Test varargs adaptation214// @@@ Test bad adaptations and assert runtime error on resolution215// @@@ Test intrinsification of adapted MH216}217218public void testMethodHandleDesc() throws Throwable {219MethodHandleDesc ctorDesc = MethodHandleDesc.of(Kind.CONSTRUCTOR, testClass, "<ignored!>", "()V");220MethodHandleDesc staticMethodDesc = MethodHandleDesc.of(Kind.STATIC, testClass, "sm", "(I)I");221MethodHandleDesc staticIMethodDesc = MethodHandleDesc.of(Kind.INTERFACE_STATIC, testInterface, "sm", "(I)I");222MethodHandleDesc instanceMethodDesc = MethodHandleDesc.of(Kind.VIRTUAL, testClass, "m", "(I)I");223MethodHandleDesc instanceIMethodDesc = MethodHandleDesc.of(Kind.INTERFACE_VIRTUAL, testInterface, "m", "(I)I");224MethodHandleDesc superMethodDesc = MethodHandleDesc.of(Kind.SPECIAL, testSuperclass, "m", "(I)I");225MethodHandleDesc superIMethodDesc = MethodHandleDesc.of(Kind.INTERFACE_SPECIAL, testInterface, "m", "(I)I");226MethodHandleDesc privateMethodDesc = MethodHandleDesc.of(Kind.SPECIAL, testClass, "pm", "(I)I");227MethodHandleDesc privateIMethodDesc = MethodHandleDesc.of(Kind.INTERFACE_SPECIAL, testInterface, "pm", "(I)I");228MethodHandleDesc privateStaticMethodDesc = MethodHandleDesc.of(Kind.STATIC, testClass, "psm", "(I)I");229MethodHandleDesc privateStaticIMethodDesc = MethodHandleDesc.of(Kind.INTERFACE_STATIC, testInterface, "psm", "(I)I");230231assertEquals(ctorDesc.invocationType(), MethodTypeDesc.of(testClass));232assertEquals(((DirectMethodHandleDesc) ctorDesc).lookupDescriptor(), "()V");233234assertEquals(staticMethodDesc.invocationType().descriptorString(), "(I)I");235assertEquals(((DirectMethodHandleDesc) staticMethodDesc).lookupDescriptor(), "(I)I");236237assertEquals(instanceMethodDesc.invocationType().descriptorString(), "(" + testClass.descriptorString() + "I)I");238assertEquals(((DirectMethodHandleDesc) instanceMethodDesc).lookupDescriptor(), "(I)I");239240for (MethodHandleDesc r : List.of(ctorDesc, staticMethodDesc, staticIMethodDesc, instanceMethodDesc, instanceIMethodDesc))241testMethodHandleDesc(r);242243TestHelpers.TestClass instance = (TestHelpers.TestClass) ((MethodHandle)ctorDesc.resolveConstantDesc(LOOKUP)).invokeExact();244TestHelpers.TestClass instance2 = (TestHelpers.TestClass) ((MethodHandle)ctorDesc.resolveConstantDesc(TestHelpers.TestClass.LOOKUP)).invokeExact();245TestHelpers.TestInterface instanceI = instance;246247assertNotSame(instance, instance2);248249assertEquals(5, (int) ((MethodHandle)staticMethodDesc.resolveConstantDesc(LOOKUP)).invokeExact(5));250assertEquals(5, (int) ((MethodHandle)staticMethodDesc.resolveConstantDesc(TestHelpers.TestClass.LOOKUP)).invokeExact(5));251assertEquals(0, (int) ((MethodHandle)staticIMethodDesc.resolveConstantDesc(LOOKUP)).invokeExact(5));252assertEquals(0, (int) ((MethodHandle)staticIMethodDesc.resolveConstantDesc(TestHelpers.TestClass.LOOKUP)).invokeExact(5));253254assertEquals(5, (int) ((MethodHandle)instanceMethodDesc.resolveConstantDesc(LOOKUP)).invokeExact(instance, 5));255assertEquals(5, (int) ((MethodHandle)instanceMethodDesc.resolveConstantDesc(TestHelpers.TestClass.LOOKUP)).invokeExact(instance, 5));256assertEquals(5, (int) ((MethodHandle)instanceIMethodDesc.resolveConstantDesc(LOOKUP)).invokeExact(instanceI, 5));257assertEquals(5, (int) ((MethodHandle)instanceIMethodDesc.resolveConstantDesc(TestHelpers.TestClass.LOOKUP)).invokeExact(instanceI, 5));258259try { superMethodDesc.resolveConstantDesc(LOOKUP); fail(); }260catch (IllegalAccessException e) { /* expected */ }261assertEquals(-1, (int) ((MethodHandle)superMethodDesc.resolveConstantDesc(TestHelpers.TestClass.LOOKUP)).invokeExact(instance, 5));262263try { superIMethodDesc.resolveConstantDesc(LOOKUP); fail(); }264catch (IllegalAccessException e) { /* expected */ }265assertEquals(0, (int) ((MethodHandle)superIMethodDesc.resolveConstantDesc(TestHelpers.TestClass.LOOKUP)).invokeExact(instance, 5));266267try { privateMethodDesc.resolveConstantDesc(LOOKUP); fail(); }268catch (IllegalAccessException e) { /* expected */ }269assertEquals(5, (int) ((MethodHandle)privateMethodDesc.resolveConstantDesc(TestHelpers.TestClass.LOOKUP)).invokeExact(instance, 5));270271try { privateIMethodDesc.resolveConstantDesc(LOOKUP); fail(); }272catch (IllegalAccessException e) { /* expected */ }273assertEquals(0, (int) ((MethodHandle)privateIMethodDesc.resolveConstantDesc(TestHelpers.TestInterface.LOOKUP)).invokeExact(instanceI, 5));274assertEquals(0, (int) ((MethodHandle)privateIMethodDesc.resolveConstantDesc(TestHelpers.TestClass.LOOKUP)).invoke(instanceI, 5));275276try { privateStaticMethodDesc.resolveConstantDesc(LOOKUP); fail(); }277catch (IllegalAccessException e) { /* expected */ }278assertEquals(5, (int) ((MethodHandle)privateStaticMethodDesc.resolveConstantDesc(TestHelpers.TestClass.LOOKUP)).invokeExact(5));279280try { privateStaticIMethodDesc.resolveConstantDesc(LOOKUP); fail(); }281catch (IllegalAccessException e) { /* expected */ }282assertEquals(0, (int) ((MethodHandle)privateStaticIMethodDesc.resolveConstantDesc(TestHelpers.TestInterface.LOOKUP)).invokeExact(5));283assertEquals(0, (int) ((MethodHandle)privateStaticIMethodDesc.resolveConstantDesc(TestHelpers.TestClass.LOOKUP)).invokeExact(5));284285MethodHandleDesc staticSetterDesc = MethodHandleDesc.ofField(STATIC_SETTER, testClass, "sf", CD_int);286MethodHandleDesc staticGetterDesc = MethodHandleDesc.ofField(STATIC_GETTER, testClass, "sf", CD_int);287MethodHandleDesc staticGetterIDesc = MethodHandleDesc.ofField(STATIC_GETTER, testInterface, "sf", CD_int);288MethodHandleDesc setterDesc = MethodHandleDesc.ofField(SETTER, testClass, "f", CD_int);289MethodHandleDesc getterDesc = MethodHandleDesc.ofField(GETTER, testClass, "f", CD_int);290291for (MethodHandleDesc r : List.of(staticSetterDesc, staticGetterDesc, staticGetterIDesc, setterDesc, getterDesc))292testMethodHandleDesc(r);293294((MethodHandle)staticSetterDesc.resolveConstantDesc(LOOKUP)).invokeExact(6); assertEquals(TestHelpers.TestClass.sf, 6);295assertEquals(6, (int) ((MethodHandle)staticGetterDesc.resolveConstantDesc(LOOKUP)).invokeExact());296assertEquals(6, (int) ((MethodHandle)staticGetterDesc.resolveConstantDesc(TestHelpers.TestClass.LOOKUP)).invokeExact());297((MethodHandle)staticSetterDesc.resolveConstantDesc(TestHelpers.TestClass.LOOKUP)).invokeExact(7); assertEquals(TestHelpers.TestClass.sf, 7);298assertEquals(7, (int) ((MethodHandle)staticGetterDesc.resolveConstantDesc(LOOKUP)).invokeExact());299assertEquals(7, (int) ((MethodHandle)staticGetterDesc.resolveConstantDesc(TestHelpers.TestClass.LOOKUP)).invokeExact());300301assertEquals(3, (int) ((MethodHandle)staticGetterIDesc.resolveConstantDesc(LOOKUP)).invokeExact());302assertEquals(3, (int) ((MethodHandle)staticGetterIDesc.resolveConstantDesc(TestHelpers.TestClass.LOOKUP)).invokeExact());303304((MethodHandle)setterDesc.resolveConstantDesc(LOOKUP)).invokeExact(instance, 6); assertEquals(instance.f, 6);305assertEquals(6, (int) ((MethodHandle)getterDesc.resolveConstantDesc(LOOKUP)).invokeExact(instance));306assertEquals(6, (int) ((MethodHandle)getterDesc.resolveConstantDesc(TestHelpers.TestClass.LOOKUP)).invokeExact(instance));307((MethodHandle)setterDesc.resolveConstantDesc(TestHelpers.TestClass.LOOKUP)).invokeExact(instance, 7); assertEquals(instance.f, 7);308assertEquals(7, (int) ((MethodHandle)getterDesc.resolveConstantDesc(LOOKUP)).invokeExact(instance));309assertEquals(7, (int) ((MethodHandle)getterDesc.resolveConstantDesc(TestHelpers.TestClass.LOOKUP)).invokeExact(instance));310}311312private void assertBadArgs(Supplier<MethodHandleDesc> supplier, String s) {313try {314MethodHandleDesc r = supplier.get();315fail("Expected failure for " + s);316}317catch (IllegalArgumentException e) {318// succeed319}320}321322public void testBadFieldMHs() {323List<String> badGetterDescs = List.of("()V", "(Ljava/lang/Object;)V", "(I)I", "(Ljava/lang/Object;I)I");324List<String> badStaticGetterDescs = List.of("()V", "(Ljava/lang/Object;)I", "(I)I", "(Ljava/lang/Object;I)I");325List<String> badSetterDescs = List.of("()V", "(I)V", "(Ljava/lang/Object;)V", "(Ljava/lang/Object;I)I", "(Ljava/lang/Object;II)V");326List<String> badStaticSetterDescs = List.of("()V", "(II)V", "()I");327328badGetterDescs.forEach(s -> assertBadArgs(() -> MethodHandleDesc.of(GETTER, helperHolderClass, "x", s), s));329badSetterDescs.forEach(s -> assertBadArgs(() -> MethodHandleDesc.of(SETTER, helperHolderClass, "x", s), s));330badStaticGetterDescs.forEach(s -> assertBadArgs(() -> MethodHandleDesc.of(STATIC_GETTER, helperHolderClass, "x", s), s));331badStaticSetterDescs.forEach(s -> assertBadArgs(() -> MethodHandleDesc.of(STATIC_SETTER, helperHolderClass, "x", s), s));332}333334@Test(expectedExceptions = IllegalArgumentException.class)335public void testBadOwners() {336MethodHandleDesc.ofMethod(VIRTUAL, ClassDesc.ofDescriptor("I"), "x", MethodTypeDesc.ofDescriptor("()I"));337}338339public void testSymbolicDescsConstants() throws ReflectiveOperationException {340int tested = 0;341Field[] fields = ConstantDescs.class.getDeclaredFields();342for (Field f : fields) {343try {344if (f.getType().equals(DirectMethodHandleDesc.class)345&& ((f.getModifiers() & Modifier.STATIC) != 0)346&& ((f.getModifiers() & Modifier.PUBLIC) != 0)) {347MethodHandleDesc r = (MethodHandleDesc) f.get(null);348MethodHandle m = (MethodHandle)r.resolveConstantDesc(MethodHandles.lookup());349testMethodHandleDesc(r, m);350++tested;351}352}353catch (Throwable e) {354fail("Error testing field " + f.getName(), e);355}356}357358assertTrue(tested > 0);359}360361public void testKind() {362for (Kind k : Kind.values()) {363assertEquals(Kind.valueOf(k.refKind), Kind.valueOf(k.refKind, k.refKind == MethodHandleInfo.REF_invokeInterface));364assertEquals(Kind.valueOf(k.refKind, k.isInterface), k);365}366// let's now verify those cases for which the value of the isInterface parameter is ignored367int[] isInterfaceIgnored = new int[] {368REF_getField,369REF_getStatic,370REF_putField,371REF_putStatic,372REF_newInvokeSpecial,373REF_invokeInterface374};375for (int refKind : isInterfaceIgnored) {376assertEquals(Kind.valueOf(refKind, false), Kind.valueOf(refKind, true));377}378379// some explicit tests for REF_invokeStatic and REF_invokeSpecial380assertNotEquals(Kind.valueOf(REF_invokeStatic, false), Kind.valueOf(REF_invokeStatic, true));381assertNotEquals(Kind.valueOf(REF_invokeSpecial, false), Kind.valueOf(REF_invokeSpecial, true));382assertEquals(Kind.valueOf(REF_invokeStatic, false), Kind.STATIC);383assertEquals(Kind.valueOf(REF_invokeStatic, true), Kind.INTERFACE_STATIC);384assertEquals(Kind.valueOf(REF_invokeSpecial, false), Kind.SPECIAL);385assertEquals(Kind.valueOf(REF_invokeSpecial, true), Kind.INTERFACE_SPECIAL);386}387}388389390