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