mcimadamore@550: /* ohair@554: * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. mcimadamore@550: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. mcimadamore@550: * mcimadamore@550: * This code is free software; you can redistribute it and/or modify it mcimadamore@550: * under the terms of the GNU General Public License version 2 only, as mcimadamore@550: * published by the Free Software Foundation. mcimadamore@550: * mcimadamore@550: * This code is distributed in the hope that it will be useful, but WITHOUT mcimadamore@550: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or mcimadamore@550: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License mcimadamore@550: * version 2 for more details (a copy is included in the LICENSE file that mcimadamore@550: * accompanied this code). mcimadamore@550: * mcimadamore@550: * You should have received a copy of the GNU General Public License version mcimadamore@550: * 2 along with this work; if not, write to the Free Software Foundation, mcimadamore@550: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. mcimadamore@550: * ohair@554: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@554: * or visit www.oracle.com if you need additional information or have any ohair@554: * questions. mcimadamore@550: */ mcimadamore@550: mcimadamore@550: /* mcimadamore@550: * @test mcimadamore@550: * @bug 6943289 mcimadamore@550: * @summary Project Coin: Improved Exception Handling for Java (aka 'multicatch') mcimadamore@550: * @run main Pos05 mcimadamore@550: */ mcimadamore@550: mcimadamore@550: import com.sun.tools.classfile.Attribute; mcimadamore@550: import com.sun.tools.classfile.ClassFile; mcimadamore@550: import com.sun.tools.classfile.Code_attribute; mcimadamore@550: import com.sun.tools.classfile.Code_attribute.Exception_data; mcimadamore@550: import com.sun.tools.classfile.Method; mcimadamore@550: import java.io.*; mcimadamore@550: mcimadamore@550: public class Pos05 { mcimadamore@550: mcimadamore@550: static class Pos05sub { mcimadamore@550: mcimadamore@550: class A extends Exception {} mcimadamore@550: class B extends Exception {} mcimadamore@550: class C extends Exception {} mcimadamore@550: mcimadamore@550: void test(boolean b1, boolean b2) { mcimadamore@550: try { mcimadamore@550: if (b1) { mcimadamore@550: throw new A(); mcimadamore@550: } mcimadamore@550: else if (b2) { mcimadamore@550: throw new B(); mcimadamore@550: } mcimadamore@550: else { mcimadamore@550: throw new C(); mcimadamore@550: } mcimadamore@550: } mcimadamore@550: catch (final A | B | C ex) { mcimadamore@550: System.out.println("Exception caught"); mcimadamore@550: } mcimadamore@550: } mcimadamore@550: } mcimadamore@550: mcimadamore@550: static final int TYPES_IN_MULTICATCH = 3; mcimadamore@550: static final String SUBTEST_NAME = Pos05sub.class.getName() + ".class"; mcimadamore@550: static final String TEST_METHOD_NAME = "test"; mcimadamore@550: mcimadamore@550: public static void main(String... args) throws Exception { mcimadamore@550: new Pos05().run(); mcimadamore@550: } mcimadamore@550: mcimadamore@550: public void run() throws Exception { mcimadamore@550: String workDir = System.getProperty("test.classes"); mcimadamore@550: File compiledTest = new File(workDir, SUBTEST_NAME); mcimadamore@550: verifyMulticatchExceptionRanges(compiledTest); mcimadamore@550: } mcimadamore@550: mcimadamore@550: void verifyMulticatchExceptionRanges(File f) { mcimadamore@550: System.err.println("verify: " + f); mcimadamore@550: try { mcimadamore@550: int count = 0; mcimadamore@550: ClassFile cf = ClassFile.read(f); mcimadamore@550: Method testMethod = null; mcimadamore@550: for (Method m : cf.methods) { mcimadamore@550: if (m.getName(cf.constant_pool).equals(TEST_METHOD_NAME)) { mcimadamore@550: testMethod = m; mcimadamore@550: break; mcimadamore@550: } mcimadamore@550: } mcimadamore@550: if (testMethod == null) { mcimadamore@550: throw new Error("Test method not found"); mcimadamore@550: } mcimadamore@550: Code_attribute ea = (Code_attribute)testMethod.attributes.get(Attribute.Code); mcimadamore@550: if (testMethod == null) { mcimadamore@550: throw new Error("Code attribute for test() method not found"); mcimadamore@550: } mcimadamore@550: Exception_data firstExceptionTable = null; mcimadamore@550: for (int i = 0 ; i < ea.exception_table_langth; i++) { mcimadamore@550: if (firstExceptionTable == null) { mcimadamore@550: firstExceptionTable = ea.exception_table[i]; mcimadamore@550: } mcimadamore@550: if (ea.exception_table[i].handler_pc != firstExceptionTable.handler_pc || mcimadamore@550: ea.exception_table[i].start_pc != firstExceptionTable.start_pc || mcimadamore@550: ea.exception_table[i].end_pc != firstExceptionTable.end_pc) { mcimadamore@550: throw new Error("Multiple overlapping catch clause found in generated code"); mcimadamore@550: } mcimadamore@550: count++; mcimadamore@550: } mcimadamore@550: if (count != TYPES_IN_MULTICATCH) { mcimadamore@550: throw new Error("Wrong number of exception data found: " + count); mcimadamore@550: } mcimadamore@550: } catch (Exception e) { mcimadamore@550: e.printStackTrace(); mcimadamore@550: throw new Error("error reading " + f +": " + e); mcimadamore@550: } mcimadamore@550: } mcimadamore@550: }