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