Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/Class/forName/NonJavaNames.java
38828 views
/*1* Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* Used by NonJavaNames.sh; needs to be run with a classpath including25* test/java/lang/Class/forName/classes26*/2728public class NonJavaNames {29public static class Baz {30public Baz(){}31}3233public static interface myInterface {34}3536NonJavaNames.myInterface create(){37// With target 1.5, this class's name will include a '+'38// instead of a '$'.39class Baz2 implements NonJavaNames.myInterface {40public Baz2(){}41}4243return new Baz2();44}4546public static void main(String[] args) throws Exception {47NonJavaNames.Baz bz = new NonJavaNames.Baz();4849String name;5051if (Class.forName(name=bz.getClass().getName()) != NonJavaNames.Baz.class) {52System.err.println("Class object from forName does not match object.class.");53System.err.println("Failures for class ``" + name + "''.");54throw new RuntimeException();55}5657NonJavaNames.myInterface bz2 = (new NonJavaNames()).create();58if (Class.forName(name=bz2.getClass().getName()) != bz2.getClass()) {59System.err.println("Class object from forName does not match getClass.");60System.err.println("Failures for class ``" + name + "''.");61throw new RuntimeException();62}6364String goodNonJavaClassNames [] = {65",",66"+",67"-",68"0",69"3",70// ":", These names won't work under windows.71// "<",72// ">",73"Z",74"]"75};7677for(String s : goodNonJavaClassNames) {78System.out.println("Testing good class name ``" + s + "''");79Class.forName(s);80}8182String badNonJavaClassNames [] = {83";",84"[",85"."86};8788for(String s : badNonJavaClassNames) {89System.out.println("Testing bad class name ``" + s + "''");90try {91Class.forName(s);92} catch (Exception e) {93// Expected behavior94continue;95}96throw new RuntimeException("Bad class name ``" + s + "'' accepted.");97}98}99}100101102