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