JavacTaskImpl.java revision 3747:80b576bd3631
1/* 2 * Copyright (c) 2005, 2016, 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26package com.sun.tools.javac.api; 27 28import java.io.IOException; 29import java.nio.CharBuffer; 30import java.util.*; 31import java.util.concurrent.Callable; 32import java.util.concurrent.atomic.AtomicBoolean; 33 34import javax.annotation.processing.Processor; 35import javax.lang.model.element.Element; 36import javax.lang.model.element.ElementKind; 37import javax.lang.model.element.TypeElement; 38import javax.lang.model.util.ElementFilter; 39import javax.tools.*; 40import javax.tools.JavaFileObject.Kind; 41 42import com.sun.source.tree.*; 43import com.sun.tools.javac.code.*; 44import com.sun.tools.javac.code.Symbol.ClassSymbol; 45import com.sun.tools.javac.code.Symbol.ModuleSymbol; 46import com.sun.tools.javac.code.Symbol.PackageSymbol; 47import com.sun.tools.javac.comp.*; 48import com.sun.tools.javac.file.BaseFileManager; 49import com.sun.tools.javac.main.*; 50import com.sun.tools.javac.main.JavaCompiler; 51import com.sun.tools.javac.parser.Parser; 52import com.sun.tools.javac.parser.ParserFactory; 53import com.sun.tools.javac.processing.AnnotationProcessingError; 54import com.sun.tools.javac.tree.*; 55import com.sun.tools.javac.tree.JCTree.JCClassDecl; 56import com.sun.tools.javac.tree.JCTree.JCCompilationUnit; 57import com.sun.tools.javac.tree.JCTree.JCModuleDecl; 58import com.sun.tools.javac.tree.JCTree.Tag; 59import com.sun.tools.javac.util.*; 60import com.sun.tools.javac.util.DefinedBy.Api; 61import com.sun.tools.javac.util.List; 62import com.sun.tools.javac.util.Log.PrefixKind; 63import com.sun.tools.javac.util.Log.WriterKind; 64 65/** 66 * Provides access to functionality specific to the JDK Java Compiler, javac. 67 * 68 * <p><b>This is NOT part of any supported API. 69 * If you write code that depends on this, you do so at your own 70 * risk. This code and its internal interfaces are subject to change 71 * or deletion without notice.</b></p> 72 * 73 * @author Peter von der Ahé 74 * @author Jonathan Gibbons 75 */ 76public class JavacTaskImpl extends BasicJavacTask { 77 private final Arguments args; 78 private JavaCompiler compiler; 79 private JavaFileManager fileManager; 80 private Locale locale; 81 private Map<JavaFileObject, JCCompilationUnit> notYetEntered; 82 private ListBuffer<Env<AttrContext>> genList; 83 private final AtomicBoolean used = new AtomicBoolean(); 84 private Iterable<? extends Processor> processors; 85 86 protected JavacTaskImpl(Context context) { 87 super(context, true); 88 args = Arguments.instance(context); 89 fileManager = context.get(JavaFileManager.class); 90 } 91 92 @Override @DefinedBy(Api.COMPILER) 93 public Boolean call() { 94 return doCall().isOK(); 95 } 96 97 /* Internal version of call exposing Main.Result. */ 98 public Main.Result doCall() { 99 try { 100 return handleExceptions(new Callable<Main.Result>() { 101 @Override 102 public Main.Result call() throws Exception { 103 prepareCompiler(false); 104 if (compiler.errorCount() > 0) 105 return Main.Result.ERROR; 106 compiler.compile(args.getFileObjects(), args.getClassNames(), processors); 107 return (compiler.errorCount() > 0) ? Main.Result.ERROR : Main.Result.OK; // FIXME? 108 } 109 }, Main.Result.SYSERR, Main.Result.ABNORMAL); 110 } finally { 111 try { 112 cleanup(); 113 } catch (ClientCodeException e) { 114 throw new RuntimeException(e.getCause()); 115 } 116 } 117 } 118 119 @Override @DefinedBy(Api.COMPILER) 120 public void setProcessors(Iterable<? extends Processor> processors) { 121 Objects.requireNonNull(processors); 122 // not mt-safe 123 if (used.get()) 124 throw new IllegalStateException(); 125 this.processors = processors; 126 } 127 128 @Override @DefinedBy(Api.COMPILER) 129 public void setLocale(Locale locale) { 130 if (used.get()) 131 throw new IllegalStateException(); 132 this.locale = locale; 133 } 134 135 private <T> T handleExceptions(Callable<T> c, T sysErrorResult, T abnormalErrorResult) { 136 try { 137 return c.call(); 138 } catch (FatalError ex) { 139 Log log = Log.instance(context); 140 Options options = Options.instance(context); 141 log.printRawLines(ex.getMessage()); 142 if (ex.getCause() != null && options.isSet("dev")) { 143 ex.getCause().printStackTrace(log.getWriter(WriterKind.NOTICE)); 144 } 145 return sysErrorResult; 146 } catch (AnnotationProcessingError | ClientCodeException e) { 147 // AnnotationProcessingError is thrown from JavacProcessingEnvironment, 148 // to forward errors thrown from an annotation processor 149 // ClientCodeException is thrown from ClientCodeWrapper, 150 // to forward errors thrown from user-supplied code for Compiler API 151 // as specified by javax.tools.JavaCompiler#getTask 152 // and javax.tools.JavaCompiler.CompilationTask#call 153 throw new RuntimeException(e.getCause()); 154 } catch (PropagatedException e) { 155 throw e.getCause(); 156 } catch (IllegalStateException e) { 157 throw e; 158 } catch (Exception | Error ex) { 159 // Nasty. If we've already reported an error, compensate 160 // for buggy compiler error recovery by swallowing thrown 161 // exceptions. 162 if (compiler == null || compiler.errorCount() == 0 163 || Options.instance(context).isSet("dev")) { 164 Log log = Log.instance(context); 165 log.printLines(PrefixKind.JAVAC, "msg.bug", JavaCompiler.version()); 166 ex.printStackTrace(log.getWriter(WriterKind.NOTICE)); 167 } 168 return abnormalErrorResult; 169 } 170 } 171 172 private void prepareCompiler(boolean forParse) { 173 if (used.getAndSet(true)) { 174 if (compiler == null) 175 throw new PropagatedException(new IllegalStateException()); 176 } else { 177 args.validate(); 178 179 //initialize compiler's default locale 180 context.put(Locale.class, locale); 181 182 // hack 183 JavacMessages messages = context.get(JavacMessages.messagesKey); 184 if (messages != null && !messages.getCurrentLocale().equals(locale)) 185 messages.setCurrentLocale(locale); 186 187 initPlugins(args.getPluginOpts()); 188 initDocLint(args.getDocLintOpts()); 189 190 // init JavaCompiler and queues 191 compiler = JavaCompiler.instance(context); 192 compiler.keepComments = true; 193 compiler.genEndPos = true; 194 notYetEntered = new HashMap<>(); 195 if (forParse) { 196 compiler.initProcessAnnotations(processors); 197 for (JavaFileObject file: args.getFileObjects()) 198 notYetEntered.put(file, null); 199 genList = new ListBuffer<>(); 200 } 201 } 202 } 203 204 <T> String toString(Iterable<T> items, String sep) { 205 String currSep = ""; 206 StringBuilder sb = new StringBuilder(); 207 for (T item: items) { 208 sb.append(currSep); 209 sb.append(item.toString()); 210 currSep = sep; 211 } 212 return sb.toString(); 213 } 214 215 void cleanup() { 216 if (compiler != null) 217 compiler.close(); 218 if (fileManager instanceof BaseFileManager && ((BaseFileManager) fileManager).autoClose) { 219 try { 220 fileManager.close(); 221 } catch (IOException ignore) { 222 } 223 } 224 compiler = null; 225 context = null; 226 notYetEntered = null; 227 } 228 229 @Override @DefinedBy(Api.COMPILER_TREE) 230 public Iterable<? extends CompilationUnitTree> parse() { 231 return handleExceptions(new Callable<Iterable<? extends CompilationUnitTree>>() { 232 @Override 233 public Iterable<? extends CompilationUnitTree> call() { 234 return parseInternal(); 235 } 236 }, List.<CompilationUnitTree>nil(), List.<CompilationUnitTree>nil()); 237 } 238 239 private Iterable<? extends CompilationUnitTree> parseInternal() { 240 try { 241 prepareCompiler(true); 242 List<JCCompilationUnit> units = compiler.parseFiles(args.getFileObjects()); 243 for (JCCompilationUnit unit: units) { 244 JavaFileObject file = unit.getSourceFile(); 245 if (notYetEntered.containsKey(file)) 246 notYetEntered.put(file, unit); 247 } 248 return units; 249 } 250 finally { 251 parsed = true; 252 if (compiler != null && compiler.log != null) 253 compiler.log.flush(); 254 } 255 } 256 257 private boolean parsed = false; 258 259 /** 260 * Translate all the abstract syntax trees to elements. 261 * 262 * @return a list of elements corresponding to the top level 263 * classes in the abstract syntax trees 264 */ 265 public Iterable<? extends Element> enter() { 266 return enter(null); 267 } 268 269 /** 270 * Translate the given abstract syntax trees to elements. 271 * 272 * @param trees a list of abstract syntax trees. 273 * @return a list of elements corresponding to the top level 274 * classes in the abstract syntax trees 275 */ 276 public Iterable<? extends Element> enter(Iterable<? extends CompilationUnitTree> trees) 277 { 278 if (trees == null && notYetEntered != null && notYetEntered.isEmpty()) 279 return List.nil(); 280 281 boolean wasInitialized = compiler != null; 282 283 prepareCompiler(true); 284 285 ListBuffer<JCCompilationUnit> roots = null; 286 287 if (trees == null) { 288 // If there are still files which were specified to be compiled 289 // (i.e. in fileObjects) but which have not yet been entered, 290 // then we make sure they have been parsed and add them to the 291 // list to be entered. 292 if (notYetEntered.size() > 0) { 293 if (!parsed) 294 parseInternal(); // TODO would be nice to specify files needed to be parsed 295 for (JavaFileObject file: args.getFileObjects()) { 296 JCCompilationUnit unit = notYetEntered.remove(file); 297 if (unit != null) { 298 if (roots == null) 299 roots = new ListBuffer<>(); 300 roots.append(unit); 301 } 302 } 303 notYetEntered.clear(); 304 } 305 } 306 else { 307 for (CompilationUnitTree cu : trees) { 308 if (cu instanceof JCCompilationUnit) { 309 if (roots == null) 310 roots = new ListBuffer<>(); 311 roots.append((JCCompilationUnit)cu); 312 notYetEntered.remove(cu.getSourceFile()); 313 } 314 else 315 throw new IllegalArgumentException(cu.toString()); 316 } 317 } 318 319 if (roots == null) { 320 if (trees == null && !wasInitialized) { 321 compiler.initModules(List.nil()); 322 } 323 return List.nil(); 324 } 325 326 List<JCCompilationUnit> units = compiler.initModules(roots.toList()); 327 328 try { 329 units = compiler.enterTrees(units); 330 331 if (notYetEntered.isEmpty()) 332 compiler.processAnnotations(units); 333 334 ListBuffer<Element> elements = new ListBuffer<>(); 335 for (JCCompilationUnit unit : units) { 336 boolean isPkgInfo = unit.sourcefile.isNameCompatible("package-info", 337 JavaFileObject.Kind.SOURCE); 338 if (isPkgInfo) { 339 elements.append(unit.packge); 340 } else { 341 for (JCTree node : unit.defs) { 342 if (node.hasTag(JCTree.Tag.CLASSDEF)) { 343 JCClassDecl cdef = (JCClassDecl) node; 344 if (cdef.sym != null) // maybe null if errors in anno processing 345 elements.append(cdef.sym); 346 } else if (node.hasTag(JCTree.Tag.MODULEDEF)) { 347 JCModuleDecl mdef = (JCModuleDecl) node; 348 if (mdef.sym != null) 349 elements.append(mdef.sym); 350 } 351 } 352 } 353 } 354 return elements.toList(); 355 } 356 finally { 357 compiler.log.flush(); 358 } 359 } 360 361 @Override @DefinedBy(Api.COMPILER_TREE) 362 public Iterable<? extends Element> analyze() { 363 return handleExceptions(new Callable<Iterable<? extends Element>>() { 364 @Override 365 public Iterable<? extends Element> call() { 366 return analyze(null); 367 } 368 }, List.<Element>nil(), List.<Element>nil()); 369 } 370 371 /** 372 * Complete all analysis on the given classes. 373 * This can be used to ensure that all compile time errors are reported. 374 * The classes must have previously been returned from {@link #enter}. 375 * If null is specified, all outstanding classes will be analyzed. 376 * 377 * @param classes a list of class elements 378 * @return the elements that were analyzed 379 */ 380 // This implementation requires that we open up privileges on JavaCompiler. 381 // An alternative implementation would be to move this code to JavaCompiler and 382 // wrap it here 383 public Iterable<? extends Element> analyze(Iterable<? extends Element> classes) { 384 enter(null); // ensure all classes have been entered 385 386 final ListBuffer<Element> results = new ListBuffer<>(); 387 try { 388 if (classes == null) { 389 handleFlowResults(compiler.flow(compiler.attribute(compiler.todo)), results); 390 } else { 391 Filter f = new Filter() { 392 @Override 393 public void process(Env<AttrContext> env) { 394 handleFlowResults(compiler.flow(compiler.attribute(env)), results); 395 } 396 }; 397 f.run(compiler.todo, classes); 398 } 399 } finally { 400 compiler.log.flush(); 401 } 402 return results; 403 } 404 // where 405 private void handleFlowResults(Queue<Env<AttrContext>> queue, ListBuffer<Element> elems) { 406 for (Env<AttrContext> env: queue) { 407 switch (env.tree.getTag()) { 408 case CLASSDEF: 409 JCClassDecl cdef = (JCClassDecl) env.tree; 410 if (cdef.sym != null) 411 elems.append(cdef.sym); 412 break; 413 case MODULEDEF: 414 JCModuleDecl mod = (JCModuleDecl) env.tree; 415 if (mod.sym != null) 416 elems.append(mod.sym); 417 break; 418 case PACKAGEDEF: 419 JCCompilationUnit unit = env.toplevel; 420 if (unit.packge != null) 421 elems.append(unit.packge); 422 break; 423 } 424 } 425 genList.addAll(queue); 426 } 427 428 @Override @DefinedBy(Api.COMPILER_TREE) 429 public Iterable<? extends JavaFileObject> generate() { 430 return handleExceptions(new Callable<Iterable<? extends JavaFileObject>>() { 431 @Override 432 public Iterable<? extends JavaFileObject> call() { 433 return generate(null); 434 } 435 }, List.<JavaFileObject>nil(), List.<JavaFileObject>nil()); 436 } 437 438 /** 439 * Generate code corresponding to the given classes. 440 * The classes must have previously been returned from {@link #enter}. 441 * If there are classes outstanding to be analyzed, that will be done before 442 * any classes are generated. 443 * If null is specified, code will be generated for all outstanding classes. 444 * 445 * @param classes a list of class elements 446 * @return the files that were generated 447 */ 448 public Iterable<? extends JavaFileObject> generate(Iterable<? extends Element> classes) { 449 final ListBuffer<JavaFileObject> results = new ListBuffer<>(); 450 try { 451 analyze(null); // ensure all classes have been parsed, entered, and analyzed 452 453 if (classes == null) { 454 compiler.generate(compiler.desugar(genList), results); 455 genList.clear(); 456 } 457 else { 458 Filter f = new Filter() { 459 @Override 460 public void process(Env<AttrContext> env) { 461 compiler.generate(compiler.desugar(ListBuffer.of(env)), results); 462 } 463 }; 464 f.run(genList, classes); 465 } 466 if (genList.isEmpty()) { 467 compiler.reportDeferredDiagnostics(); 468 cleanup(); 469 } 470 } 471 finally { 472 if (compiler != null) 473 compiler.log.flush(); 474 } 475 return results; 476 } 477 478 public Iterable<? extends Tree> pathFor(CompilationUnitTree unit, Tree node) { 479 return TreeInfo.pathFor((JCTree) node, (JCTree.JCCompilationUnit) unit).reverse(); 480 } 481 482 public void ensureEntered() { 483 args.allowEmpty(); 484 enter(null); 485 } 486 487 abstract class Filter { 488 void run(Queue<Env<AttrContext>> list, Iterable<? extends Element> elements) { 489 Set<Element> set = new HashSet<>(); 490 for (Element item: elements) { 491 set.add(item); 492 } 493 494 ListBuffer<Env<AttrContext>> defer = new ListBuffer<>(); 495 while (list.peek() != null) { 496 Env<AttrContext> env = list.remove(); 497 Symbol test = null; 498 499 if (env.tree.hasTag(Tag.MODULEDEF)) { 500 test = ((JCModuleDecl) env.tree).sym; 501 } else if (env.tree.hasTag(Tag.PACKAGEDEF)) { 502 test = env.toplevel.packge; 503 } else { 504 ClassSymbol csym = env.enclClass.sym; 505 if (csym != null) 506 test = csym.outermostClass(); 507 } 508 if (test != null && set.contains(test)) 509 process(env); 510 else 511 defer = defer.append(env); 512 } 513 514 list.addAll(defer); 515 } 516 517 abstract void process(Env<AttrContext> env); 518 } 519 520 /** 521 * For internal use only. This method will be 522 * removed without warning. 523 * @param expr the type expression to be analyzed 524 * @param scope the scope in which to analyze the type expression 525 * @return the type 526 * @throws IllegalArgumentException if the type expression of null or empty 527 */ 528 public Type parseType(String expr, TypeElement scope) { 529 if (expr == null || expr.equals("")) 530 throw new IllegalArgumentException(); 531 compiler = JavaCompiler.instance(context); 532 JavaFileObject prev = compiler.log.useSource(null); 533 ParserFactory parserFactory = ParserFactory.instance(context); 534 Attr attr = Attr.instance(context); 535 try { 536 CharBuffer buf = CharBuffer.wrap((expr+"\u0000").toCharArray(), 0, expr.length()); 537 Parser parser = parserFactory.newParser(buf, false, false, false); 538 JCTree tree = parser.parseType(); 539 return attr.attribType(tree, (Symbol.TypeSymbol)scope); 540 } finally { 541 compiler.log.useSource(prev); 542 } 543 } 544 545} 546