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
duke@1 | 1 | /* |
ohair@554 | 2 | * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved. |
duke@1 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
duke@1 | 4 | * |
duke@1 | 5 | * This code is free software; you can redistribute it and/or modify it |
duke@1 | 6 | * under the terms of the GNU General Public License version 2 only, as |
duke@1 | 7 | * published by the Free Software Foundation. |
duke@1 | 8 | * |
duke@1 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
duke@1 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
duke@1 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
duke@1 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
duke@1 | 13 | * accompanied this code). |
duke@1 | 14 | * |
duke@1 | 15 | * You should have received a copy of the GNU General Public License version |
duke@1 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
duke@1 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
duke@1 | 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. |
duke@1 | 22 | */ |
duke@1 | 23 | |
duke@1 | 24 | /* |
duke@1 | 25 | * @test |
duke@1 | 26 | * @bug 6346249 6392177 |
duke@1 | 27 | * @summary new Trees API |
duke@1 | 28 | */ |
duke@1 | 29 | |
duke@1 | 30 | import com.sun.source.tree.*; |
duke@1 | 31 | import com.sun.source.util.*; |
duke@1 | 32 | import java.io.*; |
duke@1 | 33 | import java.lang.annotation.*; |
duke@1 | 34 | import java.util.*; |
duke@1 | 35 | import javax.annotation.processing.*; |
duke@1 | 36 | import javax.lang.model.SourceVersion; |
duke@1 | 37 | import javax.lang.model.element.*; |
duke@1 | 38 | import javax.lang.model.type.*; |
duke@1 | 39 | import javax.tools.*; |
duke@1 | 40 | import com.sun.tools.javac.api.JavacTool; |
duke@1 | 41 | import com.sun.tools.javac.tree.JCTree; |
duke@1 | 42 | import com.sun.tools.javac.tree.TreeInfo; |
duke@1 | 43 | |
duke@1 | 44 | @Anno |
duke@1 | 45 | @SupportedAnnotationTypes("*") |
duke@1 | 46 | public class TestTrees extends AbstractProcessor { |
duke@1 | 47 | |
duke@1 | 48 | @Anno |
duke@1 | 49 | void annoMethod() { } |
duke@1 | 50 | |
duke@1 | 51 | @Anno |
duke@1 | 52 | int annoField; |
duke@1 | 53 | |
duke@1 | 54 | |
duke@1 | 55 | static final String testSrcDir = System.getProperty("test.src"); |
duke@1 | 56 | static final String testClassDir = System.getProperty("test.classes"); |
duke@1 | 57 | static final String self = TestTrees.class.getName(); |
duke@1 | 58 | static PrintWriter out = new PrintWriter(System.err, true); |
duke@1 | 59 | |
duke@1 | 60 | public static void main(String[] args) throws IOException { |
duke@1 | 61 | new TestTrees().run(); |
duke@1 | 62 | } |
duke@1 | 63 | |
duke@1 | 64 | void run() throws IOException { |
duke@1 | 65 | |
duke@1 | 66 | JavacTool tool = JavacTool.create(); |
duke@1 | 67 | |
duke@1 | 68 | DiagnosticListener<JavaFileObject> dl = new DiagnosticListener<JavaFileObject>() { |
duke@1 | 69 | public void report(Diagnostic d) { |
duke@1 | 70 | error(d.toString()); |
duke@1 | 71 | } |
duke@1 | 72 | }; |
duke@1 | 73 | |
duke@1 | 74 | StandardJavaFileManager fm = tool.getStandardFileManager(dl, null, null); |
duke@1 | 75 | Iterable<? extends JavaFileObject> files = |
duke@1 | 76 | fm.getJavaFileObjectsFromFiles(Arrays.asList(new File(testSrcDir, self + ".java"))); |
duke@1 | 77 | |
duke@1 | 78 | Iterable<String> opts = Arrays.asList("-d", "."); |
duke@1 | 79 | |
duke@1 | 80 | System.err.println("simple compilation, no processing"); |
duke@1 | 81 | JavacTask task = tool.getTask(out, fm, dl, opts, null, files); |
duke@1 | 82 | task.setTaskListener(new MyTaskListener(task)); |
duke@1 | 83 | if (!task.call()) |
duke@1 | 84 | throw new AssertionError("compilation failed"); |
duke@1 | 85 | |
duke@1 | 86 | opts = Arrays.asList("-d", ".", "-processorpath", testClassDir, "-processor", self); |
duke@1 | 87 | |
duke@1 | 88 | System.err.println(); |
duke@1 | 89 | System.err.println("compilation with processing"); |
duke@1 | 90 | task = tool.getTask(out, fm, dl,opts, null, files); |
duke@1 | 91 | if (!task.call()) |
duke@1 | 92 | throw new AssertionError("compilation failed"); |
duke@1 | 93 | |
duke@1 | 94 | if (errors > 0) |
duke@1 | 95 | throw new AssertionError(errors + " errors occurred"); |
duke@1 | 96 | } |
duke@1 | 97 | |
duke@1 | 98 | void testElement(Trees trees, Element e) { |
duke@1 | 99 | trees.getClass(); |
duke@1 | 100 | e.getClass(); |
duke@1 | 101 | |
duke@1 | 102 | System.err.println("testElement: " + e); |
duke@1 | 103 | Tree tree = trees.getTree(e); |
duke@1 | 104 | //System.err.println(tree); |
duke@1 | 105 | |
duke@1 | 106 | if (TreeInfo.symbolFor((JCTree)tree) != e) |
duke@1 | 107 | error("bad result from getTree"); |
duke@1 | 108 | |
duke@1 | 109 | TreePath path = trees.getPath(e); |
duke@1 | 110 | if (path == null) { |
duke@1 | 111 | error("getPath returned null"); |
duke@1 | 112 | return; |
duke@1 | 113 | } |
duke@1 | 114 | if (path.getLeaf() != tree) |
duke@1 | 115 | error("bad result from getPath"); |
duke@1 | 116 | |
duke@1 | 117 | Element e2 = trees.getElement(path); |
duke@1 | 118 | if (e2 == null) { |
duke@1 | 119 | error("getElement returned null"); |
duke@1 | 120 | return; |
duke@1 | 121 | } |
duke@1 | 122 | if (e2 != e) |
duke@1 | 123 | error("bad result from getElement"); |
duke@1 | 124 | |
duke@1 | 125 | // The TypeMirror is not available yet when annotation processing; |
duke@1 | 126 | // it is set up later during ANALYSE. |
duke@1 | 127 | TypeMirror t = trees.getTypeMirror(path); |
duke@1 | 128 | if (t != null && t.getKind() == TypeKind.DECLARED && |
duke@1 | 129 | ((DeclaredType)t).asElement() != e2) |
duke@1 | 130 | error("bad result from getTypeMirror"); |
duke@1 | 131 | |
duke@1 | 132 | for (AnnotationMirror m: e.getAnnotationMirrors()) { |
duke@1 | 133 | testAnnotation(trees, e, m); |
duke@1 | 134 | } |
duke@1 | 135 | } |
duke@1 | 136 | |
duke@1 | 137 | void testAnnotation(Trees trees, Element e, AnnotationMirror a) { |
duke@1 | 138 | System.err.println("testAnnotation: " + e + " " + a); |
duke@1 | 139 | Tree tree = trees.getTree(e, a); |
duke@1 | 140 | |
duke@1 | 141 | if (tree.getKind() != Tree.Kind.ANNOTATION) |
duke@1 | 142 | error("bad result from getTree"); |
duke@1 | 143 | |
duke@1 | 144 | TreePath path = trees.getPath(e, a); |
duke@1 | 145 | if (path.getLeaf() != tree) |
duke@1 | 146 | error("bad result from getPath"); |
duke@1 | 147 | } |
duke@1 | 148 | |
duke@1 | 149 | void error(String msg) { |
duke@1 | 150 | if (messager != null) |
duke@1 | 151 | // annotation processing will happen in a separate instance/classloader |
duke@1 | 152 | // so pass the message back to the calling instance. |
duke@1 | 153 | messager.printMessage(Diagnostic.Kind.ERROR, msg); |
duke@1 | 154 | else { |
duke@1 | 155 | System.err.println(msg); |
duke@1 | 156 | errors++; |
duke@1 | 157 | } |
duke@1 | 158 | |
duke@1 | 159 | } |
duke@1 | 160 | |
duke@1 | 161 | Messager messager; |
duke@1 | 162 | int errors; |
duke@1 | 163 | |
duke@1 | 164 | |
duke@1 | 165 | public boolean process(Set<? extends TypeElement> annos, RoundEnvironment rEnv) { |
duke@1 | 166 | Trees trees = Trees.instance(processingEnv); |
duke@1 | 167 | messager = processingEnv.getMessager(); |
duke@1 | 168 | |
duke@1 | 169 | for (Element e: rEnv.getRootElements()) { |
duke@1 | 170 | testElement(trees, e); |
duke@1 | 171 | } |
duke@1 | 172 | |
duke@1 | 173 | for (TypeElement anno: annos) { |
duke@1 | 174 | Set<? extends Element> elts = rEnv.getElementsAnnotatedWith(anno); |
duke@1 | 175 | System.err.println("anno: " + anno); |
duke@1 | 176 | System.err.println("elts: " + elts); |
duke@1 | 177 | if (elts != null) { // 6397298, should return empty set |
duke@1 | 178 | for (Element e: rEnv.getElementsAnnotatedWith(anno)) |
duke@1 | 179 | testElement(trees, e); |
duke@1 | 180 | } |
duke@1 | 181 | } |
duke@1 | 182 | |
duke@1 | 183 | return true; |
duke@1 | 184 | } |
duke@1 | 185 | |
duke@1 | 186 | @Override |
duke@1 | 187 | public SourceVersion getSupportedSourceVersion() { |
duke@1 | 188 | return SourceVersion.latest(); |
duke@1 | 189 | } |
duke@1 | 190 | |
duke@1 | 191 | class MyTaskListener implements TaskListener { |
duke@1 | 192 | MyTaskListener(JavacTask task) { |
duke@1 | 193 | this.task = task; |
duke@1 | 194 | } |
duke@1 | 195 | |
duke@1 | 196 | public void started(TaskEvent e) { |
duke@1 | 197 | System.err.println("started " + e); |
duke@1 | 198 | } |
duke@1 | 199 | |
duke@1 | 200 | public void finished(TaskEvent e) { |
duke@1 | 201 | //System.err.println("finished " + e); |
duke@1 | 202 | switch (e.getKind()) { |
duke@1 | 203 | case ANALYZE: |
duke@1 | 204 | testElement(Trees.instance(task), e.getTypeElement()); |
duke@1 | 205 | break; |
duke@1 | 206 | } |
duke@1 | 207 | } |
duke@1 | 208 | |
duke@1 | 209 | private final JavacTask task; |
duke@1 | 210 | } |
duke@1 | 211 | |
duke@1 | 212 | } |
duke@1 | 213 | |
duke@1 | 214 | @Retention(RetentionPolicy.SOURCE) |
duke@1 | 215 | @interface Anno { |
duke@1 | 216 | } |