test/tools/javac/T7093325.java

Mon, 14 Nov 2011 15:11:10 -0800

author
ksrini
date
Mon, 14 Nov 2011 15:11:10 -0800
changeset 1138
7375d4979bd3
parent 1109
3cdfa97e1be9
child 1482
954541f13717
permissions
-rw-r--r--

7106166: (javac) re-factor EndPos parser
Reviewed-by: jjg

mcimadamore@1109 1 /*
mcimadamore@1109 2 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
mcimadamore@1109 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
mcimadamore@1109 4 *
mcimadamore@1109 5 * This code is free software; you can redistribute it and/or modify it
mcimadamore@1109 6 * under the terms of the GNU General Public License version 2 only, as
mcimadamore@1109 7 * published by the Free Software Foundation.
mcimadamore@1109 8 *
mcimadamore@1109 9 * This code is distributed in the hope that it will be useful, but WITHOUT
mcimadamore@1109 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
mcimadamore@1109 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
mcimadamore@1109 12 * version 2 for more details (a copy is included in the LICENSE file that
mcimadamore@1109 13 * accompanied this code).
mcimadamore@1109 14 *
mcimadamore@1109 15 * You should have received a copy of the GNU General Public License version
mcimadamore@1109 16 * 2 along with this work; if not, write to the Free Software Foundation,
mcimadamore@1109 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
mcimadamore@1109 18 *
mcimadamore@1109 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
mcimadamore@1109 20 * or visit www.oracle.com if you need additional information or have any
mcimadamore@1109 21 * questions.
mcimadamore@1109 22 */
mcimadamore@1109 23
mcimadamore@1109 24 /*
mcimadamore@1109 25 * @test
mcimadamore@1109 26 * @bug 7093325
mcimadamore@1109 27 * @summary Redundant entry in bytecode exception table
mcimadamore@1109 28 */
mcimadamore@1109 29
mcimadamore@1109 30 import com.sun.source.util.JavacTask;
mcimadamore@1109 31 import com.sun.tools.classfile.Attribute;
mcimadamore@1109 32 import com.sun.tools.classfile.ClassFile;
mcimadamore@1109 33 import com.sun.tools.classfile.Code_attribute;
mcimadamore@1109 34 import com.sun.tools.classfile.ConstantPool.*;
mcimadamore@1109 35 import com.sun.tools.classfile.Method;
mcimadamore@1109 36 import com.sun.tools.javac.api.JavacTool;
mcimadamore@1109 37
mcimadamore@1109 38 import java.io.File;
mcimadamore@1109 39 import java.net.URI;
mcimadamore@1109 40 import java.util.Arrays;
mcimadamore@1109 41 import javax.tools.JavaCompiler;
mcimadamore@1109 42 import javax.tools.JavaFileObject;
mcimadamore@1109 43 import javax.tools.SimpleJavaFileObject;
mcimadamore@1109 44 import javax.tools.StandardJavaFileManager;
mcimadamore@1109 45 import javax.tools.ToolProvider;
mcimadamore@1109 46
mcimadamore@1109 47
mcimadamore@1109 48 public class T7093325 {
mcimadamore@1109 49
mcimadamore@1109 50 /** global decls ***/
mcimadamore@1109 51
mcimadamore@1109 52 // Create a single file manager and reuse it for each compile to save time.
mcimadamore@1109 53 static StandardJavaFileManager fm = JavacTool.create().getStandardFileManager(null, null, null);
mcimadamore@1109 54
mcimadamore@1109 55 //statistics
mcimadamore@1109 56 static int checkCount = 0;
mcimadamore@1109 57
mcimadamore@1109 58 enum StatementKind {
mcimadamore@1109 59 THROW("throw new RuntimeException();", false, false),
mcimadamore@1109 60 RETURN_NONEMPTY("System.out.println(); return;", true, false),
mcimadamore@1109 61 RETURN_EMPTY("return;", true, true),
mcimadamore@1109 62 APPLY("System.out.println();", true, false);
mcimadamore@1109 63
mcimadamore@1109 64 String stmt;
mcimadamore@1109 65 boolean canInline;
mcimadamore@1109 66 boolean empty;
mcimadamore@1109 67
mcimadamore@1109 68 private StatementKind(String stmt, boolean canInline, boolean empty) {
mcimadamore@1109 69 this.stmt = stmt;
mcimadamore@1109 70 this.canInline = canInline;
mcimadamore@1109 71 this.empty = empty;
mcimadamore@1109 72 }
mcimadamore@1109 73 }
mcimadamore@1109 74
mcimadamore@1109 75 enum CatchArity {
mcimadamore@1109 76 NONE(""),
mcimadamore@1109 77 ONE("catch (A a) { #S1 }"),
mcimadamore@1109 78 TWO("catch (B b) { #S2 }"),
mcimadamore@1109 79 THREE("catch (C c) { #S3 }"),
mcimadamore@1109 80 FOUR("catch (D d) { #S4 }");
mcimadamore@1109 81
mcimadamore@1109 82 String catchStr;
mcimadamore@1109 83
mcimadamore@1109 84 private CatchArity(String catchStr) {
mcimadamore@1109 85 this.catchStr = catchStr;
mcimadamore@1109 86 }
mcimadamore@1109 87
mcimadamore@1109 88 String catchers() {
mcimadamore@1109 89 if (this.ordinal() == 0) {
mcimadamore@1109 90 return catchStr;
mcimadamore@1109 91 } else {
mcimadamore@1109 92 return CatchArity.values()[this.ordinal() - 1].catchers() + catchStr;
mcimadamore@1109 93 }
mcimadamore@1109 94 }
mcimadamore@1109 95 }
mcimadamore@1109 96
mcimadamore@1109 97 public static void main(String... args) throws Exception {
mcimadamore@1109 98 for (CatchArity ca : CatchArity.values()) {
mcimadamore@1109 99 for (StatementKind stmt0 : StatementKind.values()) {
mcimadamore@1109 100 if (ca.ordinal() == 0) {
mcimadamore@1109 101 new T7093325(ca, stmt0).compileAndCheck();
mcimadamore@1109 102 continue;
mcimadamore@1109 103 }
mcimadamore@1109 104 for (StatementKind stmt1 : StatementKind.values()) {
mcimadamore@1109 105 if (ca.ordinal() == 1) {
mcimadamore@1109 106 new T7093325(ca, stmt0, stmt1).compileAndCheck();
mcimadamore@1109 107 continue;
mcimadamore@1109 108 }
mcimadamore@1109 109 for (StatementKind stmt2 : StatementKind.values()) {
mcimadamore@1109 110 if (ca.ordinal() == 2) {
mcimadamore@1109 111 new T7093325(ca, stmt0, stmt1, stmt2).compileAndCheck();
mcimadamore@1109 112 continue;
mcimadamore@1109 113 }
mcimadamore@1109 114 for (StatementKind stmt3 : StatementKind.values()) {
mcimadamore@1109 115 if (ca.ordinal() == 3) {
mcimadamore@1109 116 new T7093325(ca, stmt0, stmt1, stmt2, stmt3).compileAndCheck();
mcimadamore@1109 117 continue;
mcimadamore@1109 118 }
mcimadamore@1109 119 for (StatementKind stmt4 : StatementKind.values()) {
mcimadamore@1109 120 if (ca.ordinal() == 4) {
mcimadamore@1109 121 new T7093325(ca, stmt0, stmt1, stmt2, stmt3, stmt4).compileAndCheck();
mcimadamore@1109 122 continue;
mcimadamore@1109 123 }
mcimadamore@1109 124 for (StatementKind stmt5 : StatementKind.values()) {
mcimadamore@1109 125 new T7093325(ca, stmt0, stmt1, stmt2, stmt3, stmt4, stmt5).compileAndCheck();
mcimadamore@1109 126 }
mcimadamore@1109 127 }
mcimadamore@1109 128 }
mcimadamore@1109 129 }
mcimadamore@1109 130 }
mcimadamore@1109 131 }
mcimadamore@1109 132 }
mcimadamore@1109 133
mcimadamore@1109 134 System.out.println("Total checks made: " + checkCount);
mcimadamore@1109 135 }
mcimadamore@1109 136
mcimadamore@1109 137 /** instance decls **/
mcimadamore@1109 138
mcimadamore@1109 139 CatchArity ca;
mcimadamore@1109 140 StatementKind[] stmts;
mcimadamore@1109 141
mcimadamore@1109 142 public T7093325(CatchArity ca, StatementKind... stmts) {
mcimadamore@1109 143 this.ca = ca;
mcimadamore@1109 144 this.stmts = stmts;
mcimadamore@1109 145 }
mcimadamore@1109 146
mcimadamore@1109 147 void compileAndCheck() throws Exception {
mcimadamore@1109 148 final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
mcimadamore@1109 149 JavaSource source = new JavaSource();
mcimadamore@1109 150 JavacTask ct = (JavacTask)tool.getTask(null, fm, null,
mcimadamore@1109 151 null, null, Arrays.asList(source));
mcimadamore@1109 152 ct.call();
mcimadamore@1109 153 verifyBytecode(source);
mcimadamore@1109 154 }
mcimadamore@1109 155
mcimadamore@1109 156 void verifyBytecode(JavaSource source) {
mcimadamore@1109 157 checkCount++;
mcimadamore@1109 158 boolean lastInlined = false;
mcimadamore@1109 159 boolean hasCode = false;
mcimadamore@1109 160 int gapsCount = 0;
mcimadamore@1109 161 for (int i = 0; i < stmts.length ; i++) {
mcimadamore@1109 162 lastInlined = stmts[i].canInline;
mcimadamore@1109 163 hasCode = hasCode || !stmts[i].empty;
mcimadamore@1109 164 if (lastInlined && hasCode) {
mcimadamore@1109 165 hasCode = false;
mcimadamore@1109 166 gapsCount++;
mcimadamore@1109 167 }
mcimadamore@1109 168 }
mcimadamore@1109 169 if (!lastInlined) {
mcimadamore@1109 170 gapsCount++;
mcimadamore@1109 171 }
mcimadamore@1109 172
mcimadamore@1109 173 //System.out.printf("gaps %d \n %s \n", gapsCount, source.toString());
mcimadamore@1109 174
mcimadamore@1109 175 File compiledTest = new File("Test.class");
mcimadamore@1109 176 try {
mcimadamore@1109 177 ClassFile cf = ClassFile.read(compiledTest);
mcimadamore@1109 178 if (cf == null) {
mcimadamore@1109 179 throw new Error("Classfile not found: " + compiledTest.getName());
mcimadamore@1109 180 }
mcimadamore@1109 181
mcimadamore@1109 182 Method test_method = null;
mcimadamore@1109 183 for (Method m : cf.methods) {
mcimadamore@1109 184 if (m.getName(cf.constant_pool).equals("test")) {
mcimadamore@1109 185 test_method = m;
mcimadamore@1109 186 break;
mcimadamore@1109 187 }
mcimadamore@1109 188 }
mcimadamore@1109 189
mcimadamore@1109 190 if (test_method == null) {
mcimadamore@1109 191 throw new Error("Method test() not found in class Test");
mcimadamore@1109 192 }
mcimadamore@1109 193
mcimadamore@1109 194 Code_attribute code = null;
mcimadamore@1109 195 for (Attribute a : test_method.attributes) {
mcimadamore@1109 196 if (a.getName(cf.constant_pool).equals(Attribute.Code)) {
mcimadamore@1109 197 code = (Code_attribute)a;
mcimadamore@1109 198 break;
mcimadamore@1109 199 }
mcimadamore@1109 200 }
mcimadamore@1109 201
mcimadamore@1109 202 if (code == null) {
mcimadamore@1109 203 throw new Error("Code attribute not found in method test()");
mcimadamore@1109 204 }
mcimadamore@1109 205
mcimadamore@1109 206 int actualGapsCount = 0;
mcimadamore@1109 207 for (int i = 0; i < code.exception_table_langth ; i++) {
mcimadamore@1109 208 int catchType = code.exception_table[i].catch_type;
mcimadamore@1109 209 if (catchType == 0) { //any
mcimadamore@1109 210 actualGapsCount++;
mcimadamore@1109 211 }
mcimadamore@1109 212 }
mcimadamore@1109 213
mcimadamore@1109 214 if (actualGapsCount != gapsCount) {
mcimadamore@1109 215 throw new Error("Bad exception table for test()\n" +
mcimadamore@1109 216 "expected gaps: " + gapsCount + "\n" +
mcimadamore@1109 217 "found gaps: " + actualGapsCount + "\n" +
mcimadamore@1109 218 source);
mcimadamore@1109 219 }
mcimadamore@1109 220 } catch (Exception e) {
mcimadamore@1109 221 e.printStackTrace();
mcimadamore@1109 222 throw new Error("error reading " + compiledTest +": " + e);
mcimadamore@1109 223 }
mcimadamore@1109 224
mcimadamore@1109 225 }
mcimadamore@1109 226
mcimadamore@1109 227 class JavaSource extends SimpleJavaFileObject {
mcimadamore@1109 228
mcimadamore@1109 229 static final String source_template =
mcimadamore@1109 230 "class A extends RuntimeException {} \n" +
mcimadamore@1109 231 "class B extends RuntimeException {} \n" +
mcimadamore@1109 232 "class C extends RuntimeException {} \n" +
mcimadamore@1109 233 "class D extends RuntimeException {} \n" +
mcimadamore@1109 234 "class E extends RuntimeException {} \n" +
mcimadamore@1109 235 "class Test {\n" +
mcimadamore@1109 236 " void test() {\n" +
mcimadamore@1109 237 " try { #S0 } #C finally { System.out.println(); }\n" +
mcimadamore@1109 238 " }\n" +
mcimadamore@1109 239 "}";
mcimadamore@1109 240
mcimadamore@1109 241 String source;
mcimadamore@1109 242
mcimadamore@1109 243 public JavaSource() {
mcimadamore@1109 244 super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
mcimadamore@1109 245 source = source_template.replace("#C", ca.catchers());
mcimadamore@1109 246 source = source.replace("#S0", stmts[0].stmt);
mcimadamore@1109 247 for (int i = 1; i < ca.ordinal() + 1; i++) {
mcimadamore@1109 248 source = source.replace("#S" + i, stmts[i].stmt);
mcimadamore@1109 249 }
mcimadamore@1109 250 }
mcimadamore@1109 251
mcimadamore@1109 252 @Override
mcimadamore@1109 253 public String toString() {
mcimadamore@1109 254 return source;
mcimadamore@1109 255 }
mcimadamore@1109 256
mcimadamore@1109 257 @Override
mcimadamore@1109 258 public CharSequence getCharContent(boolean ignoreEncodingErrors) {
mcimadamore@1109 259 return source;
mcimadamore@1109 260 }
mcimadamore@1109 261 }
mcimadamore@1109 262 }

mercurial