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