BadInitMethod.java revision 12158:0fe2815ffa74
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/*
25 * @test
26 * @bug 8130669
27 * @summary VM prohibits <clinit> methods with return values
28 * @compile nonvoidClinit.jasm
29 * @compile clinitNonStatic.jasm
30 * @compile clinitArg.jasm
31 * @compile clinitArg51.jasm
32 * @compile badInit.jasm
33 * @run main/othervm -Xverify:all BadInitMethod
34 */
35
36// Test that non-void <clinit>, non-static <clinit>, and non-void
37// <init> methods cause ClassFormatException's to be thrown.
38public class BadInitMethod {
39    public static void main(String args[]) throws Throwable {
40
41        System.out.println("Regression test for bug 8130669");
42        try {
43            Class newClass = Class.forName("nonvoidClinit");
44            throw new RuntimeException(
45                "Expected ClassFormatError exception for non-void <clinit> not thrown");
46        } catch (java.lang.ClassFormatError e) {
47            System.out.println("Test BadInitMethod passed for non-void <clinit>");
48        }
49
50        try {
51            Class newClass = Class.forName("clinitNonStatic");
52            throw new RuntimeException(
53                "Expected ClassFormatError exception for non-static <clinit> not thrown");
54        } catch (java.lang.ClassFormatError e) {
55            System.out.println("Test BadInitMethod passed for non-static <clinit>");
56        }
57
58        // <clinit> with args is allowed in class file version < 51.
59        try {
60            Class newClass = Class.forName("clinitArg");
61        } catch (java.lang.ClassFormatError e) {
62            throw new RuntimeException(
63                "Unexpected ClassFormatError exception for <clinit> with argument in class file < 51");
64        }
65
66        // <clinit> with args is not allowed in class file version >= 51.
67        try {
68            Class newClass = Class.forName("clinitArg51");
69            throw new RuntimeException(
70                "Expected ClassFormatError exception for <clinit> with argument not thrown");
71        } catch (java.lang.ClassFormatError e) {
72            System.out.println("Test BadInitMethod passed for <clinit> with argument");
73        }
74
75        try {
76            Class newClass = Class.forName("badInit");
77            throw new RuntimeException(
78                "Expected ClassFormatError exception for non-void <init> not thrown");
79        } catch (java.lang.ClassFormatError e) {
80            System.out.println("Test BadInitMethod passed for non-void <init>");
81        }
82    }
83}
84