JavacProcessingEnvironment.java revision 4220:51b4cd2af28e
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 26package com.sun.tools.javac.processing; 27 28import java.io.Closeable; 29import java.io.IOException; 30import java.io.PrintWriter; 31import java.io.StringWriter; 32import java.lang.reflect.Method; 33import java.net.MalformedURLException; 34import java.net.URL; 35import java.nio.file.Path; 36import java.util.*; 37import java.util.Map.Entry; 38import java.util.regex.*; 39import java.util.stream.Collectors; 40 41import javax.annotation.processing.*; 42import javax.lang.model.SourceVersion; 43import javax.lang.model.element.*; 44import javax.lang.model.util.*; 45import javax.tools.JavaFileManager; 46import javax.tools.JavaFileObject; 47import javax.tools.JavaFileObject.Kind; 48import javax.tools.StandardJavaFileManager; 49 50import static javax.tools.StandardLocation.*; 51 52import com.sun.source.util.TaskEvent; 53import com.sun.tools.javac.api.MultiTaskListener; 54import com.sun.tools.javac.code.*; 55import com.sun.tools.javac.code.Scope.WriteableScope; 56import com.sun.tools.javac.code.Symbol.*; 57import com.sun.tools.javac.code.Type.ClassType; 58import com.sun.tools.javac.code.Types; 59import com.sun.tools.javac.comp.AttrContext; 60import com.sun.tools.javac.comp.Check; 61import com.sun.tools.javac.comp.Enter; 62import com.sun.tools.javac.comp.Env; 63import com.sun.tools.javac.comp.Modules; 64import com.sun.tools.javac.file.JavacFileManager; 65import com.sun.tools.javac.main.JavaCompiler; 66import com.sun.tools.javac.main.Option; 67import com.sun.tools.javac.model.JavacElements; 68import com.sun.tools.javac.model.JavacTypes; 69import com.sun.tools.javac.platform.PlatformDescription; 70import com.sun.tools.javac.platform.PlatformDescription.PluginInfo; 71import com.sun.tools.javac.resources.CompilerProperties.Errors; 72import com.sun.tools.javac.tree.*; 73import com.sun.tools.javac.tree.JCTree.*; 74import com.sun.tools.javac.util.Abort; 75import com.sun.tools.javac.util.Assert; 76import com.sun.tools.javac.util.ClientCodeException; 77import com.sun.tools.javac.util.Context; 78import com.sun.tools.javac.util.Convert; 79import com.sun.tools.javac.util.DefinedBy; 80import com.sun.tools.javac.util.DefinedBy.Api; 81import com.sun.tools.javac.util.Iterators; 82import com.sun.tools.javac.util.JCDiagnostic; 83import com.sun.tools.javac.util.JDK9Wrappers.Module; 84import com.sun.tools.javac.util.JavacMessages; 85import com.sun.tools.javac.util.List; 86import com.sun.tools.javac.util.Log; 87import com.sun.tools.javac.util.MatchingUtils; 88import com.sun.tools.javac.util.ModuleHelper; 89import com.sun.tools.javac.util.Name; 90import com.sun.tools.javac.util.Names; 91import com.sun.tools.javac.util.Options; 92 93import static com.sun.tools.javac.code.Lint.LintCategory.PROCESSING; 94import static com.sun.tools.javac.code.Kinds.Kind.*; 95import com.sun.tools.javac.comp.Annotate; 96import static com.sun.tools.javac.comp.CompileStates.CompileState; 97import static com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag.*; 98 99/** 100 * Objects of this class hold and manage the state needed to support 101 * annotation processing. 102 * 103 * <p><b>This is NOT part of any supported API. 104 * If you write code that depends on this, you do so at your own risk. 105 * This code and its internal interfaces are subject to change or 106 * deletion without notice.</b> 107 */ 108public class JavacProcessingEnvironment implements ProcessingEnvironment, Closeable { 109 private final Options options; 110 111 private final boolean printProcessorInfo; 112 private final boolean printRounds; 113 private final boolean verbose; 114 private final boolean lint; 115 private final boolean fatalErrors; 116 private final boolean werror; 117 private final boolean showResolveErrors; 118 private final boolean allowModules; 119 120 private final JavacFiler filer; 121 private final JavacMessager messager; 122 private final JavacElements elementUtils; 123 private final JavacTypes typeUtils; 124 private final JavaCompiler compiler; 125 private final Modules modules; 126 private final Types types; 127 private final Annotate annotate; 128 129 /** 130 * Holds relevant state history of which processors have been 131 * used. 132 */ 133 private DiscoveredProcessors discoveredProcs; 134 135 /** 136 * Map of processor-specific options. 137 */ 138 private final Map<String, String> processorOptions; 139 140 /** 141 */ 142 private final Set<String> unmatchedProcessorOptions; 143 144 /** 145 * Annotations implicitly processed and claimed by javac. 146 */ 147 private final Set<String> platformAnnotations; 148 149 /** 150 * Set of packages given on command line. 151 */ 152 private Set<PackageSymbol> specifiedPackages = Collections.emptySet(); 153 154 /** The log to be used for error reporting. 155 */ 156 final Log log; 157 158 /** Diagnostic factory. 159 */ 160 JCDiagnostic.Factory diags; 161 162 /** 163 * Source level of the compile. 164 */ 165 Source source; 166 167 private ClassLoader processorClassLoader; 168 private ServiceLoader<Processor> serviceLoader; 169 private SecurityException processorLoaderException; 170 171 private final JavaFileManager fileManager; 172 173 /** 174 * JavacMessages object used for localization 175 */ 176 private JavacMessages messages; 177 178 private MultiTaskListener taskListener; 179 private final Symtab symtab; 180 private final Names names; 181 private final Enter enter; 182 private final Completer initialCompleter; 183 private final Check chk; 184 185 private final Context context; 186 187 /** Get the JavacProcessingEnvironment instance for this context. */ 188 public static JavacProcessingEnvironment instance(Context context) { 189 JavacProcessingEnvironment instance = context.get(JavacProcessingEnvironment.class); 190 if (instance == null) 191 instance = new JavacProcessingEnvironment(context); 192 return instance; 193 } 194 195 protected JavacProcessingEnvironment(Context context) { 196 this.context = context; 197 context.put(JavacProcessingEnvironment.class, this); 198 log = Log.instance(context); 199 source = Source.instance(context); 200 diags = JCDiagnostic.Factory.instance(context); 201 options = Options.instance(context); 202 printProcessorInfo = options.isSet(Option.XPRINTPROCESSORINFO); 203 printRounds = options.isSet(Option.XPRINTROUNDS); 204 verbose = options.isSet(Option.VERBOSE); 205 lint = Lint.instance(context).isEnabled(PROCESSING); 206 compiler = JavaCompiler.instance(context); 207 if (options.isSet(Option.PROC, "only") || options.isSet(Option.XPRINT)) { 208 compiler.shouldStopPolicyIfNoError = CompileState.PROCESS; 209 } 210 fatalErrors = options.isSet("fatalEnterError"); 211 showResolveErrors = options.isSet("showResolveErrors"); 212 werror = options.isSet(Option.WERROR); 213 fileManager = context.get(JavaFileManager.class); 214 platformAnnotations = initPlatformAnnotations(); 215 216 // Initialize services before any processors are initialized 217 // in case processors use them. 218 filer = new JavacFiler(context); 219 messager = new JavacMessager(context, this); 220 elementUtils = JavacElements.instance(context); 221 typeUtils = JavacTypes.instance(context); 222 modules = Modules.instance(context); 223 types = Types.instance(context); 224 annotate = Annotate.instance(context); 225 processorOptions = initProcessorOptions(); 226 unmatchedProcessorOptions = initUnmatchedProcessorOptions(); 227 messages = JavacMessages.instance(context); 228 taskListener = MultiTaskListener.instance(context); 229 symtab = Symtab.instance(context); 230 names = Names.instance(context); 231 enter = Enter.instance(context); 232 initialCompleter = ClassFinder.instance(context).getCompleter(); 233 chk = Check.instance(context); 234 initProcessorLoader(); 235 236 allowModules = source.allowModules(); 237 } 238 239 public void setProcessors(Iterable<? extends Processor> processors) { 240 Assert.checkNull(discoveredProcs); 241 initProcessorIterator(processors); 242 } 243 244 private Set<String> initPlatformAnnotations() { 245 Set<String> platformAnnotations = new HashSet<>(); 246 platformAnnotations.add("java.lang.Deprecated"); 247 platformAnnotations.add("java.lang.Override"); 248 platformAnnotations.add("java.lang.SuppressWarnings"); 249 platformAnnotations.add("java.lang.annotation.Documented"); 250 platformAnnotations.add("java.lang.annotation.Inherited"); 251 platformAnnotations.add("java.lang.annotation.Retention"); 252 platformAnnotations.add("java.lang.annotation.Target"); 253 return Collections.unmodifiableSet(platformAnnotations); 254 } 255 256 private void initProcessorLoader() { 257 try { 258 if (fileManager.hasLocation(ANNOTATION_PROCESSOR_MODULE_PATH)) { 259 try { 260 serviceLoader = fileManager.getServiceLoader(ANNOTATION_PROCESSOR_MODULE_PATH, Processor.class); 261 } catch (IOException e) { 262 throw new Abort(e); 263 } 264 } else { 265 // If processorpath is not explicitly set, use the classpath. 266 processorClassLoader = fileManager.hasLocation(ANNOTATION_PROCESSOR_PATH) 267 ? fileManager.getClassLoader(ANNOTATION_PROCESSOR_PATH) 268 : fileManager.getClassLoader(CLASS_PATH); 269 270 if (options.isSet("accessInternalAPI")) 271 ModuleHelper.addExports(Module.getModule(getClass()), Module.getUnnamedModule(processorClassLoader)); 272 273 if (processorClassLoader != null && processorClassLoader instanceof Closeable) { 274 compiler.closeables = compiler.closeables.prepend((Closeable) processorClassLoader); 275 } 276 } 277 } catch (SecurityException e) { 278 processorLoaderException = e; 279 } 280 } 281 282 private void initProcessorIterator(Iterable<? extends Processor> processors) { 283 Iterator<? extends Processor> processorIterator; 284 285 if (options.isSet(Option.XPRINT)) { 286 try { 287 processorIterator = List.of(new PrintingProcessor()).iterator(); 288 } catch (Throwable t) { 289 AssertionError assertError = 290 new AssertionError("Problem instantiating PrintingProcessor."); 291 assertError.initCause(t); 292 throw assertError; 293 } 294 } else if (processors != null) { 295 processorIterator = processors.iterator(); 296 } else { 297 if (processorLoaderException == null) { 298 /* 299 * If the "-processor" option is used, search the appropriate 300 * path for the named class. Otherwise, use a service 301 * provider mechanism to create the processor iterator. 302 */ 303 String processorNames = options.get(Option.PROCESSOR); 304 if (fileManager.hasLocation(ANNOTATION_PROCESSOR_MODULE_PATH)) { 305 processorIterator = (processorNames == null) ? 306 new ServiceIterator(serviceLoader, log) : 307 new NameServiceIterator(serviceLoader, log, processorNames); 308 } else if (processorNames != null) { 309 processorIterator = new NameProcessIterator(processorNames, processorClassLoader, log); 310 } else { 311 processorIterator = new ServiceIterator(processorClassLoader, log); 312 } 313 } else { 314 /* 315 * A security exception will occur if we can't create a classloader. 316 * Ignore the exception if, with hindsight, we didn't need it anyway 317 * (i.e. no processor was specified either explicitly, or implicitly, 318 * in service configuration file.) Otherwise, we cannot continue. 319 */ 320 processorIterator = handleServiceLoaderUnavailability("proc.cant.create.loader", 321 processorLoaderException); 322 } 323 } 324 PlatformDescription platformProvider = context.get(PlatformDescription.class); 325 java.util.List<Processor> platformProcessors = Collections.emptyList(); 326 if (platformProvider != null) { 327 platformProcessors = platformProvider.getAnnotationProcessors() 328 .stream() 329 .map(PluginInfo::getPlugin) 330 .collect(Collectors.toList()); 331 } 332 List<Iterator<? extends Processor>> iterators = List.of(processorIterator, 333 platformProcessors.iterator()); 334 Iterator<? extends Processor> compoundIterator = 335 Iterators.createCompoundIterator(iterators, i -> i); 336 discoveredProcs = new DiscoveredProcessors(compoundIterator); 337 } 338 339 public <S> ServiceLoader<S> getServiceLoader(Class<S> service) { 340 if (fileManager.hasLocation(ANNOTATION_PROCESSOR_MODULE_PATH)) { 341 try { 342 return fileManager.getServiceLoader(ANNOTATION_PROCESSOR_MODULE_PATH, service); 343 } catch (IOException e) { 344 throw new Abort(e); 345 } 346 } else { 347 return ServiceLoader.load(service, getProcessorClassLoader()); 348 } 349 } 350 351 /** 352 * Returns an empty processor iterator if no processors are on the 353 * relevant path, otherwise if processors are present, logs an 354 * error. Called when a service loader is unavailable for some 355 * reason, either because a service loader class cannot be found 356 * or because a security policy prevents class loaders from being 357 * created. 358 * 359 * @param key The resource key to use to log an error message 360 * @param e If non-null, pass this exception to Abort 361 */ 362 private Iterator<Processor> handleServiceLoaderUnavailability(String key, Exception e) { 363 if (fileManager instanceof JavacFileManager) { 364 StandardJavaFileManager standardFileManager = (JavacFileManager) fileManager; 365 Iterable<? extends Path> workingPath = fileManager.hasLocation(ANNOTATION_PROCESSOR_PATH) 366 ? standardFileManager.getLocationAsPaths(ANNOTATION_PROCESSOR_PATH) 367 : standardFileManager.getLocationAsPaths(CLASS_PATH); 368 369 if (needClassLoader(options.get(Option.PROCESSOR), workingPath) ) 370 handleException(key, e); 371 372 } else { 373 handleException(key, e); 374 } 375 376 java.util.List<Processor> pl = Collections.emptyList(); 377 return pl.iterator(); 378 } 379 380 /** 381 * Handle a security exception thrown during initializing the 382 * Processor iterator. 383 */ 384 private void handleException(String key, Exception e) { 385 if (e != null) { 386 log.error(key, e.getLocalizedMessage()); 387 throw new Abort(e); 388 } else { 389 log.error(key); 390 throw new Abort(); 391 } 392 } 393 394 /** 395 * Use a service loader appropriate for the platform to provide an 396 * iterator over annotations processors; fails if a loader is 397 * needed but unavailable. 398 */ 399 private class ServiceIterator implements Iterator<Processor> { 400 Iterator<Processor> iterator; 401 Log log; 402 ServiceLoader<Processor> loader; 403 404 ServiceIterator(ClassLoader classLoader, Log log) { 405 this.log = log; 406 try { 407 try { 408 loader = ServiceLoader.load(Processor.class, classLoader); 409 this.iterator = loader.iterator(); 410 } catch (Exception e) { 411 // Fail softly if a loader is not actually needed. 412 this.iterator = handleServiceLoaderUnavailability("proc.no.service", null); 413 } 414 } catch (Throwable t) { 415 log.error("proc.service.problem"); 416 throw new Abort(t); 417 } 418 } 419 420 ServiceIterator(ServiceLoader<Processor> loader, Log log) { 421 this.log = log; 422 this.loader = loader; 423 this.iterator = loader.iterator(); 424 } 425 426 @Override 427 public boolean hasNext() { 428 try { 429 return internalHasNext(); 430 } catch(ServiceConfigurationError sce) { 431 log.error("proc.bad.config.file", sce.getLocalizedMessage()); 432 throw new Abort(sce); 433 } catch (Throwable t) { 434 throw new Abort(t); 435 } 436 } 437 438 boolean internalHasNext() { 439 return iterator.hasNext(); 440 } 441 442 @Override 443 public Processor next() { 444 try { 445 return internalNext(); 446 } catch (ServiceConfigurationError sce) { 447 log.error("proc.bad.config.file", sce.getLocalizedMessage()); 448 throw new Abort(sce); 449 } catch (Throwable t) { 450 throw new Abort(t); 451 } 452 } 453 454 Processor internalNext() { 455 return iterator.next(); 456 } 457 458 @Override 459 public void remove() { 460 throw new UnsupportedOperationException(); 461 } 462 463 public void close() { 464 if (loader != null) { 465 try { 466 loader.reload(); 467 } catch(Exception e) { 468 // Ignore problems during a call to reload. 469 } 470 } 471 } 472 } 473 474 private class NameServiceIterator extends ServiceIterator { 475 private Map<String, Processor> namedProcessorsMap = new HashMap<>();; 476 private Iterator<String> processorNames = null; 477 private Processor nextProc = null; 478 479 public NameServiceIterator(ServiceLoader<Processor> loader, Log log, String theNames) { 480 super(loader, log); 481 this.processorNames = Arrays.asList(theNames.split(",")).iterator(); 482 } 483 484 @Override 485 boolean internalHasNext() { 486 if (nextProc != null) { 487 return true; 488 } 489 if (!processorNames.hasNext()) { 490 namedProcessorsMap = null; 491 return false; 492 } 493 String processorName = processorNames.next(); 494 Processor theProcessor = namedProcessorsMap.get(processorName); 495 if (theProcessor != null) { 496 namedProcessorsMap.remove(processorName); 497 nextProc = theProcessor; 498 return true; 499 } else { 500 while (iterator.hasNext()) { 501 theProcessor = iterator.next(); 502 String name = theProcessor.getClass().getName(); 503 if (name.equals(processorName)) { 504 nextProc = theProcessor; 505 return true; 506 } else { 507 namedProcessorsMap.put(name, theProcessor); 508 } 509 } 510 log.error(Errors.ProcProcessorNotFound(processorName)); 511 return false; 512 } 513 } 514 515 @Override 516 Processor internalNext() { 517 if (hasNext()) { 518 Processor p = nextProc; 519 nextProc = null; 520 return p; 521 } else { 522 throw new NoSuchElementException(); 523 } 524 } 525 } 526 527 private static class NameProcessIterator implements Iterator<Processor> { 528 Processor nextProc = null; 529 Iterator<String> names; 530 ClassLoader processorCL; 531 Log log; 532 533 NameProcessIterator(String names, ClassLoader processorCL, Log log) { 534 this.names = Arrays.asList(names.split(",")).iterator(); 535 this.processorCL = processorCL; 536 this.log = log; 537 } 538 539 public boolean hasNext() { 540 if (nextProc != null) 541 return true; 542 else { 543 if (!names.hasNext()) { 544 return false; 545 } else { 546 Processor processor = getNextProcessor(names.next()); 547 if (processor == null) { 548 return false; 549 } else { 550 nextProc = processor; 551 return true; 552 } 553 } 554 } 555 } 556 557 private Processor getNextProcessor(String processorName) { 558 try { 559 try { 560 Class<?> processorClass = processorCL.loadClass(processorName); 561 ensureReadable(processorClass); 562 return (Processor) processorClass.getConstructor().newInstance(); 563 } catch (ClassNotFoundException cnfe) { 564 log.error("proc.processor.not.found", processorName); 565 return null; 566 } catch (ClassCastException cce) { 567 log.error("proc.processor.wrong.type", processorName); 568 return null; 569 } catch (Exception e ) { 570 log.error("proc.processor.cant.instantiate", processorName); 571 return null; 572 } 573 } catch (ClientCodeException e) { 574 throw e; 575 } catch (Throwable t) { 576 throw new AnnotationProcessingError(t); 577 } 578 } 579 580 public Processor next() { 581 if (hasNext()) { 582 Processor p = nextProc; 583 nextProc = null; 584 return p; 585 } else 586 throw new NoSuchElementException(); 587 } 588 589 public void remove () { 590 throw new UnsupportedOperationException(); 591 } 592 593 /** 594 * Ensures that the module of the given class is readable to this 595 * module. 596 */ 597 private void ensureReadable(Class<?> targetClass) { 598 try { 599 Method getModuleMethod = Class.class.getMethod("getModule"); 600 Object thisModule = getModuleMethod.invoke(this.getClass()); 601 Object targetModule = getModuleMethod.invoke(targetClass); 602 603 Class<?> moduleClass = getModuleMethod.getReturnType(); 604 Method addReadsMethod = moduleClass.getMethod("addReads", moduleClass); 605 addReadsMethod.invoke(thisModule, targetModule); 606 } catch (NoSuchMethodException e) { 607 // ignore 608 } catch (Exception e) { 609 throw new InternalError(e); 610 } 611 } 612 } 613 614 public boolean atLeastOneProcessor() { 615 return discoveredProcs.iterator().hasNext(); 616 } 617 618 private Map<String, String> initProcessorOptions() { 619 Set<String> keySet = options.keySet(); 620 Map<String, String> tempOptions = new LinkedHashMap<>(); 621 622 for(String key : keySet) { 623 if (key.startsWith("-A") && key.length() > 2) { 624 int sepIndex = key.indexOf('='); 625 String candidateKey = null; 626 String candidateValue = null; 627 628 if (sepIndex == -1) 629 candidateKey = key.substring(2); 630 else if (sepIndex >= 3) { 631 candidateKey = key.substring(2, sepIndex); 632 candidateValue = (sepIndex < key.length()-1)? 633 key.substring(sepIndex+1) : null; 634 } 635 tempOptions.put(candidateKey, candidateValue); 636 } 637 } 638 639 PlatformDescription platformProvider = context.get(PlatformDescription.class); 640 641 if (platformProvider != null) { 642 for (PluginInfo<Processor> ap : platformProvider.getAnnotationProcessors()) { 643 tempOptions.putAll(ap.getOptions()); 644 } 645 } 646 647 return Collections.unmodifiableMap(tempOptions); 648 } 649 650 private Set<String> initUnmatchedProcessorOptions() { 651 Set<String> unmatchedProcessorOptions = new HashSet<>(); 652 unmatchedProcessorOptions.addAll(processorOptions.keySet()); 653 return unmatchedProcessorOptions; 654 } 655 656 /** 657 * State about how a processor has been used by the tool. If a 658 * processor has been used on a prior round, its process method is 659 * called on all subsequent rounds, perhaps with an empty set of 660 * annotations to process. The {@code annotationSupported} method 661 * caches the supported annotation information from the first (and 662 * only) getSupportedAnnotationTypes call to the processor. 663 */ 664 static class ProcessorState { 665 public Processor processor; 666 public boolean contributed; 667 private ArrayList<Pattern> supportedAnnotationPatterns; 668 private ArrayList<String> supportedOptionNames; 669 670 ProcessorState(Processor p, Log log, Source source, boolean allowModules, ProcessingEnvironment env) { 671 processor = p; 672 contributed = false; 673 674 try { 675 processor.init(env); 676 677 checkSourceVersionCompatibility(source, log); 678 679 supportedAnnotationPatterns = new ArrayList<>(); 680 for (String importString : processor.getSupportedAnnotationTypes()) { 681 supportedAnnotationPatterns.add(importStringToPattern(allowModules, 682 importString, 683 processor, 684 log)); 685 } 686 687 supportedOptionNames = new ArrayList<>(); 688 for (String optionName : processor.getSupportedOptions() ) { 689 if (checkOptionName(optionName, log)) 690 supportedOptionNames.add(optionName); 691 } 692 693 } catch (ClientCodeException e) { 694 throw e; 695 } catch (Throwable t) { 696 throw new AnnotationProcessingError(t); 697 } 698 } 699 700 /** 701 * Checks whether or not a processor's source version is 702 * compatible with the compilation source version. The 703 * processor's source version needs to be greater than or 704 * equal to the source version of the compile. 705 */ 706 private void checkSourceVersionCompatibility(Source source, Log log) { 707 SourceVersion procSourceVersion = processor.getSupportedSourceVersion(); 708 709 if (procSourceVersion.compareTo(Source.toSourceVersion(source)) < 0 ) { 710 log.warning("proc.processor.incompatible.source.version", 711 procSourceVersion, 712 processor.getClass().getName(), 713 source.name); 714 } 715 } 716 717 private boolean checkOptionName(String optionName, Log log) { 718 boolean valid = isValidOptionName(optionName); 719 if (!valid) 720 log.error("proc.processor.bad.option.name", 721 optionName, 722 processor.getClass().getName()); 723 return valid; 724 } 725 726 public boolean annotationSupported(String annotationName) { 727 for(Pattern p: supportedAnnotationPatterns) { 728 if (p.matcher(annotationName).matches()) 729 return true; 730 } 731 return false; 732 } 733 734 /** 735 * Remove options that are matched by this processor. 736 */ 737 public void removeSupportedOptions(Set<String> unmatchedProcessorOptions) { 738 unmatchedProcessorOptions.removeAll(supportedOptionNames); 739 } 740 } 741 742 // TODO: These two classes can probably be rewritten better... 743 /** 744 * This class holds information about the processors that have 745 * been discovered so far as well as the means to discover more, if 746 * necessary. A single iterator should be used per round of 747 * annotation processing. The iterator first visits already 748 * discovered processors then fails over to the service provider 749 * mechanism if additional queries are made. 750 */ 751 class DiscoveredProcessors implements Iterable<ProcessorState> { 752 753 class ProcessorStateIterator implements Iterator<ProcessorState> { 754 DiscoveredProcessors psi; 755 Iterator<ProcessorState> innerIter; 756 boolean onProcInterator; 757 758 ProcessorStateIterator(DiscoveredProcessors psi) { 759 this.psi = psi; 760 this.innerIter = psi.procStateList.iterator(); 761 this.onProcInterator = false; 762 } 763 764 public ProcessorState next() { 765 if (!onProcInterator) { 766 if (innerIter.hasNext()) 767 return innerIter.next(); 768 else 769 onProcInterator = true; 770 } 771 772 if (psi.processorIterator.hasNext()) { 773 ProcessorState ps = new ProcessorState(psi.processorIterator.next(), 774 log, source, allowModules, 775 JavacProcessingEnvironment.this); 776 psi.procStateList.add(ps); 777 return ps; 778 } else 779 throw new NoSuchElementException(); 780 } 781 782 public boolean hasNext() { 783 if (onProcInterator) 784 return psi.processorIterator.hasNext(); 785 else 786 return innerIter.hasNext() || psi.processorIterator.hasNext(); 787 } 788 789 public void remove () { 790 throw new UnsupportedOperationException(); 791 } 792 793 /** 794 * Run all remaining processors on the procStateList that 795 * have not already run this round with an empty set of 796 * annotations. 797 */ 798 public void runContributingProcs(RoundEnvironment re) { 799 if (!onProcInterator) { 800 Set<TypeElement> emptyTypeElements = Collections.emptySet(); 801 while(innerIter.hasNext()) { 802 ProcessorState ps = innerIter.next(); 803 if (ps.contributed) 804 callProcessor(ps.processor, emptyTypeElements, re); 805 } 806 } 807 } 808 } 809 810 Iterator<? extends Processor> processorIterator; 811 ArrayList<ProcessorState> procStateList; 812 813 public ProcessorStateIterator iterator() { 814 return new ProcessorStateIterator(this); 815 } 816 817 DiscoveredProcessors(Iterator<? extends Processor> processorIterator) { 818 this.processorIterator = processorIterator; 819 this.procStateList = new ArrayList<>(); 820 } 821 822 /** 823 * Free jar files, etc. if using a service loader. 824 */ 825 public void close() { 826 if (processorIterator != null && 827 processorIterator instanceof ServiceIterator) { 828 ((ServiceIterator) processorIterator).close(); 829 } 830 } 831 } 832 833 private void discoverAndRunProcs(Set<TypeElement> annotationsPresent, 834 List<ClassSymbol> topLevelClasses, 835 List<PackageSymbol> packageInfoFiles, 836 List<ModuleSymbol> moduleInfoFiles) { 837 Map<String, TypeElement> unmatchedAnnotations = new HashMap<>(annotationsPresent.size()); 838 839 for(TypeElement a : annotationsPresent) { 840 ModuleElement mod = elementUtils.getModuleOf(a); 841 String moduleSpec = allowModules && mod != null ? mod.getQualifiedName() + "/" : ""; 842 unmatchedAnnotations.put(moduleSpec + a.getQualifiedName().toString(), 843 a); 844 } 845 846 // Give "*" processors a chance to match 847 if (unmatchedAnnotations.size() == 0) 848 unmatchedAnnotations.put("", null); 849 850 DiscoveredProcessors.ProcessorStateIterator psi = discoveredProcs.iterator(); 851 // TODO: Create proper argument values; need past round 852 // information to fill in this constructor. Note that the 1 853 // st round of processing could be the last round if there 854 // were parse errors on the initial source files; however, we 855 // are not doing processing in that case. 856 857 Set<Element> rootElements = new LinkedHashSet<>(); 858 rootElements.addAll(topLevelClasses); 859 rootElements.addAll(packageInfoFiles); 860 rootElements.addAll(moduleInfoFiles); 861 rootElements = Collections.unmodifiableSet(rootElements); 862 863 RoundEnvironment renv = new JavacRoundEnvironment(false, 864 false, 865 rootElements, 866 JavacProcessingEnvironment.this); 867 868 while(unmatchedAnnotations.size() > 0 && psi.hasNext() ) { 869 ProcessorState ps = psi.next(); 870 Set<String> matchedNames = new HashSet<>(); 871 Set<TypeElement> typeElements = new LinkedHashSet<>(); 872 873 for (Map.Entry<String, TypeElement> entry: unmatchedAnnotations.entrySet()) { 874 String unmatchedAnnotationName = entry.getKey(); 875 if (ps.annotationSupported(unmatchedAnnotationName) ) { 876 matchedNames.add(unmatchedAnnotationName); 877 TypeElement te = entry.getValue(); 878 if (te != null) 879 typeElements.add(te); 880 } 881 } 882 883 if (matchedNames.size() > 0 || ps.contributed) { 884 boolean processingResult = callProcessor(ps.processor, typeElements, renv); 885 ps.contributed = true; 886 ps.removeSupportedOptions(unmatchedProcessorOptions); 887 888 if (printProcessorInfo || verbose) { 889 log.printLines("x.print.processor.info", 890 ps.processor.getClass().getName(), 891 matchedNames.toString(), 892 processingResult); 893 } 894 895 if (processingResult) { 896 unmatchedAnnotations.keySet().removeAll(matchedNames); 897 } 898 899 } 900 } 901 unmatchedAnnotations.remove(""); 902 903 if (lint && unmatchedAnnotations.size() > 0) { 904 // Remove annotations processed by javac 905 unmatchedAnnotations.keySet().removeAll(platformAnnotations); 906 if (unmatchedAnnotations.size() > 0) { 907 log.warning("proc.annotations.without.processors", 908 unmatchedAnnotations.keySet()); 909 } 910 } 911 912 // Run contributing processors that haven't run yet 913 psi.runContributingProcs(renv); 914 } 915 916 /** 917 * Computes the set of annotations on the symbol in question. 918 * Leave class public for external testing purposes. 919 */ 920 public static class ComputeAnnotationSet extends 921 ElementScanner9<Set<TypeElement>, Set<TypeElement>> { 922 final Elements elements; 923 924 public ComputeAnnotationSet(Elements elements) { 925 super(); 926 this.elements = elements; 927 } 928 929 @Override @DefinedBy(Api.LANGUAGE_MODEL) 930 public Set<TypeElement> visitPackage(PackageElement e, Set<TypeElement> p) { 931 // Don't scan enclosed elements of a package 932 return p; 933 } 934 935 @Override @DefinedBy(Api.LANGUAGE_MODEL) 936 public Set<TypeElement> visitType(TypeElement e, Set<TypeElement> p) { 937 // Type parameters are not considered to be enclosed by a type 938 scan(e.getTypeParameters(), p); 939 return super.visitType(e, p); 940 } 941 942 @Override @DefinedBy(Api.LANGUAGE_MODEL) 943 public Set<TypeElement> visitExecutable(ExecutableElement e, Set<TypeElement> p) { 944 // Type parameters are not considered to be enclosed by an executable 945 scan(e.getTypeParameters(), p); 946 return super.visitExecutable(e, p); 947 } 948 949 void addAnnotations(Element e, Set<TypeElement> p) { 950 for (AnnotationMirror annotationMirror : 951 elements.getAllAnnotationMirrors(e) ) { 952 Element e2 = annotationMirror.getAnnotationType().asElement(); 953 p.add((TypeElement) e2); 954 } 955 } 956 957 @Override @DefinedBy(Api.LANGUAGE_MODEL) 958 public Set<TypeElement> scan(Element e, Set<TypeElement> p) { 959 addAnnotations(e, p); 960 return super.scan(e, p); 961 } 962 } 963 964 private boolean callProcessor(Processor proc, 965 Set<? extends TypeElement> tes, 966 RoundEnvironment renv) { 967 try { 968 return proc.process(tes, renv); 969 } catch (ClassFinder.BadClassFile ex) { 970 log.error("proc.cant.access.1", ex.sym, ex.getDetailValue()); 971 return false; 972 } catch (CompletionFailure ex) { 973 StringWriter out = new StringWriter(); 974 ex.printStackTrace(new PrintWriter(out)); 975 log.error("proc.cant.access", ex.sym, ex.getDetailValue(), out.toString()); 976 return false; 977 } catch (ClientCodeException e) { 978 throw e; 979 } catch (Throwable t) { 980 throw new AnnotationProcessingError(t); 981 } 982 } 983 984 /** 985 * Helper object for a single round of annotation processing. 986 */ 987 class Round { 988 /** The round number. */ 989 final int number; 990 /** The diagnostic handler for the round. */ 991 final Log.DeferredDiagnosticHandler deferredDiagnosticHandler; 992 993 /** The ASTs to be compiled. */ 994 List<JCCompilationUnit> roots; 995 /** The trees that need to be cleaned - includes roots and implicitly parsed trees. */ 996 Set<JCCompilationUnit> treesToClean; 997 /** The classes to be compiler that have were generated. */ 998 Map<ModuleSymbol, Map<String, JavaFileObject>> genClassFiles; 999 1000 /** The set of annotations to be processed this round. */ 1001 Set<TypeElement> annotationsPresent; 1002 /** The set of top level classes to be processed this round. */ 1003 List<ClassSymbol> topLevelClasses; 1004 /** The set of package-info files to be processed this round. */ 1005 List<PackageSymbol> packageInfoFiles; 1006 /** The set of module-info files to be processed this round. */ 1007 List<ModuleSymbol> moduleInfoFiles; 1008 1009 /** Create a round (common code). */ 1010 private Round(int number, Set<JCCompilationUnit> treesToClean, 1011 Log.DeferredDiagnosticHandler deferredDiagnosticHandler) { 1012 this.number = number; 1013 1014 if (number == 1) { 1015 Assert.checkNonNull(deferredDiagnosticHandler); 1016 this.deferredDiagnosticHandler = deferredDiagnosticHandler; 1017 } else { 1018 this.deferredDiagnosticHandler = new Log.DeferredDiagnosticHandler(log); 1019 compiler.setDeferredDiagnosticHandler(this.deferredDiagnosticHandler); 1020 } 1021 1022 // the following will be populated as needed 1023 topLevelClasses = List.nil(); 1024 packageInfoFiles = List.nil(); 1025 moduleInfoFiles = List.nil(); 1026 this.treesToClean = treesToClean; 1027 } 1028 1029 /** Create the first round. */ 1030 Round(List<JCCompilationUnit> roots, 1031 List<ClassSymbol> classSymbols, 1032 Set<JCCompilationUnit> treesToClean, 1033 Log.DeferredDiagnosticHandler deferredDiagnosticHandler) { 1034 this(1, treesToClean, deferredDiagnosticHandler); 1035 this.roots = roots; 1036 genClassFiles = new HashMap<>(); 1037 1038 // The reverse() in the following line is to maintain behavioural 1039 // compatibility with the previous revision of the code. Strictly speaking, 1040 // it should not be necessary, but a javah golden file test fails without it. 1041 topLevelClasses = 1042 getTopLevelClasses(roots).prependList(classSymbols.reverse()); 1043 1044 packageInfoFiles = getPackageInfoFiles(roots); 1045 1046 moduleInfoFiles = getModuleInfoFiles(roots); 1047 1048 findAnnotationsPresent(); 1049 } 1050 1051 /** Create a new round. */ 1052 private Round(Round prev, 1053 Set<JavaFileObject> newSourceFiles, Map<ModuleSymbol, Map<String,JavaFileObject>> newClassFiles) { 1054 this(prev.number+1, prev.treesToClean, null); 1055 prev.newRound(); 1056 this.genClassFiles = prev.genClassFiles; 1057 1058 List<JCCompilationUnit> parsedFiles = compiler.parseFiles(newSourceFiles); 1059 roots = prev.roots.appendList(parsedFiles); 1060 1061 // Check for errors after parsing 1062 if (unrecoverableError()) { 1063 compiler.initModules(List.nil()); 1064 return; 1065 } 1066 1067 roots = compiler.initModules(roots); 1068 1069 enterClassFiles(genClassFiles); 1070 List<ClassSymbol> newClasses = enterClassFiles(newClassFiles); 1071 for (Entry<ModuleSymbol, Map<String, JavaFileObject>> moduleAndClassFiles : newClassFiles.entrySet()) { 1072 genClassFiles.computeIfAbsent(moduleAndClassFiles.getKey(), m -> new LinkedHashMap<>()).putAll(moduleAndClassFiles.getValue()); 1073 } 1074 enterTrees(roots); 1075 1076 if (unrecoverableError()) 1077 return; 1078 1079 topLevelClasses = join( 1080 getTopLevelClasses(parsedFiles), 1081 getTopLevelClassesFromClasses(newClasses)); 1082 1083 packageInfoFiles = join( 1084 getPackageInfoFiles(parsedFiles), 1085 getPackageInfoFilesFromClasses(newClasses)); 1086 1087 moduleInfoFiles = List.nil(); //module-info cannot be generated 1088 1089 findAnnotationsPresent(); 1090 } 1091 1092 /** Create the next round to be used. */ 1093 Round next(Set<JavaFileObject> newSourceFiles, Map<ModuleSymbol, Map<String, JavaFileObject>> newClassFiles) { 1094 return new Round(this, newSourceFiles, newClassFiles); 1095 } 1096 1097 /** Prepare the compiler for the final compilation. */ 1098 void finalCompiler() { 1099 newRound(); 1100 } 1101 1102 /** Return the number of errors found so far in this round. 1103 * This may include uncoverable errors, such as parse errors, 1104 * and transient errors, such as missing symbols. */ 1105 int errorCount() { 1106 return compiler.errorCount(); 1107 } 1108 1109 /** Return the number of warnings found so far in this round. */ 1110 int warningCount() { 1111 return compiler.warningCount(); 1112 } 1113 1114 /** Return whether or not an unrecoverable error has occurred. */ 1115 boolean unrecoverableError() { 1116 if (messager.errorRaised()) 1117 return true; 1118 1119 for (JCDiagnostic d: deferredDiagnosticHandler.getDiagnostics()) { 1120 switch (d.getKind()) { 1121 case WARNING: 1122 if (werror) 1123 return true; 1124 break; 1125 1126 case ERROR: 1127 if (fatalErrors || !d.isFlagSet(RECOVERABLE)) 1128 return true; 1129 break; 1130 } 1131 } 1132 1133 return false; 1134 } 1135 1136 /** Find the set of annotations present in the set of top level 1137 * classes and package info files to be processed this round. */ 1138 void findAnnotationsPresent() { 1139 ComputeAnnotationSet annotationComputer = new ComputeAnnotationSet(elementUtils); 1140 // Use annotation processing to compute the set of annotations present 1141 annotationsPresent = new LinkedHashSet<>(); 1142 for (ClassSymbol classSym : topLevelClasses) 1143 annotationComputer.scan(classSym, annotationsPresent); 1144 for (PackageSymbol pkgSym : packageInfoFiles) 1145 annotationComputer.scan(pkgSym, annotationsPresent); 1146 for (ModuleSymbol mdlSym : moduleInfoFiles) 1147 annotationComputer.scan(mdlSym, annotationsPresent); 1148 } 1149 1150 /** Enter a set of generated class files. */ 1151 private List<ClassSymbol> enterClassFiles(Map<ModuleSymbol, Map<String, JavaFileObject>> modulesAndClassFiles) { 1152 List<ClassSymbol> list = List.nil(); 1153 1154 for (Entry<ModuleSymbol, Map<String, JavaFileObject>> moduleAndClassFiles : modulesAndClassFiles.entrySet()) { 1155 for (Map.Entry<String,JavaFileObject> entry : moduleAndClassFiles.getValue().entrySet()) { 1156 Name name = names.fromString(entry.getKey()); 1157 JavaFileObject file = entry.getValue(); 1158 if (file.getKind() != JavaFileObject.Kind.CLASS) 1159 throw new AssertionError(file); 1160 ClassSymbol cs; 1161 if (isPkgInfo(file, JavaFileObject.Kind.CLASS)) { 1162 Name packageName = Convert.packagePart(name); 1163 PackageSymbol p = symtab.enterPackage(moduleAndClassFiles.getKey(), packageName); 1164 if (p.package_info == null) 1165 p.package_info = symtab.enterClass(moduleAndClassFiles.getKey(), Convert.shortName(name), p); 1166 cs = p.package_info; 1167 cs.reset(); 1168 if (cs.classfile == null) 1169 cs.classfile = file; 1170 cs.completer = initialCompleter; 1171 } else { 1172 cs = symtab.enterClass(moduleAndClassFiles.getKey(), name); 1173 cs.reset(); 1174 cs.classfile = file; 1175 cs.completer = initialCompleter; 1176 cs.owner.members().enter(cs); //XXX - OverwriteBetweenCompilations; syms.getClass is not sufficient anymore 1177 } 1178 list = list.prepend(cs); 1179 } 1180 } 1181 return list.reverse(); 1182 } 1183 1184 /** Enter a set of syntax trees. */ 1185 private void enterTrees(List<JCCompilationUnit> roots) { 1186 compiler.enterTrees(roots); 1187 } 1188 1189 /** Run a processing round. */ 1190 void run(boolean lastRound, boolean errorStatus) { 1191 printRoundInfo(lastRound); 1192 1193 if (!taskListener.isEmpty()) 1194 taskListener.started(new TaskEvent(TaskEvent.Kind.ANNOTATION_PROCESSING_ROUND)); 1195 1196 try { 1197 if (lastRound) { 1198 filer.setLastRound(true); 1199 Set<Element> emptyRootElements = Collections.emptySet(); // immutable 1200 RoundEnvironment renv = new JavacRoundEnvironment(true, 1201 errorStatus, 1202 emptyRootElements, 1203 JavacProcessingEnvironment.this); 1204 discoveredProcs.iterator().runContributingProcs(renv); 1205 } else { 1206 discoverAndRunProcs(annotationsPresent, topLevelClasses, packageInfoFiles, moduleInfoFiles); 1207 } 1208 } catch (Throwable t) { 1209 // we're specifically expecting Abort here, but if any Throwable 1210 // comes by, we should flush all deferred diagnostics, rather than 1211 // drop them on the ground. 1212 deferredDiagnosticHandler.reportDeferredDiagnostics(); 1213 log.popDiagnosticHandler(deferredDiagnosticHandler); 1214 compiler.setDeferredDiagnosticHandler(null); 1215 throw t; 1216 } finally { 1217 if (!taskListener.isEmpty()) 1218 taskListener.finished(new TaskEvent(TaskEvent.Kind.ANNOTATION_PROCESSING_ROUND)); 1219 } 1220 } 1221 1222 void showDiagnostics(boolean showAll) { 1223 Set<JCDiagnostic.Kind> kinds = EnumSet.allOf(JCDiagnostic.Kind.class); 1224 if (!showAll) { 1225 // suppress errors, which are all presumed to be transient resolve errors 1226 kinds.remove(JCDiagnostic.Kind.ERROR); 1227 } 1228 deferredDiagnosticHandler.reportDeferredDiagnostics(kinds); 1229 log.popDiagnosticHandler(deferredDiagnosticHandler); 1230 compiler.setDeferredDiagnosticHandler(null); 1231 } 1232 1233 /** Print info about this round. */ 1234 private void printRoundInfo(boolean lastRound) { 1235 if (printRounds || verbose) { 1236 List<ClassSymbol> tlc = lastRound ? List.nil() : topLevelClasses; 1237 Set<TypeElement> ap = lastRound ? Collections.emptySet() : annotationsPresent; 1238 log.printLines("x.print.rounds", 1239 number, 1240 "{" + tlc.toString(", ") + "}", 1241 ap, 1242 lastRound); 1243 } 1244 } 1245 1246 /** Prepare for new round of annotation processing. Cleans trees, resets symbols, and 1247 * asks selected services to prepare to a new round of annotation processing. 1248 */ 1249 private void newRound() { 1250 //ensure treesToClean contains all trees, including implicitly parsed ones 1251 for (Env<AttrContext> env : enter.getEnvs()) { 1252 treesToClean.add(env.toplevel); 1253 } 1254 for (JCCompilationUnit node : treesToClean) { 1255 treeCleaner.scan(node); 1256 } 1257 chk.newRound(); 1258 enter.newRound(); 1259 filer.newRound(); 1260 messager.newRound(); 1261 compiler.newRound(); 1262 modules.newRound(); 1263 types.newRound(); 1264 annotate.newRound(); 1265 1266 boolean foundError = false; 1267 1268 for (ClassSymbol cs : symtab.getAllClasses()) { 1269 if (cs.kind == ERR) { 1270 foundError = true; 1271 break; 1272 } 1273 } 1274 1275 if (foundError) { 1276 for (ClassSymbol cs : symtab.getAllClasses()) { 1277 if (cs.classfile != null || cs.kind == ERR) { 1278 cs.reset(); 1279 cs.type = new ClassType(cs.type.getEnclosingType(), null, cs); 1280 if (cs.isCompleted()) { 1281 cs.completer = initialCompleter; 1282 } 1283 } 1284 } 1285 } 1286 } 1287 } 1288 1289 1290 // TODO: internal catch clauses?; catch and rethrow an annotation 1291 // processing error 1292 public boolean doProcessing(List<JCCompilationUnit> roots, 1293 List<ClassSymbol> classSymbols, 1294 Iterable<? extends PackageSymbol> pckSymbols, 1295 Log.DeferredDiagnosticHandler deferredDiagnosticHandler) { 1296 final Set<JCCompilationUnit> treesToClean = 1297 Collections.newSetFromMap(new IdentityHashMap<JCCompilationUnit, Boolean>()); 1298 1299 //fill already attributed implicit trees: 1300 for (Env<AttrContext> env : enter.getEnvs()) { 1301 treesToClean.add(env.toplevel); 1302 } 1303 1304 Set<PackageSymbol> specifiedPackages = new LinkedHashSet<>(); 1305 for (PackageSymbol psym : pckSymbols) 1306 specifiedPackages.add(psym); 1307 this.specifiedPackages = Collections.unmodifiableSet(specifiedPackages); 1308 1309 Round round = new Round(roots, classSymbols, treesToClean, deferredDiagnosticHandler); 1310 1311 boolean errorStatus; 1312 boolean moreToDo; 1313 do { 1314 // Run processors for round n 1315 round.run(false, false); 1316 1317 // Processors for round n have run to completion. 1318 // Check for errors and whether there is more work to do. 1319 errorStatus = round.unrecoverableError(); 1320 moreToDo = moreToDo(); 1321 1322 round.showDiagnostics(errorStatus || showResolveErrors); 1323 1324 // Set up next round. 1325 // Copy mutable collections returned from filer. 1326 round = round.next( 1327 new LinkedHashSet<>(filer.getGeneratedSourceFileObjects()), 1328 new LinkedHashMap<>(filer.getGeneratedClasses())); 1329 1330 // Check for errors during setup. 1331 if (round.unrecoverableError()) 1332 errorStatus = true; 1333 1334 } while (moreToDo && !errorStatus); 1335 1336 // run last round 1337 round.run(true, errorStatus); 1338 round.showDiagnostics(true); 1339 1340 filer.warnIfUnclosedFiles(); 1341 warnIfUnmatchedOptions(); 1342 1343 /* 1344 * If an annotation processor raises an error in a round, 1345 * that round runs to completion and one last round occurs. 1346 * The last round may also occur because no more source or 1347 * class files have been generated. Therefore, if an error 1348 * was raised on either of the last *two* rounds, the compile 1349 * should exit with a nonzero exit code. The current value of 1350 * errorStatus holds whether or not an error was raised on the 1351 * second to last round; errorRaised() gives the error status 1352 * of the last round. 1353 */ 1354 if (messager.errorRaised() 1355 || werror && round.warningCount() > 0 && round.errorCount() > 0) 1356 errorStatus = true; 1357 1358 Set<JavaFileObject> newSourceFiles = 1359 new LinkedHashSet<>(filer.getGeneratedSourceFileObjects()); 1360 roots = round.roots; 1361 1362 errorStatus = errorStatus || (compiler.errorCount() > 0); 1363 1364 round.finalCompiler(); 1365 1366 if (newSourceFiles.size() > 0) 1367 roots = roots.appendList(compiler.parseFiles(newSourceFiles)); 1368 1369 errorStatus = errorStatus || (compiler.errorCount() > 0); 1370 1371 // Free resources 1372 this.close(); 1373 1374 if (errorStatus && compiler.errorCount() == 0) { 1375 compiler.log.nerrors++; 1376 } 1377 1378 compiler.enterTreesIfNeeded(roots); 1379 1380 if (!taskListener.isEmpty()) 1381 taskListener.finished(new TaskEvent(TaskEvent.Kind.ANNOTATION_PROCESSING)); 1382 1383 return true; 1384 } 1385 1386 private void warnIfUnmatchedOptions() { 1387 if (!unmatchedProcessorOptions.isEmpty()) { 1388 log.warning("proc.unmatched.processor.options", unmatchedProcessorOptions.toString()); 1389 } 1390 } 1391 1392 /** 1393 * Free resources related to annotation processing. 1394 */ 1395 public void close() { 1396 filer.close(); 1397 if (discoveredProcs != null) // Make calling close idempotent 1398 discoveredProcs.close(); 1399 discoveredProcs = null; 1400 } 1401 1402 private List<ClassSymbol> getTopLevelClasses(List<? extends JCCompilationUnit> units) { 1403 List<ClassSymbol> classes = List.nil(); 1404 for (JCCompilationUnit unit : units) { 1405 for (JCTree node : unit.defs) { 1406 if (node.hasTag(JCTree.Tag.CLASSDEF)) { 1407 ClassSymbol sym = ((JCClassDecl) node).sym; 1408 Assert.checkNonNull(sym); 1409 classes = classes.prepend(sym); 1410 } 1411 } 1412 } 1413 return classes.reverse(); 1414 } 1415 1416 private List<ClassSymbol> getTopLevelClassesFromClasses(List<? extends ClassSymbol> syms) { 1417 List<ClassSymbol> classes = List.nil(); 1418 for (ClassSymbol sym : syms) { 1419 if (!isPkgInfo(sym)) { 1420 classes = classes.prepend(sym); 1421 } 1422 } 1423 return classes.reverse(); 1424 } 1425 1426 private List<PackageSymbol> getPackageInfoFiles(List<? extends JCCompilationUnit> units) { 1427 List<PackageSymbol> packages = List.nil(); 1428 for (JCCompilationUnit unit : units) { 1429 if (isPkgInfo(unit.sourcefile, JavaFileObject.Kind.SOURCE)) { 1430 packages = packages.prepend(unit.packge); 1431 } 1432 } 1433 return packages.reverse(); 1434 } 1435 1436 private List<PackageSymbol> getPackageInfoFilesFromClasses(List<? extends ClassSymbol> syms) { 1437 List<PackageSymbol> packages = List.nil(); 1438 for (ClassSymbol sym : syms) { 1439 if (isPkgInfo(sym)) { 1440 packages = packages.prepend((PackageSymbol) sym.owner); 1441 } 1442 } 1443 return packages.reverse(); 1444 } 1445 1446 private List<ModuleSymbol> getModuleInfoFiles(List<? extends JCCompilationUnit> units) { 1447 List<ModuleSymbol> modules = List.nil(); 1448 for (JCCompilationUnit unit : units) { 1449 if (isModuleInfo(unit.sourcefile, JavaFileObject.Kind.SOURCE) && 1450 unit.defs.nonEmpty() && 1451 unit.defs.head.hasTag(Tag.MODULEDEF)) { 1452 modules = modules.prepend(unit.modle); 1453 } 1454 } 1455 return modules.reverse(); 1456 } 1457 1458 // avoid unchecked warning from use of varargs 1459 private static <T> List<T> join(List<T> list1, List<T> list2) { 1460 return list1.appendList(list2); 1461 } 1462 1463 private boolean isPkgInfo(JavaFileObject fo, JavaFileObject.Kind kind) { 1464 return fo.isNameCompatible("package-info", kind); 1465 } 1466 1467 private boolean isPkgInfo(ClassSymbol sym) { 1468 return isPkgInfo(sym.classfile, JavaFileObject.Kind.CLASS) && (sym.packge().package_info == sym); 1469 } 1470 1471 private boolean isModuleInfo(JavaFileObject fo, JavaFileObject.Kind kind) { 1472 return fo.isNameCompatible("module-info", kind); 1473 } 1474 1475 /* 1476 * Called retroactively to determine if a class loader was required, 1477 * after we have failed to create one. 1478 */ 1479 private boolean needClassLoader(String procNames, Iterable<? extends Path> workingpath) { 1480 if (procNames != null) 1481 return true; 1482 1483 URL[] urls = new URL[1]; 1484 for(Path pathElement : workingpath) { 1485 try { 1486 urls[0] = pathElement.toUri().toURL(); 1487 if (ServiceProxy.hasService(Processor.class, urls)) 1488 return true; 1489 } catch (MalformedURLException ex) { 1490 throw new AssertionError(ex); 1491 } 1492 catch (ServiceProxy.ServiceConfigurationError e) { 1493 log.error("proc.bad.config.file", e.getLocalizedMessage()); 1494 return true; 1495 } 1496 } 1497 1498 return false; 1499 } 1500 1501 class ImplicitCompleter implements Completer { 1502 1503 private final JCCompilationUnit topLevel; 1504 1505 public ImplicitCompleter(JCCompilationUnit topLevel) { 1506 this.topLevel = topLevel; 1507 } 1508 1509 @Override public void complete(Symbol sym) throws CompletionFailure { 1510 compiler.readSourceFile(topLevel, (ClassSymbol) sym); 1511 } 1512 } 1513 1514 private final TreeScanner treeCleaner = new TreeScanner() { 1515 public void scan(JCTree node) { 1516 super.scan(node); 1517 if (node != null) 1518 node.type = null; 1519 } 1520 JCCompilationUnit topLevel; 1521 public void visitTopLevel(JCCompilationUnit node) { 1522 if (node.packge != null) { 1523 if (node.packge.package_info != null) { 1524 node.packge.package_info.reset(); 1525 } 1526 node.packge.reset(); 1527 } 1528 boolean isModuleInfo = node.sourcefile.isNameCompatible("module-info", Kind.SOURCE); 1529 if (isModuleInfo) { 1530 node.modle.reset(); 1531 node.modle.completer = sym -> modules.enter(List.of(node), node.modle.module_info); 1532 node.modle.module_info.reset(); 1533 node.modle.module_info.members_field = WriteableScope.create(node.modle.module_info); 1534 } 1535 node.packge = null; 1536 topLevel = node; 1537 try { 1538 super.visitTopLevel(node); 1539 } finally { 1540 topLevel = null; 1541 } 1542 } 1543 public void visitClassDef(JCClassDecl node) { 1544 super.visitClassDef(node); 1545 // remove generated constructor that may have been added during attribution: 1546 List<JCTree> beforeConstructor = List.nil(); 1547 List<JCTree> defs = node.defs; 1548 while (defs.nonEmpty() && !defs.head.hasTag(Tag.METHODDEF)) { 1549 beforeConstructor = beforeConstructor.prepend(defs.head); 1550 defs = defs.tail; 1551 } 1552 if (defs.nonEmpty() && 1553 (((JCMethodDecl) defs.head).mods.flags & Flags.GENERATEDCONSTR) != 0) { 1554 defs = defs.tail; 1555 while (beforeConstructor.nonEmpty()) { 1556 defs = defs.prepend(beforeConstructor.head); 1557 beforeConstructor = beforeConstructor.tail; 1558 } 1559 node.defs = defs; 1560 } 1561 if (node.sym != null) { 1562 node.sym.completer = new ImplicitCompleter(topLevel); 1563 } 1564 node.sym = null; 1565 } 1566 public void visitMethodDef(JCMethodDecl node) { 1567 // remove super constructor call that may have been added during attribution: 1568 if (TreeInfo.isConstructor(node) && node.sym != null && node.sym.owner.isEnum() && 1569 node.body.stats.nonEmpty() && TreeInfo.isSuperCall(node.body.stats.head) && 1570 node.body.stats.head.pos == node.body.pos) { 1571 node.body.stats = node.body.stats.tail; 1572 } 1573 node.sym = null; 1574 super.visitMethodDef(node); 1575 } 1576 public void visitVarDef(JCVariableDecl node) { 1577 node.sym = null; 1578 super.visitVarDef(node); 1579 } 1580 public void visitNewClass(JCNewClass node) { 1581 node.constructor = null; 1582 super.visitNewClass(node); 1583 } 1584 public void visitAssignop(JCAssignOp node) { 1585 node.operator = null; 1586 super.visitAssignop(node); 1587 } 1588 public void visitUnary(JCUnary node) { 1589 node.operator = null; 1590 super.visitUnary(node); 1591 } 1592 public void visitBinary(JCBinary node) { 1593 node.operator = null; 1594 super.visitBinary(node); 1595 } 1596 public void visitSelect(JCFieldAccess node) { 1597 node.sym = null; 1598 super.visitSelect(node); 1599 } 1600 public void visitIdent(JCIdent node) { 1601 node.sym = null; 1602 super.visitIdent(node); 1603 } 1604 public void visitAnnotation(JCAnnotation node) { 1605 node.attribute = null; 1606 super.visitAnnotation(node); 1607 } 1608 }; 1609 1610 1611 private boolean moreToDo() { 1612 return filer.newFiles(); 1613 } 1614 1615 /** 1616 * {@inheritDoc} 1617 * 1618 * Command line options suitable for presenting to annotation 1619 * processors. 1620 * {@literal "-Afoo=bar"} should be {@literal "-Afoo" => "bar"}. 1621 */ 1622 @DefinedBy(Api.ANNOTATION_PROCESSING) 1623 public Map<String,String> getOptions() { 1624 return processorOptions; 1625 } 1626 1627 @DefinedBy(Api.ANNOTATION_PROCESSING) 1628 public Messager getMessager() { 1629 return messager; 1630 } 1631 1632 @DefinedBy(Api.ANNOTATION_PROCESSING) 1633 public JavacFiler getFiler() { 1634 return filer; 1635 } 1636 1637 @DefinedBy(Api.ANNOTATION_PROCESSING) 1638 public JavacElements getElementUtils() { 1639 return elementUtils; 1640 } 1641 1642 @DefinedBy(Api.ANNOTATION_PROCESSING) 1643 public JavacTypes getTypeUtils() { 1644 return typeUtils; 1645 } 1646 1647 @DefinedBy(Api.ANNOTATION_PROCESSING) 1648 public SourceVersion getSourceVersion() { 1649 return Source.toSourceVersion(source); 1650 } 1651 1652 @DefinedBy(Api.ANNOTATION_PROCESSING) 1653 public Locale getLocale() { 1654 return messages.getCurrentLocale(); 1655 } 1656 1657 public Set<Symbol.PackageSymbol> getSpecifiedPackages() { 1658 return specifiedPackages; 1659 } 1660 1661 public static final Pattern noMatches = Pattern.compile("(\\P{all})+"); 1662 1663 /** 1664 * Convert import-style string for supported annotations into a 1665 * regex matching that string. If the string is not a valid 1666 * import-style string, return a regex that won't match anything. 1667 */ 1668 private static Pattern importStringToPattern(boolean allowModules, String s, Processor p, Log log) { 1669 String module; 1670 String pkg; 1671 int slash = s.indexOf('/'); 1672 if (slash == (-1)) { 1673 if (s.equals("*")) { 1674 return MatchingUtils.validImportStringToPattern(s); 1675 } 1676 module = allowModules ? ".*/" : ""; 1677 pkg = s; 1678 } else { 1679 module = Pattern.quote(s.substring(0, slash + 1)); 1680 pkg = s.substring(slash + 1); 1681 } 1682 if (MatchingUtils.isValidImportString(pkg)) { 1683 return Pattern.compile(module + MatchingUtils.validImportStringToPatternString(pkg)); 1684 } else { 1685 log.warning("proc.malformed.supported.string", s, p.getClass().getName()); 1686 return noMatches; // won't match any valid identifier 1687 } 1688 } 1689 1690 /** 1691 * For internal use only. This method may be removed without warning. 1692 */ 1693 public Context getContext() { 1694 return context; 1695 } 1696 1697 /** 1698 * For internal use only. This method may be removed without warning. 1699 */ 1700 public ClassLoader getProcessorClassLoader() { 1701 return processorClassLoader; 1702 } 1703 1704 public String toString() { 1705 return "javac ProcessingEnvironment"; 1706 } 1707 1708 public static boolean isValidOptionName(String optionName) { 1709 for(String s : optionName.split("\\.", -1)) { 1710 if (!SourceVersion.isIdentifier(s)) 1711 return false; 1712 } 1713 return true; 1714 } 1715} 1716