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