test/tools/javac/multicatch/Pos05.java

Tue, 24 Dec 2013 09:17:37 -0800

author
ksrini
date
Tue, 24 Dec 2013 09:17:37 -0800
changeset 2227
998b10c43157
parent 2034
ac6ec071c2b2
child 2525
2eb010b6cb22
permissions
-rw-r--r--

8029230: Update copyright year to match last edit in jdk8 langtools repository for 2013
Reviewed-by: ksrini
Contributed-by: steve.sides@oracle.com

mcimadamore@550 1 /*
ksrini@2227 2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
mcimadamore@550 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
mcimadamore@550 4 *
mcimadamore@550 5 * This code is free software; you can redistribute it and/or modify it
mcimadamore@550 6 * under the terms of the GNU General Public License version 2 only, as
mcimadamore@550 7 * published by the Free Software Foundation.
mcimadamore@550 8 *
mcimadamore@550 9 * This code is distributed in the hope that it will be useful, but WITHOUT
mcimadamore@550 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
mcimadamore@550 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
mcimadamore@550 12 * version 2 for more details (a copy is included in the LICENSE file that
mcimadamore@550 13 * accompanied this code).
mcimadamore@550 14 *
mcimadamore@550 15 * You should have received a copy of the GNU General Public License version
mcimadamore@550 16 * 2 along with this work; if not, write to the Free Software Foundation,
mcimadamore@550 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
mcimadamore@550 18 *
ohair@554 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554 20 * or visit www.oracle.com if you need additional information or have any
ohair@554 21 * questions.
mcimadamore@550 22 */
mcimadamore@550 23
mcimadamore@550 24 /*
mcimadamore@550 25 * @test
mcimadamore@550 26 * @bug 6943289
mcimadamore@550 27 * @summary Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore@550 28 * @run main Pos05
mcimadamore@550 29 */
mcimadamore@550 30
mcimadamore@550 31 import com.sun.tools.classfile.Attribute;
mcimadamore@550 32 import com.sun.tools.classfile.ClassFile;
mcimadamore@550 33 import com.sun.tools.classfile.Code_attribute;
mcimadamore@550 34 import com.sun.tools.classfile.Code_attribute.Exception_data;
mcimadamore@550 35 import com.sun.tools.classfile.Method;
mcimadamore@550 36 import java.io.*;
mcimadamore@550 37
mcimadamore@550 38 public class Pos05 {
mcimadamore@550 39
mcimadamore@550 40 static class Pos05sub {
mcimadamore@550 41
mcimadamore@550 42 class A extends Exception {}
mcimadamore@550 43 class B extends Exception {}
mcimadamore@550 44 class C extends Exception {}
mcimadamore@550 45
mcimadamore@550 46 void test(boolean b1, boolean b2) {
mcimadamore@550 47 try {
mcimadamore@550 48 if (b1) {
mcimadamore@550 49 throw new A();
mcimadamore@550 50 }
mcimadamore@550 51 else if (b2) {
mcimadamore@550 52 throw new B();
mcimadamore@550 53 }
mcimadamore@550 54 else {
mcimadamore@550 55 throw new C();
mcimadamore@550 56 }
mcimadamore@550 57 }
mcimadamore@550 58 catch (final A | B | C ex) {
mcimadamore@550 59 System.out.println("Exception caught");
mcimadamore@550 60 }
mcimadamore@550 61 }
mcimadamore@550 62 }
mcimadamore@550 63
mcimadamore@550 64 static final int TYPES_IN_MULTICATCH = 3;
mcimadamore@550 65 static final String SUBTEST_NAME = Pos05sub.class.getName() + ".class";
mcimadamore@550 66 static final String TEST_METHOD_NAME = "test";
mcimadamore@550 67
mcimadamore@550 68 public static void main(String... args) throws Exception {
mcimadamore@550 69 new Pos05().run();
mcimadamore@550 70 }
mcimadamore@550 71
mcimadamore@550 72 public void run() throws Exception {
mcimadamore@550 73 String workDir = System.getProperty("test.classes");
mcimadamore@550 74 File compiledTest = new File(workDir, SUBTEST_NAME);
mcimadamore@550 75 verifyMulticatchExceptionRanges(compiledTest);
mcimadamore@550 76 }
mcimadamore@550 77
mcimadamore@550 78 void verifyMulticatchExceptionRanges(File f) {
mcimadamore@550 79 System.err.println("verify: " + f);
mcimadamore@550 80 try {
mcimadamore@550 81 int count = 0;
mcimadamore@550 82 ClassFile cf = ClassFile.read(f);
mcimadamore@550 83 Method testMethod = null;
mcimadamore@550 84 for (Method m : cf.methods) {
mcimadamore@550 85 if (m.getName(cf.constant_pool).equals(TEST_METHOD_NAME)) {
mcimadamore@550 86 testMethod = m;
mcimadamore@550 87 break;
mcimadamore@550 88 }
mcimadamore@550 89 }
mcimadamore@550 90 if (testMethod == null) {
mcimadamore@550 91 throw new Error("Test method not found");
mcimadamore@550 92 }
mcimadamore@550 93 Code_attribute ea = (Code_attribute)testMethod.attributes.get(Attribute.Code);
mcimadamore@550 94 if (testMethod == null) {
mcimadamore@550 95 throw new Error("Code attribute for test() method not found");
mcimadamore@550 96 }
mcimadamore@550 97 Exception_data firstExceptionTable = null;
alundblad@2034 98 for (int i = 0 ; i < ea.exception_table_length; i++) {
mcimadamore@550 99 if (firstExceptionTable == null) {
mcimadamore@550 100 firstExceptionTable = ea.exception_table[i];
mcimadamore@550 101 }
mcimadamore@550 102 if (ea.exception_table[i].handler_pc != firstExceptionTable.handler_pc ||
mcimadamore@550 103 ea.exception_table[i].start_pc != firstExceptionTable.start_pc ||
mcimadamore@550 104 ea.exception_table[i].end_pc != firstExceptionTable.end_pc) {
mcimadamore@550 105 throw new Error("Multiple overlapping catch clause found in generated code");
mcimadamore@550 106 }
mcimadamore@550 107 count++;
mcimadamore@550 108 }
mcimadamore@550 109 if (count != TYPES_IN_MULTICATCH) {
mcimadamore@550 110 throw new Error("Wrong number of exception data found: " + count);
mcimadamore@550 111 }
mcimadamore@550 112 } catch (Exception e) {
mcimadamore@550 113 e.printStackTrace();
mcimadamore@550 114 throw new Error("error reading " + f +": " + e);
mcimadamore@550 115 }
mcimadamore@550 116 }
mcimadamore@550 117 }

mercurial