Path: blob/master/test/jdk/java/lang/constant/IndyDescTest.java
41149 views
/*1* Copyright (c) 2018, 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.CallSite;24import java.lang.invoke.ConstantCallSite;25import java.lang.invoke.MethodHandle;26import java.lang.invoke.MethodHandles;27import java.lang.invoke.MethodType;28import java.lang.constant.ClassDesc;29import java.lang.constant.DirectMethodHandleDesc;30import java.lang.constant.DynamicCallSiteDesc;31import java.lang.constant.MethodHandleDesc;32import java.lang.constant.MethodTypeDesc;3334import org.testng.annotations.Test;3536import static java.lang.constant.ConstantDescs.*;37import static org.testng.Assert.assertEquals;38import static org.testng.Assert.assertNotEquals;3940/**41* @test42* @compile IndyDescTest.java43* @run testng IndyDescTest44* @summary unit tests for java.lang.constant.IndyDescTest45*/46@Test47public class IndyDescTest {48public static CallSite bootstrap(MethodHandles.Lookup lookup, String name, MethodType type,49Object... args) {50if (args.length == 0)51return new ConstantCallSite(MethodHandles.constant(String.class, "Foo"));52else53return new ConstantCallSite(MethodHandles.constant(String.class, (String) args[0]));54}5556public void testIndyDesc() throws Throwable {57ClassDesc c = ClassDesc.of("IndyDescTest");58MethodTypeDesc mt = MethodTypeDesc.of(CD_CallSite, CD_MethodHandles_Lookup, CD_String, CD_MethodType, CD_Object.arrayType());59DirectMethodHandleDesc mh = MethodHandleDesc.ofMethod(DirectMethodHandleDesc.Kind.STATIC, c, "bootstrap", mt);60DynamicCallSiteDesc csd = DynamicCallSiteDesc.of(mh, "wooga", MethodTypeDesc.of(CD_String));61CallSite cs = csd.resolveCallSiteDesc(MethodHandles.lookup());62MethodHandle target = cs.getTarget();63assertEquals("Foo", target.invoke());64assertEquals("wooga", csd.invocationName());6566mh = ofCallsiteBootstrap(c, "bootstrap", CD_CallSite, CD_Object.arrayType());67csd = DynamicCallSiteDesc.of(mh, "wooga", MethodTypeDesc.of(CD_String));68cs = csd.resolveCallSiteDesc(MethodHandles.lookup());69target = cs.getTarget();70assertEquals("Foo", target.invoke());71assertEquals("wooga", csd.invocationName());7273DynamicCallSiteDesc csd2 = DynamicCallSiteDesc.of(mh, "foo", MethodTypeDesc.of(CD_String), "Bar");74CallSite cs2 = csd2.resolveCallSiteDesc(MethodHandles.lookup());75MethodHandle target2 = cs2.getTarget();76assertEquals("Bar", target2.invoke());77assertEquals("foo", csd2.invocationName());7879DynamicCallSiteDesc csd3 = DynamicCallSiteDesc.of(mh, MethodTypeDesc.of(CD_String));80CallSite cs3 = csd.resolveCallSiteDesc(MethodHandles.lookup());81MethodHandle target3 = cs3.getTarget();82assertEquals("Foo", target3.invoke());83assertEquals("_", csd3.invocationName());8485DynamicCallSiteDesc csd4 = DynamicCallSiteDesc.of(mh, "foo", MethodTypeDesc.of(CD_String)).withArgs("Bar");86CallSite cs4 = csd4.resolveCallSiteDesc(MethodHandles.lookup());87MethodHandle target4 = cs4.getTarget();88assertEquals("Bar", target4.invoke());8990DynamicCallSiteDesc csd5 = DynamicCallSiteDesc.of(mh, MethodTypeDesc.of(CD_String, CD_String))91.withNameAndType("foo", MethodTypeDesc.of(CD_String)).withArgs("Bar");92CallSite cs5 = csd5.resolveCallSiteDesc(MethodHandles.lookup());93MethodHandle target5 = cs5.getTarget();94assertEquals("Bar", target5.invoke());95assertEquals("foo", csd5.invocationName());96}9798public void testEqualsHashToString() throws Throwable {99ClassDesc c = ClassDesc.of("IndyDescTest");100MethodTypeDesc mt = MethodTypeDesc.of(CD_CallSite, CD_MethodHandles_Lookup, CD_String, CD_MethodType, CD_Object.arrayType());101DirectMethodHandleDesc mh = MethodHandleDesc.ofMethod(DirectMethodHandleDesc.Kind.STATIC, c, "bootstrap", mt);102103DynamicCallSiteDesc csd1 = DynamicCallSiteDesc.of(mh, "wooga", MethodTypeDesc.of(CD_String));104DynamicCallSiteDesc csd2 = DynamicCallSiteDesc.of(mh, "wooga", MethodTypeDesc.of(CD_String));105DynamicCallSiteDesc csd3 = DynamicCallSiteDesc.of(mh, "foo", MethodTypeDesc.of(CD_String));106assertEquals(csd1, csd2);107assertEquals(csd1.hashCode(), csd2.hashCode());108assertNotEquals(csd1, csd3);109assertNotEquals(csd1.hashCode(), csd3.hashCode());110111assertEquals(csd1.toString(), "DynamicCallSiteDesc[IndyDescTest::bootstrap(wooga/):()String]");112}113114@Test(expectedExceptions = IllegalArgumentException.class)115public void testEmptyInvocationName() throws Throwable {116ClassDesc c = ClassDesc.of("IndyDescTest");117MethodTypeDesc mt = MethodTypeDesc.of(CD_CallSite, CD_MethodHandles_Lookup, CD_String, CD_MethodType, CD_Object.arrayType());118DirectMethodHandleDesc mh = MethodHandleDesc.ofMethod(DirectMethodHandleDesc.Kind.STATIC, c, "bootstrap", mt);119DynamicCallSiteDesc csd1 = DynamicCallSiteDesc.of(mh, "", MethodTypeDesc.of(CD_String));120}121}122123124