Mon, 21 Jan 2013 11:16:28 -0800
Merge
1 /*
2 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
24 /*
25 * @test
26 * @bug 7090249
27 * @summary IllegalStateException from Trees.getScope when called from JSR 199
28 */
30 import com.sun.source.tree.IdentifierTree;
31 import java.io.File;
32 import java.util.Arrays;
33 import java.util.Collections;
34 import java.util.List;
35 import java.util.Set;
36 import javax.annotation.processing.AbstractProcessor;
37 import javax.annotation.processing.RoundEnvironment;
38 import javax.lang.model.element.Element;
39 import javax.lang.model.element.TypeElement;
40 import javax.tools.JavaCompiler;
41 import javax.tools.JavaFileObject;
42 import javax.tools.StandardJavaFileManager;
43 import javax.tools.ToolProvider;
45 import com.sun.source.util.JavacTask;
46 import com.sun.source.util.TreePath;
47 import com.sun.source.util.TreePathScanner;
48 import com.sun.source.util.Trees;
49 import javax.annotation.processing.SupportedAnnotationTypes;
50 import javax.lang.model.SourceVersion;
52 @SupportedAnnotationTypes("*")
53 public class TestGetScope extends AbstractProcessor {
54 public static void main(String... args) {
55 new TestGetScope().run();
56 }
58 public void run() {
59 File srcDir = new File(System.getProperty("test.src"));
60 File thisFile = new File(srcDir, getClass().getName() + ".java");
62 JavaCompiler c = ToolProvider.getSystemJavaCompiler();
63 StandardJavaFileManager fm = c.getStandardFileManager(null, null, null);
65 List<String> opts = Arrays.asList("-proc:only", "-doe");
66 Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(thisFile);
67 JavacTask t = (JavacTask) c.getTask(null, fm, null, opts, null, files);
68 t.setProcessors(Collections.singleton(this));
69 boolean ok = t.call();
70 if (!ok)
71 throw new Error("compilation failed");
72 }
74 @Override
75 public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
76 Trees trees = Trees.instance(processingEnv);
77 if (round++ == 0) {
78 for (Element e: roundEnv.getRootElements()) {
79 TreePath p = trees.getPath(e);
80 new Scanner().scan(p, trees);
81 }
82 }
83 return false;
84 }
86 @Override
87 public SourceVersion getSupportedSourceVersion() {
88 return SourceVersion.latest();
89 }
91 int round;
93 static class Scanner extends TreePathScanner<Void,Trees> {
94 @Override
95 public Void visitIdentifier(IdentifierTree t, Trees trees) {
96 System.err.println("visitIdentifier: " + t);
97 trees.getScope(getCurrentPath());
98 return null;
99 }
100 }
101 }