6986246: Trees object is round-specific
authorjjg
Mon, 27 Sep 2010 17:28:49 -0700
changeset 6717 0103d76cfe48
parent 6716 71df48777dd1
child 6718 56ed1962d369
6986246: Trees object is round-specific Reviewed-by: darcy
langtools/src/share/classes/com/sun/tools/javac/api/JavacTrees.java
langtools/src/share/classes/com/sun/tools/javac/processing/JavacProcessingEnvironment.java
langtools/test/tools/javac/processing/model/util/elements/doccomments/TestDocComments.java
langtools/test/tools/javac/tree/TreePosRoundsTest.java
--- a/langtools/src/share/classes/com/sun/tools/javac/api/JavacTrees.java	Mon Sep 27 14:20:39 2010 -0700
+++ b/langtools/src/share/classes/com/sun/tools/javac/api/JavacTrees.java	Mon Sep 27 17:28:49 2010 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2005, 2008, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2005, 2010, 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
@@ -56,7 +56,6 @@
 import com.sun.tools.javac.comp.MemberEnter;
 import com.sun.tools.javac.comp.Resolve;
 import com.sun.tools.javac.model.JavacElements;
-import com.sun.tools.javac.processing.JavacMessager;
 import com.sun.tools.javac.processing.JavacProcessingEnvironment;
 import com.sun.tools.javac.tree.JCTree.*;
 import com.sun.tools.javac.tree.JCTree;
@@ -81,14 +80,15 @@
  */
 public class JavacTrees extends Trees {
 
-    private final Resolve resolve;
-    private final Enter enter;
-    private final Log log;
-    private final MemberEnter memberEnter;
-    private final Attr attr;
-    private final TreeMaker treeMaker;
-    private final JavacElements elements;
-    private final JavacTaskImpl javacTaskImpl;
+    // in a world of a single context per compilation, these would all be final
+    private Resolve resolve;
+    private Enter enter;
+    private Log log;
+    private MemberEnter memberEnter;
+    private Attr attr;
+    private TreeMaker treeMaker;
+    private JavacElements elements;
+    private JavacTaskImpl javacTaskImpl;
 
     public static JavacTrees instance(JavaCompiler.CompilationTask task) {
         if (!(task instanceof JavacTaskImpl))
@@ -111,6 +111,14 @@
 
     private JavacTrees(Context context) {
         context.put(JavacTrees.class, this);
+        init(context);
+    }
+
+    public void updateContext(Context context) {
+        init(context);
+    }
+
+    private void init(Context context) {
         attr = Attr.instance(context);
         enter = Enter.instance(context);
         elements = JavacElements.instance(context);
@@ -337,6 +345,7 @@
             super(M);
         }
 
+        @Override
         public <T extends JCTree> T copy(T t, JCTree leaf) {
             T t2 = super.copy(t, leaf);
             if (t == leaf)
--- a/langtools/src/share/classes/com/sun/tools/javac/processing/JavacProcessingEnvironment.java	Mon Sep 27 14:20:39 2010 -0700
+++ b/langtools/src/share/classes/com/sun/tools/javac/processing/JavacProcessingEnvironment.java	Mon Sep 27 17:28:49 2010 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2005, 2009, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2005, 2010, 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
@@ -49,6 +49,7 @@
 import javax.tools.JavaFileObject;
 import javax.tools.DiagnosticListener;
 
+import com.sun.tools.javac.api.JavacTrees;
 import com.sun.source.util.AbstractTypeProcessor;
 import com.sun.source.util.TaskEvent;
 import com.sun.source.util.TaskListener;
@@ -1104,6 +1105,12 @@
                 task.updateContext(next);
             }
 
+            JavacTrees trees = context.get(JavacTrees.class);
+            if (trees != null) {
+                next.put(JavacTrees.class, trees);
+                trees.updateContext(next);
+            }
+
             context.clear();
             return next;
         }
--- a/langtools/test/tools/javac/processing/model/util/elements/doccomments/TestDocComments.java	Mon Sep 27 14:20:39 2010 -0700
+++ b/langtools/test/tools/javac/processing/model/util/elements/doccomments/TestDocComments.java	Mon Sep 27 17:28:49 2010 -0700
@@ -23,7 +23,7 @@
 
 /*
  * @test
- * @bug 6877202
+ * @bug 6877202 6986246
  * @summary Elements.getDocComment() is not getting JavaDocComments
  */
 
@@ -139,6 +139,7 @@
     Filer filer;
     Messager messager;
     Elements elements;
+    Trees trees;
     ScanKind skind;
 
     int round = 0;
@@ -155,6 +156,7 @@
         filer = pEnv.getFiler();
         messager = pEnv.getMessager();
         elements = pEnv.getElementUtils();
+        trees = Trees.instance(processingEnv);
         skind = ScanKind.valueOf(options.get("scan"));
     }
 
@@ -167,7 +169,6 @@
         for (Element e: roundEnv.getRootElements()) {
             System.err.println("scan " + skind + " " + e.getKind() + " " + e.getSimpleName());
             if (skind == ScanKind.TREE) {
-                Trees trees = Trees.instance(processingEnv); // cannot cache this across rounds
                 new TestTreeScanner().scan(trees.getPath(e), trees);
             }  else
                 new TestElementScanner().scan(e);
--- a/langtools/test/tools/javac/tree/TreePosRoundsTest.java	Mon Sep 27 14:20:39 2010 -0700
+++ b/langtools/test/tools/javac/tree/TreePosRoundsTest.java	Mon Sep 27 17:28:49 2010 -0700
@@ -23,7 +23,7 @@
 
 /*
  * @test
- * @bug 6985205
+ * @bug 6985205 6986246
  * @summary access to tree positions and doc comments may be lost across annotation processing rounds
  * @build TreePosRoundsTest
  * @compile -proc:only -processor TreePosRoundsTest TreePosRoundsTest.java
@@ -70,6 +70,7 @@
 
     Filer filer;
     Messager messager;
+    Trees trees;
 
     @Override
     public SourceVersion getSupportedSourceVersion() {
@@ -81,6 +82,7 @@
         super.init(pEnv);
         filer = pEnv.getFiler();
         messager = pEnv.getMessager();
+        trees = Trees.instance(pEnv);
     }
 
     int round = 0;
@@ -92,7 +94,6 @@
         // Scan trees for elements, verifying source tree positions
         for (Element e: roundEnv.getRootElements()) {
             try {
-                Trees trees = Trees.instance(processingEnv); // cannot cache this across rounds
                 TreePath p = trees.getPath(e);
                 new TestTreeScanner(p.getCompilationUnit(), trees).scan(trees.getPath(e), null);
             } catch (IOException ex) {