1 /* 2 * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 package com.sun.tools.javac.api; 25 26 import java.io.PrintStream; 27 import java.io.Writer; 28 import java.util.ArrayList; 29 import java.util.Collection; 30 import java.util.Collections; 31 import java.util.HashMap; 32 33 import com.sun.source.tree.ClassTree; 34 import com.sun.source.tree.CompilationUnitTree; 35 import com.sun.source.util.JavacTask; 36 import com.sun.source.util.TaskEvent; 37 import com.sun.source.util.TaskEvent.Kind; 38 import com.sun.source.util.TaskListener; 39 import com.sun.source.util.TreeScanner; 40 import com.sun.tools.javac.code.Kinds; 41 import com.sun.tools.javac.code.Symbol; 42 import com.sun.tools.javac.code.Symtab; 43 import com.sun.tools.javac.code.Type; 44 import com.sun.tools.javac.code.Type.ClassType; 45 import com.sun.tools.javac.code.TypeTag; 46 import com.sun.tools.javac.code.Types; 47 import com.sun.tools.javac.comp.Annotate; 48 import com.sun.tools.javac.comp.Check; 49 import com.sun.tools.javac.comp.CompileStates; 50 import com.sun.tools.javac.comp.Enter; 51 import com.sun.tools.javac.comp.Modules; 52 import com.sun.tools.javac.main.Arguments; 53 import com.sun.tools.javac.main.JavaCompiler; 54 import com.sun.tools.javac.tree.JCTree.JCClassDecl; 55 56 import javax.tools.Diagnostic; 57 import javax.tools.DiagnosticListener; 58 import javax.tools.JavaFileManager; 59 import javax.tools.JavaFileObject; 60 61 import java.util.HashSet; 62 import java.util.List; 63 import java.util.Map; 64 import java.util.Set; 65 import java.util.stream.Collectors; 66 import java.util.stream.StreamSupport; 67 68 import com.sun.tools.javac.model.JavacElements; 69 import com.sun.tools.javac.util.Context; 70 import com.sun.tools.javac.util.DefinedBy; 71 import com.sun.tools.javac.util.DefinedBy.Api; 72 import com.sun.tools.javac.util.Log; 73 74 /** 75 * A pool of reusable JavacTasks. When a task is no valid anymore, it is returned to the pool, 76 * and its Context may be reused for future processing in some cases. The reuse is achieved 77 * by replacing some components (most notably JavaCompiler and Log) with reusable counterparts, 78 * and by cleaning up leftovers from previous compilation. 79 * <p> 80 * For each combination of options, a separate task/context is created and kept, as most option 81 * values are cached inside components themselves. 82 * <p> 83 * When the compilation redefines sensitive classes (e.g. classes in the the java.* packages), the 84 * task/context is not reused. 85 * <p> 86 * When the task is reused, then packages that were already listed won't be listed again. 87 * <p> 88 * Care must be taken to only return tasks that won't be used by the original caller. 89 * <p> 90 * Care must also be taken when custom components are installed, as those are not cleaned when the 91 * task/context is reused, and subsequent getTask may return a task based on a context with these 92 * custom components. 93 * 94 * <p><b>This is NOT part of any supported API. 95 * If you write code that depends on this, you do so at your own risk. 96 * This code and its internal interfaces are subject to change or 97 * deletion without notice.</b> 98 */ 99 public class JavacTaskPool { 100 101 private static final JavacTool systemProvider = JavacTool.create(); 102 103 private final int maxPoolSize; 104 private final Map<List<String>, List<ReusableContext>> options2Contexts = new HashMap<>(); 105 private int id; 106 107 private int statReused = 0; 108 private int statNew = 0; 109 private int statPolluted = 0; 110 private int statRemoved = 0; 111 112 /**Creates the pool. 113 * 114 * @param maxPoolSize maximum number of tasks/context that will be kept in the pool. 115 */ 116 public JavacTaskPool(int maxPoolSize) { 117 this.maxPoolSize = maxPoolSize; 118 } 119 120 /**Creates a new task as if by {@link javax.tools.JavaCompiler#getTask} and runs the provided 121 * worker with it. The task is only valid while the worker is running. The internal structures 122 * may be reused from some previous compilation. 123 * 124 * @param out a Writer for additional output from the compiler; 125 * use {@code System.err} if {@code null} 126 * @param fileManager a file manager; if {@code null} use the 127 * compiler's standard filemanager 128 * @param diagnosticListener a diagnostic listener; if {@code 129 * null} use the compiler's default method for reporting 130 * diagnostics 131 * @param options compiler options, {@code null} means no options 132 * @param classes names of classes to be processed by annotation 133 * processing, {@code null} means no class names 134 * @param compilationUnits the compilation units to compile, {@code 135 * null} means no compilation units 136 * @param worker that should be run with the task 137 * @return an object representing the compilation 138 * @throws RuntimeException if an unrecoverable error 139 * occurred in a user supplied component. The 140 * {@linkplain Throwable#getCause() cause} will be the error in 141 * user code. 142 * @throws IllegalArgumentException if any of the options are invalid, 143 * or if any of the given compilation units are of other kind than 144 * {@linkplain JavaFileObject.Kind#SOURCE source} 145 */ 146 public <Z> Z getTask(Writer out, 147 JavaFileManager fileManager, 148 DiagnosticListener<? super JavaFileObject> diagnosticListener, 149 Iterable<String> options, 150 Iterable<String> classes, 151 Iterable<? extends JavaFileObject> compilationUnits, 152 Worker<Z> worker) { 153 List<String> opts = 154 StreamSupport.stream(options.spliterator(), false) 155 .collect(Collectors.toCollection(ArrayList::new)); 156 157 ReusableContext ctx; 158 159 synchronized (this) { 160 List<ReusableContext> cached = 161 options2Contexts.getOrDefault(opts, Collections.emptyList()); 162 163 if (cached.isEmpty()) { 164 ctx = new ReusableContext(opts); 165 statNew++; 166 } else { 167 ctx = cached.remove(0); 168 statReused++; 169 } 170 } 171 172 ctx.useCount++; 173 174 JavacTaskImpl task = 175 (JavacTaskImpl) systemProvider.getTask(out, fileManager, diagnosticListener, 176 opts, classes, compilationUnits, ctx); 177 178 task.addTaskListener(ctx); 179 180 Z result = worker.withTask(task); 181 182 //not returning the context to the pool if task crashes with an exception 183 //the task/context may be in a broken state 184 ctx.clear(); 185 if (ctx.polluted) { 186 statPolluted++; 187 } else { 188 task.cleanup(); 189 synchronized (this) { 190 while (cacheSize() + 1 > maxPoolSize) { 191 ReusableContext toRemove = 192 options2Contexts.values() 193 .stream() 194 .flatMap(Collection::stream) 195 .sorted((c1, c2) -> c1.timeStamp < c2.timeStamp ? -1 : 1) 196 .findFirst() 197 .get(); 198 options2Contexts.get(toRemove.arguments).remove(toRemove); 199 statRemoved++; 200 } 201 options2Contexts.computeIfAbsent(ctx.arguments, x -> new ArrayList<>()).add(ctx); 202 ctx.timeStamp = id++; 203 } 204 } 205 206 return result; 207 } 208 //where: 209 private long cacheSize() { 210 return options2Contexts.values().stream().flatMap(Collection::stream).count(); 211 } 212 213 public void printStatistics(PrintStream out) { 214 out.println(statReused + " reused Contexts"); 215 out.println(statNew + " newly created Contexts"); 216 out.println(statPolluted + " polluted Contexts"); 217 out.println(statRemoved + " removed Contexts"); 218 } 219 220 public interface Worker<Z> { 221 public Z withTask(JavacTask task); 222 } 223 224 static class ReusableContext extends Context implements TaskListener { 225 226 Set<CompilationUnitTree> roots = new HashSet<>(); 227 228 List<String> arguments; 229 boolean polluted = false; 230 231 int useCount; 232 long timeStamp; 233 234 ReusableContext(List<String> arguments) { 235 super(); 236 this.arguments = arguments; 237 put(Log.logKey, ReusableLog.factory); 238 put(JavaCompiler.compilerKey, ReusableJavaCompiler.factory); 239 } 240 241 void clear() { 242 drop(Arguments.argsKey); 243 drop(DiagnosticListener.class); 244 drop(Log.outKey); 245 drop(Log.errKey); 246 drop(JavaFileManager.class); 247 drop(JavacTask.class); 248 drop(JavacTrees.class); 249 drop(JavacElements.class); 250 251 if (ht.get(Log.logKey) instanceof ReusableLog) { 252 //log already inited - not first round 253 ((ReusableLog)Log.instance(this)).clear(); 254 Enter.instance(this).newRound(); 255 ((ReusableJavaCompiler)ReusableJavaCompiler.instance(this)).clear(); 256 Types.instance(this).newRound(); 257 Check.instance(this).newRound(); 258 Modules.instance(this).newRound(); 259 Annotate.instance(this).newRound(); 260 CompileStates.instance(this).clear(); 261 MultiTaskListener.instance(this).clear(); 262 263 //find if any of the roots have redefined java.* classes 264 Symtab syms = Symtab.instance(this); 265 pollutionScanner.scan(roots, syms); 266 roots.clear(); 267 } 268 } 269 270 /** 271 * This scanner detects as to whether the shared context has been polluted. This happens 272 * whenever a compiled program redefines a core class (in 'java.*' package) or when 273 * (typically because of cyclic inheritance) the symbol kind of a core class has been touched. 274 */ 275 TreeScanner<Void, Symtab> pollutionScanner = new TreeScanner<Void, Symtab>() { 276 @Override @DefinedBy(Api.COMPILER_TREE) 277 public Void visitClass(ClassTree node, Symtab syms) { 278 Symbol sym = ((JCClassDecl)node).sym; 279 if (sym != null) { 280 syms.removeClass(sym.packge().modle, sym.flatName()); 281 Type sup = supertype(sym); 282 if (isCoreClass(sym) || 283 (sup != null && isCoreClass(sup.tsym) && sup.tsym.kind != Kinds.Kind.TYP)) { 284 polluted = true; 285 } 286 } 287 return super.visitClass(node, syms); 288 } 289 290 private boolean isCoreClass(Symbol s) { 291 return s.flatName().toString().startsWith("java."); 292 } 293 294 private Type supertype(Symbol s) { 295 if (s.type == null || 296 !s.type.hasTag(TypeTag.CLASS)) { 297 return null; 298 } else { 299 ClassType ct = (ClassType)s.type; 300 return ct.supertype_field; 301 } 302 } 303 }; 304 305 @Override @DefinedBy(Api.COMPILER_TREE) 306 public void finished(TaskEvent e) { 307 if (e.getKind() == Kind.PARSE) { 308 roots.add(e.getCompilationUnit()); 309 } 310 } 311 312 @Override @DefinedBy(Api.COMPILER_TREE) 313 public void started(TaskEvent e) { 314 //do nothing 315 } 316 317 <T> void drop(Key<T> k) { 318 ht.remove(k); 319 } 320 321 <T> void drop(Class<T> c) { 322 ht.remove(key(c)); 323 } 324 325 /** 326 * Reusable JavaCompiler; exposes a method to clean up the component from leftovers associated with 327 * previous compilations. 328 */ 329 static class ReusableJavaCompiler extends JavaCompiler { 330 331 final static Factory<JavaCompiler> factory = ReusableJavaCompiler::new; 332 333 ReusableJavaCompiler(Context context) { 334 super(context); 335 } 336 337 @Override 338 public void close() { 339 //do nothing 340 } 341 342 void clear() { 343 newRound(); 344 } 345 346 @Override 347 protected void checkReusable() { 348 //do nothing - it's ok to reuse the compiler 349 } 350 } 351 352 /** 353 * Reusable Log; exposes a method to clean up the component from leftovers associated with 354 * previous compilations. 355 */ 356 static class ReusableLog extends Log { 357 358 final static Factory<Log> factory = ReusableLog::new; 359 360 Context context; 361 362 ReusableLog(Context context) { 363 super(context); 364 this.context = context; 365 } 366 367 void clear() { 368 recorded.clear(); 369 sourceMap.clear(); 370 nerrors = 0; 371 nwarnings = 0; 372 //Set a fake listener that will lazily lookup the context for the 'real' listener. Since 373 //this field is never updated when a new task is created, we cannot simply reset the field 374 //or keep old value. This is a hack to workaround the limitations in the current infrastructure. 375 diagListener = new DiagnosticListener<JavaFileObject>() { 376 DiagnosticListener<JavaFileObject> cachedListener; 377 378 @Override @DefinedBy(Api.COMPILER) 379 @SuppressWarnings("unchecked") 380 public void report(Diagnostic<? extends JavaFileObject> diagnostic) { 381 if (cachedListener == null) { 382 cachedListener = context.get(DiagnosticListener.class); 383 } 384 cachedListener.report(diagnostic); 385 } 386 }; 387 } 388 } 389 } 390 }