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$", "", "1"); 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 testAnon(innerName, outerName, simpleName, simpleName); 87 } 88 static void testAnon(String innerName, String outerName, String simpleName, String expected) throws Exception { 89 BytecodeGenerator bg = new BytecodeGenerator(innerName, outerName, simpleName); 90 CustomCL cl = new CustomCL(innerName, outerName, bg.getAnonymousClasses(true), bg.getAnonymousClasses(false)); 91 assertEquals(cl.loadClass(innerName).getSimpleName(), expected); 92 } 93 94 static void assertEquals(Object o1, Object o2) { 95 if (!java.util.Objects.equals(o1, o2)) { 96 throw new AssertionError(o1 + " != " + o2); 97 } 98 } 99 100 static class CustomCL extends ClassLoader { 101 final String innerName; 102 final String outerName; 103 104 final byte[] innerClassFile; 105 final byte[] outerClassFile; 106 107 CustomCL(String innerName, String outerName, byte[] innerClassFile, byte[] outerClassFile) { 108 this.innerName = innerName; 109 this.outerName = outerName; 110 this.innerClassFile = innerClassFile; 111 this.outerClassFile = outerClassFile; 112 } 113 @Override 114 protected Class<?> findClass(String name) throws ClassNotFoundException { 115 if (innerName.equals(name)) { 116 return defineClass(innerName, innerClassFile, 0, innerClassFile.length); 117 } else if (outerName.equals(name)) { 118 return defineClass(outerName, outerClassFile, 0, outerClassFile.length); 119 } else { 120 throw new ClassNotFoundException(name); 121 } 122 } 123 } 124 125 static class BytecodeGenerator { 126 final String innerName; 127 final String outerName; 128 final String simpleName; 129 130 BytecodeGenerator(String innerName, String outerName, String simpleName) { 131 this.innerName = intl(innerName); 132 this.outerName = intl(outerName); 133 this.simpleName = simpleName; 134 } 135 136 static String intl(String name) { return name.replace('.', '/'); } 137 138 static void makeDefaultCtor(ClassWriter cw) { 139 MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null); 140 mv.visitCode(); 141 mv.visitVarInsn(ALOAD, 0); 142 mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false); 143 mv.visitInsn(RETURN); 144 mv.visitMaxs(1, 1); 145 mv.visitEnd(); 146 } 147 148 void makeCtxk(ClassWriter cw, boolean isInner) { 149 if (isInner) { 150 cw.visitOuterClass(outerName, "f", "()V"); 151 } else { 152 MethodVisitor mv = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, "f", "()V", null, null); 153 mv.visitCode(); 154 mv.visitInsn(RETURN); 155 mv.visitMaxs(0, 0); 156 mv.visitEnd(); 157 } 158 } 159 160 byte[] getNestedClasses(boolean isInner) { 161 String name = (isInner ? innerName : outerName); 162 ClassWriter cw = new ClassWriter(0); 163 cw.visit(V1_7, ACC_PUBLIC + ACC_SUPER, name, null, "java/lang/Object", null); 164 165 cw.visitInnerClass(innerName, outerName, simpleName, ACC_PUBLIC | ACC_STATIC); 166 167 makeDefaultCtor(cw); 168 cw.visitEnd(); 169 return cw.toByteArray(); 170 } 171 172 byte[] getInnerClasses(boolean isInner) { 173 String name = (isInner ? innerName : outerName); 174 ClassWriter cw = new ClassWriter(0); 175 cw.visit(V1_7, ACC_PUBLIC + ACC_SUPER, name, null, "java/lang/Object", null); 176 177 cw.visitInnerClass(innerName, outerName, simpleName, ACC_PUBLIC); 178 179 makeDefaultCtor(cw); 180 cw.visitEnd(); 181 return cw.toByteArray(); 182 } 183 184 byte[] getLocalClasses(boolean isInner) { 185 String name = (isInner ? innerName : outerName); 186 ClassWriter cw = new ClassWriter(0); 187 cw.visit(V1_7, ACC_PUBLIC + ACC_SUPER, name, null, "java/lang/Object", null); 188 189 cw.visitInnerClass(innerName, null, simpleName, ACC_PUBLIC | ACC_STATIC); 190 makeCtxk(cw, isInner); 191 192 makeDefaultCtor(cw); 193 cw.visitEnd(); 194 return cw.toByteArray(); 195 } 196 197 byte[] getAnonymousClasses(boolean isInner) { 198 String name = (isInner ? innerName : outerName); 199 ClassWriter cw = new ClassWriter(0); 200 cw.visit(V1_7, ACC_PUBLIC + ACC_SUPER, name, null, "java/lang/Object", null); 201 202 cw.visitInnerClass(innerName, null, null, ACC_PUBLIC | ACC_STATIC); 203 makeCtxk(cw, isInner); 204 205 makeDefaultCtor(cw); 206 cw.visitEnd(); 207 return cw.toByteArray(); 208 } 209 } 210 }