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         DynamicCallSiteDesc csd2 = DynamicCallSiteDesc.of(mh, "foo", MethodTypeDesc.of(CD_String), "Bar");
  68         CallSite cs2 = csd2.resolveCallSiteDesc(MethodHandles.lookup());
  69         MethodHandle target2 = cs2.getTarget();
  70         assertEquals("Bar", target2.invoke());
  71         assertEquals("foo", csd2.invocationName());
  72 
  73         DynamicCallSiteDesc csd3 = DynamicCallSiteDesc.of(mh, MethodTypeDesc.of(CD_String));
  74         CallSite cs3 = csd.resolveCallSiteDesc(MethodHandles.lookup());
  75         MethodHandle target3 = cs3.getTarget();
  76         assertEquals("Foo", target3.invoke());
  77         assertEquals("_", csd3.invocationName());
  78 
  79         DynamicCallSiteDesc csd4 = DynamicCallSiteDesc.of(mh, "foo", MethodTypeDesc.of(CD_String)).withArgs("Bar");
  80         CallSite cs4 = csd4.resolveCallSiteDesc(MethodHandles.lookup());
  81         MethodHandle target4 = cs4.getTarget();
  82         assertEquals("Bar", target4.invoke());
  83 
  84         DynamicCallSiteDesc csd5 = DynamicCallSiteDesc.of(mh, MethodTypeDesc.of(CD_String, CD_String))
  85                 .withNameAndType("foo", MethodTypeDesc.of(CD_String)).withArgs("Bar");
  86         CallSite cs5 = csd5.resolveCallSiteDesc(MethodHandles.lookup());
  87         MethodHandle target5 = cs5.getTarget();
  88         assertEquals("Bar", target5.invoke());
  89         assertEquals("foo", csd5.invocationName());
  90     }
  91 
  92     public void testEqualsHashToString() throws Throwable {
  93         ClassDesc c = ClassDesc.of("IndyDescTest");
  94         MethodTypeDesc mt = MethodTypeDesc.of(CD_CallSite, CD_MethodHandles_Lookup, CD_String, CD_MethodType, CD_Object.arrayType());
  95         DirectMethodHandleDesc mh = MethodHandleDesc.ofMethod(DirectMethodHandleDesc.Kind.STATIC, c, "bootstrap", mt);
  96 
  97         DynamicCallSiteDesc csd1 = DynamicCallSiteDesc.of(mh, "wooga", MethodTypeDesc.of(CD_String));
  98         DynamicCallSiteDesc csd2 = DynamicCallSiteDesc.of(mh, "wooga", MethodTypeDesc.of(CD_String));
  99         DynamicCallSiteDesc csd3 = DynamicCallSiteDesc.of(mh, "foo", MethodTypeDesc.of(CD_String));
 100         assertEquals(csd1, csd2);
 101         assertEquals(csd1.hashCode(), csd2.hashCode());
 102         assertNotEquals(csd1, csd3);
 103         assertNotEquals(csd1.hashCode(), csd3.hashCode());
 104 
 105         assertEquals(csd1.toString(), "DynamicCallSiteDesc[IndyDescTest::bootstrap(wooga/):()String]");
 106     }
 107 
 108     @Test(expectedExceptions = IllegalArgumentException.class)
 109     public void testEmptyInvocationName() throws Throwable {
 110         ClassDesc c = ClassDesc.of("IndyDescTest");
 111         MethodTypeDesc mt = MethodTypeDesc.of(CD_CallSite, CD_MethodHandles_Lookup, CD_String, CD_MethodType, CD_Object.arrayType());
 112         DirectMethodHandleDesc mh = MethodHandleDesc.ofMethod(DirectMethodHandleDesc.Kind.STATIC, c, "bootstrap", mt);
 113         DynamicCallSiteDesc csd1 = DynamicCallSiteDesc.of(mh, "", MethodTypeDesc.of(CD_String));
 114     }
 115 }