From 5c99f60254a6429af80ffbd5264b4c3f4a876231 Mon Sep 17 00:00:00 2001 From: jjg Date: Mon, 27 Sep 2010 17:28:49 -0700 Subject: [PATCH] 6986246: Trees object is round-specific Reviewed-by: darcy --- .../com/sun/tools/javac/api/JavacTrees.java | 29 ++++++++++++------- .../JavacProcessingEnvironment.java | 9 +++++- .../elements/doccomments/TestDocComments.java | 5 ++-- test/tools/javac/tree/TreePosRoundsTest.java | 5 ++-- 4 files changed, 33 insertions(+), 15 deletions(-) diff --git a/src/share/classes/com/sun/tools/javac/api/JavacTrees.java b/src/share/classes/com/sun/tools/javac/api/JavacTrees.java index 889beb95..e1c6053b 100644 --- a/src/share/classes/com/sun/tools/javac/api/JavacTrees.java +++ b/src/share/classes/com/sun/tools/javac/api/JavacTrees.java @@ -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.Env; 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 @@ import com.sun.tools.javac.util.Pair; */ 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 @@ public class JavacTrees extends Trees { 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 @@ public class JavacTrees extends Trees { super(M); } + @Override public T copy(T t, JCTree leaf) { T t2 = super.copy(t, leaf); if (t == leaf) diff --git a/src/share/classes/com/sun/tools/javac/processing/JavacProcessingEnvironment.java b/src/share/classes/com/sun/tools/javac/processing/JavacProcessingEnvironment.java index f6623ef9..b393fe52 100644 --- a/src/share/classes/com/sun/tools/javac/processing/JavacProcessingEnvironment.java +++ b/src/share/classes/com/sun/tools/javac/processing/JavacProcessingEnvironment.java @@ -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.StandardJavaFileManager; 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 @@ public class JavacProcessingEnvironment implements ProcessingEnvironment, Closea task.updateContext(next); } + JavacTrees trees = context.get(JavacTrees.class); + if (trees != null) { + next.put(JavacTrees.class, trees); + trees.updateContext(next); + } + context.clear(); return next; } diff --git a/test/tools/javac/processing/model/util/elements/doccomments/TestDocComments.java b/test/tools/javac/processing/model/util/elements/doccomments/TestDocComments.java index d96a79b3..57cb1c3b 100644 --- a/test/tools/javac/processing/model/util/elements/doccomments/TestDocComments.java +++ b/test/tools/javac/processing/model/util/elements/doccomments/TestDocComments.java @@ -23,7 +23,7 @@ /* * @test - * @bug 6877202 + * @bug 6877202 6986246 * @summary Elements.getDocComment() is not getting JavaDocComments */ @@ -139,6 +139,7 @@ public class TestDocComments extends AbstractProcessor { Filer filer; Messager messager; Elements elements; + Trees trees; ScanKind skind; int round = 0; @@ -155,6 +156,7 @@ public class TestDocComments extends AbstractProcessor { filer = pEnv.getFiler(); messager = pEnv.getMessager(); elements = pEnv.getElementUtils(); + trees = Trees.instance(processingEnv); skind = ScanKind.valueOf(options.get("scan")); } @@ -167,7 +169,6 @@ public class TestDocComments extends AbstractProcessor { 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); diff --git a/test/tools/javac/tree/TreePosRoundsTest.java b/test/tools/javac/tree/TreePosRoundsTest.java index 40ff35a5..29a41b60 100644 --- a/test/tools/javac/tree/TreePosRoundsTest.java +++ b/test/tools/javac/tree/TreePosRoundsTest.java @@ -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 @@ public class TreePosRoundsTest extends AbstractProcessor { Filer filer; Messager messager; + Trees trees; @Override public SourceVersion getSupportedSourceVersion() { @@ -81,6 +82,7 @@ public class TreePosRoundsTest extends AbstractProcessor { super.init(pEnv); filer = pEnv.getFiler(); messager = pEnv.getMessager(); + trees = Trees.instance(pEnv); } int round = 0; @@ -92,7 +94,6 @@ public class TreePosRoundsTest extends AbstractProcessor { // 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) { -- GitLab