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.Enum.EnumDesc;
  25 import java.lang.constant.MethodTypeDesc;
  26 import java.lang.invoke.MethodHandle;
  27 import java.lang.invoke.MethodHandles;
  28 import java.lang.invoke.VarHandle;
  29 import java.lang.invoke.VarHandle.VarHandleDesc;
  30 import java.lang.constant.ClassDesc;
  31 import java.lang.constant.ConstantDesc;
  32 import java.lang.constant.ConstantDescs;
  33 import java.lang.constant.DirectMethodHandleDesc;
  34 import java.lang.constant.DynamicConstantDesc;
  35 import java.lang.constant.MethodHandleDesc;
  36 
  37 import org.testng.annotations.Test;
  38 
  39 import static java.lang.constant.ConstantDescs.CD_MethodHandle;
  40 import static java.lang.constant.ConstantDescs.CD_Object;
  41 import static java.lang.constant.ConstantDescs.CD_String;
  42 import static java.lang.constant.ConstantDescs.CD_VarHandle;
  43 import static java.lang.constant.ConstantDescs.CD_int;
  44 import static org.testng.Assert.assertEquals;
  45 import static org.testng.Assert.assertFalse;
  46 import static org.testng.Assert.assertNotEquals;
  47 import static org.testng.Assert.assertNotSame;
  48 import static org.testng.Assert.assertNull;
  49 import static org.testng.Assert.assertSame;
  50 import static org.testng.Assert.assertTrue;
  51 
  52 /**
  53  * @test
  54  * @compile CondyDescTest.java
  55  * @run testng CondyDescTest
  56  * @summary unit tests for java.lang.constant.CondyDescTest
  57  */
  58 @Test
  59 public class CondyDescTest extends SymbolicDescTest {
  60     private final static ConstantDesc[] EMPTY_ARGS = new ConstantDesc[0];
  61     private final static ClassDesc CD_ConstantBootstraps = ClassDesc.of("java.lang.invoke.ConstantBootstraps");
  62 
  63     private static<T> void testDCR(DynamicConstantDesc<T> r, T c) throws ReflectiveOperationException {
  64         assertEquals(r, DynamicConstantDesc.ofNamed(r.bootstrapMethod(), r.constantName(), r.constantType(), r.bootstrapArgs()));
  65         assertEquals(r.resolveConstantDesc(LOOKUP), c);
  66     }
  67 
  68     private void testVarHandleDesc(DynamicConstantDesc<VarHandle> r, VarHandle vh) throws ReflectiveOperationException  {
  69         testSymbolicDesc(r);
  70         assertEquals(vh.describeConstable().orElseThrow(), r);
  71     }
  72 
  73     private static<E extends Enum<E>> void testEnumDesc(EnumDesc<E> r, E e) throws ReflectiveOperationException {
  74         testSymbolicDesc(r);
  75 
  76         assertEquals(r, EnumDesc.of(r.constantType(), r.constantName()));
  77         assertEquals(r.resolveConstantDesc(LOOKUP), e);
  78     }
  79 
  80     public void testNullConstant() throws ReflectiveOperationException {
  81         DynamicConstantDesc<?> r = (DynamicConstantDesc<?>) ConstantDescs.NULL;
  82         assertEquals(r, DynamicConstantDesc.ofNamed(r.bootstrapMethod(), r.constantName(), r.constantType(), r.bootstrapArgs()));
  83         assertNull(r.resolveConstantDesc(LOOKUP));
  84     }
  85 
  86     static String concatBSM(MethodHandles.Lookup lookup, String name, Class<?> type, String a, String b) {
  87         return a + b;
  88     }
  89 
  90     public void testDynamicConstant() throws ReflectiveOperationException {
  91         DirectMethodHandleDesc bsmDesc = ConstantDescs.ofConstantBootstrap(ClassDesc.of("CondyDescTest"), "concatBSM",
  92                                                                             CD_String, CD_String, CD_String);
  93         DynamicConstantDesc<String> r = DynamicConstantDesc.of(bsmDesc, "foo", "bar");
  94         testDCR(r, "foobar");
  95     }
  96 
  97     public void testNested() throws Throwable {
  98         DirectMethodHandleDesc invoker = ConstantDescs.ofConstantBootstrap(CD_ConstantBootstraps, "invoke", CD_Object, CD_MethodHandle, CD_Object.arrayType());
  99         DirectMethodHandleDesc format = MethodHandleDesc.ofMethod(DirectMethodHandleDesc.Kind.STATIC, CD_String, "format",
 100                                                                   MethodTypeDesc.of(CD_String, CD_String, CD_Object.arrayType()));
 101 
 102         String s = (String) ((MethodHandle) invoker.resolveConstantDesc(LOOKUP))
 103                                    .invoke(LOOKUP, "", String.class,
 104                                            format.resolveConstantDesc(LOOKUP), "%s%s", "moo", "cow");
 105         assertEquals(s, "moocow");
 106 
 107         DynamicConstantDesc<String> desc = DynamicConstantDesc.of(invoker, format, "%s%s", "moo", "cow");
 108         testDCR(desc, "moocow");
 109 
 110         DynamicConstantDesc<String> desc2 = DynamicConstantDesc.of(invoker, format, "%s%s", desc, "cow");
 111         testDCR(desc2, "moocowcow");
 112     }
 113 
 114     enum MyEnum { A, B, C }
 115 
 116     public void testEnumDesc() throws ReflectiveOperationException {
 117         ClassDesc enumClass = ClassDesc.of("CondyDescTest").nested("MyEnum");
 118 
 119         testEnumDesc(EnumDesc.of(enumClass, "A"), MyEnum.A);
 120         testEnumDesc(EnumDesc.of(enumClass, "B"), MyEnum.B);
 121         testEnumDesc(EnumDesc.of(enumClass, "C"), MyEnum.C);
 122 
 123         DynamicConstantDesc<MyEnum> denum = DynamicConstantDesc.ofNamed(ConstantDescs.BSM_ENUM_CONSTANT, "A", enumClass, EMPTY_ARGS);
 124         assertEquals(MyEnum.A, denum.resolveConstantDesc(LOOKUP));
 125 
 126         EnumDesc<MyEnum> enumDesc = (EnumDesc<MyEnum>)DynamicConstantDesc.<MyEnum>ofCanonical(ConstantDescs.BSM_ENUM_CONSTANT, "A", enumClass, EMPTY_ARGS);
 127         assertEquals(MyEnum.A, enumDesc.resolveConstantDesc(LOOKUP));
 128     }
 129 
 130     static class MyClass {
 131         static int sf;
 132         int f;
 133     }
 134 
 135     public void testVarHandles() throws ReflectiveOperationException {
 136         ClassDesc testClass = ClassDesc.of("CondyDescTest").nested("MyClass");
 137         MyClass instance = new MyClass();
 138 
 139         // static varHandle
 140         VarHandleDesc vhc = VarHandleDesc.ofStaticField(testClass, "sf", CD_int);
 141         VarHandle varHandle = LOOKUP.findStaticVarHandle(MyClass.class, "sf", int.class);
 142         testVarHandleDesc(vhc, varHandle);
 143 
 144         assertEquals(varHandle.varType(), int.class);
 145         varHandle.set(8);
 146         assertEquals(8, (int) varHandle.get());
 147         assertEquals(MyClass.sf, 8);
 148 
 149         // static varHandle
 150         vhc = VarHandleDesc.ofField(testClass, "f", CD_int);
 151         varHandle = LOOKUP.findVarHandle(MyClass.class, "f", int.class);
 152         testVarHandleDesc(vhc, varHandle);
 153 
 154         assertEquals(varHandle.varType(), int.class);
 155         varHandle.set(instance, 9);
 156         assertEquals(9, (int) varHandle.get(instance));
 157         assertEquals(instance.f, 9);
 158 
 159         vhc = VarHandleDesc.ofArray(CD_int.arrayType());
 160         varHandle = MethodHandles.arrayElementVarHandle(int[].class);
 161         testVarHandleDesc(vhc, varHandle);
 162 
 163         int[] ints = new int[3];
 164         varHandle.set(ints, 0, 1);
 165         varHandle.set(ints, 1, 2);
 166         varHandle.set(ints, 2, 3);
 167 
 168         assertEquals(1, varHandle.get(ints, 0));
 169         assertEquals(2, varHandle.get(ints, 1));
 170         assertEquals(3, varHandle.get(ints, 2));
 171         assertEquals(1, ints[0]);
 172         assertEquals(2, ints[1]);
 173         assertEquals(3, ints[2]);
 174 
 175         // static var handle obtained using the DynamicConstantDesc
 176         DynamicConstantDesc<VarHandle> dcd = DynamicConstantDesc.ofNamed(ConstantDescs.BSM_VARHANDLE_STATIC_FIELD, "sf", CD_VarHandle, new ConstantDesc[] {testClass, CD_int });
 177         VarHandle vh = dcd.resolveConstantDesc(LOOKUP);
 178         testVarHandleDesc(dcd, vh);
 179 
 180         VarHandleDesc vhd = (VarHandleDesc) DynamicConstantDesc.ofCanonical(ConstantDescs.BSM_VARHANDLE_STATIC_FIELD, "sf", CD_VarHandle, new ConstantDesc[] {testClass, CD_int });
 181         vh = vhd.resolveConstantDesc(LOOKUP);
 182         testVarHandleDesc(vhd, vh);
 183 
 184         dcd = DynamicConstantDesc.ofNamed(ConstantDescs.BSM_VARHANDLE_FIELD, "f", CD_VarHandle, new ConstantDesc[] {testClass, CD_int });
 185         vh = dcd.resolveConstantDesc(LOOKUP);
 186         testVarHandleDesc(dcd, vh);
 187 
 188         vhd = (VarHandleDesc) DynamicConstantDesc.ofCanonical(ConstantDescs.BSM_VARHANDLE_FIELD, "f", CD_VarHandle, new ConstantDesc[] {testClass, CD_int });
 189         vh = vhd.resolveConstantDesc(LOOKUP);
 190         testVarHandleDesc(vhd, vh);
 191 
 192         dcd = DynamicConstantDesc.ofNamed(ConstantDescs.BSM_VARHANDLE_ARRAY, "_", CD_VarHandle, new ConstantDesc[] {CD_int.arrayType() });
 193         vh = dcd.resolveConstantDesc(LOOKUP);
 194         testVarHandleDesc(dcd, vh);
 195 
 196         vhd = (VarHandleDesc)DynamicConstantDesc.ofCanonical(ConstantDescs.BSM_VARHANDLE_ARRAY, "_", CD_VarHandle, new ConstantDesc[] {CD_int.arrayType() });
 197         vh = vhd.resolveConstantDesc(LOOKUP);
 198         testVarHandleDesc(vhd, vh);
 199     }
 200 
 201     private<T> void assertLifted(ConstantDesc prototype,
 202                                  DynamicConstantDesc<T> nonCanonical,
 203                                  ConstantDesc canonical) {
 204         Class<?> clazz = prototype.getClass();
 205 
 206         assertNotSame(canonical, nonCanonical);
 207         assertTrue(clazz.isAssignableFrom(canonical.getClass()));
 208         assertFalse(clazz.isAssignableFrom(nonCanonical.getClass()));
 209         assertEquals(prototype, canonical);
 210         assertEquals(canonical, prototype);
 211         if (prototype instanceof DynamicConstantDesc) {
 212             assertEquals(canonical, nonCanonical);
 213             assertEquals(nonCanonical, canonical);
 214             assertEquals(prototype, nonCanonical);
 215             assertEquals(nonCanonical, prototype);
 216         }
 217     }
 218 
 219     public void testLifting() {
 220         DynamicConstantDesc<Object> unliftedNull = DynamicConstantDesc.ofNamed(ConstantDescs.BSM_NULL_CONSTANT, "_", CD_Object, EMPTY_ARGS);
 221         assertEquals(ConstantDescs.NULL, unliftedNull);
 222         assertNotSame(ConstantDescs.NULL, unliftedNull);
 223         assertSame(ConstantDescs.NULL, DynamicConstantDesc.ofCanonical(ConstantDescs.BSM_NULL_CONSTANT, "_", CD_Object, EMPTY_ARGS));
 224         assertSame(ConstantDescs.NULL, DynamicConstantDesc.ofCanonical(ConstantDescs.BSM_NULL_CONSTANT, "_", CD_String, EMPTY_ARGS));
 225         assertSame(ConstantDescs.NULL, DynamicConstantDesc.ofCanonical(ConstantDescs.BSM_NULL_CONSTANT, "wahoo", CD_Object, EMPTY_ARGS));
 226 
 227         assertLifted(CD_int,
 228                      DynamicConstantDesc.ofNamed(ConstantDescs.BSM_PRIMITIVE_CLASS, "I", ConstantDescs.CD_Class, EMPTY_ARGS),
 229                      DynamicConstantDesc.ofCanonical(ConstantDescs.BSM_PRIMITIVE_CLASS, "I", ConstantDescs.CD_Class, EMPTY_ARGS));
 230 
 231         ClassDesc enumClass = ClassDesc.of("CondyDescTest").nested("MyEnum");
 232         assertLifted(EnumDesc.of(enumClass, "A"),
 233                      DynamicConstantDesc.ofNamed(ConstantDescs.BSM_ENUM_CONSTANT, "A", enumClass, EMPTY_ARGS),
 234                      DynamicConstantDesc.<MyEnum>ofCanonical(ConstantDescs.BSM_ENUM_CONSTANT, "A", enumClass, EMPTY_ARGS));
 235 
 236 
 237         ClassDesc testClass = ClassDesc.of("CondyDescTest").nested("MyClass");
 238 
 239         assertLifted(VarHandleDesc.ofStaticField(testClass, "sf", CD_int),
 240                      DynamicConstantDesc.ofNamed(ConstantDescs.BSM_VARHANDLE_STATIC_FIELD, "sf", CD_VarHandle, new ConstantDesc[] {testClass, CD_int }),
 241                      DynamicConstantDesc.ofCanonical(ConstantDescs.BSM_VARHANDLE_STATIC_FIELD, "sf", CD_VarHandle, new ConstantDesc[] {testClass, CD_int }));
 242         assertLifted(VarHandleDesc.ofField(testClass, "f", CD_int),
 243                      DynamicConstantDesc.ofNamed(ConstantDescs.BSM_VARHANDLE_FIELD, "f", CD_VarHandle, new ConstantDesc[] {testClass, CD_int }),
 244                      DynamicConstantDesc.ofCanonical(ConstantDescs.BSM_VARHANDLE_FIELD, "f", CD_VarHandle, new ConstantDesc[] {testClass, CD_int }));
 245 
 246         assertLifted(VarHandleDesc.ofArray(CD_int.arrayType()),
 247                      DynamicConstantDesc.ofNamed(ConstantDescs.BSM_VARHANDLE_ARRAY, "_", CD_VarHandle, new ConstantDesc[] {CD_int.arrayType() }),
 248                      DynamicConstantDesc.ofCanonical(ConstantDescs.BSM_VARHANDLE_ARRAY, "_", CD_VarHandle, new ConstantDesc[] {CD_int.arrayType() }));
 249     }
 250 }