1 /*
   2  * Copyright (c) 2015, 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 /* @test
  25  * @bug 8057919
  26  * @summary Class.getSimpleName() should work for non-JLS compliant class names
  27  */
  28 import jdk.internal.org.objectweb.asm.*;
  29 import static jdk.internal.org.objectweb.asm.Opcodes.*;
  30 
  31 public class GetSimpleNameTest {
  32     static class NestedClass {}
  33     class InnerClass {}
  34 
  35     static Class<?> f1() {
  36         class LocalClass {}
  37         return LocalClass.class;
  38     }
  39 
  40     public static void main(String[] args) throws Exception {
  41         assertEquals(NestedClass.class.getSimpleName(), "NestedClass");
  42         assertEquals( InnerClass.class.getSimpleName(),  "InnerClass");
  43         assertEquals(             f1().getSimpleName(),  "LocalClass");
  44 
  45         java.io.Serializable anon = new java.io.Serializable() {};
  46         assertEquals(anon.getClass().getSimpleName(), "");
  47 
  48         // Java class names, prepended enclosing class name.
  49         testNested("p.Outer$Nested", "p.Outer", "Nested");
  50         testInner( "p.Outer$Inner",  "p.Inner",  "Inner");
  51         testLocal( "p.Outer$1Local", "p.Outer",  "Local");
  52         testAnon(  "p.Outer$1",      "p.Outer",       "");
  53 
  54         // Non-Java class names, prepended enclosing class name.
  55         testNested("p.$C1$Nested", "p.$C1$", "Nested");
  56         testInner( "p.$C1$Inner",  "p.$C1$",  "Inner");
  57         testLocal( "p.$C1$Local",  "p.$C1$",  "Local");
  58         testAnon(  "p.$C1$1",      "p.$C1$",       "");
  59 
  60         // Non-Java class names, unrelated class names.
  61         testNested("p1.$Nested$", "p2.$C1$", "Nested");
  62         testInner( "p1.$Inner$",  "p2.$C1$",  "Inner");
  63         testLocal( "p1.$Local$",  "p2.$C1$",  "Local");
  64         testAnon(  "p1.$anon$",   "p2.$C1$",       "");
  65     }
  66 
  67     static void testNested(String innerName, String outerName, String simpleName) throws Exception {
  68         BytecodeGenerator bg = new BytecodeGenerator(innerName, outerName, simpleName);
  69         CustomCL cl = new CustomCL(innerName, outerName, bg.getNestedClasses(true), bg.getNestedClasses(false));
  70         assertEquals(cl.loadClass(innerName).getSimpleName(), simpleName);
  71     }
  72 
  73     static void testInner(String innerName, String outerName, String simpleName) throws Exception {
  74         BytecodeGenerator bg = new BytecodeGenerator(innerName, outerName, simpleName);
  75         CustomCL cl = new CustomCL(innerName, outerName, bg.getInnerClasses(true), bg.getInnerClasses(false));
  76         assertEquals(cl.loadClass(innerName).getSimpleName(), simpleName);
  77     }
  78 
  79     static void testLocal(String innerName, String outerName, String simpleName) throws Exception {
  80         BytecodeGenerator bg = new BytecodeGenerator(innerName, outerName, simpleName);
  81         CustomCL cl = new CustomCL(innerName, outerName, bg.getLocalClasses(true), bg.getLocalClasses(false));
  82         assertEquals(cl.loadClass(innerName).getSimpleName(), simpleName);
  83     }
  84 
  85     static void testAnon(String innerName, String outerName, String simpleName) throws Exception {
  86         BytecodeGenerator bg = new BytecodeGenerator(innerName, outerName, simpleName);
  87         CustomCL cl = new CustomCL(innerName, outerName, bg.getAnonymousClasses(true), bg.getAnonymousClasses(false));
  88         assertEquals(cl.loadClass(innerName).getSimpleName(), simpleName);
  89     }
  90 
  91     static void assertEquals(Object o1, Object o2) {
  92         if (!java.util.Objects.equals(o1, o2)) {
  93             throw new AssertionError(o1 + " != " + o2);
  94         }
  95     }
  96 
  97     static class CustomCL extends ClassLoader {
  98         final String  innerName;
  99         final String  outerName;
 100 
 101         final byte[] innerClassFile;
 102         final byte[] outerClassFile;
 103 
 104         CustomCL(String innerName, String outerName, byte[] innerClassFile, byte[] outerClassFile) {
 105             this.innerName = innerName;
 106             this.outerName = outerName;
 107             this.innerClassFile = innerClassFile;
 108             this.outerClassFile = outerClassFile;
 109         }
 110         @Override
 111         protected Class<?> findClass(String name) throws ClassNotFoundException {
 112             if (innerName.equals(name)) {
 113                 return defineClass(innerName, innerClassFile, 0, innerClassFile.length);
 114             } else if (outerName.equals(name)) {
 115                 return defineClass(outerName, outerClassFile, 0, outerClassFile.length);
 116             } else {
 117                 throw new ClassNotFoundException(name);
 118             }
 119         }
 120     }
 121 
 122     static class BytecodeGenerator {
 123         final String innerName;
 124         final String outerName;
 125         final String simpleName;
 126 
 127         BytecodeGenerator(String innerName, String outerName, String simpleName) {
 128             this.innerName = intl(innerName);
 129             this.outerName = intl(outerName);
 130             this.simpleName = simpleName;
 131         }
 132 
 133         static String intl(String name) { return name.replace('.', '/'); }
 134 
 135         static void makeDefaultCtor(ClassWriter cw) {
 136             MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
 137             mv.visitCode();
 138             mv.visitVarInsn(ALOAD, 0);
 139             mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
 140             mv.visitInsn(RETURN);
 141             mv.visitMaxs(1, 1);
 142             mv.visitEnd();
 143         }
 144 
 145         void makeCtxk(ClassWriter cw, boolean isInner) {
 146             if (isInner) {
 147                 cw.visitOuterClass(outerName, "f", "()V");
 148             } else {
 149                 MethodVisitor mv = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, "f", "()V", null, null);
 150                 mv.visitCode();
 151                 mv.visitInsn(RETURN);
 152                 mv.visitMaxs(0, 0);
 153                 mv.visitEnd();
 154             }
 155         }
 156 
 157         byte[] getNestedClasses(boolean isInner) {
 158             String name = (isInner ? innerName : outerName);
 159             ClassWriter cw = new ClassWriter(0);
 160             cw.visit(V1_7, ACC_PUBLIC + ACC_SUPER, name, null, "java/lang/Object", null);
 161 
 162             cw.visitInnerClass(innerName, outerName, simpleName, ACC_PUBLIC | ACC_STATIC);
 163 
 164             makeDefaultCtor(cw);
 165             cw.visitEnd();
 166             return cw.toByteArray();
 167         }
 168 
 169         byte[] getInnerClasses(boolean isInner) {
 170             String name = (isInner ? innerName : outerName);
 171             ClassWriter cw = new ClassWriter(0);
 172             cw.visit(V1_7, ACC_PUBLIC + ACC_SUPER, name, null, "java/lang/Object", null);
 173 
 174             cw.visitInnerClass(innerName, outerName, simpleName, ACC_PUBLIC);
 175 
 176             makeDefaultCtor(cw);
 177             cw.visitEnd();
 178             return cw.toByteArray();
 179         }
 180 
 181         byte[] getLocalClasses(boolean isInner) {
 182             String name = (isInner ? innerName : outerName);
 183             ClassWriter cw = new ClassWriter(0);
 184             cw.visit(V1_7, ACC_PUBLIC + ACC_SUPER, name, null, "java/lang/Object", null);
 185 
 186             cw.visitInnerClass(innerName, null, simpleName, ACC_PUBLIC | ACC_STATIC);
 187             makeCtxk(cw, isInner);
 188 
 189             makeDefaultCtor(cw);
 190             cw.visitEnd();
 191             return cw.toByteArray();
 192         }
 193 
 194         byte[] getAnonymousClasses(boolean isInner) {
 195             String name = (isInner ? innerName : outerName);
 196             ClassWriter cw = new ClassWriter(0);
 197             cw.visit(V1_7, ACC_PUBLIC + ACC_SUPER, name, null, "java/lang/Object", null);
 198 
 199             cw.visitInnerClass(innerName, null, null, ACC_PUBLIC | ACC_STATIC);
 200             makeCtxk(cw, isInner);
 201 
 202             makeDefaultCtor(cw);
 203             cw.visitEnd();
 204             return cw.toByteArray();
 205         }
 206     }
 207 }