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