changeset 50738:3248898657dc condy-folding

yet another manual merge :(
author vromero
date Thu, 24 May 2018 10:24:16 -0700
parents acd4969ec1f8 cd7f4c80a4db
children 7811a4252913
files test/jdk/java/lang/invoke/constant/IndyRefTest.java test/jdk/java/lang/invoke/constant/MethodHandleRefTest.java test/jdk/java/lang/invoke/constant/MethodTypeRefTest.java
diffstat 3 files changed, 128 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/test/jdk/java/lang/invoke/constant/IndyRefTest.java	Thu May 24 07:15:23 2018 -0700
+++ b/test/jdk/java/lang/invoke/constant/IndyRefTest.java	Thu May 24 10:24:16 2018 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -23,17 +23,51 @@
  * questions.
  */
 
+import java.lang.invoke.CallSite;
+import java.lang.invoke.ConstantCallSite;
+import java.lang.invoke.MethodHandle;
+import java.lang.invoke.MethodHandles;
+import java.lang.invoke.MethodType;
+import java.lang.invoke.constant.ClassDesc;
+import java.lang.invoke.constant.ConstantMethodHandleDesc;
+import java.lang.invoke.constant.DynamicCallSiteDesc;
+import java.lang.invoke.constant.MethodHandleDesc;
+import java.lang.invoke.constant.MethodTypeDesc;
+
 import org.testng.annotations.Test;
 
+import static java.lang.invoke.constant.ConstantDescs.*;
+import static org.testng.Assert.assertEquals;
+
 /**
- * IndyRefTest
- *
- * @author Brian Goetz
+ * @test
+ * @compile -XDfolding=false IndyRefTest.java
+ * @run testng IndyRefTest
+ * @summary unit tests for java.lang.invoke.constant.IndyRefTest
  */
 @Test
 public class IndyRefTest {
-    // @@@ non-intrinsified: get dynamic invoker, and invoke
-    // test toSymRef and back
-    // test canonicalization; StringConcat?
-    // @@@ intrinsified tests too
+    public static CallSite bootstrap(MethodHandles.Lookup lookup, String name, MethodType type,
+                                     Object... args) {
+        if (args.length == 0)
+            return new ConstantCallSite(MethodHandles.constant(String.class, "Foo"));
+        else
+            return new ConstantCallSite(MethodHandles.constant(String.class, (String) args[0]));
+    }
+
+    public void testIndyRef() throws Throwable {
+        ClassDesc c = ClassDesc.of("IndyRefTest");
+        MethodTypeDesc mt = MethodTypeDesc.of(CR_CallSite, CR_MethodHandles_Lookup, CR_String, CR_MethodType, CR_Object.arrayType());
+        ConstantMethodHandleDesc mh = MethodHandleDesc.of(MethodHandleDesc.Kind.STATIC, c, "bootstrap", mt);
+        DynamicCallSiteDesc csd = DynamicCallSiteDesc.of(mh, "wooga", MethodTypeDesc.of(CR_String));
+        CallSite cs = csd.resolveCallSiteDesc(MethodHandles.lookup());
+        MethodHandle target = cs.getTarget();
+        assertEquals("Foo", target.invoke());
+
+        DynamicCallSiteDesc csd2 = DynamicCallSiteDesc.of(mh, "wooga", MethodTypeDesc.of(CR_String), "Bar");
+        CallSite cs2 = csd2.resolveCallSiteDesc(MethodHandles.lookup());
+        MethodHandle target2 = cs2.getTarget();
+        assertEquals("Bar", target2.invoke());
+    }
 }
+
--- a/test/jdk/java/lang/invoke/constant/MethodHandleRefTest.java	Thu May 24 07:15:23 2018 -0700
+++ b/test/jdk/java/lang/invoke/constant/MethodHandleRefTest.java	Thu May 24 10:24:16 2018 -0700
@@ -114,6 +114,15 @@
                             LOOKUP.findVirtual(List.class, "isEmpty", MethodType.fromMethodDescriptorString("()Z", null)));
         testMethodHandleRef(MethodHandleDesc.of(MethodHandleDesc.Kind.CONSTRUCTOR, ClassDesc.of("java.util.ArrayList"), "<init>", CR_void),
                             LOOKUP.findConstructor(ArrayList.class, MethodType.methodType(void.class)));
+        testMethodHandleRef(MethodHandleDesc.ofConstructor(ClassDesc.of("java.util.ArrayList")),
+                            LOOKUP.findConstructor(ArrayList.class, MethodType.methodType(void.class)));
+        // bad constructor non void return type
+        try {
+            MethodHandleDesc.of(MethodHandleDesc.Kind.CONSTRUCTOR, ClassDesc.of("java.util.ArrayList"), "<init>", CR_int);
+            fail("should have failed: non void return type for constructor");
+        } catch (IllegalArgumentException ex) {
+            // good
+        }
     }
 
     public void testAsType() throws Throwable {
--- a/test/jdk/java/lang/invoke/constant/MethodTypeRefTest.java	Thu May 24 07:15:23 2018 -0700
+++ b/test/jdk/java/lang/invoke/constant/MethodTypeRefTest.java	Thu May 24 10:24:16 2018 -0700
@@ -33,6 +33,8 @@
 
 import org.testng.annotations.Test;
 
+import static java.lang.invoke.constant.ConstantDescs.CR_int;
+import static java.lang.invoke.constant.ConstantDescs.CR_void;
 import static java.util.stream.Collectors.joining;
 import static java.util.stream.Collectors.toList;
 import static org.testng.Assert.assertEquals;
@@ -127,6 +129,8 @@
             testMethodTypeRef(newRef, mt.dropParameterTypes(i, i+1));
         }
 
+        badDropParametersTypes(CR_void, paramDescs);
+
         // addParam
         for (int i=0; i <= paramTypes.length; i++) {
             for (ClassDesc p : paramTypes) {
@@ -139,6 +143,69 @@
                 testMethodTypeRef(newRef, mt.insertParameterTypes(i, p.resolveConstantDesc(LOOKUP)));
             }
         }
+
+        badInsertParametersTypes(CR_void, paramDescs);
+    }
+
+    private void badInsertParametersTypes(ClassDesc returnType, String... paramDescTypes) {
+        ClassDesc[] paramTypes =
+                IntStream.rangeClosed(0, paramDescTypes.length - 1)
+                        .mapToObj(i -> ClassDesc.ofDescriptor(paramDescTypes[i])).toArray(ClassDesc[]::new);
+        MethodTypeDesc mtRef = MethodTypeDesc.of(returnType, paramTypes);
+        try {
+            MethodTypeDesc newRef = mtRef.insertParameterTypes(-1, paramTypes);
+            fail("pos < 0 should have failed");
+        } catch (IndexOutOfBoundsException ex) {
+            // good
+        }
+
+        try {
+            MethodTypeDesc newRef = mtRef.insertParameterTypes(paramTypes.length + 1, paramTypes);
+            fail("pos > current arguments length should have failed");
+        } catch (IndexOutOfBoundsException ex) {
+            // good
+        }
+    }
+
+    private void badDropParametersTypes(ClassDesc returnType, String... paramDescTypes) {
+        ClassDesc[] paramTypes =
+                IntStream.rangeClosed(0, paramDescTypes.length - 1)
+                        .mapToObj(i -> ClassDesc.ofDescriptor(paramDescTypes[i])).toArray(ClassDesc[]::new);
+        MethodTypeDesc mtRef = MethodTypeDesc.of(returnType, paramTypes);
+        try {
+            MethodTypeDesc newRef = mtRef.dropParameterTypes(-1, 0);
+            fail("start index < 0 should have failed");
+        } catch (IndexOutOfBoundsException ex) {
+            // good
+        }
+
+        try {
+            MethodTypeDesc newRef = mtRef.dropParameterTypes(paramTypes.length, 0);
+            fail("start index = arguments.length should have failed");
+        } catch (IndexOutOfBoundsException ex) {
+            // good
+        }
+
+        try {
+            MethodTypeDesc newRef = mtRef.dropParameterTypes(paramTypes.length + 1, 0);
+            fail("start index > arguments.length should have failed");
+        } catch (IndexOutOfBoundsException ex) {
+            // good
+        }
+
+        try {
+            MethodTypeDesc newRef = mtRef.dropParameterTypes(0, paramTypes.length + 1);
+            fail("end index > arguments.length should have failed");
+        } catch (IndexOutOfBoundsException ex) {
+            // good
+        }
+
+        try {
+            MethodTypeDesc newRef = mtRef.dropParameterTypes(1, 0);
+            fail("start index > end index should have failed");
+        } catch (IllegalArgumentException ex) {
+            // good
+        }
     }
 
     public void testMethodTypeRef() throws ReflectiveOperationException {
@@ -167,5 +234,15 @@
                 // good
             }
         }
+
+        // try with void arguments, this will stress another code path in particular
+        // ConstantMethodTypeDesc::init
+        try {
+            MethodTypeDesc r = MethodTypeDesc.of(CR_int, CR_void);
+            fail("can't reach here");
+        }
+        catch (IllegalArgumentException e) {
+            // good
+        }
     }
 }