< prev index next >

test/jdk/java/lang/Class/forName/NonJavaNames.java

Print this page

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 /*
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 }
101 

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 import java.io.IOException;
25 import java.nio.file.Files;
26 import java.nio.file.Path;
27 import java.nio.file.Paths;
28 
29 import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
30 import org.testng.annotations.BeforeClass;
31 import org.testng.annotations.Test;
32 
33 /*
34  * @test
35  * @bug 4952558
36  * @library /test/lib
37  * @build NonJavaNames
38  * @run testng/othervm NonJavaNames
39  * @summary Verify names that aren't legal Java names are accepted by forName.
40  */
41 
42 public class NonJavaNames {
43     public static class Baz {
44         public Baz(){}
45     }
46 
47     public static interface myInterface {
48     }
49 
50      NonJavaNames.myInterface create(){
51          // With target 1.5, this class's name will include a '+'
52          // instead of a '$'.
53          class Baz2 implements NonJavaNames.myInterface {
54              public Baz2(){}
55          }
56 
57         return new Baz2();
58      }
59 
60     private static final String SRC_DIR = System.getProperty("test.src");
61     private static final String TEST_SRC = SRC_DIR + "/classes/";
62     private static final String TEST_CLASSES = System.getProperty("test.classes", ".");
63     Path dhyphenPath, dcommaPath, dperiodPath, dleftsquarePath, drightsquarePath, dplusPath, dsemicolonPath, dzeroPath, dthreePath, dzadePath;
64 
65     @BeforeClass
66     public void createInvalidNameClasses() throws IOException {
67         Path hyphenPath = Paths.get(TEST_SRC + "hyphen.class");
68         Path commaPath = Paths.get(TEST_SRC + "comma.class");
69         Path periodPath = Paths.get(TEST_SRC + "period.class");
70         Path leftsquarePath = Paths.get(TEST_SRC + "left-square.class");
71         Path rightsquarePath = Paths.get(TEST_SRC + "right-square.class");
72         Path plusPath = Paths.get(TEST_SRC + "plus.class");
73         Path semicolonPath = Paths.get(TEST_SRC + "semicolon.class");
74         Path zeroPath = Paths.get(TEST_SRC + "0.class");
75         Path threePath = Paths.get(TEST_SRC + "3.class");
76         Path zadePath = Paths.get(TEST_SRC + "Z.class");
77 
78         dhyphenPath = Paths.get(TEST_CLASSES + "/-.class");
79         dcommaPath = Paths.get(TEST_CLASSES + "/,.class");
80         dperiodPath = Paths.get(TEST_CLASSES + "/..class");
81         dleftsquarePath = Paths.get(TEST_CLASSES + "/[.class");
82         drightsquarePath = Paths.get(TEST_CLASSES + "/].class");
83         dplusPath = Paths.get(TEST_CLASSES + "/+.class");
84         dsemicolonPath = Paths.get(TEST_CLASSES + "/;.class");
85         dzeroPath = Paths.get(TEST_CLASSES + "/0.class");
86         dthreePath = Paths.get(TEST_CLASSES + "/3.class");
87         dzadePath = Paths.get(TEST_CLASSES + "/Z.class");
88 
89         Files.copy(hyphenPath, dhyphenPath, REPLACE_EXISTING);
90         Files.copy(commaPath, dcommaPath, REPLACE_EXISTING);
91         Files.copy(periodPath, dperiodPath, REPLACE_EXISTING);
92         Files.copy(leftsquarePath, dleftsquarePath, REPLACE_EXISTING);
93         Files.copy(rightsquarePath, drightsquarePath, REPLACE_EXISTING);
94         Files.copy(plusPath, dplusPath, REPLACE_EXISTING);
95         Files.copy(semicolonPath, dsemicolonPath, REPLACE_EXISTING);
96         Files.copy(zeroPath, dzeroPath, REPLACE_EXISTING);
97         Files.copy(threePath, dthreePath, REPLACE_EXISTING);
98         Files.copy(zadePath, dzadePath, REPLACE_EXISTING);
99     }
100 
101     @Test
102     public void testGoodNonJavaClassNames() throws Throwable {
103         NonJavaNames.Baz bz = new NonJavaNames.Baz();
104         String name;
105 
106         if (Class.forName(name=bz.getClass().getName()) != NonJavaNames.Baz.class) {
107             System.err.println("Class object from forName does not match object.class.");
108             System.err.println("Failures for class ``" + name + "''.");
109             throw new RuntimeException();
110         }
111 
112         NonJavaNames.myInterface bz2 = (new NonJavaNames()).create();
113         if (Class.forName(name=bz2.getClass().getName()) != bz2.getClass()) {
114             System.err.println("Class object from forName does not match getClass.");
115             System.err.println("Failures for class ``" + name + "''.");
116             throw new RuntimeException();
117         }
118 
119         String goodNonJavaClassNames []  = {
120                 ",",
121                 "+",
122                 "-",
123                 "0",
124                 "3",
125                 // ":", These names won't work under windows.
126                 // "<",
127                 // ">",
128                 "Z",
129                 "]"
130         };
131 
132         for(String s : goodNonJavaClassNames) {
133             System.out.println("Testing good class name ``" + s + "''");
134             Class.forName(s);
135         }
136     }
137 
138     @Test
139     public void testBadNonJavaClassNames() {
140         String badNonJavaClassNames [] = {";", "[", "."};


141 
142         for(String s : badNonJavaClassNames) {
143             System.out.println("Testing bad class name ``" + s + "''");
144             try {
145                 Class.forName(s);
146             } catch (Exception e) {
147                 // Expected behavior
148                 continue;
149             }
150             throw new RuntimeException("Bad class name ``" + s + "'' accepted.");
151         }
152     }
153 }
154 
< prev index next >