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