test/tools/javac/T7093325.java

Thu, 10 Jan 2013 19:38:57 -0800

author
jjg
date
Thu, 10 Jan 2013 19:38:57 -0800
changeset 1490
fc4cb1577ad6
parent 1482
954541f13717
child 1520
5c956be64b9e
permissions
-rw-r--r--

8004834: Add doclint support into javadoc
Reviewed-by: darcy

mcimadamore@1109 1 /*
vromero@1482 2 * Copyright (c) 2011, 2013, 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
vromero@1482 28 * @library lib
vromero@1482 29 * @build JavacTestingAbstractThreadedTest
vromero@1482 30 * @run main T7093325
mcimadamore@1109 31 */
mcimadamore@1109 32
vromero@1482 33 import java.io.File;
vromero@1482 34 import java.net.URI;
vromero@1482 35 import java.util.Arrays;
vromero@1482 36 import javax.tools.JavaCompiler;
vromero@1482 37 import javax.tools.JavaFileObject;
vromero@1482 38 import javax.tools.SimpleJavaFileObject;
vromero@1482 39 import javax.tools.ToolProvider;
vromero@1482 40
mcimadamore@1109 41 import com.sun.source.util.JavacTask;
mcimadamore@1109 42 import com.sun.tools.classfile.Attribute;
mcimadamore@1109 43 import com.sun.tools.classfile.ClassFile;
mcimadamore@1109 44 import com.sun.tools.classfile.Code_attribute;
mcimadamore@1109 45 import com.sun.tools.classfile.ConstantPool.*;
mcimadamore@1109 46 import com.sun.tools.classfile.Method;
mcimadamore@1109 47
vromero@1482 48 public class T7093325
vromero@1482 49 extends JavacTestingAbstractThreadedTest
vromero@1482 50 implements Runnable {
mcimadamore@1109 51
mcimadamore@1109 52 enum StatementKind {
mcimadamore@1109 53 THROW("throw new RuntimeException();", false, false),
mcimadamore@1109 54 RETURN_NONEMPTY("System.out.println(); return;", true, false),
mcimadamore@1109 55 RETURN_EMPTY("return;", true, true),
mcimadamore@1109 56 APPLY("System.out.println();", true, false);
mcimadamore@1109 57
mcimadamore@1109 58 String stmt;
mcimadamore@1109 59 boolean canInline;
mcimadamore@1109 60 boolean empty;
mcimadamore@1109 61
mcimadamore@1109 62 private StatementKind(String stmt, boolean canInline, boolean empty) {
mcimadamore@1109 63 this.stmt = stmt;
mcimadamore@1109 64 this.canInline = canInline;
mcimadamore@1109 65 this.empty = empty;
mcimadamore@1109 66 }
mcimadamore@1109 67 }
mcimadamore@1109 68
mcimadamore@1109 69 enum CatchArity {
mcimadamore@1109 70 NONE(""),
mcimadamore@1109 71 ONE("catch (A a) { #S1 }"),
mcimadamore@1109 72 TWO("catch (B b) { #S2 }"),
mcimadamore@1109 73 THREE("catch (C c) { #S3 }"),
mcimadamore@1109 74 FOUR("catch (D d) { #S4 }");
mcimadamore@1109 75
mcimadamore@1109 76 String catchStr;
mcimadamore@1109 77
mcimadamore@1109 78 private CatchArity(String catchStr) {
mcimadamore@1109 79 this.catchStr = catchStr;
mcimadamore@1109 80 }
mcimadamore@1109 81
mcimadamore@1109 82 String catchers() {
mcimadamore@1109 83 if (this.ordinal() == 0) {
mcimadamore@1109 84 return catchStr;
mcimadamore@1109 85 } else {
vromero@1482 86 return CatchArity.values()[this.ordinal() - 1].catchers() +
vromero@1482 87 catchStr;
mcimadamore@1109 88 }
mcimadamore@1109 89 }
mcimadamore@1109 90 }
mcimadamore@1109 91
mcimadamore@1109 92 public static void main(String... args) throws Exception {
mcimadamore@1109 93 for (CatchArity ca : CatchArity.values()) {
mcimadamore@1109 94 for (StatementKind stmt0 : StatementKind.values()) {
mcimadamore@1109 95 if (ca.ordinal() == 0) {
vromero@1482 96 pool.execute(new T7093325(ca, stmt0));
mcimadamore@1109 97 continue;
mcimadamore@1109 98 }
mcimadamore@1109 99 for (StatementKind stmt1 : StatementKind.values()) {
mcimadamore@1109 100 if (ca.ordinal() == 1) {
vromero@1482 101 pool.execute(new T7093325(ca, stmt0, stmt1));
mcimadamore@1109 102 continue;
mcimadamore@1109 103 }
mcimadamore@1109 104 for (StatementKind stmt2 : StatementKind.values()) {
mcimadamore@1109 105 if (ca.ordinal() == 2) {
vromero@1482 106 pool.execute(new T7093325(ca, stmt0, stmt1, stmt2));
mcimadamore@1109 107 continue;
mcimadamore@1109 108 }
mcimadamore@1109 109 for (StatementKind stmt3 : StatementKind.values()) {
mcimadamore@1109 110 if (ca.ordinal() == 3) {
vromero@1482 111 pool.execute(
vromero@1482 112 new T7093325(ca, stmt0, stmt1, stmt2, stmt3));
mcimadamore@1109 113 continue;
mcimadamore@1109 114 }
mcimadamore@1109 115 for (StatementKind stmt4 : StatementKind.values()) {
mcimadamore@1109 116 if (ca.ordinal() == 4) {
vromero@1482 117 pool.execute(
vromero@1482 118 new T7093325(ca, stmt0, stmt1,
vromero@1482 119 stmt2, stmt3, stmt4));
mcimadamore@1109 120 continue;
mcimadamore@1109 121 }
mcimadamore@1109 122 for (StatementKind stmt5 : StatementKind.values()) {
vromero@1482 123 pool.execute(
vromero@1482 124 new T7093325(ca, stmt0, stmt1, stmt2,
vromero@1482 125 stmt3, stmt4, stmt5));
mcimadamore@1109 126 }
mcimadamore@1109 127 }
mcimadamore@1109 128 }
mcimadamore@1109 129 }
mcimadamore@1109 130 }
mcimadamore@1109 131 }
mcimadamore@1109 132 }
mcimadamore@1109 133
vromero@1482 134 checkAfterExec();
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
vromero@1482 147 @Override
vromero@1482 148 public void run() {
vromero@1482 149 int id = checkCount.incrementAndGet();
mcimadamore@1109 150 final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
vromero@1482 151 JavaSource source = new JavaSource(id);
vromero@1482 152 JavacTask ct = (JavacTask)tool.getTask(null, fm.get(), null,
mcimadamore@1109 153 null, null, Arrays.asList(source));
mcimadamore@1109 154 ct.call();
vromero@1482 155 verifyBytecode(source, id);
mcimadamore@1109 156 }
mcimadamore@1109 157
vromero@1482 158 void verifyBytecode(JavaSource source, int id) {
mcimadamore@1109 159 boolean lastInlined = false;
mcimadamore@1109 160 boolean hasCode = false;
mcimadamore@1109 161 int gapsCount = 0;
mcimadamore@1109 162 for (int i = 0; i < stmts.length ; i++) {
mcimadamore@1109 163 lastInlined = stmts[i].canInline;
mcimadamore@1109 164 hasCode = hasCode || !stmts[i].empty;
mcimadamore@1109 165 if (lastInlined && hasCode) {
mcimadamore@1109 166 hasCode = false;
mcimadamore@1109 167 gapsCount++;
mcimadamore@1109 168 }
mcimadamore@1109 169 }
mcimadamore@1109 170 if (!lastInlined) {
mcimadamore@1109 171 gapsCount++;
mcimadamore@1109 172 }
mcimadamore@1109 173
mcimadamore@1109 174 //System.out.printf("gaps %d \n %s \n", gapsCount, source.toString());
mcimadamore@1109 175
vromero@1482 176 File compiledTest = new File(String.format("Test%s.class", id));
mcimadamore@1109 177 try {
mcimadamore@1109 178 ClassFile cf = ClassFile.read(compiledTest);
mcimadamore@1109 179 if (cf == null) {
vromero@1482 180 throw new Error("Classfile not found: " +
vromero@1482 181 compiledTest.getName());
mcimadamore@1109 182 }
mcimadamore@1109 183
mcimadamore@1109 184 Method test_method = null;
mcimadamore@1109 185 for (Method m : cf.methods) {
mcimadamore@1109 186 if (m.getName(cf.constant_pool).equals("test")) {
mcimadamore@1109 187 test_method = m;
mcimadamore@1109 188 break;
mcimadamore@1109 189 }
mcimadamore@1109 190 }
mcimadamore@1109 191
mcimadamore@1109 192 if (test_method == null) {
mcimadamore@1109 193 throw new Error("Method test() not found in class Test");
mcimadamore@1109 194 }
mcimadamore@1109 195
mcimadamore@1109 196 Code_attribute code = null;
mcimadamore@1109 197 for (Attribute a : test_method.attributes) {
mcimadamore@1109 198 if (a.getName(cf.constant_pool).equals(Attribute.Code)) {
mcimadamore@1109 199 code = (Code_attribute)a;
mcimadamore@1109 200 break;
mcimadamore@1109 201 }
mcimadamore@1109 202 }
mcimadamore@1109 203
mcimadamore@1109 204 if (code == null) {
mcimadamore@1109 205 throw new Error("Code attribute not found in method test()");
mcimadamore@1109 206 }
mcimadamore@1109 207
mcimadamore@1109 208 int actualGapsCount = 0;
mcimadamore@1109 209 for (int i = 0; i < code.exception_table_langth ; i++) {
mcimadamore@1109 210 int catchType = code.exception_table[i].catch_type;
mcimadamore@1109 211 if (catchType == 0) { //any
mcimadamore@1109 212 actualGapsCount++;
mcimadamore@1109 213 }
mcimadamore@1109 214 }
mcimadamore@1109 215
mcimadamore@1109 216 if (actualGapsCount != gapsCount) {
mcimadamore@1109 217 throw new Error("Bad exception table for test()\n" +
mcimadamore@1109 218 "expected gaps: " + gapsCount + "\n" +
mcimadamore@1109 219 "found gaps: " + actualGapsCount + "\n" +
mcimadamore@1109 220 source);
mcimadamore@1109 221 }
mcimadamore@1109 222 } catch (Exception e) {
mcimadamore@1109 223 e.printStackTrace();
mcimadamore@1109 224 throw new Error("error reading " + compiledTest +": " + e);
mcimadamore@1109 225 }
mcimadamore@1109 226
mcimadamore@1109 227 }
mcimadamore@1109 228
mcimadamore@1109 229 class JavaSource extends SimpleJavaFileObject {
mcimadamore@1109 230
mcimadamore@1109 231 static final String source_template =
mcimadamore@1109 232 "class A extends RuntimeException {} \n" +
mcimadamore@1109 233 "class B extends RuntimeException {} \n" +
mcimadamore@1109 234 "class C extends RuntimeException {} \n" +
mcimadamore@1109 235 "class D extends RuntimeException {} \n" +
mcimadamore@1109 236 "class E extends RuntimeException {} \n" +
vromero@1482 237 "class Test#ID {\n" +
mcimadamore@1109 238 " void test() {\n" +
mcimadamore@1109 239 " try { #S0 } #C finally { System.out.println(); }\n" +
mcimadamore@1109 240 " }\n" +
mcimadamore@1109 241 "}";
mcimadamore@1109 242
mcimadamore@1109 243 String source;
mcimadamore@1109 244
vromero@1482 245 public JavaSource(int id) {
vromero@1482 246 super(URI.create(String.format("myfo:/Test%s.java", id)),
vromero@1482 247 JavaFileObject.Kind.SOURCE);
mcimadamore@1109 248 source = source_template.replace("#C", ca.catchers());
mcimadamore@1109 249 source = source.replace("#S0", stmts[0].stmt);
vromero@1482 250 source = source.replace("#ID", String.valueOf(id));
mcimadamore@1109 251 for (int i = 1; i < ca.ordinal() + 1; i++) {
mcimadamore@1109 252 source = source.replace("#S" + i, stmts[i].stmt);
mcimadamore@1109 253 }
mcimadamore@1109 254 }
mcimadamore@1109 255
mcimadamore@1109 256 @Override
mcimadamore@1109 257 public String toString() {
mcimadamore@1109 258 return source;
mcimadamore@1109 259 }
mcimadamore@1109 260
mcimadamore@1109 261 @Override
mcimadamore@1109 262 public CharSequence getCharContent(boolean ignoreEncodingErrors) {
mcimadamore@1109 263 return source;
mcimadamore@1109 264 }
mcimadamore@1109 265 }
vromero@1482 266
mcimadamore@1109 267 }

mercurial