51 import com.sun.source.util.TaskEvent;
52 import com.sun.source.util.TaskEvent.Kind;
53 import com.sun.source.util.TaskListener;
54 import com.sun.source.util.TreeScanner;
55 import com.sun.tools.javac.code.Kinds;
56 import com.sun.tools.javac.code.Preview;
57 import com.sun.tools.javac.code.Symbol;
58 import com.sun.tools.javac.code.Symtab;
59 import com.sun.tools.javac.code.Type;
60 import com.sun.tools.javac.code.Type.ClassType;
61 import com.sun.tools.javac.code.TypeTag;
62 import com.sun.tools.javac.code.Types;
63 import com.sun.tools.javac.comp.Annotate;
64 import com.sun.tools.javac.comp.Check;
65 import com.sun.tools.javac.comp.CompileStates;
66 import com.sun.tools.javac.comp.Enter;
67 import com.sun.tools.javac.comp.Modules;
68 import com.sun.tools.javac.main.Arguments;
69 import com.sun.tools.javac.main.JavaCompiler;
70 import com.sun.tools.javac.model.JavacElements;
71 import com.sun.tools.javac.tree.JCTree.JCClassDecl;
72 import com.sun.tools.javac.tree.JCTree.LetExpr;
73 import com.sun.tools.javac.util.Context;
74 import com.sun.tools.javac.util.DefinedBy;
75 import com.sun.tools.javac.util.DefinedBy.Api;
76 import com.sun.tools.javac.util.Log;
77
78 /**
79 * A pool of reusable JavacTasks. When a task is no valid anymore, it is returned to the pool,
80 * and its Context may be reused for future processing in some cases. The reuse is achieved
81 * by replacing some components (most notably JavaCompiler and Log) with reusable counterparts,
82 * and by cleaning up leftovers from previous compilation.
83 * <p>
84 * For each combination of options, a separate task/context is created and kept, as most option
85 * values are cached inside components themselves.
86 * <p>
87 * When the compilation redefines sensitive classes (e.g. classes in the the java.* packages), the
88 * task/context is not reused.
89 * <p>
90 * When the task is reused, then packages that were already listed won't be listed again.
91 * <p>
92 * Care must be taken to only return tasks that won't be used by the original caller.
93 * <p>
94 * Care must also be taken when custom components are installed, as those are not cleaned when the
95 * task/context is reused, and subsequent getTask may return a task based on a context with these
96 * custom components.
235
236 int useCount;
237 long timeStamp;
238
239 ReusableContext(List<String> arguments) {
240 super();
241 this.arguments = arguments;
242 put(Log.logKey, ReusableLog.factory);
243 put(JavaCompiler.compilerKey, ReusableJavaCompiler.factory);
244 }
245
246 void clear() {
247 drop(Arguments.argsKey);
248 drop(DiagnosticListener.class);
249 drop(Log.outKey);
250 drop(Log.errKey);
251 drop(JavaFileManager.class);
252 drop(JavacTask.class);
253 drop(JavacTrees.class);
254 drop(JavacElements.class);
255
256 if (ht.get(Log.logKey) instanceof ReusableLog) {
257 //log already inited - not first round
258 ((ReusableLog)Log.instance(this)).clear();
259 Enter.instance(this).newRound();
260 ((ReusableJavaCompiler)ReusableJavaCompiler.instance(this)).clear();
261 Types.instance(this).newRound();
262 Check.instance(this).newRound();
263 Check.instance(this).clear(); //clear mandatory warning handlers
264 Preview.instance(this).clear(); //clear mandatory warning handlers
265 Modules.instance(this).newRound();
266 Annotate.instance(this).newRound();
267 CompileStates.instance(this).clear();
268 MultiTaskListener.instance(this).clear();
269
270 //find if any of the roots have redefined java.* classes
271 Symtab syms = Symtab.instance(this);
272 pollutionScanner.scan(roots, syms);
273 roots.clear();
274 }
275 }
276
277 /**
278 * This scanner detects as to whether the shared context has been polluted. This happens
279 * whenever a compiled program redefines a core class (in 'java.*' package) or when
280 * (typically because of cyclic inheritance) the symbol kind of a core class has been touched.
281 */
282 TreeScanner<Void, Symtab> pollutionScanner = new TreeScanner<Void, Symtab>() {
283 @Override @DefinedBy(Api.COMPILER_TREE)
284 public Void scan(Tree tree, Symtab syms) {
285 if (tree instanceof LetExpr) {
286 LetExpr le = (LetExpr) tree;
287 scan(le.defs, syms);
288 scan(le.expr, syms);
|
51 import com.sun.source.util.TaskEvent;
52 import com.sun.source.util.TaskEvent.Kind;
53 import com.sun.source.util.TaskListener;
54 import com.sun.source.util.TreeScanner;
55 import com.sun.tools.javac.code.Kinds;
56 import com.sun.tools.javac.code.Preview;
57 import com.sun.tools.javac.code.Symbol;
58 import com.sun.tools.javac.code.Symtab;
59 import com.sun.tools.javac.code.Type;
60 import com.sun.tools.javac.code.Type.ClassType;
61 import com.sun.tools.javac.code.TypeTag;
62 import com.sun.tools.javac.code.Types;
63 import com.sun.tools.javac.comp.Annotate;
64 import com.sun.tools.javac.comp.Check;
65 import com.sun.tools.javac.comp.CompileStates;
66 import com.sun.tools.javac.comp.Enter;
67 import com.sun.tools.javac.comp.Modules;
68 import com.sun.tools.javac.main.Arguments;
69 import com.sun.tools.javac.main.JavaCompiler;
70 import com.sun.tools.javac.model.JavacElements;
71 import com.sun.tools.javac.platform.PlatformDescription;
72 import com.sun.tools.javac.tree.JCTree.JCClassDecl;
73 import com.sun.tools.javac.tree.JCTree.LetExpr;
74 import com.sun.tools.javac.util.Context;
75 import com.sun.tools.javac.util.DefinedBy;
76 import com.sun.tools.javac.util.DefinedBy.Api;
77 import com.sun.tools.javac.util.Log;
78 import com.sun.tools.javac.util.Options;
79
80 /**
81 * A pool of reusable JavacTasks. When a task is no valid anymore, it is returned to the pool,
82 * and its Context may be reused for future processing in some cases. The reuse is achieved
83 * by replacing some components (most notably JavaCompiler and Log) with reusable counterparts,
84 * and by cleaning up leftovers from previous compilation.
85 * <p>
86 * For each combination of options, a separate task/context is created and kept, as most option
87 * values are cached inside components themselves.
88 * <p>
89 * When the compilation redefines sensitive classes (e.g. classes in the the java.* packages), the
90 * task/context is not reused.
91 * <p>
92 * When the task is reused, then packages that were already listed won't be listed again.
93 * <p>
94 * Care must be taken to only return tasks that won't be used by the original caller.
95 * <p>
96 * Care must also be taken when custom components are installed, as those are not cleaned when the
97 * task/context is reused, and subsequent getTask may return a task based on a context with these
98 * custom components.
237
238 int useCount;
239 long timeStamp;
240
241 ReusableContext(List<String> arguments) {
242 super();
243 this.arguments = arguments;
244 put(Log.logKey, ReusableLog.factory);
245 put(JavaCompiler.compilerKey, ReusableJavaCompiler.factory);
246 }
247
248 void clear() {
249 drop(Arguments.argsKey);
250 drop(DiagnosticListener.class);
251 drop(Log.outKey);
252 drop(Log.errKey);
253 drop(JavaFileManager.class);
254 drop(JavacTask.class);
255 drop(JavacTrees.class);
256 drop(JavacElements.class);
257 drop(PlatformDescription.class);
258
259 if (ht.get(Log.logKey) instanceof ReusableLog) {
260 //log already inited - not first round
261 ((ReusableLog)Log.instance(this)).clear();
262 Enter.instance(this).newRound();
263 ((ReusableJavaCompiler)ReusableJavaCompiler.instance(this)).clear();
264 Types.instance(this).newRound();
265 Check.instance(this).newRound();
266 Check.instance(this).clear(); //clear mandatory warning handlers
267 Preview.instance(this).clear(); //clear mandatory warning handlers
268 Modules.instance(this).newRound();
269 Annotate.instance(this).newRound();
270 CompileStates.instance(this).clear();
271 MultiTaskListener.instance(this).clear();
272 Options.instance(this).clear();
273
274 //find if any of the roots have redefined java.* classes
275 Symtab syms = Symtab.instance(this);
276 pollutionScanner.scan(roots, syms);
277 roots.clear();
278 }
279 }
280
281 /**
282 * This scanner detects as to whether the shared context has been polluted. This happens
283 * whenever a compiled program redefines a core class (in 'java.*' package) or when
284 * (typically because of cyclic inheritance) the symbol kind of a core class has been touched.
285 */
286 TreeScanner<Void, Symtab> pollutionScanner = new TreeScanner<Void, Symtab>() {
287 @Override @DefinedBy(Api.COMPILER_TREE)
288 public Void scan(Tree tree, Symtab syms) {
289 if (tree instanceof LetExpr) {
290 LetExpr le = (LetExpr) tree;
291 scan(le.defs, syms);
292 scan(le.expr, syms);
|