changeset 1122:1807fc3fd33c

7090249: IllegalStateException from Trees.getScope when called from JSR 199 Reviewed-by: mcimadamore
author jjg
date Wed, 14 Sep 2011 12:14:30 -0700
parents 0f3da6af9799
children a6e2c1840ea1
files src/share/classes/com/sun/tools/javac/api/JavacTaskImpl.java src/share/classes/com/sun/tools/javac/api/JavacTrees.java test/tools/javac/api/TestGetScope.java
diffstat 3 files changed, 123 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/com/sun/tools/javac/api/JavacTaskImpl.java	Wed Sep 14 12:07:50 2011 -0700
+++ b/src/share/classes/com/sun/tools/javac/api/JavacTaskImpl.java	Wed Sep 14 12:14:30 2011 -0700
@@ -274,6 +274,9 @@
     public Iterable<? extends TypeElement> enter(Iterable<? extends CompilationUnitTree> trees)
         throws IOException
     {
+        if (trees == null && notYetEntered != null && notYetEntered.isEmpty())
+            return List.nil();
+
         prepareCompiler();
 
         ListBuffer<JCCompilationUnit> roots = null;
--- a/src/share/classes/com/sun/tools/javac/api/JavacTrees.java	Wed Sep 14 12:07:50 2011 -0700
+++ b/src/share/classes/com/sun/tools/javac/api/JavacTrees.java	Wed Sep 14 12:14:30 2011 -0700
@@ -65,6 +65,7 @@
 import com.sun.tools.javac.tree.TreeCopier;
 import com.sun.tools.javac.tree.TreeInfo;
 import com.sun.tools.javac.tree.TreeMaker;
+import com.sun.tools.javac.util.Assert;
 import com.sun.tools.javac.util.Context;
 import com.sun.tools.javac.util.JCDiagnostic;
 import com.sun.tools.javac.util.List;
@@ -263,9 +264,10 @@
         if (!(path.getLeaf() instanceof JCTree))  // implicit null-check
             throw new IllegalArgumentException();
 
-        // if we're being invoked via from a JSR199 client, we need to make sure
-        // all the classes have been entered; if we're being invoked from JSR269,
-        // then the classes will already have been entered.
+        // if we're being invoked from a Tree API client via parse/enter/analyze,
+        // we need to make sure all the classes have been entered;
+        // if we're being invoked from JSR 199 or JSR 269, then the classes
+        // will already have been entered.
         if (javacTaskImpl != null) {
             try {
                 javacTaskImpl.enter(null);
@@ -313,10 +315,19 @@
                     break;
                 case BLOCK: {
 //                    System.err.println("BLOCK: ");
-                    if (method != null)
-                        env = memberEnter.getMethodEnv(method, env);
-                    JCTree body = copier.copy((JCTree)tree, (JCTree) path.getLeaf());
-                    env = attribStatToTree(body, env, copier.leafCopy);
+                    if (method != null) {
+                        try {
+                            Assert.check(method.body == tree);
+                            method.body = copier.copy((JCBlock)tree, (JCTree) path.getLeaf());
+                            env = memberEnter.getMethodEnv(method, env);
+                            env = attribStatToTree(method.body, env, copier.leafCopy);
+                        } finally {
+                            method.body = (JCBlock) tree;
+                        }
+                    } else {
+                        JCBlock body = copier.copy((JCBlock)tree, (JCTree) path.getLeaf());
+                        env = attribStatToTree(body, env, copier.leafCopy);
+                    }
                     return env;
                 }
                 default:
@@ -329,7 +340,7 @@
                     }
             }
         }
-        return field != null ? memberEnter.getInitEnv(field, env) : env;
+        return (field != null) ? memberEnter.getInitEnv(field, env) : env;
     }
 
     private Env<AttrContext> attribStatToTree(JCTree stat, Env<AttrContext>env, JCTree tree) {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/tools/javac/api/TestGetScope.java	Wed Sep 14 12:14:30 2011 -0700
@@ -0,0 +1,101 @@
+/*
+ * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @bug 7090249
+ * @summary IllegalStateException from Trees.getScope when called from JSR 199
+ */
+
+import com.sun.source.tree.IdentifierTree;
+import java.io.File;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.Set;
+import javax.annotation.processing.AbstractProcessor;
+import javax.annotation.processing.RoundEnvironment;
+import javax.lang.model.element.Element;
+import javax.lang.model.element.TypeElement;
+import javax.tools.JavaCompiler;
+import javax.tools.JavaFileObject;
+import javax.tools.StandardJavaFileManager;
+import javax.tools.ToolProvider;
+
+import com.sun.source.util.JavacTask;
+import com.sun.source.util.TreePath;
+import com.sun.source.util.TreePathScanner;
+import com.sun.source.util.Trees;
+import javax.annotation.processing.SupportedAnnotationTypes;
+import javax.lang.model.SourceVersion;
+
+@SupportedAnnotationTypes("*")
+public class TestGetScope extends AbstractProcessor {
+    public static void main(String... args) {
+        new TestGetScope().run();
+    }
+
+    public void run() {
+        File srcDir = new File(System.getProperty("test.src"));
+        File thisFile = new File(srcDir, getClass().getName() + ".java");
+
+        JavaCompiler c = ToolProvider.getSystemJavaCompiler();
+        StandardJavaFileManager fm = c.getStandardFileManager(null, null, null);
+
+        List<String> opts = Arrays.asList("-proc:only", "-doe");
+        Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(thisFile);
+        JavacTask t = (JavacTask) c.getTask(null, fm, null, opts, null, files);
+        t.setProcessors(Collections.singleton(this));
+        boolean ok = t.call();
+        if (!ok)
+            throw new Error("compilation failed");
+    }
+
+    @Override
+    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
+        Trees trees = Trees.instance(processingEnv);
+        if (round++ == 0) {
+            for (Element e: roundEnv.getRootElements()) {
+                TreePath p = trees.getPath(e);
+                new Scanner().scan(p, trees);
+            }
+        }
+        return false;
+    }
+
+    @Override
+    public SourceVersion getSupportedSourceVersion() {
+        return SourceVersion.latest();
+    }
+
+    int round;
+
+    static class Scanner extends TreePathScanner<Void,Trees> {
+        @Override
+        public Void visitIdentifier(IdentifierTree t, Trees trees) {
+            System.err.println("visitIdentifier: " + t);
+            trees.getScope(getCurrentPath());
+            return null;
+        }
+    }
+}