Mon, 17 Oct 2011 12:54:33 +0100
7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
Summary: Duplicate aliasing check during subtyping leads to spurious varargs diagnostic
Reviewed-by: jjg
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 2010, 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 6930507 |
aoqi@0 | 27 | * @summary Symbols for anonymous and local classes made too late for use by java tree API |
aoqi@0 | 28 | */ |
aoqi@0 | 29 | |
aoqi@0 | 30 | import java.io.*; |
aoqi@0 | 31 | import java.util.*; |
aoqi@0 | 32 | import javax.annotation.processing.*; |
aoqi@0 | 33 | import javax.lang.model.SourceVersion; |
aoqi@0 | 34 | import javax.lang.model.element.*; |
aoqi@0 | 35 | import javax.tools.Diagnostic; |
aoqi@0 | 36 | import static javax.lang.model.util.ElementFilter.*; |
aoqi@0 | 37 | |
aoqi@0 | 38 | import com.sun.source.tree.*; |
aoqi@0 | 39 | import com.sun.source.util.*; |
aoqi@0 | 40 | |
aoqi@0 | 41 | @SupportedOptions({"test", "last"}) |
aoqi@0 | 42 | @SupportedAnnotationTypes("*") |
aoqi@0 | 43 | public class TestGetElement extends AbstractProcessor { |
aoqi@0 | 44 | public static void main(String... args) throws Exception { |
aoqi@0 | 45 | new TestGetElement().run(); |
aoqi@0 | 46 | } |
aoqi@0 | 47 | |
aoqi@0 | 48 | public TestGetElement() { } |
aoqi@0 | 49 | |
aoqi@0 | 50 | public void run() throws Exception { |
aoqi@0 | 51 | final String testSrc = System.getProperty("test.src"); |
aoqi@0 | 52 | final String testClasses = System.getProperty("test.classes"); |
aoqi@0 | 53 | final String myClassName = getClass().getName(); |
aoqi@0 | 54 | final String mySrc = new File(testSrc, myClassName + ".java").getPath(); |
aoqi@0 | 55 | |
aoqi@0 | 56 | final int NUM_TESTS = 90; // #decls in this source file |
aoqi@0 | 57 | for (int i = 1; i <= NUM_TESTS; i++) { |
aoqi@0 | 58 | System.err.println("test " + i); |
aoqi@0 | 59 | File testDir = new File("test" + i); |
aoqi@0 | 60 | File classesDir = new File(testDir, "classes"); |
aoqi@0 | 61 | classesDir.mkdirs(); |
aoqi@0 | 62 | String[] args = { |
aoqi@0 | 63 | "-d", classesDir.getPath(), |
aoqi@0 | 64 | "-processorpath", testClasses, |
aoqi@0 | 65 | "-processor", myClassName, |
aoqi@0 | 66 | "-proc:only", |
aoqi@0 | 67 | "-Atest=" + i, |
aoqi@0 | 68 | "-Alast=" + (i == NUM_TESTS), |
aoqi@0 | 69 | mySrc |
aoqi@0 | 70 | }; |
aoqi@0 | 71 | |
aoqi@0 | 72 | // System.err.println("compile: " + Arrays.asList(args)); |
aoqi@0 | 73 | |
aoqi@0 | 74 | StringWriter sw = new StringWriter(); |
aoqi@0 | 75 | PrintWriter pw = new PrintWriter(sw); |
aoqi@0 | 76 | int rc = com.sun.tools.javac.Main.compile(args, pw); |
aoqi@0 | 77 | pw.close(); |
aoqi@0 | 78 | String out = sw.toString(); |
aoqi@0 | 79 | if (out != null) |
aoqi@0 | 80 | System.err.println(out); |
aoqi@0 | 81 | if (rc != 0) { |
aoqi@0 | 82 | System.err.println("compilation failed: rc=" + rc); |
aoqi@0 | 83 | errors++; |
aoqi@0 | 84 | } |
aoqi@0 | 85 | } |
aoqi@0 | 86 | |
aoqi@0 | 87 | if (errors > 0) |
aoqi@0 | 88 | throw new Exception(errors + " errors occurred"); |
aoqi@0 | 89 | } |
aoqi@0 | 90 | |
aoqi@0 | 91 | |
aoqi@0 | 92 | int errors; |
aoqi@0 | 93 | |
aoqi@0 | 94 | public boolean process(Set<? extends TypeElement> annotations, |
aoqi@0 | 95 | RoundEnvironment roundEnvironment) |
aoqi@0 | 96 | { |
aoqi@0 | 97 | if (roundEnvironment.processingOver()) |
aoqi@0 | 98 | return true; |
aoqi@0 | 99 | |
aoqi@0 | 100 | Map<String,String> options = processingEnv.getOptions(); |
aoqi@0 | 101 | int test = Integer.parseInt(options.get("test")); |
aoqi@0 | 102 | boolean _last = Boolean.parseBoolean(options.get("last")); |
aoqi@0 | 103 | |
aoqi@0 | 104 | Trees trees = Trees.instance(processingEnv); |
aoqi@0 | 105 | Scanner scanner = new Scanner(trees, _last); |
aoqi@0 | 106 | int nelems = 0; |
aoqi@0 | 107 | for (TypeElement e : typesIn(roundEnvironment.getRootElements())) { |
aoqi@0 | 108 | nelems += scanner.scan(trees.getPath(e), test); |
aoqi@0 | 109 | } |
aoqi@0 | 110 | |
aoqi@0 | 111 | Messager m = processingEnv.getMessager(); |
aoqi@0 | 112 | int EXPECT = 1; |
aoqi@0 | 113 | if (nelems != EXPECT) { |
aoqi@0 | 114 | m.printMessage(Diagnostic.Kind.ERROR, |
aoqi@0 | 115 | "Unexpected number of elements found: " + nelems + " expected: " + EXPECT); |
aoqi@0 | 116 | } |
aoqi@0 | 117 | return true; |
aoqi@0 | 118 | } |
aoqi@0 | 119 | |
aoqi@0 | 120 | @Override |
aoqi@0 | 121 | public SourceVersion getSupportedSourceVersion() { |
aoqi@0 | 122 | return SourceVersion.latest(); |
aoqi@0 | 123 | } |
aoqi@0 | 124 | |
aoqi@0 | 125 | class Scanner extends TreePathScanner<Integer,Integer> { |
aoqi@0 | 126 | final Trees trees; |
aoqi@0 | 127 | final boolean last; |
aoqi@0 | 128 | int count; |
aoqi@0 | 129 | |
aoqi@0 | 130 | Scanner(Trees trees, boolean last) { |
aoqi@0 | 131 | this.trees = trees; |
aoqi@0 | 132 | this.last = last; |
aoqi@0 | 133 | } |
aoqi@0 | 134 | |
aoqi@0 | 135 | @Override |
aoqi@0 | 136 | public Integer visitClass(ClassTree tree, Integer test) { |
aoqi@0 | 137 | return reduce(check(test), super.visitClass(tree, test)); |
aoqi@0 | 138 | } |
aoqi@0 | 139 | |
aoqi@0 | 140 | @Override |
aoqi@0 | 141 | public Integer visitMethod(MethodTree tree, Integer test) { |
aoqi@0 | 142 | return reduce(check(test), super.visitMethod(tree, test)); |
aoqi@0 | 143 | } |
aoqi@0 | 144 | |
aoqi@0 | 145 | @Override |
aoqi@0 | 146 | public Integer visitVariable(VariableTree tree, Integer test) { |
aoqi@0 | 147 | return reduce(check(test), super.visitVariable(tree, test)); |
aoqi@0 | 148 | } |
aoqi@0 | 149 | |
aoqi@0 | 150 | @Override |
aoqi@0 | 151 | public Integer reduce(Integer i1, Integer i2) { |
aoqi@0 | 152 | if (i1 == null || i1.intValue() == 0) |
aoqi@0 | 153 | return i2; |
aoqi@0 | 154 | if (i2 == null || i2.intValue() == 0) |
aoqi@0 | 155 | return i1; |
aoqi@0 | 156 | return (i1 + i2); |
aoqi@0 | 157 | } |
aoqi@0 | 158 | |
aoqi@0 | 159 | int check(int test) { |
aoqi@0 | 160 | count++; |
aoqi@0 | 161 | |
aoqi@0 | 162 | if (count != test) |
aoqi@0 | 163 | return 0; |
aoqi@0 | 164 | |
aoqi@0 | 165 | TreePath p = getCurrentPath(); |
aoqi@0 | 166 | Element e = trees.getElement(p); |
aoqi@0 | 167 | |
aoqi@0 | 168 | String text = p.getLeaf().toString().replaceAll("\\s+", " ").trim(); |
aoqi@0 | 169 | int MAXLEN = 40; |
aoqi@0 | 170 | if (text.length() > MAXLEN) |
aoqi@0 | 171 | text = text.substring(0, MAXLEN - 3) + "..."; |
aoqi@0 | 172 | |
aoqi@0 | 173 | System.err.println(String.format("%3d: %-" + MAXLEN + "s -- %s", |
aoqi@0 | 174 | count, text, |
aoqi@0 | 175 | (e == null ? "null" : e.getKind() + " " + e))); |
aoqi@0 | 176 | |
aoqi@0 | 177 | Messager m = processingEnv.getMessager(); |
aoqi@0 | 178 | if (e == null) { |
aoqi@0 | 179 | m.printMessage(Diagnostic.Kind.ERROR, "Null element found for " + text); |
aoqi@0 | 180 | return 0; |
aoqi@0 | 181 | } |
aoqi@0 | 182 | |
aoqi@0 | 183 | if (last && !e.getSimpleName().contentEquals("last")) { |
aoqi@0 | 184 | m.printMessage(Diagnostic.Kind.ERROR, "Unexpected name in last test: " |
aoqi@0 | 185 | + e.getSimpleName() + ", expected: last"); |
aoqi@0 | 186 | } |
aoqi@0 | 187 | |
aoqi@0 | 188 | return 1; |
aoqi@0 | 189 | } |
aoqi@0 | 190 | } |
aoqi@0 | 191 | |
aoqi@0 | 192 | // following are all fodder for the test |
aoqi@0 | 193 | |
aoqi@0 | 194 | class MemberClass { |
aoqi@0 | 195 | class NestedMemberClass { } |
aoqi@0 | 196 | } |
aoqi@0 | 197 | |
aoqi@0 | 198 | { |
aoqi@0 | 199 | class InnerClassInInit { } |
aoqi@0 | 200 | Object o = new Object() { }; |
aoqi@0 | 201 | } |
aoqi@0 | 202 | |
aoqi@0 | 203 | TestGetElement(TestGetElement unused) { |
aoqi@0 | 204 | class InnerClassInConstr { } |
aoqi@0 | 205 | Object o = new Object() { }; |
aoqi@0 | 206 | } |
aoqi@0 | 207 | |
aoqi@0 | 208 | void m() { |
aoqi@0 | 209 | class InnerClassInMethod { } |
aoqi@0 | 210 | Object o = new Object() { }; |
aoqi@0 | 211 | |
aoqi@0 | 212 | class C { |
aoqi@0 | 213 | class MemberClass { |
aoqi@0 | 214 | class NestedMemberClass { } |
aoqi@0 | 215 | } |
aoqi@0 | 216 | |
aoqi@0 | 217 | { |
aoqi@0 | 218 | class InnerClassInInit { } |
aoqi@0 | 219 | Object o = new Object() { }; |
aoqi@0 | 220 | } |
aoqi@0 | 221 | |
aoqi@0 | 222 | C(Object unused) { |
aoqi@0 | 223 | class InnerClassInConstr { } |
aoqi@0 | 224 | Object o = new Object() { }; |
aoqi@0 | 225 | } |
aoqi@0 | 226 | |
aoqi@0 | 227 | void m() { |
aoqi@0 | 228 | class InnerClassInMethod { } |
aoqi@0 | 229 | Object o = new Object() { }; |
aoqi@0 | 230 | } |
aoqi@0 | 231 | } |
aoqi@0 | 232 | } |
aoqi@0 | 233 | |
aoqi@0 | 234 | int last; // this name is verified by the test to make sure that all decls are checked |
aoqi@0 | 235 | } |