changeset 50928:05169a7b9350 jep-334

adding test to improve code coverage
author vromero
date Sat, 02 Jun 2018 19:34:57 -0700
parents f0212b293c0e
children a5ca4f50cb3b 0f629ccfee9a
files src/java.base/share/classes/java/lang/invoke/constant/ConstantUtils.java src/java.base/share/classes/java/lang/invoke/constant/DynamicCallSiteDesc.java test/jdk/java/lang/invoke/constant/ClassRefTest.java test/jdk/java/lang/invoke/constant/ConstantUtilsTest.java test/jdk/java/lang/invoke/constant/IndyRefTest.java test/jdk/java/lang/invoke/constant/MethodHandleRefTest.java
diffstat 6 files changed, 165 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.base/share/classes/java/lang/invoke/constant/ConstantUtils.java	Fri Jun 01 10:10:42 2018 -0700
+++ b/src/java.base/share/classes/java/lang/invoke/constant/ConstantUtils.java	Sat Jun 02 19:34:57 2018 -0700
@@ -24,11 +24,8 @@
  */
 package java.lang.invoke.constant;
 
-import java.util.ArrayList;
-import java.util.List;
-import java.util.NoSuchElementException;
-import java.util.Optional;
-import java.util.Set;
+import java.util.*;
+import static java.util.Objects.requireNonNull;
 
 /**
  * Helper methods for the implementation of {@code java.lang.invoke.constant}.
@@ -55,6 +52,7 @@
      * @return the name passed if valid
      */
     public static String validateMemberName(String name) {
+        requireNonNull(name);
         if (name.length() == 0)
             throw new IllegalArgumentException("zero-length member name");
         for (int i=0; i<name.length(); i++) {
@@ -112,6 +110,9 @@
     public static<T> Optional<DynamicConstantDesc<T>> symbolizeHelper(MethodHandleDesc bootstrap,
                                                                ClassDesc type,
                                                                Constable<?>... args) {
+        requireNonNull(bootstrap);
+        requireNonNull(type);
+        requireNonNull(args);
         try {
             ConstantDesc<?>[] quotedArgs = new ConstantDesc<?>[args.length + 1];
             quotedArgs[0] = bootstrap;
--- a/src/java.base/share/classes/java/lang/invoke/constant/DynamicCallSiteDesc.java	Fri Jun 01 10:10:42 2018 -0700
+++ b/src/java.base/share/classes/java/lang/invoke/constant/DynamicCallSiteDesc.java	Sat Jun 02 19:34:57 2018 -0700
@@ -184,11 +184,6 @@
         return new DynamicCallSiteDesc(bootstrapMethod, invocationName, invocationType, bootstrapArgs);
     }
 
-    private DynamicCallSiteDesc canonicalize() {
-        // @@@ MethodDesc
-        return this;
-    }
-
     /**
      * Returns the invocation name that would appear in the {@code NameAndType}
      * operand of the {@code invokedynamic}.
--- a/test/jdk/java/lang/invoke/constant/ClassRefTest.java	Fri Jun 01 10:10:42 2018 -0700
+++ b/test/jdk/java/lang/invoke/constant/ClassRefTest.java	Sat Jun 02 19:34:57 2018 -0700
@@ -26,6 +26,7 @@
 import java.lang.invoke.MethodHandles;
 import java.lang.invoke.constant.ClassDesc;
 import java.lang.invoke.constant.ConstantDescs;
+import java.lang.invoke.constant.ConstantUtils;
 import java.lang.reflect.Array;
 import java.lang.reflect.Field;
 import java.lang.reflect.Modifier;
@@ -239,7 +240,17 @@
             }
         }
 
-        ClassDesc barDesc = ClassDesc.of("Bar");
+        List<String> badBinaryNames = List.of("I;", "[]", "Ljava/lang/String",
+                "Ljava.lang.String;", "java/lang/String");
+        for (String d : badBinaryNames) {
+            try {
+                ClassDesc constant = ClassDesc.of(d);
+                fail(d);
+            } catch (IllegalArgumentException e) {
+                // good
+            }
+        }
+
         for (Primitives p : Primitives.values()) {
             testBadInnerClasses(ClassDesc.ofDescriptor(p.descriptor), "any");
             testBadInnerClasses(ClassDesc.ofDescriptor(p.descriptor), "any", "other");
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/java/lang/invoke/constant/ConstantUtilsTest.java	Sat Jun 02 19:34:57 2018 -0700
@@ -0,0 +1,93 @@
+/*
+ * 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
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+import java.lang.invoke.*;
+import java.lang.invoke.constant.*;
+import java.util.*;
+
+import org.testng.annotations.Test;
+
+import static org.testng.Assert.*;
+
+/**
+ * @test
+ * @compile -XDfolding=false ConstantUtilsTest.java
+ * @run testng ConstantUtilsTest
+ * @summary unit tests for methods of java.lang.invoke.constant.ConstantUtils that are not covered by other unit tests
+ */
+@Test
+public class ConstantUtilsTest {
+    private static ClassDesc thisClass = ClassDesc.of("MethodHandleRefTest");
+
+    public void testValidateMemberName() {
+        try {
+            ConstantUtils.validateMemberName(null);
+            fail("");
+        } catch (NullPointerException e) {
+            // good
+        }
+
+        try {
+            ConstantUtils.validateMemberName("");
+            fail("");
+        } catch (IllegalArgumentException e) {
+            // good
+        }
+
+        List<String> badNames = List.of(".", ";", "[", "/", "<", ">");
+        for (String n : badNames) {
+            try {
+                ConstantUtils.validateMemberName(n);
+                fail(n);
+            } catch (IllegalArgumentException e) {
+                // good
+            }
+        }
+    }
+
+    public void testSymbolizeHelper() {
+        ConstantMethodHandleDesc mh = MethodHandleDesc.of(MethodHandleDesc.Kind.VIRTUAL, ConstantDescs.CR_String, "isEmpty", "()Z");
+        try {
+            ConstantUtils.symbolizeHelper(mh, null, "");
+            fail("");
+        } catch (NullPointerException e) {
+            // good
+        }
+
+        try {
+            ConstantUtils.symbolizeHelper(null, ConstantDescs.CR_ClassDesc, "");
+            fail("");
+        } catch (NullPointerException e) {
+            // good
+        }
+
+        try {
+            ConstantUtils.symbolizeHelper(null, null, "");
+            fail("");
+        } catch (NullPointerException e) {
+            // good
+        }
+    }
+}
--- a/test/jdk/java/lang/invoke/constant/IndyRefTest.java	Fri Jun 01 10:10:42 2018 -0700
+++ b/test/jdk/java/lang/invoke/constant/IndyRefTest.java	Sat Jun 02 19:34:57 2018 -0700
@@ -38,6 +38,7 @@
 
 import static java.lang.invoke.constant.ConstantDescs.*;
 import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertNotEquals;
 
 /**
  * @test
@@ -63,11 +64,54 @@
         CallSite cs = csd.resolveCallSiteDesc(MethodHandles.lookup());
         MethodHandle target = cs.getTarget();
         assertEquals("Foo", target.invoke());
+        assertEquals("wooga", csd.invocationName());
 
-        DynamicCallSiteDesc csd2 = DynamicCallSiteDesc.of(mh, "wooga", MethodTypeDesc.of(CR_String), "Bar");
+        DynamicCallSiteDesc csd2 = DynamicCallSiteDesc.of(mh, "foo", MethodTypeDesc.of(CR_String), "Bar");
         CallSite cs2 = csd2.resolveCallSiteDesc(MethodHandles.lookup());
         MethodHandle target2 = cs2.getTarget();
         assertEquals("Bar", target2.invoke());
+        assertEquals("foo", csd2.invocationName());
+
+        DynamicCallSiteDesc csd3 = DynamicCallSiteDesc.of(mh, MethodTypeDesc.of(CR_String));
+        CallSite cs3 = csd.resolveCallSiteDesc(MethodHandles.lookup());
+        MethodHandle target3 = cs3.getTarget();
+        assertEquals("Foo", target3.invoke());
+        assertEquals("_", csd3.invocationName());
+
+        DynamicCallSiteDesc csd4 = DynamicCallSiteDesc.of(mh, "foo", MethodTypeDesc.of(CR_String)).withArgs("Bar");
+        CallSite cs4 = csd4.resolveCallSiteDesc(MethodHandles.lookup());
+        MethodHandle target4 = cs4.getTarget();
+        assertEquals("Bar", target4.invoke());
+
+        DynamicCallSiteDesc csd5 = DynamicCallSiteDesc.of(mh, MethodTypeDesc.of(CR_String, CR_String))
+                .withNameAndType("foo", MethodTypeDesc.of(CR_String)).withArgs("Bar");
+        CallSite cs5 = csd5.resolveCallSiteDesc(MethodHandles.lookup());
+        MethodHandle target5 = cs5.getTarget();
+        assertEquals("Bar", target5.invoke());
+        assertEquals("foo", csd5.invocationName());
+    }
+
+    public void testEqualsHashToString() 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 csd1 = DynamicCallSiteDesc.of(mh, "wooga", MethodTypeDesc.of(CR_String));
+        DynamicCallSiteDesc csd2 = DynamicCallSiteDesc.of(mh, "wooga", MethodTypeDesc.of(CR_String));
+        DynamicCallSiteDesc csd3 = DynamicCallSiteDesc.of(mh, "foo", MethodTypeDesc.of(CR_String));
+        assertEquals(csd1, csd2);
+        assertEquals(csd1.hashCode(), csd2.hashCode());
+        assertNotEquals(csd1, csd3);
+        assertNotEquals(csd1.hashCode(), csd3.hashCode());
+
+        assertEquals(csd1.toString(), "DynamicCallSiteDesc[IndyRefTest::bootstrap(wooga/):()String]");
+    }
+
+    @Test(expectedExceptions = IllegalArgumentException.class)
+    public void testEmptyInvocationName() 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 csd1 = DynamicCallSiteDesc.of(mh, "", MethodTypeDesc.of(CR_String));
     }
 }
-
--- a/test/jdk/java/lang/invoke/constant/MethodHandleRefTest.java	Fri Jun 01 10:10:42 2018 -0700
+++ b/test/jdk/java/lang/invoke/constant/MethodHandleRefTest.java	Sat Jun 02 19:34:57 2018 -0700
@@ -45,6 +45,7 @@
 import static java.lang.invoke.constant.MethodHandleDesc.Kind.SETTER;
 import static java.lang.invoke.constant.MethodHandleDesc.Kind.STATIC_GETTER;
 import static java.lang.invoke.constant.MethodHandleDesc.Kind.STATIC_SETTER;
+import static java.lang.invoke.constant.MethodHandleDesc.Kind.VIRTUAL;
 import static java.lang.invoke.constant.ConstantDescs.CR_Integer;
 import static java.lang.invoke.constant.ConstantDescs.CR_List;
 import static java.lang.invoke.constant.ConstantDescs.CR_Object;
@@ -128,10 +129,11 @@
     public void testAsType() throws Throwable {
         MethodHandleDesc mhr = MethodHandleDesc.of(MethodHandleDesc.Kind.STATIC, ClassDesc.of("java.lang.Integer"), "valueOf",
                                                    MethodTypeDesc.of(CR_Integer, CR_int));
-            MethodHandleDesc takesInteger = mhr.asType(MethodTypeDesc.of(CR_Integer, CR_Integer));
+        MethodHandleDesc takesInteger = mhr.asType(MethodTypeDesc.of(CR_Integer, CR_Integer));
         testMethodHandleRef(takesInteger);
         MethodHandle mh1 = takesInteger.resolveConstantDesc(LOOKUP);
         assertEquals((Integer) 3, (Integer) mh1.invokeExact((Integer) 3));
+        assertEquals(takesInteger.toString(), "MethodHandleDesc[STATIC/Integer::valueOf(int)Integer].asType(Integer)Integer");
 
         try {
             Integer i = (Integer) mh1.invokeExact(3);
@@ -265,6 +267,11 @@
         badStaticSetterDescs.forEach(s -> assertBadArgs(() -> MethodHandleDesc.of(STATIC_SETTER, thisClass, "x", s), s));
     }
 
+    @Test(expectedExceptions = IllegalArgumentException.class)
+    public void testBadOwners() {
+        MethodHandleDesc.of(VIRTUAL, ClassDesc.ofDescriptor("I"), "x", MethodTypeDesc.ofDescriptor("()I"));
+    }
+
     public void testSymbolicRefsConstants() throws ReflectiveOperationException {
         int tested = 0;
         Field[] fields = ConstantDescs.class.getDeclaredFields();