test/tools/javac/annotations/6550655/T6550655.java

Tue, 12 Mar 2013 17:39:34 +0100

author
jfranck
date
Tue, 12 Mar 2013 17:39:34 +0100
changeset 1629
f427043f8c65
parent 0
959103a6100f
permissions
-rw-r--r--

7196531: Duplicate error messages on repeating annotations
Reviewed-by: jjg

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 */
aoqi@0 23
aoqi@0 24 /**
aoqi@0 25 * @test
aoqi@0 26 * @bug 6550655
aoqi@0 27 * @summary javac crashes when compiling against an annotated class
aoqi@0 28 */
aoqi@0 29
aoqi@0 30 import java.io.File;
aoqi@0 31 import java.net.URI;
aoqi@0 32 import java.util.Arrays;
aoqi@0 33
aoqi@0 34 import javax.tools.Diagnostic;
aoqi@0 35 import javax.tools.DiagnosticListener;
aoqi@0 36 import javax.tools.JavaCompiler;
aoqi@0 37 import javax.tools.JavaCompiler.CompilationTask;
aoqi@0 38 import javax.tools.JavaFileObject;
aoqi@0 39 import javax.tools.SimpleJavaFileObject;
aoqi@0 40 import javax.tools.ToolProvider;
aoqi@0 41
aoqi@0 42 public class T6550655 {
aoqi@0 43
aoqi@0 44 JavaCompiler javacTool;
aoqi@0 45 File testDir;
aoqi@0 46 TestKind testKind;
aoqi@0 47 EnumActionKind actionKind;
aoqi@0 48
aoqi@0 49 String testSource = "enum E { NORTH, SOUTH, WEST, EAST; }\n" +
aoqi@0 50 "@I(val = E.NORTH)class A {}\n" +
aoqi@0 51 "@interface I { E val(); }";
aoqi@0 52
aoqi@0 53 T6550655(JavaCompiler javacTool, File testDir, TestKind testKind, EnumActionKind actionKind) {
aoqi@0 54 this.javacTool = javacTool;
aoqi@0 55 this.testDir = testDir;
aoqi@0 56 this.testKind = testKind;
aoqi@0 57 this.actionKind = actionKind;
aoqi@0 58 }
aoqi@0 59
aoqi@0 60 void test() {
aoqi@0 61 testDir.mkdirs();
aoqi@0 62 compile(null, new JavaSource("Test.java", testSource));
aoqi@0 63 actionKind.doAction(this);
aoqi@0 64 compile(new DiagnosticChecker(), testKind.source);
aoqi@0 65 }
aoqi@0 66
aoqi@0 67 void compile(DiagnosticChecker dc, JavaSource... sources) {
aoqi@0 68 try {
aoqi@0 69 CompilationTask ct = javacTool.getTask(null, null, dc,
aoqi@0 70 Arrays.asList("-d", testDir.getAbsolutePath(), "-cp", testDir.getAbsolutePath()),
aoqi@0 71 null, Arrays.asList(sources));
aoqi@0 72 ct.call();
aoqi@0 73 }
aoqi@0 74 catch (Exception e) {
aoqi@0 75 error("Internal compilation error");
aoqi@0 76 }
aoqi@0 77 }
aoqi@0 78
aoqi@0 79 void replaceEnum(String newSource) {
aoqi@0 80 compile(null, new JavaSource("Replace.java", newSource));
aoqi@0 81 };
aoqi@0 82
aoqi@0 83 void removeEnum() {
aoqi@0 84 File enumClass = new File(testDir, "E.class");
aoqi@0 85 if (!enumClass.exists()) {
aoqi@0 86 error("Expected file E.class does not exists in folder " + testDir);
aoqi@0 87 }
aoqi@0 88 enumClass.delete();
aoqi@0 89 };
aoqi@0 90
aoqi@0 91 void error(String msg) {
aoqi@0 92 System.err.println(msg);
aoqi@0 93 nerrors++;
aoqi@0 94 }
aoqi@0 95
aoqi@0 96 class DiagnosticChecker implements DiagnosticListener<JavaFileObject> {
aoqi@0 97
aoqi@0 98 String[][] expectedKeys = new String[][] {
aoqi@0 99 // DIRECT, INDIRECT
aoqi@0 100 {/*REPLACE1*/ "compiler.err.cant.resolve.location" , "compiler.warn.unknown.enum.constant" },
aoqi@0 101 {/*REPLACE2*/ "compiler.err.cant.resolve.location.args", "compiler.warn.annotation.method.not.found" },
aoqi@0 102 {/*REMOVE*/ "compiler.err.cant.resolve" , "compiler.warn.unknown.enum.constant.reason" } };
aoqi@0 103
aoqi@0 104 String keyToIgnore = "compiler.err.attribute.value.must.be.constant";
aoqi@0 105
aoqi@0 106 public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
aoqi@0 107 String expectedCode = expectedKeys[actionKind.ordinal()][testKind.ordinal()];
aoqi@0 108 if (!diagnostic.getCode().equals(keyToIgnore) &&
aoqi@0 109 !diagnostic.getCode().equals(expectedCode)) {
aoqi@0 110 error("Unexpected diagnostic" +
aoqi@0 111 "\nfound " + diagnostic.getCode() +
aoqi@0 112 "\nexpected " + expectedCode +
aoqi@0 113 "\ntestKind " + testKind +
aoqi@0 114 "\nactionKind " + actionKind);
aoqi@0 115 }
aoqi@0 116 }
aoqi@0 117 }
aoqi@0 118
aoqi@0 119 //global declarations
aoqi@0 120
aoqi@0 121 enum EnumActionKind {
aoqi@0 122 REPLACE1("enum E { SOUTH, WEST, EAST; }") {
aoqi@0 123 @Override
aoqi@0 124 void doAction(T6550655 test) {
aoqi@0 125 test.replaceEnum(optionalSource);
aoqi@0 126 }
aoqi@0 127 },
aoqi@0 128 REPLACE2("@interface I { E valNew() default E.EAST; }") {
aoqi@0 129 @Override
aoqi@0 130 void doAction(T6550655 test) {
aoqi@0 131 test.replaceEnum(optionalSource);
aoqi@0 132 }
aoqi@0 133 },
aoqi@0 134 REMOVE(null) {
aoqi@0 135 @Override
aoqi@0 136 void doAction(T6550655 test) { test.removeEnum(); }
aoqi@0 137 };
aoqi@0 138
aoqi@0 139 String optionalSource;
aoqi@0 140
aoqi@0 141 private EnumActionKind(String optionalSource) {
aoqi@0 142 this.optionalSource = optionalSource;
aoqi@0 143 }
aoqi@0 144
aoqi@0 145 abstract void doAction(T6550655 test);
aoqi@0 146 }
aoqi@0 147
aoqi@0 148 enum TestKind {
aoqi@0 149 DIRECT("@I(val = E.NORTH)class C1 {}"),
aoqi@0 150 INDIRECT("class C2 { A a; }");
aoqi@0 151
aoqi@0 152 JavaSource source;
aoqi@0 153
aoqi@0 154 private TestKind(final String code) {
aoqi@0 155 this.source = new JavaSource("Test.java", code);
aoqi@0 156 }
aoqi@0 157 }
aoqi@0 158
aoqi@0 159 public static void main(String[] args) throws Exception {
aoqi@0 160 String SCRATCH_DIR = System.getProperty("user.dir");
aoqi@0 161 JavaCompiler javacTool = ToolProvider.getSystemJavaCompiler();
aoqi@0 162 int n = 0;
aoqi@0 163 for (TestKind testKind : TestKind.values()) {
aoqi@0 164 for (EnumActionKind actionKind : EnumActionKind.values()) {
aoqi@0 165 File testDir = new File(SCRATCH_DIR, "test"+n);
aoqi@0 166 new T6550655(javacTool, testDir, testKind, actionKind).test();
aoqi@0 167 n++;
aoqi@0 168 }
aoqi@0 169 }
aoqi@0 170 if (nerrors > 0) {
aoqi@0 171 throw new AssertionError("Some errors have been detected");
aoqi@0 172 }
aoqi@0 173 }
aoqi@0 174
aoqi@0 175 static class JavaSource extends SimpleJavaFileObject {
aoqi@0 176
aoqi@0 177 String source;
aoqi@0 178
aoqi@0 179 public JavaSource(String filename, String source) {
aoqi@0 180 super(URI.create("myfo:/" + filename), JavaFileObject.Kind.SOURCE);
aoqi@0 181 this.source = source;
aoqi@0 182 }
aoqi@0 183
aoqi@0 184 @Override
aoqi@0 185 public CharSequence getCharContent(boolean ignoreEncodingErrors) {
aoqi@0 186 return source;
aoqi@0 187 }
aoqi@0 188 }
aoqi@0 189
aoqi@0 190 static int nerrors = 0;
aoqi@0 191 }

mercurial