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
jjg@309 | 1 | /* |
ohair@554 | 2 | * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved. |
jjg@309 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
jjg@309 | 4 | * |
jjg@309 | 5 | * This code is free software; you can redistribute it and/or modify it |
jjg@309 | 6 | * under the terms of the GNU General Public License version 2 only, as |
jjg@309 | 7 | * published by the Free Software Foundation. |
jjg@309 | 8 | * |
jjg@309 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
jjg@309 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
jjg@309 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
jjg@309 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
jjg@309 | 13 | * accompanied this code). |
jjg@309 | 14 | * |
jjg@309 | 15 | * You should have received a copy of the GNU General Public License version |
jjg@309 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
jjg@309 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
jjg@309 | 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. |
jjg@309 | 22 | */ |
jjg@309 | 23 | |
jjg@309 | 24 | /* |
jjg@309 | 25 | * @test |
jjg@309 | 26 | * @bug 6473148 |
jjg@309 | 27 | * @summary TreePath.iterator() throws NPE |
jjg@309 | 28 | */ |
jjg@309 | 29 | import java.io.*; |
jjg@309 | 30 | import java.util.Arrays; |
jjg@309 | 31 | import java.util.Iterator; |
jjg@309 | 32 | import java.util.Set; |
jjg@309 | 33 | |
jjg@309 | 34 | import javax.annotation.processing.*; |
jjg@309 | 35 | import javax.lang.model.SourceVersion; |
jjg@309 | 36 | import javax.lang.model.element.Element; |
jjg@309 | 37 | import javax.lang.model.element.TypeElement; |
jjg@309 | 38 | import javax.lang.model.util.ElementFilter; |
jjg@309 | 39 | import javax.tools.JavaCompiler; |
jjg@309 | 40 | import javax.tools.JavaFileObject; |
jjg@309 | 41 | import javax.tools.StandardJavaFileManager; |
jjg@309 | 42 | import javax.tools.ToolProvider; |
jjg@309 | 43 | |
jjg@309 | 44 | import com.sun.source.tree.Tree; |
jjg@309 | 45 | import com.sun.source.util.*; |
jjg@309 | 46 | |
jjg@309 | 47 | @SupportedAnnotationTypes("*") |
jjg@309 | 48 | public class TestTreePath extends AbstractProcessor { |
jjg@309 | 49 | |
jjg@309 | 50 | @Override |
jjg@309 | 51 | public boolean process(Set<? extends TypeElement> annotations, |
jjg@309 | 52 | RoundEnvironment roundEnv) { |
jjg@309 | 53 | final Trees trees = Trees.instance(this.processingEnv); |
jjg@309 | 54 | for (Element element : ElementFilter.typesIn(roundEnv.getRootElements())) { |
jjg@309 | 55 | checkTreePath(trees, element, 2); |
jjg@309 | 56 | for (Element member : element.getEnclosedElements()) |
jjg@309 | 57 | checkTreePath(trees, member, 3); |
jjg@309 | 58 | } |
jjg@309 | 59 | return true; |
jjg@309 | 60 | } |
jjg@309 | 61 | |
jjg@309 | 62 | private void checkTreePath(Trees trees, Element element, int expectedLength) { |
jjg@309 | 63 | TreePath path = trees.getPath(element); |
jjg@309 | 64 | assert path != null; |
jjg@309 | 65 | |
jjg@309 | 66 | int enhancedLength = 0; |
jjg@309 | 67 | for (Tree tree : path) |
jjg@309 | 68 | ++enhancedLength; |
jjg@309 | 69 | |
jjg@309 | 70 | if (enhancedLength != expectedLength) |
jjg@309 | 71 | throw new RuntimeException("found path length is wrong"); |
jjg@309 | 72 | |
jjg@309 | 73 | int normalLoopLength = 0; |
jjg@309 | 74 | Iterator<Tree> iter = path.iterator(); |
jjg@309 | 75 | while (iter.hasNext()) { |
jjg@309 | 76 | iter.next(); |
jjg@309 | 77 | ++normalLoopLength; |
jjg@309 | 78 | } |
jjg@309 | 79 | if (normalLoopLength != expectedLength) |
jjg@309 | 80 | throw new RuntimeException("found path length is wrong"); |
jjg@309 | 81 | |
jjg@309 | 82 | TreePath curr = path; |
jjg@309 | 83 | // using getParent |
jjg@309 | 84 | int whileLoopLength = 0; |
jjg@309 | 85 | while (curr != null) { |
jjg@309 | 86 | ++whileLoopLength; |
jjg@309 | 87 | curr = curr.getParentPath(); |
jjg@309 | 88 | } |
jjg@309 | 89 | if (whileLoopLength != expectedLength) |
jjg@309 | 90 | throw new RuntimeException("found path length is wrong"); |
jjg@309 | 91 | } |
jjg@309 | 92 | |
jjg@309 | 93 | @Override |
jjg@309 | 94 | public SourceVersion getSupportedSourceVersion() { |
jjg@309 | 95 | return SourceVersion.latest(); |
jjg@309 | 96 | } |
jjg@309 | 97 | |
jjg@309 | 98 | File writeTestFile() throws IOException { |
jjg@309 | 99 | File f = new File("Test.java"); |
jjg@309 | 100 | PrintWriter out = new PrintWriter(new FileWriter(f)); |
jjg@309 | 101 | out.println("class Test { void method() { } }"); |
jjg@309 | 102 | out.close(); |
jjg@309 | 103 | return f; |
jjg@309 | 104 | } |
jjg@309 | 105 | |
jjg@309 | 106 | public void run() throws IOException { |
jjg@309 | 107 | JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); |
jjg@309 | 108 | StandardJavaFileManager fileManager |
jjg@309 | 109 | = compiler.getStandardFileManager(null, null, null); |
jjg@309 | 110 | Iterable<? extends JavaFileObject> tests |
jjg@309 | 111 | = fileManager.getJavaFileObjects(writeTestFile()); |
jjg@309 | 112 | |
jjg@309 | 113 | JavaCompiler.CompilationTask task = |
jjg@309 | 114 | ToolProvider.getSystemJavaCompiler().getTask( |
jjg@309 | 115 | null, null, null, |
jjg@309 | 116 | Arrays.asList("-processor", this.getClass().getName()), null, |
jjg@309 | 117 | tests); |
jjg@309 | 118 | task.call(); |
jjg@309 | 119 | } |
jjg@309 | 120 | |
jjg@309 | 121 | public static void main(String[] args) throws IOException { |
jjg@309 | 122 | new TestTreePath().run(); |
jjg@309 | 123 | } |
jjg@309 | 124 | } |