BadClassFiles.java revision 8289:cb8946eda85b
1/*
2 * Copyright (c) 2013, 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/*
25 * @test
26 * @run main BadClassFiles
27 * @summary The reflection API should throw the correct exceptions.
28 */
29import java.lang.Class;
30import java.lang.reflect.Method;
31import java.lang.reflect.Parameter;
32import java.lang.reflect.MalformedParametersException;
33import java.lang.ClassLoader;
34import java.lang.ClassNotFoundException;
35import java.io.File;
36import java.io.FileOutputStream;
37import java.io.IOException;
38
39public class BadClassFiles {
40    private int errors = 0;
41
42
43    /* Class files were created by compiling the following source and
44     * then editing it:
45     *
46     * public class EmptyName {
47     *     public void m(int a, int b) {}
48     * }
49     *
50     */
51    private static final byte[] EmptyName_bytes = {
52        -54,-2,-70,-66,0,0,0,52,
53        0,18,10,0,3,0,15,7,
54        0,16,7,0,17,1,0,6,
55        60,105,110,105,116,62,1,0,
56        3,40,41,86,1,0,4,67,
57        111,100,101,1,0,15,76,105,
58        110,101,78,117,109,98,101,114,
59        84,97,98,108,101,1,0,1,
60        109,1,0,5,40,73,73,41,
61        86,1,0,16,77,101,116,104,
62        111,100,80,97,114,97,109,101,
63        116,101,114,115,1,0,0,1,
64        0,1,98,1,0,10,83,111,
65        117,114,99,101,70,105,108,101,
66        1,0,14,69,109,112,116,121,
67        78,97,109,101,46,106,97,118,
68        97,12,0,4,0,5,1,0,
69        9,69,109,112,116,121,78,97,
70        109,101,1,0,16,106,97,118,
71        97,47,108,97,110,103,47,79,
72        98,106,101,99,116,0,33,0,
73        2,0,3,0,0,0,0,0,
74        2,0,1,0,4,0,5,0,
75        1,0,6,0,0,0,29,0,
76        1,0,1,0,0,0,5,42,
77        -73,0,1,-79,0,0,0,1,
78        0,7,0,0,0,6,0,1,
79        0,0,0,1,0,1,0,8,
80        0,9,0,2,0,6,0,0,
81        0,25,0,0,0,3,0,0,
82        0,1,-79,0,0,0,1,0,
83        7,0,0,0,6,0,1,0,
84        0,0,2,0,10,0,0,0,
85        9,2,0,11,0,0,0,12,
86        0,0,0,1,0,13,0,0,
87        0,2,0,14
88    };
89
90    private static final byte[] BadModifiers_bytes = {
91        -54,-2,-70,-66,0,0,0,52,
92        0,18,10,0,3,0,15,7,
93        0,16,7,0,17,1,0,6,
94        60,105,110,105,116,62,1,0,
95        3,40,41,86,1,0,4,67,
96        111,100,101,1,0,15,76,105,
97        110,101,78,117,109,98,101,114,
98        84,97,98,108,101,1,0,1,
99        109,1,0,5,40,73,73,41,
100        86,1,0,16,77,101,116,104,
101        111,100,80,97,114,97,109,101,
102        116,101,114,115,1,0,1,97,
103        1,0,1,98,1,0,10,83,
104        111,117,114,99,101,70,105,108,
105        101,1,0,17,66,97,100,77,
106        111,100,105,102,105,101,114,115,
107        46,106,97,118,97,12,0,4,
108        0,5,1,0,12,66,97,100,
109        77,111,100,105,102,105,101,114,
110        115,1,0,16,106,97,118,97,
111        47,108,97,110,103,47,79,98,
112        106,101,99,116,0,33,0,2,
113        0,3,0,0,0,0,0,2,
114        0,1,0,4,0,5,0,1,
115        0,6,0,0,0,29,0,1,
116        0,1,0,0,0,5,42,-73,
117        0,1,-79,0,0,0,1,0,
118        7,0,0,0,6,0,1,0,
119        0,0,1,0,1,0,8,0,
120        9,0,2,0,6,0,0,0,
121        25,0,0,0,3,0,0,0,
122        1,-79,0,0,0,1,0,7,
123        0,0,0,6,0,1,0,0,
124        0,2,0,10,0,0,0,9,
125        2,0,11,0,51,51,12,0,
126        0,0,1,0,13,0,0,0,
127        2,0,14
128    };
129
130    private static final byte[] BadNameIndex_bytes = {
131        -54,-2,-70,-66,0,0,0,52,
132        0,18,10,0,3,0,15,7,
133        0,16,7,0,17,1,0,6,
134        60,105,110,105,116,62,1,0,
135        3,40,41,86,1,0,4,67,
136        111,100,101,1,0,15,76,105,
137        110,101,78,117,109,98,101,114,
138        84,97,98,108,101,1,0,1,
139        109,1,0,5,40,73,73,41,
140        86,1,0,16,77,101,116,104,
141        111,100,80,97,114,97,109,101,
142        116,101,114,115,1,0,1,97,
143        1,0,1,98,1,0,10,83,
144        111,117,114,99,101,70,105,108,
145        101,1,0,17,66,97,100,78,
146        97,109,101,73,110,100,101,120,
147        46,106,97,118,97,12,0,4,
148        0,5,1,0,12,66,97,100,
149        78,97,109,101,73,110,100,101,
150        120,1,0,16,106,97,118,97,
151        47,108,97,110,103,47,79,98,
152        106,101,99,116,0,33,0,2,
153        0,3,0,0,0,0,0,2,
154        0,1,0,4,0,5,0,1,
155        0,6,0,0,0,29,0,1,
156        0,1,0,0,0,5,42,-73,
157        0,1,-79,0,0,0,1,0,
158        7,0,0,0,6,0,1,0,
159        0,0,1,0,1,0,8,0,
160        9,0,2,0,6,0,0,0,
161        25,0,0,0,3,0,0,0,
162        1,-79,0,0,0,1,0,7,
163        0,0,0,6,0,1,0,0,
164        0,2,0,10,0,0,0,9,
165        2,0,1,0,0,0,12,0,
166        0,0,1,0,13,0,0,0,
167        2,0,14
168    };
169
170    private static final byte[] NameIndexOutOfBounds_bytes = {
171        -54,-2,-70,-66,0,0,0,52,
172        0,18,10,0,3,0,15,7,
173        0,16,7,0,17,1,0,6,
174        60,105,110,105,116,62,1,0,
175        3,40,41,86,1,0,4,67,
176        111,100,101,1,0,15,76,105,
177        110,101,78,117,109,98,101,114,
178        84,97,98,108,101,1,0,1,
179        109,1,0,5,40,73,73,41,
180        86,1,0,16,77,101,116,104,
181        111,100,80,97,114,97,109,101,
182        116,101,114,115,1,0,1,97,
183        1,0,1,98,1,0,10,83,
184        111,117,114,99,101,70,105,108,
185        101,1,0,25,78,97,109,101,
186        73,110,100,101,120,79,117,116,
187        79,102,66,111,117,110,100,115,
188        46,106,97,118,97,12,0,4,
189        0,5,1,0,20,78,97,109,
190        101,73,110,100,101,120,79,117,
191        116,79,102,66,111,117,110,100,
192        115,1,0,16,106,97,118,97,
193        47,108,97,110,103,47,79,98,
194        106,101,99,116,0,33,0,2,
195        0,3,0,0,0,0,0,2,
196        0,1,0,4,0,5,0,1,
197        0,6,0,0,0,29,0,1,
198        0,1,0,0,0,5,42,-73,
199        0,1,-79,0,0,0,1,0,
200        7,0,0,0,6,0,1,0,
201        0,0,1,0,1,0,8,0,
202        9,0,2,0,6,0,0,0,
203        25,0,0,0,3,0,0,0,
204        1,-79,0,0,0,1,0,7,
205        0,0,0,6,0,1,0,0,
206        0,2,0,10,0,0,0,9,
207        2,0,-1,0,0,0,12,0,
208        0,0,1,0,13,0,0,0,
209        2,0,14
210
211    };
212
213    private static final byte[] ExtraParams_bytes = {
214        -54,-2,-70,-66,0,0,0,52,
215        0,18,10,0,3,0,15,7,
216        0,16,7,0,17,1,0,6,
217        60,105,110,105,116,62,1,0,
218        3,40,41,86,1,0,4,67,
219        111,100,101,1,0,15,76,105,
220        110,101,78,117,109,98,101,114,
221        84,97,98,108,101,1,0,1,
222        109,1,0,5,40,73,73,41,
223        86,1,0,16,77,101,116,104,
224        111,100,80,97,114,97,109,101,
225        116,101,114,115,1,0,1,97,
226        1,0,1,98,1,0,10,83,
227        111,117,114,99,101,70,105,108,
228        101,1,0,16,69,120,116,114,
229        97,80,97,114,97,109,115,46,
230        106,97,118,97,12,0,4,0,
231        5,1,0,11,69,120,116,114,
232        97,80,97,114,97,109,115,1,
233        0,16,106,97,118,97,47,108,
234        97,110,103,47,79,98,106,101,
235        99,116,0,33,0,2,0,3,
236        0,0,0,0,0,2,0,1,
237        0,4,0,5,0,1,0,6,
238        0,0,0,29,0,1,0,1,
239        0,0,0,5,42,-73,0,1,
240        -79,0,0,0,1,0,7,0,
241        0,0,6,0,1,0,0,0,
242        1,0,1,0,8,0,9,0,
243        2,0,6,0,0,0,25,0,
244        0,0,3,0,0,0,1,-79,
245        0,0,0,1,0,7,0,0,
246        0,6,0,1,0,0,0,2,
247        0,10,0,0,0,13,3,0,
248        11,0,0,0,12,0,0,0,
249        11,0,0,0,1,0,13,0,
250        0,0,2,0,14
251    };
252
253    private static final byte[] BadName1_bytes = {
254        -54,-2,-70,-66,0,0,0,52,
255        0,18,10,0,3,0,15,7,
256        0,16,7,0,17,1,0,6,
257        60,105,110,105,116,62,1,0,
258        3,40,41,86,1,0,4,67,
259        111,100,101,1,0,15,76,105,
260        110,101,78,117,109,98,101,114,
261        84,97,98,108,101,1,0,1,
262        109,1,0,5,40,73,73,41,
263        86,1,0,16,77,101,116,104,
264        111,100,80,97,114,97,109,101,
265        116,101,114,115,1,0,1,46,
266        1,0,1,98,1,0,10,83,
267        111,117,114,99,101,70,105,108,
268        101,1,0,13,66,97,100,78,
269        97,109,101,49,46,106,97,118,
270        97,12,0,4,0,5,1,0,
271        8,66,97,100,78,97,109,101,
272        49,1,0,16,106,97,118,97,
273        47,108,97,110,103,47,79,98,
274        106,101,99,116,0,33,0,2,
275        0,3,0,0,0,0,0,2,
276        0,1,0,4,0,5,0,1,
277        0,6,0,0,0,29,0,1,
278        0,1,0,0,0,5,42,-73,
279        0,1,-79,0,0,0,1,0,
280        7,0,0,0,6,0,1,0,
281        0,0,1,0,1,0,8,0,
282        9,0,2,0,6,0,0,0,
283        25,0,0,0,3,0,0,0,
284        1,-79,0,0,0,1,0,7,
285        0,0,0,6,0,1,0,0,
286        0,2,0,10,0,0,0,9,
287        2,0,11,0,0,0,12,0,
288        0,0,1,0,13,0,0,0,
289        2,0,14
290    };
291
292    private static final byte[] BadName2_bytes = {
293        -54,-2,-70,-66,0,0,0,52,
294        0,18,10,0,3,0,15,7,
295        0,16,7,0,17,1,0,6,
296        60,105,110,105,116,62,1,0,
297        3,40,41,86,1,0,4,67,
298        111,100,101,1,0,15,76,105,
299        110,101,78,117,109,98,101,114,
300        84,97,98,108,101,1,0,1,
301        109,1,0,5,40,73,73,41,
302        86,1,0,16,77,101,116,104,
303        111,100,80,97,114,97,109,101,
304        116,101,114,115,1,0,1,91,
305        1,0,1,98,1,0,10,83,
306        111,117,114,99,101,70,105,108,
307        101,1,0,13,66,97,100,78,
308        97,109,101,50,46,106,97,118,
309        97,12,0,4,0,5,1,0,
310        8,66,97,100,78,97,109,101,
311        50,1,0,16,106,97,118,97,
312        47,108,97,110,103,47,79,98,
313        106,101,99,116,0,33,0,2,
314        0,3,0,0,0,0,0,2,
315        0,1,0,4,0,5,0,1,
316        0,6,0,0,0,29,0,1,
317        0,1,0,0,0,5,42,-73,
318        0,1,-79,0,0,0,1,0,
319        7,0,0,0,6,0,1,0,
320        0,0,1,0,1,0,8,0,
321        9,0,2,0,6,0,0,0,
322        25,0,0,0,3,0,0,0,
323        1,-79,0,0,0,1,0,7,
324        0,0,0,6,0,1,0,0,
325        0,2,0,10,0,0,0,9,
326        2,0,11,0,0,0,12,0,
327        0,0,1,0,13,0,0,0,
328        2,0,14
329    };
330
331    private static final byte[] BadName3_bytes = {
332        -54,-2,-70,-66,0,0,0,52,
333        0,18,10,0,3,0,15,7,
334        0,16,7,0,17,1,0,6,
335        60,105,110,105,116,62,1,0,
336        3,40,41,86,1,0,4,67,
337        111,100,101,1,0,15,76,105,
338        110,101,78,117,109,98,101,114,
339        84,97,98,108,101,1,0,1,
340        109,1,0,5,40,73,73,41,
341        86,1,0,16,77,101,116,104,
342        111,100,80,97,114,97,109,101,
343        116,101,114,115,1,0,1,59,
344        1,0,1,98,1,0,10,83,
345        111,117,114,99,101,70,105,108,
346        101,1,0,13,66,97,100,78,
347        97,109,101,51,46,106,97,118,
348        97,12,0,4,0,5,1,0,
349        8,66,97,100,78,97,109,101,
350        51,1,0,16,106,97,118,97,
351        47,108,97,110,103,47,79,98,
352        106,101,99,116,0,33,0,2,
353        0,3,0,0,0,0,0,2,
354        0,1,0,4,0,5,0,1,
355        0,6,0,0,0,29,0,1,
356        0,1,0,0,0,5,42,-73,
357        0,1,-79,0,0,0,1,0,
358        7,0,0,0,6,0,1,0,
359        0,0,1,0,1,0,8,0,
360        9,0,2,0,6,0,0,0,
361        25,0,0,0,3,0,0,0,
362        1,-79,0,0,0,1,0,7,
363        0,0,0,6,0,1,0,0,
364        0,2,0,10,0,0,0,9,
365        2,0,11,0,0,0,12,0,
366        0,0,1,0,13,0,0,0,
367        2,0,14
368    };
369
370    private static final byte[] BadName4_bytes = {
371        -54,-2,-70,-66,0,0,0,52,
372        0,18,10,0,3,0,15,7,
373        0,16,7,0,17,1,0,6,
374        60,105,110,105,116,62,1,0,
375        3,40,41,86,1,0,4,67,
376        111,100,101,1,0,15,76,105,
377        110,101,78,117,109,98,101,114,
378        84,97,98,108,101,1,0,1,
379        109,1,0,5,40,73,73,41,
380        86,1,0,16,77,101,116,104,
381        111,100,80,97,114,97,109,101,
382        116,101,114,115,1,0,1,47,
383        1,0,1,98,1,0,10,83,
384        111,117,114,99,101,70,105,108,
385        101,1,0,13,66,97,100,78,
386        97,109,101,52,46,106,97,118,
387        97,12,0,4,0,5,1,0,
388        8,66,97,100,78,97,109,101,
389        52,1,0,16,106,97,118,97,
390        47,108,97,110,103,47,79,98,
391        106,101,99,116,0,33,0,2,
392        0,3,0,0,0,0,0,2,
393        0,1,0,4,0,5,0,1,
394        0,6,0,0,0,29,0,1,
395        0,1,0,0,0,5,42,-73,
396        0,1,-79,0,0,0,1,0,
397        7,0,0,0,6,0,1,0,
398        0,0,1,0,1,0,8,0,
399        9,0,2,0,6,0,0,0,
400        25,0,0,0,3,0,0,0,
401        1,-79,0,0,0,1,0,7,
402        0,0,0,6,0,1,0,0,
403        0,2,0,10,0,0,0,9,
404        2,0,11,0,0,0,12,0,
405        0,0,1,0,13,0,0,0,
406        2,0,14
407    };
408
409    private static class InMemoryClassLoader extends ClassLoader {
410        public Class<?> defineClass(String name, byte[] b) {
411            return defineClass(name, b, 0, b.length);
412        }
413    };
414
415    private static final InMemoryClassLoader loader = new InMemoryClassLoader();
416
417    private final Class<?>[] classes;
418
419    private BadClassFiles() throws ClassNotFoundException {
420        classes = new Class<?>[] {
421            loader.defineClass("EmptyName", EmptyName_bytes),
422            loader.defineClass("BadModifiers", BadModifiers_bytes),
423            loader.defineClass("BadNameIndex", BadNameIndex_bytes),
424            loader.defineClass("NameIndexOutOfBounds", NameIndexOutOfBounds_bytes),
425            loader.defineClass("ExtraParams", ExtraParams_bytes),
426            // Name with .
427            loader.defineClass("BadName1", BadName1_bytes),
428            // Name with [
429            loader.defineClass("BadName2", BadName2_bytes),
430            // Name with ;
431            loader.defineClass("BadName3", BadName3_bytes),
432            // Name with /
433            loader.defineClass("BadName4", BadName4_bytes)
434        };
435    }
436
437    public static void main(String... args)
438        throws NoSuchMethodException, IOException, ClassNotFoundException {
439        new BadClassFiles().run();
440    }
441
442    public void assertBadParameters(Class<?> cls) throws NoSuchMethodException {
443        try {
444            System.err.println("Trying " + cls);
445            final Method method = cls.getMethod("m", int.class, int.class);
446            final Parameter[] params = method.getParameters();
447            System.err.println("Name " + params[0].getName());
448            System.err.println("Did not see expected exception");
449            errors++;
450        } catch(MalformedParametersException e) {
451            System.err.println("Expected exception seen");
452        }
453    }
454
455    public void run() throws NoSuchMethodException {
456        for (Class<?> cls : classes)
457            assertBadParameters(cls);
458
459        if (errors != 0)
460            throw new RuntimeException(errors + " errors in test");
461    }
462}
463