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