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