J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2003 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /* |
| 25 | * Used by NonJavaNames.sh; needs to be run with a classpath including |
| 26 | * test/java/lang/Class/forName/classes |
| 27 | */ |
| 28 | |
| 29 | public class NonJavaNames { |
| 30 | public static class Baz { |
| 31 | public Baz(){} |
| 32 | } |
| 33 | |
| 34 | public static interface myInterface { |
| 35 | } |
| 36 | |
| 37 | NonJavaNames.myInterface create(){ |
| 38 | // With target 1.5, this class's name will include a '+' |
| 39 | // instead of a '$'. |
| 40 | class Baz2 implements NonJavaNames.myInterface { |
| 41 | public Baz2(){} |
| 42 | } |
| 43 | |
| 44 | return new Baz2(); |
| 45 | } |
| 46 | |
| 47 | public static void main(String[] args) throws Exception { |
| 48 | NonJavaNames.Baz bz = new NonJavaNames.Baz(); |
| 49 | |
| 50 | String name; |
| 51 | |
| 52 | if (Class.forName(name=bz.getClass().getName()) != NonJavaNames.Baz.class) { |
| 53 | System.err.println("Class object from forName does not match object.class."); |
| 54 | System.err.println("Failures for class ``" + name + "''."); |
| 55 | throw new RuntimeException(); |
| 56 | } |
| 57 | |
| 58 | NonJavaNames.myInterface bz2 = (new NonJavaNames()).create(); |
| 59 | if (Class.forName(name=bz2.getClass().getName()) != bz2.getClass()) { |
| 60 | System.err.println("Class object from forName does not match getClass."); |
| 61 | System.err.println("Failures for class ``" + name + "''."); |
| 62 | throw new RuntimeException(); |
| 63 | } |
| 64 | |
| 65 | String goodNonJavaClassNames [] = { |
| 66 | ",", |
| 67 | "+", |
| 68 | "-", |
| 69 | "0", |
| 70 | "3", |
| 71 | // ":", These names won't work under windows. |
| 72 | // "<", |
| 73 | // ">", |
| 74 | "Z", |
| 75 | "]" |
| 76 | }; |
| 77 | |
| 78 | for(String s : goodNonJavaClassNames) { |
| 79 | System.out.println("Testing good class name ``" + s + "''"); |
| 80 | Class.forName(s); |
| 81 | } |
| 82 | |
| 83 | String badNonJavaClassNames [] = { |
| 84 | ";", |
| 85 | "[", |
| 86 | "." |
| 87 | }; |
| 88 | |
| 89 | for(String s : badNonJavaClassNames) { |
| 90 | System.out.println("Testing bad class name ``" + s + "''"); |
| 91 | try { |
| 92 | Class.forName(s); |
| 93 | } catch (Exception e) { |
| 94 | // Expected behavior |
| 95 | continue; |
| 96 | } |
| 97 | throw new RuntimeException("Bad class name ``" + s + "'' accepted."); |
| 98 | } |
| 99 | } |
| 100 | } |