1 /*
   2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import java.lang.invoke.CallSite;
  25 import java.lang.invoke.ConstantCallSite;
  26 import java.lang.invoke.MethodHandle;
  27 import java.lang.invoke.MethodHandles;
  28 import java.lang.invoke.MethodType;
  29 import java.lang.constant.ClassDesc;
  30 import java.lang.constant.DirectMethodHandleDesc;
  31 import java.lang.constant.DynamicCallSiteDesc;
  32 import java.lang.constant.MethodHandleDesc;
  33 import java.lang.constant.MethodTypeDesc;
  34 
  35 import org.testng.annotations.Test;
  36 
  37 import static java.lang.constant.ConstantDescs.*;
  38 import static org.testng.Assert.assertEquals;
  39 import static org.testng.Assert.assertNotEquals;
  40 
  41 /**
  42  * @test
  43  * @compile IndyDescTest.java
  44  * @run testng IndyDescTest
  45  * @summary unit tests for java.lang.constant.IndyDescTest
  46  */
  47 @Test
  48 public class IndyDescTest {
  49     public static CallSite bootstrap(MethodHandles.Lookup lookup, String name, MethodType type,
  50                                      Object... args) {
  51         if (args.length == 0)
  52             return new ConstantCallSite(MethodHandles.constant(String.class, "Foo"));
  53         else
  54             return new ConstantCallSite(MethodHandles.constant(String.class, (String) args[0]));
  55     }
  56 
  57     public void testIndyDesc() throws Throwable {
  58         ClassDesc c = ClassDesc.of("IndyDescTest");
  59         MethodTypeDesc mt = MethodTypeDesc.of(CD_CallSite, CD_MethodHandles_Lookup, CD_String, CD_MethodType, CD_Object.arrayType());
  60         DirectMethodHandleDesc mh = MethodHandleDesc.ofMethod(DirectMethodHandleDesc.Kind.STATIC, c, "bootstrap", mt);
  61         DynamicCallSiteDesc csd = DynamicCallSiteDesc.of(mh, "wooga", MethodTypeDesc.of(CD_String));
  62         CallSite cs = csd.resolveCallSiteDesc(MethodHandles.lookup());
  63         MethodHandle target = cs.getTarget();
  64         assertEquals("Foo", target.invoke());
  65         assertEquals("wooga", csd.invocationName());
  66 
  67         mh = ofCallsiteBootstrap(c, "bootstrap", CD_CallSite, CD_Object.arrayType());
  68         csd = DynamicCallSiteDesc.of(mh, "wooga", MethodTypeDesc.of(CD_String));
  69         cs = csd.resolveCallSiteDesc(MethodHandles.lookup());
  70         target = cs.getTarget();
  71         assertEquals("Foo", target.invoke());
  72         assertEquals("wooga", csd.invocationName());
  73 
  74         DynamicCallSiteDesc csd2 = DynamicCallSiteDesc.of(mh, "foo", MethodTypeDesc.of(CD_String), "Bar");
  75         CallSite cs2 = csd2.resolveCallSiteDesc(MethodHandles.lookup());
  76         MethodHandle target2 = cs2.getTarget();
  77         assertEquals("Bar", target2.invoke());
  78         assertEquals("foo", csd2.invocationName());
  79 
  80         DynamicCallSiteDesc csd3 = DynamicCallSiteDesc.of(mh, MethodTypeDesc.of(CD_String));
  81         CallSite cs3 = csd.resolveCallSiteDesc(MethodHandles.lookup());
  82         MethodHandle target3 = cs3.getTarget();
  83         assertEquals("Foo", target3.invoke());
  84         assertEquals("_", csd3.invocationName());
  85 
  86         DynamicCallSiteDesc csd4 = DynamicCallSiteDesc.of(mh, "foo", MethodTypeDesc.of(CD_String)).withArgs("Bar");
  87         CallSite cs4 = csd4.resolveCallSiteDesc(MethodHandles.lookup());
  88         MethodHandle target4 = cs4.getTarget();
  89         assertEquals("Bar", target4.invoke());
  90 
  91         DynamicCallSiteDesc csd5 = DynamicCallSiteDesc.of(mh, MethodTypeDesc.of(CD_String, CD_String))
  92                 .withNameAndType("foo", MethodTypeDesc.of(CD_String)).withArgs("Bar");
  93         CallSite cs5 = csd5.resolveCallSiteDesc(MethodHandles.lookup());
  94         MethodHandle target5 = cs5.getTarget();
  95         assertEquals("Bar", target5.invoke());
  96         assertEquals("foo", csd5.invocationName());
  97     }
  98 
  99     public void testEqualsHashToString() throws Throwable {
 100         ClassDesc c = ClassDesc.of("IndyDescTest");
 101         MethodTypeDesc mt = MethodTypeDesc.of(CD_CallSite, CD_MethodHandles_Lookup, CD_String, CD_MethodType, CD_Object.arrayType());
 102         DirectMethodHandleDesc mh = MethodHandleDesc.ofMethod(DirectMethodHandleDesc.Kind.STATIC, c, "bootstrap", mt);
 103 
 104         DynamicCallSiteDesc csd1 = DynamicCallSiteDesc.of(mh, "wooga", MethodTypeDesc.of(CD_String));
 105         DynamicCallSiteDesc csd2 = DynamicCallSiteDesc.of(mh, "wooga", MethodTypeDesc.of(CD_String));
 106         DynamicCallSiteDesc csd3 = DynamicCallSiteDesc.of(mh, "foo", MethodTypeDesc.of(CD_String));
 107         assertEquals(csd1, csd2);
 108         assertEquals(csd1.hashCode(), csd2.hashCode());
 109         assertNotEquals(csd1, csd3);
 110         assertNotEquals(csd1.hashCode(), csd3.hashCode());
 111 
 112         assertEquals(csd1.toString(), "DynamicCallSiteDesc[IndyDescTest::bootstrap(wooga/):()String]");
 113     }
 114 
 115     @Test(expectedExceptions = IllegalArgumentException.class)
 116     public void testEmptyInvocationName() throws Throwable {
 117         ClassDesc c = ClassDesc.of("IndyDescTest");
 118         MethodTypeDesc mt = MethodTypeDesc.of(CD_CallSite, CD_MethodHandles_Lookup, CD_String, CD_MethodType, CD_Object.arrayType());
 119         DirectMethodHandleDesc mh = MethodHandleDesc.ofMethod(DirectMethodHandleDesc.Kind.STATIC, c, "bootstrap", mt);
 120         DynamicCallSiteDesc csd1 = DynamicCallSiteDesc.of(mh, "", MethodTypeDesc.of(CD_String));
 121     }
 122 }