1/* 2 * Copyright (c) 2011, 2015, 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 */ 23 24/* 25 * @test 26 * @bug 7090249 27 * @summary IllegalStateException from Trees.getScope when called from JSR 199 28 * @modules jdk.compiler 29 */ 30 31import com.sun.source.tree.IdentifierTree; 32import java.io.File; 33import java.io.IOException; 34import java.util.Arrays; 35import java.util.Collections; 36import java.util.List; 37import java.util.Set; 38import javax.annotation.processing.AbstractProcessor; 39import javax.annotation.processing.RoundEnvironment; 40import javax.lang.model.element.Element; 41import javax.lang.model.element.TypeElement; 42import javax.tools.JavaCompiler; 43import javax.tools.JavaFileObject; 44import javax.tools.StandardJavaFileManager; 45import javax.tools.ToolProvider; 46 47import com.sun.source.util.JavacTask; 48import com.sun.source.util.TreePath; 49import com.sun.source.util.TreePathScanner; 50import com.sun.source.util.Trees; 51import javax.annotation.processing.SupportedAnnotationTypes; 52import javax.lang.model.SourceVersion; 53 54@SupportedAnnotationTypes("*") 55public class TestGetScope extends AbstractProcessor { 56 public static void main(String... args) throws IOException { 57 new TestGetScope().run(); 58 } 59 60 public void run() throws IOException { 61 File srcDir = new File(System.getProperty("test.src")); 62 File thisFile = new File(srcDir, getClass().getName() + ".java"); 63 64 JavaCompiler c = ToolProvider.getSystemJavaCompiler(); 65 try (StandardJavaFileManager fm = c.getStandardFileManager(null, null, null)) { 66 67 List<String> opts = Arrays.asList("-proc:only", "-doe"); 68 Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(thisFile); 69 JavacTask t = (JavacTask) c.getTask(null, fm, null, opts, null, files); 70 t.setProcessors(Collections.singleton(this)); 71 boolean ok = t.call(); 72 if (!ok) 73 throw new Error("compilation failed"); 74 } 75 } 76 77 @Override 78 public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { 79 Trees trees = Trees.instance(processingEnv); 80 if (round++ == 0) { 81 for (Element e: roundEnv.getRootElements()) { 82 TreePath p = trees.getPath(e); 83 new Scanner().scan(p, trees); 84 } 85 } 86 return false; 87 } 88 89 @Override 90 public SourceVersion getSupportedSourceVersion() { 91 return SourceVersion.latest(); 92 } 93 94 int round; 95 96 static class Scanner extends TreePathScanner<Void,Trees> { 97 @Override 98 public Void visitIdentifier(IdentifierTree t, Trees trees) { 99 System.err.println("visitIdentifier: " + t); 100 trees.getScope(getCurrentPath()); 101 return null; 102 } 103 } 104} 105