Tue, 05 Oct 2010 11:34:43 -0700
6988836: A new JavacElements is created for each round of annotation processing
Reviewed-by: darcy
1.1 --- a/src/share/classes/com/sun/tools/javac/model/JavacElements.java Sun Oct 03 19:40:15 2010 +0100 1.2 +++ b/src/share/classes/com/sun/tools/javac/model/JavacElements.java Tue Oct 05 11:34:43 2010 -0700 1.3 @@ -66,32 +66,26 @@ 1.4 private Types types; 1.5 private Enter enter; 1.6 1.7 - private static final Context.Key<JavacElements> KEY = 1.8 - new Context.Key<JavacElements>(); 1.9 - 1.10 public static JavacElements instance(Context context) { 1.11 - JavacElements instance = context.get(KEY); 1.12 - if (instance == null) { 1.13 + JavacElements instance = context.get(JavacElements.class); 1.14 + if (instance == null) 1.15 instance = new JavacElements(context); 1.16 - context.put(KEY, instance); 1.17 - } 1.18 return instance; 1.19 } 1.20 1.21 /** 1.22 * Public for use only by JavacProcessingEnvironment 1.23 */ 1.24 - // TODO JavacElements constructor should be protected 1.25 - public JavacElements(Context context) { 1.26 + protected JavacElements(Context context) { 1.27 setContext(context); 1.28 } 1.29 1.30 /** 1.31 * Use a new context. May be called from outside to update 1.32 * internal state for a new annotation-processing round. 1.33 - * This instance is *not* then registered with the new context. 1.34 */ 1.35 public void setContext(Context context) { 1.36 + context.put(JavacElements.class, this); 1.37 javaCompiler = JavaCompiler.instance(context); 1.38 syms = Symtab.instance(context); 1.39 names = Names.instance(context);
2.1 --- a/src/share/classes/com/sun/tools/javac/model/JavacTypes.java Sun Oct 03 19:40:15 2010 +0100 2.2 +++ b/src/share/classes/com/sun/tools/javac/model/JavacTypes.java Tue Oct 05 11:34:43 2010 -0700 2.3 @@ -47,32 +47,26 @@ 2.4 private Symtab syms; 2.5 private Types types; 2.6 2.7 - private static final Context.Key<JavacTypes> KEY = 2.8 - new Context.Key<JavacTypes>(); 2.9 - 2.10 public static JavacTypes instance(Context context) { 2.11 - JavacTypes instance = context.get(KEY); 2.12 - if (instance == null) { 2.13 + JavacTypes instance = context.get(JavacTypes.class); 2.14 + if (instance == null) 2.15 instance = new JavacTypes(context); 2.16 - context.put(KEY, instance); 2.17 - } 2.18 return instance; 2.19 } 2.20 2.21 /** 2.22 * Public for use only by JavacProcessingEnvironment 2.23 */ 2.24 - // TODO JavacTypes constructor should be protected 2.25 - public JavacTypes(Context context) { 2.26 + protected JavacTypes(Context context) { 2.27 setContext(context); 2.28 } 2.29 2.30 /** 2.31 * Use a new context. May be called from outside to update 2.32 * internal state for a new annotation-processing round. 2.33 - * This instance is *not* then registered with the new context. 2.34 */ 2.35 public void setContext(Context context) { 2.36 + context.put(JavacTypes.class, this); 2.37 syms = Symtab.instance(context); 2.38 types = Types.instance(context); 2.39 }
3.1 --- a/src/share/classes/com/sun/tools/javac/processing/JavacProcessingEnvironment.java Sun Oct 03 19:40:15 2010 +0100 3.2 +++ b/src/share/classes/com/sun/tools/javac/processing/JavacProcessingEnvironment.java Tue Oct 05 11:34:43 2010 -0700 3.3 @@ -173,12 +173,12 @@ 3.4 platformAnnotations = initPlatformAnnotations(); 3.5 foundTypeProcessors = false; 3.6 3.7 - // Initialize services before any processors are initialzied 3.8 + // Initialize services before any processors are initialized 3.9 // in case processors use them. 3.10 filer = new JavacFiler(context); 3.11 messager = new JavacMessager(context, this); 3.12 - elementUtils = new JavacElements(context); 3.13 - typeUtils = new JavacTypes(context); 3.14 + elementUtils = JavacElements.instance(context); 3.15 + typeUtils = JavacTypes.instance(context); 3.16 processorOptions = initProcessorOptions(context); 3.17 unmatchedProcessorOptions = initUnmatchedProcessorOptions(); 3.18 messages = JavacMessages.instance(context); 3.19 @@ -865,8 +865,6 @@ 3.20 this(prev.nextContext(), prev.number+1, prev.compiler.log.nwarnings); 3.21 this.genClassFiles = prev.genClassFiles; 3.22 3.23 - updateProcessingState(); 3.24 - 3.25 List<JCCompilationUnit> parsedFiles = compiler.parseFiles(newSourceFiles); 3.26 roots = cleanTrees(prev.roots).appendList(parsedFiles); 3.27 3.28 @@ -1029,15 +1027,6 @@ 3.29 log.reportDeferredDiagnostics(kinds); 3.30 } 3.31 3.32 - /** Update the processing state for the current context. */ 3.33 - private void updateProcessingState() { 3.34 - filer.newRound(context); 3.35 - messager.newRound(context); 3.36 - 3.37 - elementUtils.setContext(context); 3.38 - typeUtils.setContext(context); 3.39 - } 3.40 - 3.41 /** Print info about this round. */ 3.42 private void printRoundInfo(boolean lastRound) { 3.43 if (printRounds || verbose) { 3.44 @@ -1100,6 +1089,11 @@ 3.45 JavaCompiler nextCompiler = JavaCompiler.instance(next); 3.46 nextCompiler.initRound(oldCompiler); 3.47 3.48 + filer.newRound(next); 3.49 + messager.newRound(next); 3.50 + elementUtils.setContext(next); 3.51 + typeUtils.setContext(next); 3.52 + 3.53 JavacTaskImpl task = context.get(JavacTaskImpl.class); 3.54 if (task != null) { 3.55 next.put(JavacTaskImpl.class, task);
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/test/tools/javac/processing/environment/round/TestContext.java Tue Oct 05 11:34:43 2010 -0700 4.3 @@ -0,0 +1,96 @@ 4.4 +/* 4.5 + * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. 4.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4.7 + * 4.8 + * This code is free software; you can redistribute it and/or modify it 4.9 + * under the terms of the GNU General Public License version 2 only, as 4.10 + * published by the Free Software Foundation. 4.11 + * 4.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 4.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 4.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 4.15 + * version 2 for more details (a copy is included in the LICENSE file that 4.16 + * accompanied this code). 4.17 + * 4.18 + * You should have received a copy of the GNU General Public License version 4.19 + * 2 along with this work; if not, write to the Free Software Foundation, 4.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 4.21 + * 4.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 4.23 + * or visit www.oracle.com if you need additional information or have any 4.24 + * questions. 4.25 + */ 4.26 + 4.27 +/* 4.28 + * @test 4.29 + * @bug 6988836 4.30 + * @summary A new JavacElements is created for each round of annotation processing 4.31 + * @library ../../../lib 4.32 + * @build JavacTestingAbstractProcessor TestContext 4.33 + * @compile/process -processor TestContext -XprintRounds TestContext 4.34 + */ 4.35 + 4.36 +import java.io.*; 4.37 +import java.util.*; 4.38 +import javax.annotation.processing.*; 4.39 +import javax.lang.model.element.*; 4.40 +import javax.tools.*; 4.41 +import static javax.tools.Diagnostic.Kind.*; 4.42 + 4.43 +import com.sun.source.util.Trees; 4.44 +import com.sun.tools.javac.api.JavacTrees; 4.45 +import com.sun.tools.javac.model.JavacElements; 4.46 +import com.sun.tools.javac.model.JavacTypes; 4.47 +import com.sun.tools.javac.processing.JavacProcessingEnvironment; 4.48 +import com.sun.tools.javac.util.Context; 4.49 + 4.50 +public class TestContext extends JavacTestingAbstractProcessor { 4.51 + 4.52 + Trees treeUtils; 4.53 + int round = 0; 4.54 + 4.55 + @Override 4.56 + public void init(ProcessingEnvironment pEnv) { 4.57 + super.init(pEnv); 4.58 + treeUtils = Trees.instance(processingEnv); 4.59 + } 4.60 + 4.61 + @Override 4.62 + public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { 4.63 + round++; 4.64 + 4.65 + JavacProcessingEnvironment jpe = (JavacProcessingEnvironment) processingEnv; 4.66 + Context c = jpe.getContext(); 4.67 + check(c.get(JavacElements.class), eltUtils); 4.68 + check(c.get(JavacTypes.class), typeUtils); 4.69 + check(c.get(JavacTrees.class), treeUtils); 4.70 + 4.71 + final int MAXROUNDS = 3; 4.72 + if (round < MAXROUNDS) 4.73 + generateSource("Gen" + round); 4.74 + 4.75 + return true; 4.76 + } 4.77 + 4.78 + <T> void check(T actual, T expected) { 4.79 +// messager.printMessage(NOTE, "expect: " + expected); 4.80 +// messager.printMessage(NOTE, "actual: " + actual); 4.81 + 4.82 + if (actual != expected) { 4.83 + messager.printMessage(ERROR, 4.84 + "round " + round + " unexpected value for " + expected.getClass().getName() + ": " + actual); 4.85 + } 4.86 + } 4.87 + 4.88 + void generateSource(String name) { 4.89 + String text = "class " + name + " { }\n"; 4.90 + 4.91 + try (Writer out = filer.createSourceFile(name).openWriter()) { 4.92 + out.write(text); 4.93 + } catch (IOException e) { 4.94 + throw new Error(e); 4.95 + } 4.96 + } 4.97 + 4.98 +} 4.99 +