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, boolean allowModules, ProcessingEnvironment env) { 672 processor = p; 673 contributed = false; 674 675 try { 676 processor.init(env); 677 678 checkSourceVersionCompatibility(source, log); 679 680 supportedAnnotationPatterns = new ArrayList<>(); 681 for (String importString : processor.getSupportedAnnotationTypes()) { 682 supportedAnnotationPatterns.add(importStringToPattern(allowModules, 683 importString, 684 processor, 685 log)); 686 } 687 688 supportedOptionNames = new ArrayList<>(); 689 for (String optionName : processor.getSupportedOptions() ) { 690 if (checkOptionName(optionName, log)) 691 supportedOptionNames.add(optionName); 692 } 693 694 } catch (ClientCodeException e) { 695 throw e; 696 } catch (Throwable t) { 697 throw new AnnotationProcessingError(t); 698 } 699 } 700 701 /** 702 * Checks whether or not a processor's source version is 703 * compatible with the compilation source version. The 704 * processor's source version needs to be greater than or 705 * equal to the source version of the compile. 706 */ 707 private void checkSourceVersionCompatibility(Source source, Log log) { 708 SourceVersion procSourceVersion = processor.getSupportedSourceVersion(); 709 710 if (procSourceVersion.compareTo(Source.toSourceVersion(source)) < 0 ) { 711 log.warning(Warnings.ProcProcessorIncompatibleSourceVersion(procSourceVersion, 712 processor.getClass().getName(), 713 source.name)); 714 } 715 } 716 717 private boolean checkOptionName(String optionName, Log log) { 718 boolean valid = isValidOptionName(optionName); 719 if (!valid) 720 log.error(Errors.ProcProcessorBadOptionName(optionName, 721 processor.getClass().getName())); 722 return valid; 723 } 724 725 public boolean annotationSupported(String annotationName) { 726 for(Pattern p: supportedAnnotationPatterns) { 727 if (p.matcher(annotationName).matches()) 728 return true; 729 } 730 return false; 731 } 732 733 /** 734 * Remove options that are matched by this processor. 735 */ 736 public void removeSupportedOptions(Set<String> unmatchedProcessorOptions) { 737 unmatchedProcessorOptions.removeAll(supportedOptionNames); 738 } 739 } 740 741 // TODO: These two classes can probably be rewritten better... 742 /** 743 * This class holds information about the processors that have 744 * been discovered so far as well as the means to discover more, if 745 * necessary. A single iterator should be used per round of 746 * annotation processing. The iterator first visits already 747 * discovered processors then fails over to the service provider 748 * mechanism if additional queries are made. 749 */ 750 class DiscoveredProcessors implements Iterable<ProcessorState> { 751 752 class ProcessorStateIterator implements Iterator<ProcessorState> { 753 DiscoveredProcessors psi; 754 Iterator<ProcessorState> innerIter; 755 boolean onProcInterator; 756 757 ProcessorStateIterator(DiscoveredProcessors psi) { 758 this.psi = psi; 759 this.innerIter = psi.procStateList.iterator(); 760 this.onProcInterator = false; 761 } 762 763 public ProcessorState next() { 764 if (!onProcInterator) { 765 if (innerIter.hasNext()) 766 return innerIter.next(); 767 else 768 onProcInterator = true; 769 } 770 771 if (psi.processorIterator.hasNext()) { 772 ProcessorState ps = new ProcessorState(psi.processorIterator.next(), 773 log, source, Feature.MODULES.allowedInSource(source), 774 JavacProcessingEnvironment.this); 775 psi.procStateList.add(ps); 776 return ps; 777 } else 778 throw new NoSuchElementException(); 779 } 780 781 public boolean hasNext() { 782 if (onProcInterator) 783 return psi.processorIterator.hasNext(); 784 else 785 return innerIter.hasNext() || psi.processorIterator.hasNext(); 786 } 787 788 public void remove () { 789 throw new UnsupportedOperationException(); 790 } 791 792 /** 793 * Run all remaining processors on the procStateList that 794 * have not already run this round with an empty set of 795 * annotations. 796 */ 797 public void runContributingProcs(RoundEnvironment re) { 798 if (!onProcInterator) { 799 Set<TypeElement> emptyTypeElements = Collections.emptySet(); 800 while(innerIter.hasNext()) { 801 ProcessorState ps = innerIter.next(); 802 if (ps.contributed) 803 callProcessor(ps.processor, emptyTypeElements, re); 804 } 805 } 806 } 807 } 808 809 Iterator<? extends Processor> processorIterator; 810 ArrayList<ProcessorState> procStateList; 811 812 public ProcessorStateIterator iterator() { 813 return new ProcessorStateIterator(this); 814 } 815 816 DiscoveredProcessors(Iterator<? extends Processor> processorIterator) { 817 this.processorIterator = processorIterator; 818 this.procStateList = new ArrayList<>(); 819 } 820 821 /** 822 * Free jar files, etc. if using a service loader. 823 */ 824 public void close() { 825 if (processorIterator != null && 826 processorIterator instanceof ServiceIterator) { 827 ((ServiceIterator) processorIterator).close(); 828 } 829 } 830 } 831 832 private void discoverAndRunProcs(Set<TypeElement> annotationsPresent, 833 List<ClassSymbol> topLevelClasses, 834 List<PackageSymbol> packageInfoFiles, 835 List<ModuleSymbol> moduleInfoFiles) { 836 Map<String, TypeElement> unmatchedAnnotations = new HashMap<>(annotationsPresent.size()); 837 838 for(TypeElement a : annotationsPresent) { 839 ModuleElement mod = elementUtils.getModuleOf(a); 840 String moduleSpec = Feature.MODULES.allowedInSource(source) && mod != null ? mod.getQualifiedName() + "/" : ""; 841 unmatchedAnnotations.put(moduleSpec + a.getQualifiedName().toString(), 842 a); 843 } 844 845 // Give "*" processors a chance to match 846 if (unmatchedAnnotations.size() == 0) 847 unmatchedAnnotations.put("", null); 848 849 DiscoveredProcessors.ProcessorStateIterator psi = discoveredProcs.iterator(); 850 // TODO: Create proper argument values; need past round 851 // information to fill in this constructor. Note that the 1 852 // st round of processing could be the last round if there 853 // were parse errors on the initial source files; however, we 854 // are not doing processing in that case. 855 856 Set<Element> rootElements = new LinkedHashSet<>(); 857 rootElements.addAll(topLevelClasses); 858 rootElements.addAll(packageInfoFiles); 859 rootElements.addAll(moduleInfoFiles); 860 rootElements = Collections.unmodifiableSet(rootElements); 861 862 RoundEnvironment renv = new JavacRoundEnvironment(false, 863 false, 864 rootElements, 865 JavacProcessingEnvironment.this); 866 867 while(unmatchedAnnotations.size() > 0 && psi.hasNext() ) { 868 ProcessorState ps = psi.next(); 869 Set<String> matchedNames = new HashSet<>(); 870 Set<TypeElement> typeElements = new LinkedHashSet<>(); 871 872 for (Map.Entry<String, TypeElement> entry: unmatchedAnnotations.entrySet()) { 873 String unmatchedAnnotationName = entry.getKey(); 874 if (ps.annotationSupported(unmatchedAnnotationName) ) { 875 matchedNames.add(unmatchedAnnotationName); 876 TypeElement te = entry.getValue(); 877 if (te != null) 878 typeElements.add(te); 879 } 880 } 881 882 if (matchedNames.size() > 0 || ps.contributed) { 883 boolean processingResult = callProcessor(ps.processor, typeElements, renv); 884 ps.contributed = true; 885 ps.removeSupportedOptions(unmatchedProcessorOptions); 886 887 if (printProcessorInfo || verbose) { 888 log.printLines("x.print.processor.info", 889 ps.processor.getClass().getName(), 890 matchedNames.toString(), 891 processingResult); 892 } 893 894 if (processingResult) { 895 unmatchedAnnotations.keySet().removeAll(matchedNames); 896 } 897 898 } 899 } 900 unmatchedAnnotations.remove(""); 901 902 if (lint && unmatchedAnnotations.size() > 0) { 903 // Remove annotations processed by javac 904 unmatchedAnnotations.keySet().removeAll(platformAnnotations); 905 if (unmatchedAnnotations.size() > 0) { 906 log.warning(Warnings.ProcAnnotationsWithoutProcessors(unmatchedAnnotations.keySet())); 907 } 908 } 909 910 // Run contributing processors that haven't run yet 911 psi.runContributingProcs(renv); 912 } 913 914 /** 915 * Computes the set of annotations on the symbol in question. 916 * Leave class public for external testing purposes. 917 */ 918 public static class ComputeAnnotationSet extends 919 ElementScanner9<Set<TypeElement>, Set<TypeElement>> { 920 final Elements elements; 921 922 public ComputeAnnotationSet(Elements elements) { 923 super(); 924 this.elements = elements; 925 } 926 927 @Override @DefinedBy(Api.LANGUAGE_MODEL) 928 public Set<TypeElement> visitPackage(PackageElement e, Set<TypeElement> p) { 929 // Don't scan enclosed elements of a package 930 return p; 931 } 932 933 @Override @DefinedBy(Api.LANGUAGE_MODEL) 934 public Set<TypeElement> visitType(TypeElement e, Set<TypeElement> p) { 935 // Type parameters are not considered to be enclosed by a type 936 scan(e.getTypeParameters(), p); 937 return super.visitType(e, p); 938 } 939 940 @Override @DefinedBy(Api.LANGUAGE_MODEL) 941 public Set<TypeElement> visitExecutable(ExecutableElement e, Set<TypeElement> p) { 942 // Type parameters are not considered to be enclosed by an executable 943 scan(e.getTypeParameters(), p); 944 return super.visitExecutable(e, p); 945 } 946 947 void addAnnotations(Element e, Set<TypeElement> p) { 948 for (AnnotationMirror annotationMirror : 949 elements.getAllAnnotationMirrors(e) ) { 950 Element e2 = annotationMirror.getAnnotationType().asElement(); 951 p.add((TypeElement) e2); 952 } 953 } 954 955 @Override @DefinedBy(Api.LANGUAGE_MODEL) 956 public Set<TypeElement> scan(Element e, Set<TypeElement> p) { 957 addAnnotations(e, p); 958 return super.scan(e, p); 959 } 960 } 961 962 private boolean callProcessor(Processor proc, 963 Set<? extends TypeElement> tes, 964 RoundEnvironment renv) { 965 Handler prevDeferredHandler = dcfh.setHandler(dcfh.userCodeHandler); 966 try { 967 return proc.process(tes, renv); 968 } catch (ClassFinder.BadClassFile ex) { 969 log.error(Errors.ProcCantAccess1(ex.sym, ex.getDetailValue())); 970 return false; 971 } catch (CompletionFailure ex) { 972 StringWriter out = new StringWriter(); 973 ex.printStackTrace(new PrintWriter(out)); 974 log.error(Errors.ProcCantAccess(ex.sym, ex.getDetailValue(), out.toString())); 975 return false; 976 } catch (ClientCodeException e) { 977 throw e; 978 } catch (Throwable t) { 979 throw new AnnotationProcessingError(t); 980 } finally { 981 dcfh.setHandler(prevDeferredHandler); 982 } 983 } 984 985 /** 986 * Helper object for a single round of annotation processing. 987 */ 988 class Round { 989 /** The round number. */ 990 final int number; 991 /** The diagnostic handler for the round. */ 992 final Log.DeferredDiagnosticHandler deferredDiagnosticHandler; 993 994 /** The ASTs to be compiled. */ 995 List<JCCompilationUnit> roots; 996 /** The trees that need to be cleaned - includes roots and implicitly parsed trees. */ 997 Set<JCCompilationUnit> treesToClean; 998 /** The classes to be compiler that have were generated. */ 999 Map<ModuleSymbol, Map<String, JavaFileObject>> genClassFiles; 1000 1001 /** The set of annotations to be processed this round. */ 1002 Set<TypeElement> annotationsPresent; 1003 /** The set of top level classes to be processed this round. */ 1004 List<ClassSymbol> topLevelClasses; 1005 /** The set of package-info files to be processed this round. */ 1006 List<PackageSymbol> packageInfoFiles; 1007 /** The set of module-info files to be processed this round. */ 1008 List<ModuleSymbol> moduleInfoFiles; 1009 1010 /** Create a round (common code). */ 1011 private Round(int number, Set<JCCompilationUnit> treesToClean, 1012 Log.DeferredDiagnosticHandler deferredDiagnosticHandler) { 1013 this.number = number; 1014 1015 if (number == 1) { 1016 Assert.checkNonNull(deferredDiagnosticHandler); 1017 this.deferredDiagnosticHandler = deferredDiagnosticHandler; 1018 } else { 1019 this.deferredDiagnosticHandler = new Log.DeferredDiagnosticHandler(log); 1020 compiler.setDeferredDiagnosticHandler(this.deferredDiagnosticHandler); 1021 } 1022 1023 // the following will be populated as needed 1024 topLevelClasses = List.nil(); 1025 packageInfoFiles = List.nil(); 1026 moduleInfoFiles = List.nil(); 1027 this.treesToClean = treesToClean; 1028 } 1029 1030 /** Create the first round. */ 1031 Round(List<JCCompilationUnit> roots, 1032 List<ClassSymbol> classSymbols, 1033 Set<JCCompilationUnit> treesToClean, 1034 Log.DeferredDiagnosticHandler deferredDiagnosticHandler) { 1035 this(1, treesToClean, deferredDiagnosticHandler); 1036 this.roots = roots; 1037 genClassFiles = new HashMap<>(); 1038 1039 // The reverse() in the following line is to maintain behavioural 1040 // compatibility with the previous revision of the code. Strictly speaking, 1041 // it should not be necessary, but a javah golden file test fails without it. 1042 topLevelClasses = 1043 getTopLevelClasses(roots).prependList(classSymbols.reverse()); 1044 1045 packageInfoFiles = getPackageInfoFiles(roots); 1046 1047 moduleInfoFiles = getModuleInfoFiles(roots); 1048 1049 findAnnotationsPresent(); 1050 } 1051 1052 /** Create a new round. */ 1053 private Round(Round prev, 1054 Set<JavaFileObject> newSourceFiles, Map<ModuleSymbol, Map<String,JavaFileObject>> newClassFiles) { 1055 this(prev.number+1, prev.treesToClean, null); 1056 prev.newRound(); 1057 this.genClassFiles = prev.genClassFiles; 1058 1059 List<JCCompilationUnit> parsedFiles = compiler.parseFiles(newSourceFiles); 1060 roots = prev.roots.appendList(parsedFiles); 1061 1062 // Check for errors after parsing 1063 if (unrecoverableError()) { 1064 compiler.initModules(List.nil()); 1065 return; 1066 } 1067 1068 roots = compiler.initModules(roots); 1069 1070 enterClassFiles(genClassFiles); 1071 List<ClassSymbol> newClasses = enterClassFiles(newClassFiles); 1072 for (Entry<ModuleSymbol, Map<String, JavaFileObject>> moduleAndClassFiles : newClassFiles.entrySet()) { 1073 genClassFiles.computeIfAbsent(moduleAndClassFiles.getKey(), m -> new LinkedHashMap<>()).putAll(moduleAndClassFiles.getValue()); 1074 } 1075 enterTrees(roots); 1076 1077 if (unrecoverableError()) 1078 return; 1079 1080 topLevelClasses = join( 1081 getTopLevelClasses(parsedFiles), 1082 getTopLevelClassesFromClasses(newClasses)); 1083 1084 packageInfoFiles = join( 1085 getPackageInfoFiles(parsedFiles), 1086 getPackageInfoFilesFromClasses(newClasses)); 1087 1088 moduleInfoFiles = List.nil(); //module-info cannot be generated 1089 1090 findAnnotationsPresent(); 1091 } 1092 1093 /** Create the next round to be used. */ 1094 Round next(Set<JavaFileObject> newSourceFiles, Map<ModuleSymbol, Map<String, JavaFileObject>> newClassFiles) { 1095 return new Round(this, newSourceFiles, newClassFiles); 1096 } 1097 1098 /** Prepare the compiler for the final compilation. */ 1099 void finalCompiler() { 1100 newRound(); 1101 } 1102 1103 /** Return the number of errors found so far in this round. 1104 * This may include uncoverable errors, such as parse errors, 1105 * and transient errors, such as missing symbols. */ 1106 int errorCount() { 1107 return compiler.errorCount(); 1108 } 1109 1110 /** Return the number of warnings found so far in this round. */ 1111 int warningCount() { 1112 return compiler.warningCount(); 1113 } 1114 1115 /** Return whether or not an unrecoverable error has occurred. */ 1116 boolean unrecoverableError() { 1117 if (messager.errorRaised()) 1118 return true; 1119 1120 for (JCDiagnostic d: deferredDiagnosticHandler.getDiagnostics()) { 1121 switch (d.getKind()) { 1122 case WARNING: 1123 if (werror) 1124 return true; 1125 break; 1126 1127 case ERROR: 1128 if (fatalErrors || !d.isFlagSet(RECOVERABLE)) 1129 return true; 1130 break; 1131 } 1132 } 1133 1134 return false; 1135 } 1136 1137 /** Find the set of annotations present in the set of top level 1138 * classes and package info files to be processed this round. */ 1139 void findAnnotationsPresent() { 1140 ComputeAnnotationSet annotationComputer = new ComputeAnnotationSet(elementUtils); 1141 // Use annotation processing to compute the set of annotations present 1142 annotationsPresent = new LinkedHashSet<>(); 1143 for (ClassSymbol classSym : topLevelClasses) 1144 annotationComputer.scan(classSym, annotationsPresent); 1145 for (PackageSymbol pkgSym : packageInfoFiles) 1146 annotationComputer.scan(pkgSym, annotationsPresent); 1147 for (ModuleSymbol mdlSym : moduleInfoFiles) 1148 annotationComputer.scan(mdlSym, annotationsPresent); 1149 } 1150 1151 /** Enter a set of generated class files. */ 1152 private List<ClassSymbol> enterClassFiles(Map<ModuleSymbol, Map<String, JavaFileObject>> modulesAndClassFiles) { 1153 List<ClassSymbol> list = List.nil(); 1154 1155 for (Entry<ModuleSymbol, Map<String, JavaFileObject>> moduleAndClassFiles : modulesAndClassFiles.entrySet()) { 1156 for (Map.Entry<String,JavaFileObject> entry : moduleAndClassFiles.getValue().entrySet()) { 1157 Name name = names.fromString(entry.getKey()); 1158 JavaFileObject file = entry.getValue(); 1159 if (file.getKind() != JavaFileObject.Kind.CLASS) 1160 throw new AssertionError(file); 1161 ClassSymbol cs; 1162 if (isPkgInfo(file, JavaFileObject.Kind.CLASS)) { 1163 Name packageName = Convert.packagePart(name); 1164 PackageSymbol p = symtab.enterPackage(moduleAndClassFiles.getKey(), packageName); 1165 if (p.package_info == null) 1166 p.package_info = symtab.enterClass(moduleAndClassFiles.getKey(), Convert.shortName(name), p); 1167 cs = p.package_info; 1168 cs.reset(); 1169 if (cs.classfile == null) 1170 cs.classfile = file; 1171 cs.completer = initialCompleter; 1172 } else { 1173 cs = symtab.enterClass(moduleAndClassFiles.getKey(), name); 1174 cs.reset(); 1175 cs.classfile = file; 1176 cs.completer = initialCompleter; 1177 cs.owner.members().enter(cs); //XXX - OverwriteBetweenCompilations; syms.getClass is not sufficient anymore 1178 } 1179 list = list.prepend(cs); 1180 } 1181 } 1182 return list.reverse(); 1183 } 1184 1185 /** Enter a set of syntax trees. */ 1186 private void enterTrees(List<JCCompilationUnit> roots) { 1187 compiler.enterTrees(roots); 1188 } 1189 1190 /** Run a processing round. */ 1191 void run(boolean lastRound, boolean errorStatus) { 1192 printRoundInfo(lastRound); 1193 1194 if (!taskListener.isEmpty()) 1195 taskListener.started(new TaskEvent(TaskEvent.Kind.ANNOTATION_PROCESSING_ROUND)); 1196 1197 try { 1198 if (lastRound) { 1199 filer.setLastRound(true); 1200 Set<Element> emptyRootElements = Collections.emptySet(); // immutable 1201 RoundEnvironment renv = new JavacRoundEnvironment(true, 1202 errorStatus, 1203 emptyRootElements, 1204 JavacProcessingEnvironment.this); 1205 discoveredProcs.iterator().runContributingProcs(renv); 1206 } else { 1207 discoverAndRunProcs(annotationsPresent, topLevelClasses, packageInfoFiles, moduleInfoFiles); 1208 } 1209 } catch (Throwable t) { 1210 // we're specifically expecting Abort here, but if any Throwable 1211 // comes by, we should flush all deferred diagnostics, rather than 1212 // drop them on the ground. 1213 deferredDiagnosticHandler.reportDeferredDiagnostics(); 1214 log.popDiagnosticHandler(deferredDiagnosticHandler); 1215 compiler.setDeferredDiagnosticHandler(null); 1216 throw t; 1217 } finally { 1218 if (!taskListener.isEmpty()) 1219 taskListener.finished(new TaskEvent(TaskEvent.Kind.ANNOTATION_PROCESSING_ROUND)); 1220 } 1221 } 1222 1223 void showDiagnostics(boolean showAll) { 1224 Set<JCDiagnostic.Kind> kinds = EnumSet.allOf(JCDiagnostic.Kind.class); 1225 if (!showAll) { 1226 // suppress errors, which are all presumed to be transient resolve errors 1227 kinds.remove(JCDiagnostic.Kind.ERROR); 1228 } 1229 deferredDiagnosticHandler.reportDeferredDiagnostics(kinds); 1230 log.popDiagnosticHandler(deferredDiagnosticHandler); 1231 compiler.setDeferredDiagnosticHandler(null); 1232 } 1233 1234 /** Print info about this round. */ 1235 private void printRoundInfo(boolean lastRound) { 1236 if (printRounds || verbose) { 1237 List<ClassSymbol> tlc = lastRound ? List.nil() : topLevelClasses; 1238 Set<TypeElement> ap = lastRound ? Collections.emptySet() : annotationsPresent; 1239 log.printLines("x.print.rounds", 1240 number, 1241 "{" + tlc.toString(", ") + "}", 1242 ap, 1243 lastRound); 1244 } 1245 } 1246 1247 /** Prepare for new round of annotation processing. Cleans trees, resets symbols, and 1248 * asks selected services to prepare to a new round of annotation processing. 1249 */ 1250 private void newRound() { 1251 //ensure treesToClean contains all trees, including implicitly parsed ones 1252 for (Env<AttrContext> env : enter.getEnvs()) { 1253 treesToClean.add(env.toplevel); 1254 } 1255 for (JCCompilationUnit node : treesToClean) { 1256 treeCleaner.scan(node); 1257 } 1258 chk.newRound(); 1259 enter.newRound(); 1260 filer.newRound(); 1261 messager.newRound(); 1262 compiler.newRound(); 1263 modules.newRound(); 1264 types.newRound(); 1265 annotate.newRound(); 1266 1267 boolean foundError = false; 1268 1269 for (ClassSymbol cs : symtab.getAllClasses()) { 1270 if (cs.kind == ERR) { 1271 foundError = true; 1272 break; 1273 } 1274 } 1275 1276 if (foundError) { 1277 for (ClassSymbol cs : symtab.getAllClasses()) { 1278 if (cs.classfile != null || cs.kind == ERR) { 1279 cs.reset(); 1280 cs.type = new ClassType(cs.type.getEnclosingType(), null, cs); 1281 if (cs.isCompleted()) { 1282 cs.completer = initialCompleter; 1283 } 1284 } 1285 } 1286 } 1287 } 1288 } 1289 1290 1291 // TODO: internal catch clauses?; catch and rethrow an annotation 1292 // processing error 1293 public boolean doProcessing(List<JCCompilationUnit> roots, 1294 List<ClassSymbol> classSymbols, 1295 Iterable<? extends PackageSymbol> pckSymbols, 1296 Log.DeferredDiagnosticHandler deferredDiagnosticHandler) { 1297 final Set<JCCompilationUnit> treesToClean = 1298 Collections.newSetFromMap(new IdentityHashMap<JCCompilationUnit, Boolean>()); 1299 1300 //fill already attributed implicit trees: 1301 for (Env<AttrContext> env : enter.getEnvs()) { 1302 treesToClean.add(env.toplevel); 1303 } 1304 1305 Set<PackageSymbol> specifiedPackages = new LinkedHashSet<>(); 1306 for (PackageSymbol psym : pckSymbols) 1307 specifiedPackages.add(psym); 1308 this.specifiedPackages = Collections.unmodifiableSet(specifiedPackages); 1309 1310 Round round = new Round(roots, classSymbols, treesToClean, deferredDiagnosticHandler); 1311 1312 boolean errorStatus; 1313 boolean moreToDo; 1314 do { 1315 // Run processors for round n 1316 round.run(false, false); 1317 1318 // Processors for round n have run to completion. 1319 // Check for errors and whether there is more work to do. 1320 errorStatus = round.unrecoverableError(); 1321 moreToDo = moreToDo(); 1322 1323 round.showDiagnostics(errorStatus || showResolveErrors); 1324 1325 // Set up next round. 1326 // Copy mutable collections returned from filer. 1327 round = round.next( 1328 new LinkedHashSet<>(filer.getGeneratedSourceFileObjects()), 1329 new LinkedHashMap<>(filer.getGeneratedClasses())); 1330 1331 // Check for errors during setup. 1332 if (round.unrecoverableError()) 1333 errorStatus = true; 1334 1335 } while (moreToDo && !errorStatus); 1336 1337 // run last round 1338 round.run(true, errorStatus); 1339 round.showDiagnostics(true); 1340 1341 filer.warnIfUnclosedFiles(); 1342 warnIfUnmatchedOptions(); 1343 1344 /* 1345 * If an annotation processor raises an error in a round, 1346 * that round runs to completion and one last round occurs. 1347 * The last round may also occur because no more source or 1348 * class files have been generated. Therefore, if an error 1349 * was raised on either of the last *two* rounds, the compile 1350 * should exit with a nonzero exit code. The current value of 1351 * errorStatus holds whether or not an error was raised on the 1352 * second to last round; errorRaised() gives the error status 1353 * of the last round. 1354 */ 1355 if (messager.errorRaised() 1356 || werror && round.warningCount() > 0 && round.errorCount() > 0) 1357 errorStatus = true; 1358 1359 Set<JavaFileObject> newSourceFiles = 1360 new LinkedHashSet<>(filer.getGeneratedSourceFileObjects()); 1361 roots = round.roots; 1362 1363 errorStatus = errorStatus || (compiler.errorCount() > 0); 1364 1365 round.finalCompiler(); 1366 1367 if (newSourceFiles.size() > 0) 1368 roots = roots.appendList(compiler.parseFiles(newSourceFiles)); 1369 1370 errorStatus = errorStatus || (compiler.errorCount() > 0); 1371 1372 // Free resources 1373 this.close(); 1374 1375 if (errorStatus && compiler.errorCount() == 0) { 1376 compiler.log.nerrors++; 1377 } 1378 1379 compiler.enterTreesIfNeeded(roots); 1380 1381 if (!taskListener.isEmpty()) 1382 taskListener.finished(new TaskEvent(TaskEvent.Kind.ANNOTATION_PROCESSING)); 1383 1384 return true; 1385 } 1386 1387 private void warnIfUnmatchedOptions() { 1388 if (!unmatchedProcessorOptions.isEmpty()) { 1389 log.warning(Warnings.ProcUnmatchedProcessorOptions(unmatchedProcessorOptions.toString())); 1390 } 1391 } 1392 1393 /** 1394 * Free resources related to annotation processing. 1395 */ 1396 public void close() { 1397 filer.close(); 1398 if (discoveredProcs != null) // Make calling close idempotent 1399 discoveredProcs.close(); 1400 discoveredProcs = null; 1401 } 1402 1403 private List<ClassSymbol> getTopLevelClasses(List<? extends JCCompilationUnit> units) { 1404 List<ClassSymbol> classes = List.nil(); 1405 for (JCCompilationUnit unit : units) { 1406 for (JCTree node : unit.defs) { 1407 if (node.hasTag(JCTree.Tag.CLASSDEF)) { 1408 ClassSymbol sym = ((JCClassDecl) node).sym; 1409 Assert.checkNonNull(sym); 1410 classes = classes.prepend(sym); 1411 } 1412 } 1413 } 1414 return classes.reverse(); 1415 } 1416 1417 private List<ClassSymbol> getTopLevelClassesFromClasses(List<? extends ClassSymbol> syms) { 1418 List<ClassSymbol> classes = List.nil(); 1419 for (ClassSymbol sym : syms) { 1420 if (!isPkgInfo(sym)) { 1421 classes = classes.prepend(sym); 1422 } 1423 } 1424 return classes.reverse(); 1425 } 1426 1427 private List<PackageSymbol> getPackageInfoFiles(List<? extends JCCompilationUnit> units) { 1428 List<PackageSymbol> packages = List.nil(); 1429 for (JCCompilationUnit unit : units) { 1430 if (isPkgInfo(unit.sourcefile, JavaFileObject.Kind.SOURCE)) { 1431 packages = packages.prepend(unit.packge); 1432 } 1433 } 1434 return packages.reverse(); 1435 } 1436 1437 private List<PackageSymbol> getPackageInfoFilesFromClasses(List<? extends ClassSymbol> syms) { 1438 List<PackageSymbol> packages = List.nil(); 1439 for (ClassSymbol sym : syms) { 1440 if (isPkgInfo(sym)) { 1441 packages = packages.prepend((PackageSymbol) sym.owner); 1442 } 1443 } 1444 return packages.reverse(); 1445 } 1446 1447 private List<ModuleSymbol> getModuleInfoFiles(List<? extends JCCompilationUnit> units) { 1448 List<ModuleSymbol> modules = List.nil(); 1449 for (JCCompilationUnit unit : units) { 1450 if (isModuleInfo(unit.sourcefile, JavaFileObject.Kind.SOURCE) && 1451 unit.defs.nonEmpty() && 1452 unit.defs.head.hasTag(Tag.MODULEDEF)) { 1453 modules = modules.prepend(unit.modle); 1454 } 1455 } 1456 return modules.reverse(); 1457 } 1458 1459 // avoid unchecked warning from use of varargs 1460 private static <T> List<T> join(List<T> list1, List<T> list2) { 1461 return list1.appendList(list2); 1462 } 1463 1464 private boolean isPkgInfo(JavaFileObject fo, JavaFileObject.Kind kind) { 1465 return fo.isNameCompatible("package-info", kind); 1466 } 1467 1468 private boolean isPkgInfo(ClassSymbol sym) { 1469 return isPkgInfo(sym.classfile, JavaFileObject.Kind.CLASS) && (sym.packge().package_info == sym); 1470 } 1471 1472 private boolean isModuleInfo(JavaFileObject fo, JavaFileObject.Kind kind) { 1473 return fo.isNameCompatible("module-info", kind); 1474 } 1475 1476 /* 1477 * Called retroactively to determine if a class loader was required, 1478 * after we have failed to create one. 1479 */ 1480 private boolean needClassLoader(String procNames, Iterable<? extends Path> workingpath) { 1481 if (procNames != null) 1482 return true; 1483 1484 URL[] urls = new URL[1]; 1485 for(Path pathElement : workingpath) { 1486 try { 1487 urls[0] = pathElement.toUri().toURL(); 1488 if (ServiceProxy.hasService(Processor.class, urls)) 1489 return true; 1490 } catch (MalformedURLException ex) { 1491 throw new AssertionError(ex); 1492 } 1493 catch (ServiceProxy.ServiceConfigurationError e) { 1494 log.error(Errors.ProcBadConfigFile(e.getLocalizedMessage())); 1495 return true; 1496 } 1497 } 1498 1499 return false; 1500 } 1501 1502 class ImplicitCompleter implements Completer { 1503 1504 private final JCCompilationUnit topLevel; 1505 1506 public ImplicitCompleter(JCCompilationUnit topLevel) { 1507 this.topLevel = topLevel; 1508 } 1509 1510 @Override public void complete(Symbol sym) throws CompletionFailure { 1511 compiler.readSourceFile(topLevel, (ClassSymbol) sym); 1512 } 1513 } 1514 1515 private final TreeScanner treeCleaner = new TreeScanner() { 1516 public void scan(JCTree node) { 1517 super.scan(node); 1518 if (node != null) 1519 node.type = null; 1520 } 1521 JCCompilationUnit topLevel; 1522 public void visitTopLevel(JCCompilationUnit node) { 1523 if (node.packge != null) { 1524 if (node.packge.package_info != null) { 1525 node.packge.package_info.reset(); 1526 } 1527 node.packge.reset(); 1528 } 1529 boolean isModuleInfo = node.sourcefile.isNameCompatible("module-info", Kind.SOURCE); 1530 if (isModuleInfo) { 1531 node.modle.reset(); 1532 node.modle.completer = sym -> modules.enter(List.of(node), node.modle.module_info); 1533 node.modle.module_info.reset(); 1534 node.modle.module_info.members_field = WriteableScope.create(node.modle.module_info); 1535 } 1536 node.packge = null; 1537 topLevel = node; 1538 try { 1539 super.visitTopLevel(node); 1540 } finally { 1541 topLevel = null; 1542 } 1543 } 1544 public void visitClassDef(JCClassDecl node) { 1545 super.visitClassDef(node); 1546 // remove generated constructor that may have been added during attribution: 1547 List<JCTree> beforeConstructor = List.nil(); 1548 List<JCTree> defs = node.defs; 1549 while (defs.nonEmpty() && !defs.head.hasTag(Tag.METHODDEF)) { 1550 beforeConstructor = beforeConstructor.prepend(defs.head); 1551 defs = defs.tail; 1552 } 1553 if (defs.nonEmpty() && 1554 (((JCMethodDecl) defs.head).mods.flags & Flags.GENERATEDCONSTR) != 0) { 1555 defs = defs.tail; 1556 while (beforeConstructor.nonEmpty()) { 1557 defs = defs.prepend(beforeConstructor.head); 1558 beforeConstructor = beforeConstructor.tail; 1559 } 1560 node.defs = defs; 1561 } 1562 if (node.sym != null) { 1563 node.sym.completer = new ImplicitCompleter(topLevel); 1564 } 1565 node.sym = null; 1566 } 1567 public void visitMethodDef(JCMethodDecl node) { 1568 // remove super constructor call that may have been added during attribution: 1569 if (TreeInfo.isConstructor(node) && node.sym != null && node.sym.owner.isEnum() && 1570 node.body.stats.nonEmpty() && TreeInfo.isSuperCall(node.body.stats.head) && 1571 node.body.stats.head.pos == node.body.pos) { 1572 node.body.stats = node.body.stats.tail; 1573 } 1574 node.sym = null; 1575 super.visitMethodDef(node); 1576 } 1577 public void visitVarDef(JCVariableDecl node) { 1578 node.sym = null; 1579 super.visitVarDef(node); 1580 } 1581 public void visitNewClass(JCNewClass node) { 1582 node.constructor = null; 1583 super.visitNewClass(node); 1584 } 1585 public void visitAssignop(JCAssignOp node) { 1586 node.operator = null; 1587 super.visitAssignop(node); 1588 } 1589 public void visitUnary(JCUnary node) { 1590 node.operator = null; 1591 super.visitUnary(node); 1592 } 1593 public void visitBinary(JCBinary node) { 1594 node.operator = null; 1595 super.visitBinary(node); 1596 } 1597 public void visitSelect(JCFieldAccess node) { 1598 node.sym = null; 1599 super.visitSelect(node); 1600 } 1601 public void visitIdent(JCIdent node) { 1602 node.sym = null; 1603 super.visitIdent(node); 1604 } 1605 public void visitAnnotation(JCAnnotation node) { 1606 node.attribute = null; 1607 super.visitAnnotation(node); 1608 } 1609 }; 1610 1611 1612 private boolean moreToDo() { 1613 return filer.newFiles(); 1614 } 1615 1616 /** 1617 * {@inheritDoc} 1618 * 1619 * Command line options suitable for presenting to annotation 1620 * processors. 1621 * {@literal "-Afoo=bar"} should be {@literal "-Afoo" => "bar"}. 1622 */ 1623 @DefinedBy(Api.ANNOTATION_PROCESSING) 1624 public Map<String,String> getOptions() { 1625 return processorOptions; 1626 } 1627 1628 @DefinedBy(Api.ANNOTATION_PROCESSING) 1629 public Messager getMessager() { 1630 return messager; 1631 } 1632 1633 @DefinedBy(Api.ANNOTATION_PROCESSING) 1634 public JavacFiler getFiler() { 1635 return filer; 1636 } 1637 1638 @DefinedBy(Api.ANNOTATION_PROCESSING) 1639 public JavacElements getElementUtils() { 1640 return elementUtils; 1641 } 1642 1643 @DefinedBy(Api.ANNOTATION_PROCESSING) 1644 public JavacTypes getTypeUtils() { 1645 return typeUtils; 1646 } 1647 1648 @DefinedBy(Api.ANNOTATION_PROCESSING) 1649 public SourceVersion getSourceVersion() { 1650 return Source.toSourceVersion(source); 1651 } 1652 1653 @DefinedBy(Api.ANNOTATION_PROCESSING) 1654 public Locale getLocale() { 1655 return messages.getCurrentLocale(); 1656 } 1657 1658 public Set<Symbol.PackageSymbol> getSpecifiedPackages() { 1659 return specifiedPackages; 1660 } 1661 1662 public static final Pattern noMatches = Pattern.compile("(\\P{all})+"); 1663 1664 /** 1665 * Convert import-style string for supported annotations into a 1666 * regex matching that string. If the string is not a valid 1667 * import-style string, return a regex that won't match anything. 1668 */ 1669 private static Pattern importStringToPattern(boolean allowModules, String s, Processor p, Log log) { 1670 String module; 1671 String pkg; 1672 int slash = s.indexOf('/'); 1673 if (slash == (-1)) { 1674 if (s.equals("*")) { 1675 return MatchingUtils.validImportStringToPattern(s); 1676 } 1677 module = allowModules ? ".*/" : ""; 1678 pkg = s; 1679 } else { 1680 module = Pattern.quote(s.substring(0, slash + 1)); 1681 pkg = s.substring(slash + 1); 1682 } 1683 if (MatchingUtils.isValidImportString(pkg)) { 1684 return Pattern.compile(module + MatchingUtils.validImportStringToPatternString(pkg)); 1685 } else { 1686 log.warning(Warnings.ProcMalformedSupportedString(s, p.getClass().getName())); 1687 return noMatches; // won't match any valid identifier 1688 } 1689 } 1690 1691 /** 1692 * For internal use only. This method may be removed without warning. 1693 */ 1694 public Context getContext() { 1695 return context; 1696 } 1697 1698 /** 1699 * For internal use only. This method may be removed without warning. 1700 */ 1701 public ClassLoader getProcessorClassLoader() { 1702 return processorClassLoader; 1703 } 1704 1705 public String toString() { 1706 return "javac ProcessingEnvironment"; 1707 } 1708 1709 public static boolean isValidOptionName(String optionName) { 1710 for(String s : optionName.split("\\.", -1)) { 1711 if (!SourceVersion.isIdentifier(s)) 1712 return false; 1713 } 1714 return true; 1715 } 1716 }