Path: blob/master/test/jdk/java/lang/constant/MethodHandleDescTest.java
41149 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 org.testng.Assert.assertEquals;56import static org.testng.Assert.assertNotSame;57import static org.testng.Assert.assertSame;58import static org.testng.Assert.assertTrue;59import static org.testng.Assert.fail;6061/**62* @test63* @compile MethodHandleDescTest.java64* @run testng MethodHandleDescTest65* @summary unit tests for java.lang.constant.MethodHandleDesc66*/67@Test68public class MethodHandleDescTest extends SymbolicDescTest {69private static ClassDesc helperHolderClass = ClassDesc.of("TestHelpers");70private static ClassDesc testClass = helperHolderClass.nested("TestClass");71private static ClassDesc testInterface = helperHolderClass.nested("TestInterface");72private static ClassDesc testSuperclass = helperHolderClass.nested("TestSuperclass");737475private static void assertMHEquals(MethodHandle a, MethodHandle b) {76MethodHandleInfo ia = LOOKUP.revealDirect(a);77MethodHandleInfo ib = LOOKUP.revealDirect(b);78assertEquals(ia.getDeclaringClass(), ib.getDeclaringClass());79assertEquals(ia.getName(), ib.getName());80assertEquals(ia.getMethodType(), ib.getMethodType());81assertEquals(ia.getReferenceKind(), ib.getReferenceKind());82}8384private void testMethodHandleDesc(MethodHandleDesc r) throws ReflectiveOperationException {85if (r instanceof DirectMethodHandleDesc) {86testSymbolicDesc(r);8788DirectMethodHandleDesc rr = (DirectMethodHandleDesc) r;89assertEquals(r, MethodHandleDesc.of(rr.kind(), rr.owner(), rr.methodName(), rr.lookupDescriptor()));90assertEquals(r.invocationType().resolveConstantDesc(LOOKUP), ((MethodHandle) r.resolveConstantDesc(LOOKUP)).type());91}92else {93testSymbolicDescForwardOnly(r);94}95}9697private String lookupDescriptor(DirectMethodHandleDesc rr) {98switch (rr.kind()) {99case VIRTUAL:100case SPECIAL:101case INTERFACE_VIRTUAL:102case INTERFACE_SPECIAL:103return rr.invocationType().dropParameterTypes(0, 1).descriptorString();104case CONSTRUCTOR:105return rr.invocationType().changeReturnType(CD_void).descriptorString();106default:107return rr.invocationType().descriptorString();108}109}110111private void testMethodHandleDesc(MethodHandleDesc r, MethodHandle mh) throws ReflectiveOperationException {112testMethodHandleDesc(r);113114assertMHEquals(((MethodHandle) r.resolveConstantDesc(LOOKUP)), mh);115assertEquals(mh.describeConstable().orElseThrow(), r);116117// compare extractable properties: refKind, owner, name, type118MethodHandleInfo mhi = LOOKUP.revealDirect(mh);119DirectMethodHandleDesc rr = (DirectMethodHandleDesc) r;120assertEquals(mhi.getDeclaringClass().descriptorString(), rr.owner().descriptorString());121assertEquals(mhi.getName(), rr.methodName());122assertEquals(mhi.getReferenceKind(), rr.kind().refKind);123MethodType type = mhi.getMethodType();124assertEquals(type.toMethodDescriptorString(), lookupDescriptor(rr));125}126127public void testSimpleMHs() throws ReflectiveOperationException {128MethodHandle MH_String_isEmpty = LOOKUP.findVirtual(String.class, "isEmpty", MethodType.fromMethodDescriptorString("()Z", null));129testMethodHandleDesc(MethodHandleDesc.of(Kind.VIRTUAL, CD_String, "isEmpty", "()Z"), MH_String_isEmpty);130testMethodHandleDesc(MethodHandleDesc.ofMethod(Kind.VIRTUAL, CD_String, "isEmpty", MethodTypeDesc.of(CD_boolean)), MH_String_isEmpty);131132MethodHandle MH_List_isEmpty = LOOKUP.findVirtual(List.class, "isEmpty", MethodType.fromMethodDescriptorString("()Z", null));133testMethodHandleDesc(MethodHandleDesc.of(Kind.INTERFACE_VIRTUAL, CD_List, "isEmpty", "()Z"), MH_List_isEmpty);134testMethodHandleDesc(MethodHandleDesc.ofMethod(Kind.INTERFACE_VIRTUAL, CD_List, "isEmpty", MethodTypeDesc.of(CD_boolean)), MH_List_isEmpty);135136MethodHandle MH_String_format = LOOKUP.findStatic(String.class, "format", MethodType.methodType(String.class, String.class, Object[].class));137testMethodHandleDesc(MethodHandleDesc.of(Kind.STATIC, CD_String, "format", MethodType.methodType(String.class, String.class, Object[].class).descriptorString()),138MH_String_format);139testMethodHandleDesc(MethodHandleDesc.ofMethod(Kind.STATIC, CD_String, "format", MethodTypeDesc.of(CD_String, CD_String, CD_Object.arrayType())),140MH_String_format);141142MethodHandle MH_ArrayList_new = LOOKUP.findConstructor(ArrayList.class, MethodType.methodType(void.class));143testMethodHandleDesc(MethodHandleDesc.ofMethod(Kind.CONSTRUCTOR, ClassDesc.of("java.util.ArrayList"), "<init>", MethodTypeDesc.of(CD_void)),144MH_ArrayList_new);145testMethodHandleDesc(MethodHandleDesc.ofConstructor(ClassDesc.of("java.util.ArrayList")), MH_ArrayList_new);146147// bad constructor non void return type148try {149MethodHandleDesc.of(Kind.CONSTRUCTOR, ClassDesc.of("java.util.ArrayList"), "<init>", "()I");150fail("should have failed: non void return type for constructor");151} catch (IllegalArgumentException ex) {152// good153}154155// null list of parameters156try {157MethodHandleDesc.ofConstructor(ClassDesc.of("java.util.ArrayList", null));158fail("should have failed: null list of parameters");159} catch (NullPointerException ex) {160// good161}162163// null elements in list of parameters164try {165ClassDesc[] paramList = new ClassDesc[1];166paramList[0] = null;167MethodHandleDesc.ofConstructor(ClassDesc.of("java.util.ArrayList"), paramList);168fail("should have failed: null content in list of parameters");169} catch (NullPointerException ex) {170// good171}172}173174public void testAsType() throws Throwable {175MethodHandleDesc mhr = MethodHandleDesc.ofMethod(Kind.STATIC, ClassDesc.of("java.lang.Integer"), "valueOf",176MethodTypeDesc.of(CD_Integer, CD_int));177MethodHandleDesc takesInteger = mhr.asType(MethodTypeDesc.of(CD_Integer, CD_Integer));178testMethodHandleDesc(takesInteger);179MethodHandle mh1 = (MethodHandle) takesInteger.resolveConstantDesc(LOOKUP);180assertEquals((Integer) 3, (Integer) mh1.invokeExact((Integer) 3));181assertEquals(takesInteger.toString(), "MethodHandleDesc[STATIC/Integer::valueOf(int)Integer].asType(Integer)Integer");182183try {184Integer i = (Integer) mh1.invokeExact(3);185fail("Expected WMTE");186}187catch (WrongMethodTypeException ignored) { }188189MethodHandleDesc takesInt = takesInteger.asType(MethodTypeDesc.of(CD_Integer, CD_int));190testMethodHandleDesc(takesInt);191MethodHandle mh2 = (MethodHandle) takesInt.resolveConstantDesc(LOOKUP);192assertEquals((Integer) 3, (Integer) mh2.invokeExact(3));193194try {195Integer i = (Integer) mh2.invokeExact((Integer) 3);196fail("Expected WMTE");197}198catch (WrongMethodTypeException ignored) { }199200// Short circuit optimization201MethodHandleDesc same = mhr.asType(mhr.invocationType());202assertSame(mhr, same);203204try {205mhr.asType(null);206fail("Expected NPE");207} catch (NullPointerException ex) {208// good209}210211// @@@ Test varargs adaptation212// @@@ Test bad adaptations and assert runtime error on resolution213// @@@ Test intrinsification of adapted MH214}215216public void testMethodHandleDesc() throws Throwable {217MethodHandleDesc ctorDesc = MethodHandleDesc.of(Kind.CONSTRUCTOR, testClass, "<ignored!>", "()V");218MethodHandleDesc staticMethodDesc = MethodHandleDesc.of(Kind.STATIC, testClass, "sm", "(I)I");219MethodHandleDesc staticIMethodDesc = MethodHandleDesc.of(Kind.INTERFACE_STATIC, testInterface, "sm", "(I)I");220MethodHandleDesc instanceMethodDesc = MethodHandleDesc.of(Kind.VIRTUAL, testClass, "m", "(I)I");221MethodHandleDesc instanceIMethodDesc = MethodHandleDesc.of(Kind.INTERFACE_VIRTUAL, testInterface, "m", "(I)I");222MethodHandleDesc superMethodDesc = MethodHandleDesc.of(Kind.SPECIAL, testSuperclass, "m", "(I)I");223MethodHandleDesc superIMethodDesc = MethodHandleDesc.of(Kind.INTERFACE_SPECIAL, testInterface, "m", "(I)I");224MethodHandleDesc privateMethodDesc = MethodHandleDesc.of(Kind.SPECIAL, testClass, "pm", "(I)I");225MethodHandleDesc privateIMethodDesc = MethodHandleDesc.of(Kind.INTERFACE_SPECIAL, testInterface, "pm", "(I)I");226MethodHandleDesc privateStaticMethodDesc = MethodHandleDesc.of(Kind.STATIC, testClass, "psm", "(I)I");227MethodHandleDesc privateStaticIMethodDesc = MethodHandleDesc.of(Kind.INTERFACE_STATIC, testInterface, "psm", "(I)I");228229assertEquals(ctorDesc.invocationType(), MethodTypeDesc.of(testClass));230assertEquals(((DirectMethodHandleDesc) ctorDesc).lookupDescriptor(), "()V");231232assertEquals(staticMethodDesc.invocationType().descriptorString(), "(I)I");233assertEquals(((DirectMethodHandleDesc) staticMethodDesc).lookupDescriptor(), "(I)I");234235assertEquals(instanceMethodDesc.invocationType().descriptorString(), "(" + testClass.descriptorString() + "I)I");236assertEquals(((DirectMethodHandleDesc) instanceMethodDesc).lookupDescriptor(), "(I)I");237238for (MethodHandleDesc r : List.of(ctorDesc, staticMethodDesc, staticIMethodDesc, instanceMethodDesc, instanceIMethodDesc))239testMethodHandleDesc(r);240241TestHelpers.TestClass instance = (TestHelpers.TestClass) ((MethodHandle)ctorDesc.resolveConstantDesc(LOOKUP)).invokeExact();242TestHelpers.TestClass instance2 = (TestHelpers.TestClass) ((MethodHandle)ctorDesc.resolveConstantDesc(TestHelpers.TestClass.LOOKUP)).invokeExact();243TestHelpers.TestInterface instanceI = instance;244245assertNotSame(instance, instance2);246247assertEquals(5, (int) ((MethodHandle)staticMethodDesc.resolveConstantDesc(LOOKUP)).invokeExact(5));248assertEquals(5, (int) ((MethodHandle)staticMethodDesc.resolveConstantDesc(TestHelpers.TestClass.LOOKUP)).invokeExact(5));249assertEquals(0, (int) ((MethodHandle)staticIMethodDesc.resolveConstantDesc(LOOKUP)).invokeExact(5));250assertEquals(0, (int) ((MethodHandle)staticIMethodDesc.resolveConstantDesc(TestHelpers.TestClass.LOOKUP)).invokeExact(5));251252assertEquals(5, (int) ((MethodHandle)instanceMethodDesc.resolveConstantDesc(LOOKUP)).invokeExact(instance, 5));253assertEquals(5, (int) ((MethodHandle)instanceMethodDesc.resolveConstantDesc(TestHelpers.TestClass.LOOKUP)).invokeExact(instance, 5));254assertEquals(5, (int) ((MethodHandle)instanceIMethodDesc.resolveConstantDesc(LOOKUP)).invokeExact(instanceI, 5));255assertEquals(5, (int) ((MethodHandle)instanceIMethodDesc.resolveConstantDesc(TestHelpers.TestClass.LOOKUP)).invokeExact(instanceI, 5));256257try { superMethodDesc.resolveConstantDesc(LOOKUP); fail(); }258catch (IllegalAccessException e) { /* expected */ }259assertEquals(-1, (int) ((MethodHandle)superMethodDesc.resolveConstantDesc(TestHelpers.TestClass.LOOKUP)).invokeExact(instance, 5));260261try { superIMethodDesc.resolveConstantDesc(LOOKUP); fail(); }262catch (IllegalAccessException e) { /* expected */ }263assertEquals(0, (int) ((MethodHandle)superIMethodDesc.resolveConstantDesc(TestHelpers.TestClass.LOOKUP)).invokeExact(instance, 5));264265try { privateMethodDesc.resolveConstantDesc(LOOKUP); fail(); }266catch (IllegalAccessException e) { /* expected */ }267assertEquals(5, (int) ((MethodHandle)privateMethodDesc.resolveConstantDesc(TestHelpers.TestClass.LOOKUP)).invokeExact(instance, 5));268269try { privateIMethodDesc.resolveConstantDesc(LOOKUP); fail(); }270catch (IllegalAccessException e) { /* expected */ }271assertEquals(0, (int) ((MethodHandle)privateIMethodDesc.resolveConstantDesc(TestHelpers.TestInterface.LOOKUP)).invokeExact(instanceI, 5));272assertEquals(0, (int) ((MethodHandle)privateIMethodDesc.resolveConstantDesc(TestHelpers.TestClass.LOOKUP)).invoke(instanceI, 5));273274try { privateStaticMethodDesc.resolveConstantDesc(LOOKUP); fail(); }275catch (IllegalAccessException e) { /* expected */ }276assertEquals(5, (int) ((MethodHandle)privateStaticMethodDesc.resolveConstantDesc(TestHelpers.TestClass.LOOKUP)).invokeExact(5));277278try { privateStaticIMethodDesc.resolveConstantDesc(LOOKUP); fail(); }279catch (IllegalAccessException e) { /* expected */ }280assertEquals(0, (int) ((MethodHandle)privateStaticIMethodDesc.resolveConstantDesc(TestHelpers.TestInterface.LOOKUP)).invokeExact(5));281assertEquals(0, (int) ((MethodHandle)privateStaticIMethodDesc.resolveConstantDesc(TestHelpers.TestClass.LOOKUP)).invokeExact(5));282283MethodHandleDesc staticSetterDesc = MethodHandleDesc.ofField(STATIC_SETTER, testClass, "sf", CD_int);284MethodHandleDesc staticGetterDesc = MethodHandleDesc.ofField(STATIC_GETTER, testClass, "sf", CD_int);285MethodHandleDesc staticGetterIDesc = MethodHandleDesc.ofField(STATIC_GETTER, testInterface, "sf", CD_int);286MethodHandleDesc setterDesc = MethodHandleDesc.ofField(SETTER, testClass, "f", CD_int);287MethodHandleDesc getterDesc = MethodHandleDesc.ofField(GETTER, testClass, "f", CD_int);288289for (MethodHandleDesc r : List.of(staticSetterDesc, staticGetterDesc, staticGetterIDesc, setterDesc, getterDesc))290testMethodHandleDesc(r);291292((MethodHandle)staticSetterDesc.resolveConstantDesc(LOOKUP)).invokeExact(6); assertEquals(TestHelpers.TestClass.sf, 6);293assertEquals(6, (int) ((MethodHandle)staticGetterDesc.resolveConstantDesc(LOOKUP)).invokeExact());294assertEquals(6, (int) ((MethodHandle)staticGetterDesc.resolveConstantDesc(TestHelpers.TestClass.LOOKUP)).invokeExact());295((MethodHandle)staticSetterDesc.resolveConstantDesc(TestHelpers.TestClass.LOOKUP)).invokeExact(7); assertEquals(TestHelpers.TestClass.sf, 7);296assertEquals(7, (int) ((MethodHandle)staticGetterDesc.resolveConstantDesc(LOOKUP)).invokeExact());297assertEquals(7, (int) ((MethodHandle)staticGetterDesc.resolveConstantDesc(TestHelpers.TestClass.LOOKUP)).invokeExact());298299assertEquals(3, (int) ((MethodHandle)staticGetterIDesc.resolveConstantDesc(LOOKUP)).invokeExact());300assertEquals(3, (int) ((MethodHandle)staticGetterIDesc.resolveConstantDesc(TestHelpers.TestClass.LOOKUP)).invokeExact());301302((MethodHandle)setterDesc.resolveConstantDesc(LOOKUP)).invokeExact(instance, 6); assertEquals(instance.f, 6);303assertEquals(6, (int) ((MethodHandle)getterDesc.resolveConstantDesc(LOOKUP)).invokeExact(instance));304assertEquals(6, (int) ((MethodHandle)getterDesc.resolveConstantDesc(TestHelpers.TestClass.LOOKUP)).invokeExact(instance));305((MethodHandle)setterDesc.resolveConstantDesc(TestHelpers.TestClass.LOOKUP)).invokeExact(instance, 7); assertEquals(instance.f, 7);306assertEquals(7, (int) ((MethodHandle)getterDesc.resolveConstantDesc(LOOKUP)).invokeExact(instance));307assertEquals(7, (int) ((MethodHandle)getterDesc.resolveConstantDesc(TestHelpers.TestClass.LOOKUP)).invokeExact(instance));308}309310private void assertBadArgs(Supplier<MethodHandleDesc> supplier, String s) {311try {312MethodHandleDesc r = supplier.get();313fail("Expected failure for " + s);314}315catch (IllegalArgumentException e) {316// succeed317}318}319320public void testBadFieldMHs() {321List<String> badGetterDescs = List.of("()V", "(Ljava/lang/Object;)V", "(I)I", "(Ljava/lang/Object;I)I");322List<String> badStaticGetterDescs = List.of("()V", "(Ljava/lang/Object;)I", "(I)I", "(Ljava/lang/Object;I)I");323List<String> badSetterDescs = List.of("()V", "(I)V", "(Ljava/lang/Object;)V", "(Ljava/lang/Object;I)I", "(Ljava/lang/Object;II)V");324List<String> badStaticSetterDescs = List.of("()V", "(II)V", "()I");325326badGetterDescs.forEach(s -> assertBadArgs(() -> MethodHandleDesc.of(GETTER, helperHolderClass, "x", s), s));327badSetterDescs.forEach(s -> assertBadArgs(() -> MethodHandleDesc.of(SETTER, helperHolderClass, "x", s), s));328badStaticGetterDescs.forEach(s -> assertBadArgs(() -> MethodHandleDesc.of(STATIC_GETTER, helperHolderClass, "x", s), s));329badStaticSetterDescs.forEach(s -> assertBadArgs(() -> MethodHandleDesc.of(STATIC_SETTER, helperHolderClass, "x", s), s));330}331332@Test(expectedExceptions = IllegalArgumentException.class)333public void testBadOwners() {334MethodHandleDesc.ofMethod(VIRTUAL, ClassDesc.ofDescriptor("I"), "x", MethodTypeDesc.ofDescriptor("()I"));335}336337public void testSymbolicDescsConstants() throws ReflectiveOperationException {338int tested = 0;339Field[] fields = ConstantDescs.class.getDeclaredFields();340for (Field f : fields) {341try {342if (f.getType().equals(DirectMethodHandleDesc.class)343&& ((f.getModifiers() & Modifier.STATIC) != 0)344&& ((f.getModifiers() & Modifier.PUBLIC) != 0)) {345MethodHandleDesc r = (MethodHandleDesc) f.get(null);346MethodHandle m = (MethodHandle)r.resolveConstantDesc(MethodHandles.lookup());347testMethodHandleDesc(r, m);348++tested;349}350}351catch (Throwable e) {352fail("Error testing field " + f.getName(), e);353}354}355356assertTrue(tested > 0);357}358359public void testKind() {360for (Kind k : Kind.values()) {361assertEquals(Kind.valueOf(k.refKind, k.isInterface), k);362}363}364}365366367