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