Merge
authorzmajo
Wed, 15 Apr 2015 07:49:25 +0000
changeset 30345 9c3d245dd8cc
parent 30344 72127fbf9974 (current diff)
parent 30343 bf75edd66df0 (diff)
child 30346 a0bacb211630
child 30362 2b921e0eb3e0
Merge
--- a/jdk/src/java.base/share/classes/java/lang/Class.java	Wed Apr 15 09:37:51 2015 +0200
+++ b/jdk/src/java.base/share/classes/java/lang/Class.java	Wed Apr 15 07:49:25 2015 +0000
@@ -1312,7 +1312,7 @@
         // e) Anonymous classes
 
 
-        // JVM Spec 4.8.6: A class must have an EnclosingMethod
+        // JVM Spec 4.7.7: A class must have an EnclosingMethod
         // attribute if and only if it is a local class or an
         // anonymous class.
         EnclosingMethodInfo enclosingInfo = getEnclosingMethodInfo();
@@ -1357,28 +1357,7 @@
             simpleName = getName();
             return simpleName.substring(simpleName.lastIndexOf('.')+1); // strip the package name
         }
-        // According to JLS3 "Binary Compatibility" (13.1) the binary
-        // name of non-package classes (not top level) is the binary
-        // name of the immediately enclosing class followed by a '$' followed by:
-        // (for nested and inner classes): the simple name.
-        // (for local classes): 1 or more digits followed by the simple name.
-        // (for anonymous classes): 1 or more digits.
-
-        // Since getSimpleBinaryName() will strip the binary name of
-        // the immediately enclosing class, we are now looking at a
-        // string that matches the regular expression "\$[0-9]*"
-        // followed by a simple name (considering the simple of an
-        // anonymous class to be the empty string).
-
-        // Remove leading "\$[0-9]*" from the name
-        int length = simpleName.length();
-        if (length < 1 || simpleName.charAt(0) != '$')
-            throw new InternalError("Malformed class name");
-        int index = 1;
-        while (index < length && isAsciiDigit(simpleName.charAt(index)))
-            index++;
-        // Eventually, this is the empty string iff this is an anonymous class
-        return simpleName.substring(index);
+        return simpleName;
     }
 
     /**
@@ -1489,20 +1468,20 @@
         Class<?> enclosingClass = getEnclosingClass();
         if (enclosingClass == null) // top level class
             return null;
-        // Otherwise, strip the enclosing class' name
-        try {
-            return getName().substring(enclosingClass.getName().length());
-        } catch (IndexOutOfBoundsException ex) {
-            throw new InternalError("Malformed class name", ex);
-        }
+        String name = getSimpleBinaryName0();
+        if (name == null) // anonymous class
+            return "";
+        return name;
     }
 
+    private native String getSimpleBinaryName0();
+
     /**
      * Returns {@code true} if this is a local class or an anonymous
      * class.  Returns {@code false} otherwise.
      */
     private boolean isLocalOrAnonymousClass() {
-        // JVM Spec 4.8.6: A class must have an EnclosingMethod
+        // JVM Spec 4.7.7: A class must have an EnclosingMethod
         // attribute if and only if it is a local class or an
         // anonymous class.
         return getEnclosingMethodInfo() != null;
--- a/jdk/src/java.base/share/classes/java/lang/invoke/DirectMethodHandle.java	Wed Apr 15 09:37:51 2015 +0200
+++ b/jdk/src/java.base/share/classes/java/lang/invoke/DirectMethodHandle.java	Wed Apr 15 07:49:25 2015 +0000
@@ -691,10 +691,4 @@
             }
         }
     }
-
-    @Override
-    void customize() {
-        assert(form.customized == null);
-        // No need to customize DMHs.
-    }
 }
--- a/jdk/src/java.base/share/classes/java/lang/invoke/InvokerBytecodeGenerator.java	Wed Apr 15 09:37:51 2015 +0200
+++ b/jdk/src/java.base/share/classes/java/lang/invoke/InvokerBytecodeGenerator.java	Wed Apr 15 07:49:25 2015 +0000
@@ -847,11 +847,7 @@
             refKind = REF_invokeVirtual;
         }
 
-        if (member.getDeclaringClass().isInterface() && refKind == REF_invokeVirtual) {
-            // Methods from Object declared in an interface can be resolved by JVM to invokevirtual kind.
-            // Need to convert it back to invokeinterface to pass verification and make the invocation works as expected.
-            refKind = REF_invokeInterface;
-        }
+        assert(!(member.getDeclaringClass().isInterface() && refKind == REF_invokeVirtual));
 
         // push arguments
         emitPushArguments(name);
--- a/jdk/src/java.base/share/classes/java/lang/invoke/MethodHandleNatives.java	Wed Apr 15 09:37:51 2015 +0200
+++ b/jdk/src/java.base/share/classes/java/lang/invoke/MethodHandleNatives.java	Wed Apr 15 07:49:25 2015 +0000
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2008, 2015, 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
@@ -55,16 +55,6 @@
     static native Object staticFieldBase(MemberName self);  // e.g., returns clazz
     static native Object getMemberVMInfo(MemberName self);  // returns {vmindex,vmtarget}
 
-    /// MethodHandle support
-
-    /** Fetch MH-related JVM parameter.
-     *  which=0 retrieves MethodHandlePushLimit
-     *  which=1 retrieves stack slot push size (in address units)
-     */
-    static native int getConstant(int which);
-
-    static final boolean COUNT_GWT;
-
     /// CallSite support
 
     /** Tell the JVM that we need to change the target of a CallSite. */
@@ -74,102 +64,30 @@
     private static native void registerNatives();
     static {
         registerNatives();
-        COUNT_GWT                   = getConstant(Constants.GC_COUNT_GWT) != 0;
 
         // The JVM calls MethodHandleNatives.<clinit>.  Cascade the <clinit> calls as needed:
         MethodHandleImpl.initStatics();
     }
 
-    // All compile-time constants go here.
-    // There is an opportunity to check them against the JVM's idea of them.
+    /**
+     * Compile-time constants go here. This collection exists not only for
+     * reference from clients, but also for ensuring the VM and JDK agree on the
+     * values of these constants (see {@link #verifyConstants()}).
+     */
     static class Constants {
         Constants() { } // static only
-        // MethodHandleImpl
-        static final int // for getConstant
-                GC_COUNT_GWT = 4,
-                GC_LAMBDA_SUPPORT = 5;
 
-        // MemberName
-        // The JVM uses values of -2 and above for vtable indexes.
-        // Field values are simple positive offsets.
-        // Ref: src/share/vm/oops/methodOop.hpp
-        // This value is negative enough to avoid such numbers,
-        // but not too negative.
-        static final int
-                MN_IS_METHOD           = 0x00010000, // method (not constructor)
-                MN_IS_CONSTRUCTOR      = 0x00020000, // constructor
-                MN_IS_FIELD            = 0x00040000, // field
-                MN_IS_TYPE             = 0x00080000, // nested type
-                MN_CALLER_SENSITIVE    = 0x00100000, // @CallerSensitive annotation detected
-                MN_REFERENCE_KIND_SHIFT = 24, // refKind
-                MN_REFERENCE_KIND_MASK = 0x0F000000 >> MN_REFERENCE_KIND_SHIFT,
-                // The SEARCH_* bits are not for MN.flags but for the matchFlags argument of MHN.getMembers:
-                MN_SEARCH_SUPERCLASSES = 0x00100000,
-                MN_SEARCH_INTERFACES   = 0x00200000;
-
-        /**
-         * Basic types as encoded in the JVM.  These code values are not
-         * intended for use outside this class.  They are used as part of
-         * a private interface between the JVM and this class.
-         */
         static final int
-            T_BOOLEAN  =  4,
-            T_CHAR     =  5,
-            T_FLOAT    =  6,
-            T_DOUBLE   =  7,
-            T_BYTE     =  8,
-            T_SHORT    =  9,
-            T_INT      = 10,
-            T_LONG     = 11,
-            T_OBJECT   = 12,
-            //T_ARRAY    = 13
-            T_VOID     = 14,
-            //T_ADDRESS  = 15
-            T_ILLEGAL  = 99;
-
-        /**
-         * Constant pool entry types.
-         */
-        static final byte
-            CONSTANT_Utf8                = 1,
-            CONSTANT_Integer             = 3,
-            CONSTANT_Float               = 4,
-            CONSTANT_Long                = 5,
-            CONSTANT_Double              = 6,
-            CONSTANT_Class               = 7,
-            CONSTANT_String              = 8,
-            CONSTANT_Fieldref            = 9,
-            CONSTANT_Methodref           = 10,
-            CONSTANT_InterfaceMethodref  = 11,
-            CONSTANT_NameAndType         = 12,
-            CONSTANT_MethodHandle        = 15,  // JSR 292
-            CONSTANT_MethodType          = 16,  // JSR 292
-            CONSTANT_InvokeDynamic       = 18,
-            CONSTANT_LIMIT               = 19;   // Limit to tags found in classfiles
-
-        /**
-         * Access modifier flags.
-         */
-        static final char
-            ACC_PUBLIC                 = 0x0001,
-            ACC_PRIVATE                = 0x0002,
-            ACC_PROTECTED              = 0x0004,
-            ACC_STATIC                 = 0x0008,
-            ACC_FINAL                  = 0x0010,
-            ACC_SYNCHRONIZED           = 0x0020,
-            ACC_VOLATILE               = 0x0040,
-            ACC_TRANSIENT              = 0x0080,
-            ACC_NATIVE                 = 0x0100,
-            ACC_INTERFACE              = 0x0200,
-            ACC_ABSTRACT               = 0x0400,
-            ACC_STRICT                 = 0x0800,
-            ACC_SYNTHETIC              = 0x1000,
-            ACC_ANNOTATION             = 0x2000,
-            ACC_ENUM                   = 0x4000,
-            // aliases:
-            ACC_SUPER                  = ACC_SYNCHRONIZED,
-            ACC_BRIDGE                 = ACC_VOLATILE,
-            ACC_VARARGS                = ACC_TRANSIENT;
+            MN_IS_METHOD           = 0x00010000, // method (not constructor)
+            MN_IS_CONSTRUCTOR      = 0x00020000, // constructor
+            MN_IS_FIELD            = 0x00040000, // field
+            MN_IS_TYPE             = 0x00080000, // nested type
+            MN_CALLER_SENSITIVE    = 0x00100000, // @CallerSensitive annotation detected
+            MN_REFERENCE_KIND_SHIFT = 24, // refKind
+            MN_REFERENCE_KIND_MASK = 0x0F000000 >> MN_REFERENCE_KIND_SHIFT,
+            // The SEARCH_* bits are not for MN.flags but for the matchFlags argument of MHN.getMembers:
+            MN_SEARCH_SUPERCLASSES = 0x00100000,
+            MN_SEARCH_INTERFACES   = 0x00200000;
 
         /**
          * Constant pool reference-kind codes, as used by CONSTANT_MethodHandle CP entries.
--- a/jdk/src/java.base/share/native/include/jvm.h	Wed Apr 15 09:37:51 2015 +0200
+++ b/jdk/src/java.base/share/native/include/jvm.h	Wed Apr 15 07:49:25 2015 +0000
@@ -395,6 +395,9 @@
 JNIEXPORT jclass JNICALL
 JVM_GetDeclaringClass(JNIEnv *env, jclass ofClass);
 
+JNIEXPORT jstring JNICALL
+JVM_GetSimpleBinaryName(JNIEnv *env, jclass ofClass);
+
 /* Generics support (JDK 1.5) */
 JNIEXPORT jstring JNICALL
 JVM_GetClassSignature(JNIEnv *env, jclass cls);
--- a/jdk/src/java.base/share/native/libjava/Class.c	Wed Apr 15 09:37:51 2015 +0200
+++ b/jdk/src/java.base/share/native/libjava/Class.c	Wed Apr 15 07:49:25 2015 +0000
@@ -67,6 +67,7 @@
     {"getProtectionDomain0", "()" PD,       (void *)&JVM_GetProtectionDomain},
     {"getDeclaredClasses0",  "()[" CLS,      (void *)&JVM_GetDeclaredClasses},
     {"getDeclaringClass0",   "()" CLS,      (void *)&JVM_GetDeclaringClass},
+    {"getSimpleBinaryName0", "()" STR,      (void *)&JVM_GetSimpleBinaryName},
     {"getGenericSignature0", "()" STR,      (void *)&JVM_GetClassSignature},
     {"getRawAnnotations",      "()" BA,        (void *)&JVM_GetClassAnnotations},
     {"getConstantPool",     "()" CPL,       (void *)&JVM_GetClassConstantPool},
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/lang/Class/getSimpleName/GetSimpleNameTest.java	Wed Apr 15 07:49:25 2015 +0000
@@ -0,0 +1,207 @@
+/*
+ * Copyright (c) 2015, 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.
+ *
+ * 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.
+ */
+
+/* @test
+ * @bug 8057919
+ * @summary Class.getSimpleName() should work for non-JLS compliant class names
+ */
+import jdk.internal.org.objectweb.asm.*;
+import static jdk.internal.org.objectweb.asm.Opcodes.*;
+
+public class GetSimpleNameTest {
+    static class NestedClass {}
+    class InnerClass {}
+
+    static Class<?> f1() {
+        class LocalClass {}
+        return LocalClass.class;
+    }
+
+    public static void main(String[] args) throws Exception {
+        assertEquals(NestedClass.class.getSimpleName(), "NestedClass");
+        assertEquals( InnerClass.class.getSimpleName(),  "InnerClass");
+        assertEquals(             f1().getSimpleName(),  "LocalClass");
+
+        java.io.Serializable anon = new java.io.Serializable() {};
+        assertEquals(anon.getClass().getSimpleName(), "");
+
+        // Java class names, prepended enclosing class name.
+        testNested("p.Outer$Nested", "p.Outer", "Nested");
+        testInner( "p.Outer$Inner",  "p.Inner",  "Inner");
+        testLocal( "p.Outer$1Local", "p.Outer",  "Local");
+        testAnon(  "p.Outer$1",      "p.Outer",       "");
+
+        // Non-Java class names, prepended enclosing class name.
+        testNested("p.$C1$Nested", "p.$C1$", "Nested");
+        testInner( "p.$C1$Inner",  "p.$C1$",  "Inner");
+        testLocal( "p.$C1$Local",  "p.$C1$",  "Local");
+        testAnon(  "p.$C1$1",      "p.$C1$",       "");
+
+        // Non-Java class names, unrelated class names.
+        testNested("p1.$Nested$", "p2.$C1$", "Nested");
+        testInner( "p1.$Inner$",  "p2.$C1$",  "Inner");
+        testLocal( "p1.$Local$",  "p2.$C1$",  "Local");
+        testAnon(  "p1.$anon$",   "p2.$C1$",       "");
+    }
+
+    static void testNested(String innerName, String outerName, String simpleName) throws Exception {
+        BytecodeGenerator bg = new BytecodeGenerator(innerName, outerName, simpleName);
+        CustomCL cl = new CustomCL(innerName, outerName, bg.getNestedClasses(true), bg.getNestedClasses(false));
+        assertEquals(cl.loadClass(innerName).getSimpleName(), simpleName);
+    }
+
+    static void testInner(String innerName, String outerName, String simpleName) throws Exception {
+        BytecodeGenerator bg = new BytecodeGenerator(innerName, outerName, simpleName);
+        CustomCL cl = new CustomCL(innerName, outerName, bg.getInnerClasses(true), bg.getInnerClasses(false));
+        assertEquals(cl.loadClass(innerName).getSimpleName(), simpleName);
+    }
+
+    static void testLocal(String innerName, String outerName, String simpleName) throws Exception {
+        BytecodeGenerator bg = new BytecodeGenerator(innerName, outerName, simpleName);
+        CustomCL cl = new CustomCL(innerName, outerName, bg.getLocalClasses(true), bg.getLocalClasses(false));
+        assertEquals(cl.loadClass(innerName).getSimpleName(), simpleName);
+    }
+
+    static void testAnon(String innerName, String outerName, String simpleName) throws Exception {
+        BytecodeGenerator bg = new BytecodeGenerator(innerName, outerName, simpleName);
+        CustomCL cl = new CustomCL(innerName, outerName, bg.getAnonymousClasses(true), bg.getAnonymousClasses(false));
+        assertEquals(cl.loadClass(innerName).getSimpleName(), simpleName);
+    }
+
+    static void assertEquals(Object o1, Object o2) {
+        if (!java.util.Objects.equals(o1, o2)) {
+            throw new AssertionError(o1 + " != " + o2);
+        }
+    }
+
+    static class CustomCL extends ClassLoader {
+        final String  innerName;
+        final String  outerName;
+
+        final byte[] innerClassFile;
+        final byte[] outerClassFile;
+
+        CustomCL(String innerName, String outerName, byte[] innerClassFile, byte[] outerClassFile) {
+            this.innerName = innerName;
+            this.outerName = outerName;
+            this.innerClassFile = innerClassFile;
+            this.outerClassFile = outerClassFile;
+        }
+        @Override
+        protected Class<?> findClass(String name) throws ClassNotFoundException {
+            if (innerName.equals(name)) {
+                return defineClass(innerName, innerClassFile, 0, innerClassFile.length);
+            } else if (outerName.equals(name)) {
+                return defineClass(outerName, outerClassFile, 0, outerClassFile.length);
+            } else {
+                throw new ClassNotFoundException(name);
+            }
+        }
+    }
+
+    static class BytecodeGenerator {
+        final String innerName;
+        final String outerName;
+        final String simpleName;
+
+        BytecodeGenerator(String innerName, String outerName, String simpleName) {
+            this.innerName = intl(innerName);
+            this.outerName = intl(outerName);
+            this.simpleName = simpleName;
+        }
+
+        static String intl(String name) { return name.replace('.', '/'); }
+
+        static void makeDefaultCtor(ClassWriter cw) {
+            MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
+            mv.visitCode();
+            mv.visitVarInsn(ALOAD, 0);
+            mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
+            mv.visitInsn(RETURN);
+            mv.visitMaxs(1, 1);
+            mv.visitEnd();
+        }
+
+        void makeCtxk(ClassWriter cw, boolean isInner) {
+            if (isInner) {
+                cw.visitOuterClass(outerName, "f", "()V");
+            } else {
+                MethodVisitor mv = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, "f", "()V", null, null);
+                mv.visitCode();
+                mv.visitInsn(RETURN);
+                mv.visitMaxs(0, 0);
+                mv.visitEnd();
+            }
+        }
+
+        byte[] getNestedClasses(boolean isInner) {
+            String name = (isInner ? innerName : outerName);
+            ClassWriter cw = new ClassWriter(0);
+            cw.visit(V1_7, ACC_PUBLIC + ACC_SUPER, name, null, "java/lang/Object", null);
+
+            cw.visitInnerClass(innerName, outerName, simpleName, ACC_PUBLIC | ACC_STATIC);
+
+            makeDefaultCtor(cw);
+            cw.visitEnd();
+            return cw.toByteArray();
+        }
+
+        byte[] getInnerClasses(boolean isInner) {
+            String name = (isInner ? innerName : outerName);
+            ClassWriter cw = new ClassWriter(0);
+            cw.visit(V1_7, ACC_PUBLIC + ACC_SUPER, name, null, "java/lang/Object", null);
+
+            cw.visitInnerClass(innerName, outerName, simpleName, ACC_PUBLIC);
+
+            makeDefaultCtor(cw);
+            cw.visitEnd();
+            return cw.toByteArray();
+        }
+
+        byte[] getLocalClasses(boolean isInner) {
+            String name = (isInner ? innerName : outerName);
+            ClassWriter cw = new ClassWriter(0);
+            cw.visit(V1_7, ACC_PUBLIC + ACC_SUPER, name, null, "java/lang/Object", null);
+
+            cw.visitInnerClass(innerName, null, simpleName, ACC_PUBLIC | ACC_STATIC);
+            makeCtxk(cw, isInner);
+
+            makeDefaultCtor(cw);
+            cw.visitEnd();
+            return cw.toByteArray();
+        }
+
+        byte[] getAnonymousClasses(boolean isInner) {
+            String name = (isInner ? innerName : outerName);
+            ClassWriter cw = new ClassWriter(0);
+            cw.visit(V1_7, ACC_PUBLIC + ACC_SUPER, name, null, "java/lang/Object", null);
+
+            cw.visitInnerClass(innerName, null, null, ACC_PUBLIC | ACC_STATIC);
+            makeCtxk(cw, isInner);
+
+            makeDefaultCtor(cw);
+            cw.visitEnd();
+            return cw.toByteArray();
+        }
+    }
+}