Wed, 14 Sep 2011 12:14:30 -0700
7090249: IllegalStateException from Trees.getScope when called from JSR 199
Reviewed-by: mcimadamore
1.1 --- a/src/share/classes/com/sun/tools/javac/api/JavacTaskImpl.java Wed Sep 14 12:07:50 2011 -0700 1.2 +++ b/src/share/classes/com/sun/tools/javac/api/JavacTaskImpl.java Wed Sep 14 12:14:30 2011 -0700 1.3 @@ -274,6 +274,9 @@ 1.4 public Iterable<? extends TypeElement> enter(Iterable<? extends CompilationUnitTree> trees) 1.5 throws IOException 1.6 { 1.7 + if (trees == null && notYetEntered != null && notYetEntered.isEmpty()) 1.8 + return List.nil(); 1.9 + 1.10 prepareCompiler(); 1.11 1.12 ListBuffer<JCCompilationUnit> roots = null;
2.1 --- a/src/share/classes/com/sun/tools/javac/api/JavacTrees.java Wed Sep 14 12:07:50 2011 -0700 2.2 +++ b/src/share/classes/com/sun/tools/javac/api/JavacTrees.java Wed Sep 14 12:14:30 2011 -0700 2.3 @@ -65,6 +65,7 @@ 2.4 import com.sun.tools.javac.tree.TreeCopier; 2.5 import com.sun.tools.javac.tree.TreeInfo; 2.6 import com.sun.tools.javac.tree.TreeMaker; 2.7 +import com.sun.tools.javac.util.Assert; 2.8 import com.sun.tools.javac.util.Context; 2.9 import com.sun.tools.javac.util.JCDiagnostic; 2.10 import com.sun.tools.javac.util.List; 2.11 @@ -263,9 +264,10 @@ 2.12 if (!(path.getLeaf() instanceof JCTree)) // implicit null-check 2.13 throw new IllegalArgumentException(); 2.14 2.15 - // if we're being invoked via from a JSR199 client, we need to make sure 2.16 - // all the classes have been entered; if we're being invoked from JSR269, 2.17 - // then the classes will already have been entered. 2.18 + // if we're being invoked from a Tree API client via parse/enter/analyze, 2.19 + // we need to make sure all the classes have been entered; 2.20 + // if we're being invoked from JSR 199 or JSR 269, then the classes 2.21 + // will already have been entered. 2.22 if (javacTaskImpl != null) { 2.23 try { 2.24 javacTaskImpl.enter(null); 2.25 @@ -313,10 +315,19 @@ 2.26 break; 2.27 case BLOCK: { 2.28 // System.err.println("BLOCK: "); 2.29 - if (method != null) 2.30 - env = memberEnter.getMethodEnv(method, env); 2.31 - JCTree body = copier.copy((JCTree)tree, (JCTree) path.getLeaf()); 2.32 - env = attribStatToTree(body, env, copier.leafCopy); 2.33 + if (method != null) { 2.34 + try { 2.35 + Assert.check(method.body == tree); 2.36 + method.body = copier.copy((JCBlock)tree, (JCTree) path.getLeaf()); 2.37 + env = memberEnter.getMethodEnv(method, env); 2.38 + env = attribStatToTree(method.body, env, copier.leafCopy); 2.39 + } finally { 2.40 + method.body = (JCBlock) tree; 2.41 + } 2.42 + } else { 2.43 + JCBlock body = copier.copy((JCBlock)tree, (JCTree) path.getLeaf()); 2.44 + env = attribStatToTree(body, env, copier.leafCopy); 2.45 + } 2.46 return env; 2.47 } 2.48 default: 2.49 @@ -329,7 +340,7 @@ 2.50 } 2.51 } 2.52 } 2.53 - return field != null ? memberEnter.getInitEnv(field, env) : env; 2.54 + return (field != null) ? memberEnter.getInitEnv(field, env) : env; 2.55 } 2.56 2.57 private Env<AttrContext> attribStatToTree(JCTree stat, Env<AttrContext>env, JCTree tree) {
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/test/tools/javac/api/TestGetScope.java Wed Sep 14 12:14:30 2011 -0700 3.3 @@ -0,0 +1,101 @@ 3.4 +/* 3.5 + * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. 3.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3.7 + * 3.8 + * This code is free software; you can redistribute it and/or modify it 3.9 + * under the terms of the GNU General Public License version 2 only, as 3.10 + * published by the Free Software Foundation. 3.11 + * 3.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 3.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 3.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 3.15 + * version 2 for more details (a copy is included in the LICENSE file that 3.16 + * accompanied this code). 3.17 + * 3.18 + * You should have received a copy of the GNU General Public License version 3.19 + * 2 along with this work; if not, write to the Free Software Foundation, 3.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 3.21 + * 3.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 3.23 + * or visit www.oracle.com if you need additional information or have any 3.24 + * questions. 3.25 + */ 3.26 + 3.27 +/* 3.28 + * @test 3.29 + * @bug 7090249 3.30 + * @summary IllegalStateException from Trees.getScope when called from JSR 199 3.31 + */ 3.32 + 3.33 +import com.sun.source.tree.IdentifierTree; 3.34 +import java.io.File; 3.35 +import java.util.Arrays; 3.36 +import java.util.Collections; 3.37 +import java.util.List; 3.38 +import java.util.Set; 3.39 +import javax.annotation.processing.AbstractProcessor; 3.40 +import javax.annotation.processing.RoundEnvironment; 3.41 +import javax.lang.model.element.Element; 3.42 +import javax.lang.model.element.TypeElement; 3.43 +import javax.tools.JavaCompiler; 3.44 +import javax.tools.JavaFileObject; 3.45 +import javax.tools.StandardJavaFileManager; 3.46 +import javax.tools.ToolProvider; 3.47 + 3.48 +import com.sun.source.util.JavacTask; 3.49 +import com.sun.source.util.TreePath; 3.50 +import com.sun.source.util.TreePathScanner; 3.51 +import com.sun.source.util.Trees; 3.52 +import javax.annotation.processing.SupportedAnnotationTypes; 3.53 +import javax.lang.model.SourceVersion; 3.54 + 3.55 +@SupportedAnnotationTypes("*") 3.56 +public class TestGetScope extends AbstractProcessor { 3.57 + public static void main(String... args) { 3.58 + new TestGetScope().run(); 3.59 + } 3.60 + 3.61 + public void run() { 3.62 + File srcDir = new File(System.getProperty("test.src")); 3.63 + File thisFile = new File(srcDir, getClass().getName() + ".java"); 3.64 + 3.65 + JavaCompiler c = ToolProvider.getSystemJavaCompiler(); 3.66 + StandardJavaFileManager fm = c.getStandardFileManager(null, null, null); 3.67 + 3.68 + List<String> opts = Arrays.asList("-proc:only", "-doe"); 3.69 + Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(thisFile); 3.70 + JavacTask t = (JavacTask) c.getTask(null, fm, null, opts, null, files); 3.71 + t.setProcessors(Collections.singleton(this)); 3.72 + boolean ok = t.call(); 3.73 + if (!ok) 3.74 + throw new Error("compilation failed"); 3.75 + } 3.76 + 3.77 + @Override 3.78 + public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { 3.79 + Trees trees = Trees.instance(processingEnv); 3.80 + if (round++ == 0) { 3.81 + for (Element e: roundEnv.getRootElements()) { 3.82 + TreePath p = trees.getPath(e); 3.83 + new Scanner().scan(p, trees); 3.84 + } 3.85 + } 3.86 + return false; 3.87 + } 3.88 + 3.89 + @Override 3.90 + public SourceVersion getSupportedSourceVersion() { 3.91 + return SourceVersion.latest(); 3.92 + } 3.93 + 3.94 + int round; 3.95 + 3.96 + static class Scanner extends TreePathScanner<Void,Trees> { 3.97 + @Override 3.98 + public Void visitIdentifier(IdentifierTree t, Trees trees) { 3.99 + System.err.println("visitIdentifier: " + t); 3.100 + trees.getScope(getCurrentPath()); 3.101 + return null; 3.102 + } 3.103 + } 3.104 +}