langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/processing/JavacProcessingEnvironment.java
author alundblad
Tue, 28 Apr 2015 22:25:36 +0200
changeset 30066 d74c06a92bd8
parent 29427 44f4e6905b67
child 31506 4e07f827a794
permissions -rw-r--r--
8078600: Infinite loop when compiling annotations with -XDcompletionDeps Summary: Added Completer::isTerminal and added NULL_COMPLETER. Reviewed-by: jlahoda, mcimadamore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
30066
d74c06a92bd8 8078600: Infinite loop when compiling annotations with -XDcompletionDeps
alundblad
parents: 29427
diff changeset
     2
 * Copyright (c) 2005, 2015, 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;
1dd22bdb9ca5 6714364: refactor javac File handling code into new javac.file package
jjg
parents: 169
diff changeset
    29
import java.io.File;
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;
731
1dd22bdb9ca5 6714364: refactor javac File handling code into new javac.file package
jjg
parents: 169
diff changeset
    32
import java.net.MalformedURLException;
12016
1990493b64db 7093891: support multiple task listeners
jjg
parents: 11316
diff changeset
    33
import java.net.URL;
1990493b64db 7093891: support multiple task listeners
jjg
parents: 11316
diff changeset
    34
import java.util.*;
1990493b64db 7093891: support multiple task listeners
jjg
parents: 11316
diff changeset
    35
import java.util.regex.*;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
import javax.annotation.processing.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
import javax.lang.model.SourceVersion;
18669
99572d59c916 7162089: Add support for repeating annotations to javax.annotation.processing
darcy
parents: 17551
diff changeset
    39
import javax.lang.model.element.*;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
import javax.lang.model.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
import javax.tools.JavaFileManager;
12016
1990493b64db 7093891: support multiple task listeners
jjg
parents: 11316
diff changeset
    42
import javax.tools.JavaFileObject;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
import javax.tools.StandardJavaFileManager;
12016
1990493b64db 7093891: support multiple task listeners
jjg
parents: 11316
diff changeset
    44
import static javax.tools.StandardLocation.*;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
731
1dd22bdb9ca5 6714364: refactor javac File handling code into new javac.file package
jjg
parents: 169
diff changeset
    46
import com.sun.source.util.TaskEvent;
12016
1990493b64db 7093891: support multiple task listeners
jjg
parents: 11316
diff changeset
    47
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
    48
import com.sun.tools.javac.code.*;
1dd22bdb9ca5 6714364: refactor javac File handling code into new javac.file package
jjg
parents: 169
diff changeset
    49
import com.sun.tools.javac.code.Symbol.*;
23810
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
    50
import com.sun.tools.javac.code.Type.ClassType;
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
    51
import com.sun.tools.javac.code.Types;
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
    52
import com.sun.tools.javac.comp.AttrContext;
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
    53
import com.sun.tools.javac.comp.Check;
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
    54
import com.sun.tools.javac.comp.Enter;
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
    55
import com.sun.tools.javac.comp.Env;
731
1dd22bdb9ca5 6714364: refactor javac File handling code into new javac.file package
jjg
parents: 169
diff changeset
    56
import com.sun.tools.javac.file.JavacFileManager;
1dd22bdb9ca5 6714364: refactor javac File handling code into new javac.file package
jjg
parents: 169
diff changeset
    57
import com.sun.tools.javac.main.JavaCompiler;
1dd22bdb9ca5 6714364: refactor javac File handling code into new javac.file package
jjg
parents: 169
diff changeset
    58
import com.sun.tools.javac.model.JavacElements;
1dd22bdb9ca5 6714364: refactor javac File handling code into new javac.file package
jjg
parents: 169
diff changeset
    59
import com.sun.tools.javac.model.JavacTypes;
1dd22bdb9ca5 6714364: refactor javac File handling code into new javac.file package
jjg
parents: 169
diff changeset
    60
import com.sun.tools.javac.tree.*;
1dd22bdb9ca5 6714364: refactor javac File handling code into new javac.file package
jjg
parents: 169
diff changeset
    61
import com.sun.tools.javac.tree.JCTree.*;
1dd22bdb9ca5 6714364: refactor javac File handling code into new javac.file package
jjg
parents: 169
diff changeset
    62
import com.sun.tools.javac.util.Abort;
8032
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents: 8031
diff changeset
    63
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
    64
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
    65
import com.sun.tools.javac.util.Context;
4871
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents: 3764
diff changeset
    66
import com.sun.tools.javac.util.Convert;
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
    67
import com.sun.tools.javac.util.DefinedBy;
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
    68
import com.sun.tools.javac.util.DefinedBy.Api;
6581
f58f0ce45802 6980707: Reduce use of IOException in JavaCompiler
jjg
parents: 6355
diff changeset
    69
import com.sun.tools.javac.util.JCDiagnostic;
12016
1990493b64db 7093891: support multiple task listeners
jjg
parents: 11316
diff changeset
    70
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
    71
import com.sun.tools.javac.util.List;
1dd22bdb9ca5 6714364: refactor javac File handling code into new javac.file package
jjg
parents: 169
diff changeset
    72
import com.sun.tools.javac.util.Log;
29427
44f4e6905b67 8071851: Provide filtering of doclint checking based on packages
jlahoda
parents: 27224
diff changeset
    73
import com.sun.tools.javac.util.MatchingUtils;
731
1dd22bdb9ca5 6714364: refactor javac File handling code into new javac.file package
jjg
parents: 169
diff changeset
    74
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
    75
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
    76
import com.sun.tools.javac.util.Options;
17551
03f330c02d97 8004082: test/tools/javac/plugin/showtype/Test.java fails on windows: jtreg can't delete plugin.jar
jjg
parents: 16968
diff changeset
    77
import com.sun.tools.javac.util.ServiceLoader;
12016
1990493b64db 7093891: support multiple task listeners
jjg
parents: 11316
diff changeset
    78
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
    79
import static com.sun.tools.javac.code.Kinds.Kind.*;
12016
1990493b64db 7093891: support multiple task listeners
jjg
parents: 11316
diff changeset
    80
import static com.sun.tools.javac.main.Option.*;
16968
19f0da2d3143 8010659: Javac Crashes while building OpenJFX
vromero
parents: 15385
diff changeset
    81
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
    82
import static com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag.*;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
 * Objects of this class hold and manage the state needed to support
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
 * annotation processing.
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5841
diff changeset
    88
 * <p><b>This is NOT part of any supported API.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
 * If you write code that depends on this, you do so at your own risk.
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
 * This code and its internal interfaces are subject to change or
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
 * deletion without notice.</b>
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
public class JavacProcessingEnvironment implements ProcessingEnvironment, Closeable {
19651
b1aa46cc2198 8023515: import type-annotations updates
jjg
parents: 18669
diff changeset
    94
    private final Options options;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
    private final boolean printProcessorInfo;
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
    private final boolean printRounds;
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
    private final boolean verbose;
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
    private final boolean lint;
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
    private final boolean fatalErrors;
6153
277c719ff46e 6403456: -Werror should work with annotation processing
jjg
parents: 6143
diff changeset
   101
    private final boolean werror;
6582
c7a4fb5a2f86 6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents: 6581
diff changeset
   102
    private final boolean showResolveErrors;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
    private final JavacFiler filer;
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
    private final JavacMessager messager;
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
    private final JavacElements elementUtils;
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
    private final JavacTypes typeUtils;
23810
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
   108
    private final Types types;
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
   109
    private final JavaCompiler compiler;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
     * Holds relevant state history of which processors have been
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
     * used.
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
    private DiscoveredProcessors discoveredProcs;
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
     * Map of processor-specific options.
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
    private final Map<String, String> processorOptions;
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
    private final Set<String> unmatchedProcessorOptions;
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
     * Annotations implicitly processed and claimed by javac.
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
    private final Set<String> platformAnnotations;
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
     * Set of packages given on command line.
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
    private Set<PackageSymbol> specifiedPackages = Collections.emptySet();
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
    /** The log to be used for error reporting.
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
     */
23810
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
   138
    final Log log;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
6581
f58f0ce45802 6980707: Reduce use of IOException in JavaCompiler
jjg
parents: 6355
diff changeset
   140
    /** Diagnostic factory.
f58f0ce45802 6980707: Reduce use of IOException in JavaCompiler
jjg
parents: 6355
diff changeset
   141
     */
f58f0ce45802 6980707: Reduce use of IOException in JavaCompiler
jjg
parents: 6355
diff changeset
   142
    JCDiagnostic.Factory diags;
f58f0ce45802 6980707: Reduce use of IOException in JavaCompiler
jjg
parents: 6355
diff changeset
   143
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
     * Source level of the compile.
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
    Source source;
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
3656
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents: 3300
diff changeset
   149
    private ClassLoader processorClassLoader;
14548
aa687b312c97 8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents: 14538
diff changeset
   150
    private SecurityException processorClassLoaderException;
3656
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents: 3300
diff changeset
   151
1471
57506cdfb7b4 6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents: 1260
diff changeset
   152
    /**
57506cdfb7b4 6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents: 1260
diff changeset
   153
     * JavacMessages object used for localization
57506cdfb7b4 6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents: 1260
diff changeset
   154
     */
57506cdfb7b4 6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents: 1260
diff changeset
   155
    private JavacMessages messages;
57506cdfb7b4 6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents: 1260
diff changeset
   156
12016
1990493b64db 7093891: support multiple task listeners
jjg
parents: 11316
diff changeset
   157
    private MultiTaskListener taskListener;
23810
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
   158
    private final Symtab symtab;
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
   159
    private final Names names;
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
   160
    private final Enter enter;
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
   161
    private final Completer initialCompleter;
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
   162
    private final Check chk;
12016
1990493b64db 7093891: support multiple task listeners
jjg
parents: 11316
diff changeset
   163
23810
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
   164
    private final Context context;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
14548
aa687b312c97 8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents: 14538
diff changeset
   166
    /** Get the JavacProcessingEnvironment instance for this context. */
aa687b312c97 8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents: 14538
diff changeset
   167
    public static JavacProcessingEnvironment instance(Context context) {
aa687b312c97 8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents: 14538
diff changeset
   168
        JavacProcessingEnvironment instance = context.get(JavacProcessingEnvironment.class);
aa687b312c97 8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents: 14538
diff changeset
   169
        if (instance == null)
aa687b312c97 8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents: 14538
diff changeset
   170
            instance = new JavacProcessingEnvironment(context);
aa687b312c97 8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents: 14538
diff changeset
   171
        return instance;
aa687b312c97 8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents: 14538
diff changeset
   172
    }
aa687b312c97 8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents: 14538
diff changeset
   173
aa687b312c97 8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents: 14538
diff changeset
   174
    protected JavacProcessingEnvironment(Context context) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
        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
   176
        context.put(JavacProcessingEnvironment.class, this);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
        log = Log.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
        source = Source.instance(context);
6581
f58f0ce45802 6980707: Reduce use of IOException in JavaCompiler
jjg
parents: 6355
diff changeset
   179
        diags = JCDiagnostic.Factory.instance(context);
f58f0ce45802 6980707: Reduce use of IOException in JavaCompiler
jjg
parents: 6355
diff changeset
   180
        options = Options.instance(context);
6721
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 6717
diff changeset
   181
        printProcessorInfo = options.isSet(XPRINTPROCESSORINFO);
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 6717
diff changeset
   182
        printRounds = options.isSet(XPRINTROUNDS);
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 6717
diff changeset
   183
        verbose = options.isSet(VERBOSE);
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 6717
diff changeset
   184
        lint = Lint.instance(context).isEnabled(PROCESSING);
23810
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
   185
        compiler = JavaCompiler.instance(context);
14050
9bfad4b4b6a2 7196464: upgrade JavaCompiler.shouldStopPolicy to accomodate policies in face of error and no error
jjg
parents: 14044
diff changeset
   186
        if (options.isSet(PROC, "only") || options.isSet(XPRINT)) {
9bfad4b4b6a2 7196464: upgrade JavaCompiler.shouldStopPolicy to accomodate policies in face of error and no error
jjg
parents: 14044
diff changeset
   187
            compiler.shouldStopPolicyIfNoError = CompileState.PROCESS;
9bfad4b4b6a2 7196464: upgrade JavaCompiler.shouldStopPolicy to accomodate policies in face of error and no error
jjg
parents: 14044
diff changeset
   188
        }
6721
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 6717
diff changeset
   189
        fatalErrors = options.isSet("fatalEnterError");
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 6717
diff changeset
   190
        showResolveErrors = options.isSet("showResolveErrors");
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 6717
diff changeset
   191
        werror = options.isSet(WERROR);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
        platformAnnotations = initPlatformAnnotations();
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
6924
ef7a9281ad2f 6988836: A new JavacElements is created for each round of annotation processing
jjg
parents: 6721
diff changeset
   194
        // Initialize services before any processors are initialized
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
        // in case processors use them.
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
        filer = new JavacFiler(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
        messager = new JavacMessager(context, this);
6924
ef7a9281ad2f 6988836: A new JavacElements is created for each round of annotation processing
jjg
parents: 6721
diff changeset
   198
        elementUtils = JavacElements.instance(context);
ef7a9281ad2f 6988836: A new JavacElements is created for each round of annotation processing
jjg
parents: 6721
diff changeset
   199
        typeUtils = JavacTypes.instance(context);
23810
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
   200
        types = Types.instance(context);
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
   201
        processorOptions = initProcessorOptions();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
        unmatchedProcessorOptions = initUnmatchedProcessorOptions();
1471
57506cdfb7b4 6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents: 1260
diff changeset
   203
        messages = JavacMessages.instance(context);
12016
1990493b64db 7093891: support multiple task listeners
jjg
parents: 11316
diff changeset
   204
        taskListener = MultiTaskListener.instance(context);
23810
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
   205
        symtab = Symtab.instance(context);
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
   206
        names = Names.instance(context);
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
   207
        enter = Enter.instance(context);
24604
7f68545b5128 8041422: Split javac ClassReader into ClassReader+ClassFinder
jjg
parents: 24396
diff changeset
   208
        initialCompleter = ClassFinder.instance(context).getCompleter();
23810
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
   209
        chk = Check.instance(context);
14548
aa687b312c97 8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents: 14538
diff changeset
   210
        initProcessorClassLoader();
aa687b312c97 8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents: 14538
diff changeset
   211
    }
aa687b312c97 8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents: 14538
diff changeset
   212
aa687b312c97 8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents: 14538
diff changeset
   213
    public void setProcessors(Iterable<? extends Processor> processors) {
aa687b312c97 8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents: 14538
diff changeset
   214
        Assert.checkNull(discoveredProcs);
23810
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
   215
        initProcessorIterator(processors);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   217
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
    private Set<String> initPlatformAnnotations() {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 20615
diff changeset
   219
        Set<String> platformAnnotations = new HashSet<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   220
        platformAnnotations.add("java.lang.Deprecated");
06bc494ca11e Initial load
duke
parents:
diff changeset
   221
        platformAnnotations.add("java.lang.Override");
06bc494ca11e Initial load
duke
parents:
diff changeset
   222
        platformAnnotations.add("java.lang.SuppressWarnings");
06bc494ca11e Initial load
duke
parents:
diff changeset
   223
        platformAnnotations.add("java.lang.annotation.Documented");
06bc494ca11e Initial load
duke
parents:
diff changeset
   224
        platformAnnotations.add("java.lang.annotation.Inherited");
06bc494ca11e Initial load
duke
parents:
diff changeset
   225
        platformAnnotations.add("java.lang.annotation.Retention");
06bc494ca11e Initial load
duke
parents:
diff changeset
   226
        platformAnnotations.add("java.lang.annotation.Target");
06bc494ca11e Initial load
duke
parents:
diff changeset
   227
        return Collections.unmodifiableSet(platformAnnotations);
06bc494ca11e Initial load
duke
parents:
diff changeset
   228
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   229
14548
aa687b312c97 8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents: 14538
diff changeset
   230
    private void initProcessorClassLoader() {
aa687b312c97 8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents: 14538
diff changeset
   231
        JavaFileManager fileManager = context.get(JavaFileManager.class);
aa687b312c97 8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents: 14538
diff changeset
   232
        try {
aa687b312c97 8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents: 14538
diff changeset
   233
            // If processorpath is not explicitly set, use the classpath.
aa687b312c97 8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents: 14538
diff changeset
   234
            processorClassLoader = fileManager.hasLocation(ANNOTATION_PROCESSOR_PATH)
aa687b312c97 8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents: 14538
diff changeset
   235
                ? fileManager.getClassLoader(ANNOTATION_PROCESSOR_PATH)
aa687b312c97 8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents: 14538
diff changeset
   236
                : fileManager.getClassLoader(CLASS_PATH);
aa687b312c97 8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents: 14538
diff changeset
   237
aa687b312c97 8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents: 14538
diff changeset
   238
            if (processorClassLoader != null && processorClassLoader instanceof Closeable) {
aa687b312c97 8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents: 14538
diff changeset
   239
                compiler.closeables = compiler.closeables.prepend((Closeable) processorClassLoader);
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
        } catch (SecurityException e) {
aa687b312c97 8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents: 14538
diff changeset
   242
            processorClassLoaderException = e;
aa687b312c97 8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents: 14538
diff changeset
   243
        }
aa687b312c97 8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents: 14538
diff changeset
   244
    }
aa687b312c97 8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents: 14538
diff changeset
   245
23810
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
   246
    private void initProcessorIterator(Iterable<? extends Processor> processors) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
        Iterator<? extends Processor> processorIterator;
06bc494ca11e Initial load
duke
parents:
diff changeset
   248
6721
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 6717
diff changeset
   249
        if (options.isSet(XPRINT)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   250
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   251
                Processor processor = PrintingProcessor.class.newInstance();
06bc494ca11e Initial load
duke
parents:
diff changeset
   252
                processorIterator = List.of(processor).iterator();
06bc494ca11e Initial load
duke
parents:
diff changeset
   253
            } catch (Throwable t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   254
                AssertionError assertError =
06bc494ca11e Initial load
duke
parents:
diff changeset
   255
                    new AssertionError("Problem instantiating PrintingProcessor.");
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
                assertError.initCause(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
                throw assertError;
06bc494ca11e Initial load
duke
parents:
diff changeset
   258
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   259
        } else if (processors != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
            processorIterator = processors.iterator();
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
        } else {
6721
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 6717
diff changeset
   262
            String processorNames = options.get(PROCESSOR);
14548
aa687b312c97 8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents: 14538
diff changeset
   263
            if (processorClassLoaderException == null) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   264
                /*
06bc494ca11e Initial load
duke
parents:
diff changeset
   265
                 * If the "-processor" option is used, search the appropriate
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
                 * path for the named class.  Otherwise, use a service
06bc494ca11e Initial load
duke
parents:
diff changeset
   267
                 * provider mechanism to create the processor iterator.
06bc494ca11e Initial load
duke
parents:
diff changeset
   268
                 */
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
                if (processorNames != null) {
3656
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents: 3300
diff changeset
   270
                    processorIterator = new NameProcessIterator(processorNames, processorClassLoader, log);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
                } else {
3656
d4e34b76b0c3 6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents: 3300
diff changeset
   272
                    processorIterator = new ServiceIterator(processorClassLoader, log);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
                }
14548
aa687b312c97 8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents: 14538
diff changeset
   274
            } else {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
                /*
06bc494ca11e Initial load
duke
parents:
diff changeset
   276
                 * A security exception will occur if we can't create a classloader.
06bc494ca11e Initial load
duke
parents:
diff changeset
   277
                 * Ignore the exception if, with hindsight, we didn't need it anyway
06bc494ca11e Initial load
duke
parents:
diff changeset
   278
                 * (i.e. no processor was specified either explicitly, or implicitly,
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
                 * in service configuration file.) Otherwise, we cannot continue.
06bc494ca11e Initial load
duke
parents:
diff changeset
   280
                 */
14548
aa687b312c97 8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents: 14538
diff changeset
   281
                processorIterator = handleServiceLoaderUnavailability("proc.cant.create.loader",
aa687b312c97 8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents: 14538
diff changeset
   282
                        processorClassLoaderException);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   283
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   284
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
        discoveredProcs = new DiscoveredProcessors(processorIterator);
06bc494ca11e Initial load
duke
parents:
diff changeset
   286
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   287
06bc494ca11e Initial load
duke
parents:
diff changeset
   288
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   289
     * Returns an empty processor iterator if no processors are on the
06bc494ca11e Initial load
duke
parents:
diff changeset
   290
     * relevant path, otherwise if processors are present, logs an
06bc494ca11e Initial load
duke
parents:
diff changeset
   291
     * error.  Called when a service loader is unavailable for some
06bc494ca11e Initial load
duke
parents:
diff changeset
   292
     * reason, either because a service loader class cannot be found
06bc494ca11e Initial load
duke
parents:
diff changeset
   293
     * or because a security policy prevents class loaders from being
06bc494ca11e Initial load
duke
parents:
diff changeset
   294
     * created.
06bc494ca11e Initial load
duke
parents:
diff changeset
   295
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   296
     * @param key The resource key to use to log an error message
06bc494ca11e Initial load
duke
parents:
diff changeset
   297
     * @param e   If non-null, pass this exception to Abort
06bc494ca11e Initial load
duke
parents:
diff changeset
   298
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   299
    private Iterator<Processor> handleServiceLoaderUnavailability(String key, Exception e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   300
        JavaFileManager fileManager = context.get(JavaFileManager.class);
06bc494ca11e Initial load
duke
parents:
diff changeset
   301
06bc494ca11e Initial load
duke
parents:
diff changeset
   302
        if (fileManager instanceof JavacFileManager) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   303
            StandardJavaFileManager standardFileManager = (JavacFileManager) fileManager;
06bc494ca11e Initial load
duke
parents:
diff changeset
   304
            Iterable<? extends File> workingPath = fileManager.hasLocation(ANNOTATION_PROCESSOR_PATH)
06bc494ca11e Initial load
duke
parents:
diff changeset
   305
                ? standardFileManager.getLocation(ANNOTATION_PROCESSOR_PATH)
06bc494ca11e Initial load
duke
parents:
diff changeset
   306
                : standardFileManager.getLocation(CLASS_PATH);
06bc494ca11e Initial load
duke
parents:
diff changeset
   307
6721
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 6717
diff changeset
   308
            if (needClassLoader(options.get(PROCESSOR), workingPath) )
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   309
                handleException(key, e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   310
06bc494ca11e Initial load
duke
parents:
diff changeset
   311
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   312
            handleException(key, e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   313
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   314
06bc494ca11e Initial load
duke
parents:
diff changeset
   315
        java.util.List<Processor> pl = Collections.emptyList();
06bc494ca11e Initial load
duke
parents:
diff changeset
   316
        return pl.iterator();
06bc494ca11e Initial load
duke
parents:
diff changeset
   317
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   318
06bc494ca11e Initial load
duke
parents:
diff changeset
   319
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   320
     * Handle a security exception thrown during initializing the
06bc494ca11e Initial load
duke
parents:
diff changeset
   321
     * Processor iterator.
06bc494ca11e Initial load
duke
parents:
diff changeset
   322
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   323
    private void handleException(String key, Exception e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   324
        if (e != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   325
            log.error(key, e.getLocalizedMessage());
06bc494ca11e Initial load
duke
parents:
diff changeset
   326
            throw new Abort(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   327
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   328
            log.error(key);
06bc494ca11e Initial load
duke
parents:
diff changeset
   329
            throw new Abort();
06bc494ca11e Initial load
duke
parents:
diff changeset
   330
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   331
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   332
06bc494ca11e Initial load
duke
parents:
diff changeset
   333
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   334
     * 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
   335
     * 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
   336
     * needed but unavailable.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   337
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   338
    private class ServiceIterator implements Iterator<Processor> {
10186
31a86b7e18ca 6575445: Update annotation processor to only use java.util.ServiceLoader
darcy
parents: 10182
diff changeset
   339
        private Iterator<Processor> iterator;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   340
        private Log log;
10186
31a86b7e18ca 6575445: Update annotation processor to only use java.util.ServiceLoader
darcy
parents: 10182
diff changeset
   341
        private ServiceLoader<Processor> loader;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   342
06bc494ca11e Initial load
duke
parents:
diff changeset
   343
        ServiceIterator(ClassLoader classLoader, Log log) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   344
            this.log = log;
06bc494ca11e Initial load
duke
parents:
diff changeset
   345
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   346
                try {
10186
31a86b7e18ca 6575445: Update annotation processor to only use java.util.ServiceLoader
darcy
parents: 10182
diff changeset
   347
                    loader = ServiceLoader.load(Processor.class, classLoader);
31a86b7e18ca 6575445: Update annotation processor to only use java.util.ServiceLoader
darcy
parents: 10182
diff changeset
   348
                    this.iterator = loader.iterator();
31a86b7e18ca 6575445: Update annotation processor to only use java.util.ServiceLoader
darcy
parents: 10182
diff changeset
   349
                } catch (Exception e) {
31a86b7e18ca 6575445: Update annotation processor to only use java.util.ServiceLoader
darcy
parents: 10182
diff changeset
   350
                    // 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
   351
                    this.iterator = handleServiceLoaderUnavailability("proc.no.service", null);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   352
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   353
            } catch (Throwable t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   354
                log.error("proc.service.problem");
06bc494ca11e Initial load
duke
parents:
diff changeset
   355
                throw new Abort(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
   356
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   357
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   358
06bc494ca11e Initial load
duke
parents:
diff changeset
   359
        public boolean hasNext() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   360
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   361
                return iterator.hasNext();
10186
31a86b7e18ca 6575445: Update annotation processor to only use java.util.ServiceLoader
darcy
parents: 10182
diff changeset
   362
            } catch(ServiceConfigurationError sce) {
31a86b7e18ca 6575445: Update annotation processor to only use java.util.ServiceLoader
darcy
parents: 10182
diff changeset
   363
                log.error("proc.bad.config.file", sce.getLocalizedMessage());
31a86b7e18ca 6575445: Update annotation processor to only use java.util.ServiceLoader
darcy
parents: 10182
diff changeset
   364
                throw new Abort(sce);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   365
            } catch (Throwable t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   366
                throw new Abort(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
   367
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   368
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   369
06bc494ca11e Initial load
duke
parents:
diff changeset
   370
        public Processor next() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   371
            try {
10186
31a86b7e18ca 6575445: Update annotation processor to only use java.util.ServiceLoader
darcy
parents: 10182
diff changeset
   372
                return iterator.next();
31a86b7e18ca 6575445: Update annotation processor to only use java.util.ServiceLoader
darcy
parents: 10182
diff changeset
   373
            } catch (ServiceConfigurationError sce) {
31a86b7e18ca 6575445: Update annotation processor to only use java.util.ServiceLoader
darcy
parents: 10182
diff changeset
   374
                log.error("proc.bad.config.file", sce.getLocalizedMessage());
31a86b7e18ca 6575445: Update annotation processor to only use java.util.ServiceLoader
darcy
parents: 10182
diff changeset
   375
                throw new Abort(sce);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   376
            } catch (Throwable t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   377
                throw new Abort(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
   378
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   379
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   380
06bc494ca11e Initial load
duke
parents:
diff changeset
   381
        public void remove() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   382
            throw new UnsupportedOperationException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   383
        }
3764
f0077f165983 6548708: Annotation processing should free service loader if there are no processors
darcy
parents: 3656
diff changeset
   384
f0077f165983 6548708: Annotation processing should free service loader if there are no processors
darcy
parents: 3656
diff changeset
   385
        public void close() {
10186
31a86b7e18ca 6575445: Update annotation processor to only use java.util.ServiceLoader
darcy
parents: 10182
diff changeset
   386
            if (loader != null) {
3764
f0077f165983 6548708: Annotation processing should free service loader if there are no processors
darcy
parents: 3656
diff changeset
   387
                try {
10186
31a86b7e18ca 6575445: Update annotation processor to only use java.util.ServiceLoader
darcy
parents: 10182
diff changeset
   388
                    loader.reload();
3764
f0077f165983 6548708: Annotation processing should free service loader if there are no processors
darcy
parents: 3656
diff changeset
   389
                } catch(Exception e) {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 20615
diff changeset
   390
                    // 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
   391
                }
f0077f165983 6548708: Annotation processing should free service loader if there are no processors
darcy
parents: 3656
diff changeset
   392
            }
f0077f165983 6548708: Annotation processing should free service loader if there are no processors
darcy
parents: 3656
diff changeset
   393
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   394
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   395
06bc494ca11e Initial load
duke
parents:
diff changeset
   396
06bc494ca11e Initial load
duke
parents:
diff changeset
   397
    private static class NameProcessIterator implements Iterator<Processor> {
06bc494ca11e Initial load
duke
parents:
diff changeset
   398
        Processor nextProc = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   399
        Iterator<String> names;
06bc494ca11e Initial load
duke
parents:
diff changeset
   400
        ClassLoader processorCL;
06bc494ca11e Initial load
duke
parents:
diff changeset
   401
        Log log;
06bc494ca11e Initial load
duke
parents:
diff changeset
   402
06bc494ca11e Initial load
duke
parents:
diff changeset
   403
        NameProcessIterator(String names, ClassLoader processorCL, Log log) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   404
            this.names = Arrays.asList(names.split(",")).iterator();
06bc494ca11e Initial load
duke
parents:
diff changeset
   405
            this.processorCL = processorCL;
06bc494ca11e Initial load
duke
parents:
diff changeset
   406
            this.log = log;
06bc494ca11e Initial load
duke
parents:
diff changeset
   407
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   408
06bc494ca11e Initial load
duke
parents:
diff changeset
   409
        public boolean hasNext() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   410
            if (nextProc != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   411
                return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   412
            else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   413
                if (!names.hasNext())
06bc494ca11e Initial load
duke
parents:
diff changeset
   414
                    return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   415
                else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   416
                    String processorName = names.next();
06bc494ca11e Initial load
duke
parents:
diff changeset
   417
06bc494ca11e Initial load
duke
parents:
diff changeset
   418
                    Processor processor;
06bc494ca11e Initial load
duke
parents:
diff changeset
   419
                    try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   420
                        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   421
                            processor =
06bc494ca11e Initial load
duke
parents:
diff changeset
   422
                                (Processor) (processorCL.loadClass(processorName).newInstance());
06bc494ca11e Initial load
duke
parents:
diff changeset
   423
                        } catch (ClassNotFoundException cnfe) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   424
                            log.error("proc.processor.not.found", processorName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   425
                            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   426
                        } catch (ClassCastException cce) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   427
                            log.error("proc.processor.wrong.type", processorName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   428
                            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   429
                        } catch (Exception e ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   430
                            log.error("proc.processor.cant.instantiate", processorName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   431
                            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   432
                        }
9071
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 9069
diff changeset
   433
                    } catch(ClientCodeException e) {
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 9069
diff changeset
   434
                        throw e;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   435
                    } catch(Throwable t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   436
                        throw new AnnotationProcessingError(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
   437
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   438
                    nextProc = processor;
06bc494ca11e Initial load
duke
parents:
diff changeset
   439
                    return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   440
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   441
06bc494ca11e Initial load
duke
parents:
diff changeset
   442
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   443
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   444
06bc494ca11e Initial load
duke
parents:
diff changeset
   445
        public Processor next() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   446
            if (hasNext()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   447
                Processor p = nextProc;
06bc494ca11e Initial load
duke
parents:
diff changeset
   448
                nextProc = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   449
                return p;
06bc494ca11e Initial load
duke
parents:
diff changeset
   450
            } else
06bc494ca11e Initial load
duke
parents:
diff changeset
   451
                throw new NoSuchElementException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   452
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   453
06bc494ca11e Initial load
duke
parents:
diff changeset
   454
        public void remove () {
06bc494ca11e Initial load
duke
parents:
diff changeset
   455
            throw new UnsupportedOperationException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   456
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   457
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   458
06bc494ca11e Initial load
duke
parents:
diff changeset
   459
    public boolean atLeastOneProcessor() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   460
        return discoveredProcs.iterator().hasNext();
06bc494ca11e Initial load
duke
parents:
diff changeset
   461
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   462
23810
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
   463
    private Map<String, String> initProcessorOptions() {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   464
        Set<String> keySet = options.keySet();
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 20615
diff changeset
   465
        Map<String, String> tempOptions = new LinkedHashMap<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   466
06bc494ca11e Initial load
duke
parents:
diff changeset
   467
        for(String key : keySet) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   468
            if (key.startsWith("-A") && key.length() > 2) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   469
                int sepIndex = key.indexOf('=');
06bc494ca11e Initial load
duke
parents:
diff changeset
   470
                String candidateKey = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   471
                String candidateValue = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   472
06bc494ca11e Initial load
duke
parents:
diff changeset
   473
                if (sepIndex == -1)
06bc494ca11e Initial load
duke
parents:
diff changeset
   474
                    candidateKey = key.substring(2);
06bc494ca11e Initial load
duke
parents:
diff changeset
   475
                else if (sepIndex >= 3) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   476
                    candidateKey = key.substring(2, sepIndex);
06bc494ca11e Initial load
duke
parents:
diff changeset
   477
                    candidateValue = (sepIndex < key.length()-1)?
06bc494ca11e Initial load
duke
parents:
diff changeset
   478
                        key.substring(sepIndex+1) : null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   479
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   480
                tempOptions.put(candidateKey, candidateValue);
06bc494ca11e Initial load
duke
parents:
diff changeset
   481
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   482
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   483
06bc494ca11e Initial load
duke
parents:
diff changeset
   484
        return Collections.unmodifiableMap(tempOptions);
06bc494ca11e Initial load
duke
parents:
diff changeset
   485
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   486
06bc494ca11e Initial load
duke
parents:
diff changeset
   487
    private Set<String> initUnmatchedProcessorOptions() {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 20615
diff changeset
   488
        Set<String> unmatchedProcessorOptions = new HashSet<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   489
        unmatchedProcessorOptions.addAll(processorOptions.keySet());
06bc494ca11e Initial load
duke
parents:
diff changeset
   490
        return unmatchedProcessorOptions;
06bc494ca11e Initial load
duke
parents:
diff changeset
   491
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   492
06bc494ca11e Initial load
duke
parents:
diff changeset
   493
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   494
     * State about how a processor has been used by the tool.  If a
06bc494ca11e Initial load
duke
parents:
diff changeset
   495
     * processor has been used on a prior round, its process method is
06bc494ca11e Initial load
duke
parents:
diff changeset
   496
     * 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
   497
     * annotations to process.  The {@code annotationSupported} method
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   498
     * caches the supported annotation information from the first (and
06bc494ca11e Initial load
duke
parents:
diff changeset
   499
     * only) getSupportedAnnotationTypes call to the processor.
06bc494ca11e Initial load
duke
parents:
diff changeset
   500
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   501
    static class ProcessorState {
06bc494ca11e Initial load
duke
parents:
diff changeset
   502
        public Processor processor;
06bc494ca11e Initial load
duke
parents:
diff changeset
   503
        public boolean   contributed;
06bc494ca11e Initial load
duke
parents:
diff changeset
   504
        private ArrayList<Pattern> supportedAnnotationPatterns;
06bc494ca11e Initial load
duke
parents:
diff changeset
   505
        private ArrayList<String>  supportedOptionNames;
06bc494ca11e Initial load
duke
parents:
diff changeset
   506
06bc494ca11e Initial load
duke
parents:
diff changeset
   507
        ProcessorState(Processor p, Log log, Source source, ProcessingEnvironment env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   508
            processor = p;
06bc494ca11e Initial load
duke
parents:
diff changeset
   509
            contributed = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   510
06bc494ca11e Initial load
duke
parents:
diff changeset
   511
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   512
                processor.init(env);
06bc494ca11e Initial load
duke
parents:
diff changeset
   513
06bc494ca11e Initial load
duke
parents:
diff changeset
   514
                checkSourceVersionCompatibility(source, log);
06bc494ca11e Initial load
duke
parents:
diff changeset
   515
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 20615
diff changeset
   516
                supportedAnnotationPatterns = new ArrayList<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   517
                for (String importString : processor.getSupportedAnnotationTypes()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   518
                    supportedAnnotationPatterns.add(importStringToPattern(importString,
06bc494ca11e Initial load
duke
parents:
diff changeset
   519
                                                                          processor,
06bc494ca11e Initial load
duke
parents:
diff changeset
   520
                                                                          log));
06bc494ca11e Initial load
duke
parents:
diff changeset
   521
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   522
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 20615
diff changeset
   523
                supportedOptionNames = new ArrayList<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   524
                for (String optionName : processor.getSupportedOptions() ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   525
                    if (checkOptionName(optionName, log))
06bc494ca11e Initial load
duke
parents:
diff changeset
   526
                        supportedOptionNames.add(optionName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   527
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   528
9071
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 9069
diff changeset
   529
            } catch (ClientCodeException e) {
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 9069
diff changeset
   530
                throw e;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   531
            } catch (Throwable t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   532
                throw new AnnotationProcessingError(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
   533
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   534
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   535
06bc494ca11e Initial load
duke
parents:
diff changeset
   536
        /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   537
         * Checks whether or not a processor's source version is
06bc494ca11e Initial load
duke
parents:
diff changeset
   538
         * compatible with the compilation source version.  The
06bc494ca11e Initial load
duke
parents:
diff changeset
   539
         * processor's source version needs to be greater than or
06bc494ca11e Initial load
duke
parents:
diff changeset
   540
         * equal to the source version of the compile.
06bc494ca11e Initial load
duke
parents:
diff changeset
   541
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   542
        private void checkSourceVersionCompatibility(Source source, Log log) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   543
            SourceVersion procSourceVersion = processor.getSupportedSourceVersion();
06bc494ca11e Initial load
duke
parents:
diff changeset
   544
06bc494ca11e Initial load
duke
parents:
diff changeset
   545
            if (procSourceVersion.compareTo(Source.toSourceVersion(source)) < 0 )  {
06bc494ca11e Initial load
duke
parents:
diff changeset
   546
                log.warning("proc.processor.incompatible.source.version",
06bc494ca11e Initial load
duke
parents:
diff changeset
   547
                            procSourceVersion,
06bc494ca11e Initial load
duke
parents:
diff changeset
   548
                            processor.getClass().getName(),
06bc494ca11e Initial load
duke
parents:
diff changeset
   549
                            source.name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   550
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   551
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   552
06bc494ca11e Initial load
duke
parents:
diff changeset
   553
        private boolean checkOptionName(String optionName, Log log) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   554
            boolean valid = isValidOptionName(optionName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   555
            if (!valid)
06bc494ca11e Initial load
duke
parents:
diff changeset
   556
                log.error("proc.processor.bad.option.name",
06bc494ca11e Initial load
duke
parents:
diff changeset
   557
                            optionName,
06bc494ca11e Initial load
duke
parents:
diff changeset
   558
                            processor.getClass().getName());
06bc494ca11e Initial load
duke
parents:
diff changeset
   559
            return valid;
06bc494ca11e Initial load
duke
parents:
diff changeset
   560
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   561
06bc494ca11e Initial load
duke
parents:
diff changeset
   562
        public boolean annotationSupported(String annotationName) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   563
            for(Pattern p: supportedAnnotationPatterns) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   564
                if (p.matcher(annotationName).matches())
06bc494ca11e Initial load
duke
parents:
diff changeset
   565
                    return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   566
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   567
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   568
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   569
06bc494ca11e Initial load
duke
parents:
diff changeset
   570
        /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   571
         * Remove options that are matched by this processor.
06bc494ca11e Initial load
duke
parents:
diff changeset
   572
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   573
        public void removeSupportedOptions(Set<String> unmatchedProcessorOptions) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   574
            unmatchedProcessorOptions.removeAll(supportedOptionNames);
06bc494ca11e Initial load
duke
parents:
diff changeset
   575
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   576
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   577
06bc494ca11e Initial load
duke
parents:
diff changeset
   578
    // TODO: These two classes can probably be rewritten better...
06bc494ca11e Initial load
duke
parents:
diff changeset
   579
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   580
     * This class holds information about the processors that have
06bc494ca11e Initial load
duke
parents:
diff changeset
   581
     * been discoverd so far as well as the means to discover more, if
06bc494ca11e Initial load
duke
parents:
diff changeset
   582
     * necessary.  A single iterator should be used per round of
06bc494ca11e Initial load
duke
parents:
diff changeset
   583
     * 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
   584
     * discovered processors then fails over to the service provider
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   585
     * mechanism if additional queries are made.
06bc494ca11e Initial load
duke
parents:
diff changeset
   586
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   587
    class DiscoveredProcessors implements Iterable<ProcessorState> {
06bc494ca11e Initial load
duke
parents:
diff changeset
   588
06bc494ca11e Initial load
duke
parents:
diff changeset
   589
        class ProcessorStateIterator implements Iterator<ProcessorState> {
06bc494ca11e Initial load
duke
parents:
diff changeset
   590
            DiscoveredProcessors psi;
06bc494ca11e Initial load
duke
parents:
diff changeset
   591
            Iterator<ProcessorState> innerIter;
06bc494ca11e Initial load
duke
parents:
diff changeset
   592
            boolean onProcInterator;
06bc494ca11e Initial load
duke
parents:
diff changeset
   593
06bc494ca11e Initial load
duke
parents:
diff changeset
   594
            ProcessorStateIterator(DiscoveredProcessors psi) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   595
                this.psi = psi;
06bc494ca11e Initial load
duke
parents:
diff changeset
   596
                this.innerIter = psi.procStateList.iterator();
06bc494ca11e Initial load
duke
parents:
diff changeset
   597
                this.onProcInterator = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   598
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   599
06bc494ca11e Initial load
duke
parents:
diff changeset
   600
            public ProcessorState next() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   601
                if (!onProcInterator) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   602
                    if (innerIter.hasNext())
06bc494ca11e Initial load
duke
parents:
diff changeset
   603
                        return innerIter.next();
06bc494ca11e Initial load
duke
parents:
diff changeset
   604
                    else
06bc494ca11e Initial load
duke
parents:
diff changeset
   605
                        onProcInterator = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   606
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   607
06bc494ca11e Initial load
duke
parents:
diff changeset
   608
                if (psi.processorIterator.hasNext()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   609
                    ProcessorState ps = new ProcessorState(psi.processorIterator.next(),
06bc494ca11e Initial load
duke
parents:
diff changeset
   610
                                                           log, source, JavacProcessingEnvironment.this);
06bc494ca11e Initial load
duke
parents:
diff changeset
   611
                    psi.procStateList.add(ps);
06bc494ca11e Initial load
duke
parents:
diff changeset
   612
                    return ps;
06bc494ca11e Initial load
duke
parents:
diff changeset
   613
                } else
06bc494ca11e Initial load
duke
parents:
diff changeset
   614
                    throw new NoSuchElementException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   615
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   616
06bc494ca11e Initial load
duke
parents:
diff changeset
   617
            public boolean hasNext() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   618
                if (onProcInterator)
06bc494ca11e Initial load
duke
parents:
diff changeset
   619
                    return  psi.processorIterator.hasNext();
06bc494ca11e Initial load
duke
parents:
diff changeset
   620
                else
06bc494ca11e Initial load
duke
parents:
diff changeset
   621
                    return innerIter.hasNext() || psi.processorIterator.hasNext();
06bc494ca11e Initial load
duke
parents:
diff changeset
   622
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   623
06bc494ca11e Initial load
duke
parents:
diff changeset
   624
            public void remove () {
06bc494ca11e Initial load
duke
parents:
diff changeset
   625
                throw new UnsupportedOperationException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   626
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   627
06bc494ca11e Initial load
duke
parents:
diff changeset
   628
            /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   629
             * Run all remaining processors on the procStateList that
06bc494ca11e Initial load
duke
parents:
diff changeset
   630
             * have not already run this round with an empty set of
06bc494ca11e Initial load
duke
parents:
diff changeset
   631
             * annotations.
06bc494ca11e Initial load
duke
parents:
diff changeset
   632
             */
06bc494ca11e Initial load
duke
parents:
diff changeset
   633
            public void runContributingProcs(RoundEnvironment re) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   634
                if (!onProcInterator) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   635
                    Set<TypeElement> emptyTypeElements = Collections.emptySet();
06bc494ca11e Initial load
duke
parents:
diff changeset
   636
                    while(innerIter.hasNext()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   637
                        ProcessorState ps = innerIter.next();
06bc494ca11e Initial load
duke
parents:
diff changeset
   638
                        if (ps.contributed)
06bc494ca11e Initial load
duke
parents:
diff changeset
   639
                            callProcessor(ps.processor, emptyTypeElements, re);
06bc494ca11e Initial load
duke
parents:
diff changeset
   640
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   641
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   642
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   643
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   644
06bc494ca11e Initial load
duke
parents:
diff changeset
   645
        Iterator<? extends Processor> processorIterator;
06bc494ca11e Initial load
duke
parents:
diff changeset
   646
        ArrayList<ProcessorState>  procStateList;
06bc494ca11e Initial load
duke
parents:
diff changeset
   647
06bc494ca11e Initial load
duke
parents:
diff changeset
   648
        public ProcessorStateIterator iterator() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   649
            return new ProcessorStateIterator(this);
06bc494ca11e Initial load
duke
parents:
diff changeset
   650
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   651
06bc494ca11e Initial load
duke
parents:
diff changeset
   652
        DiscoveredProcessors(Iterator<? extends Processor> processorIterator) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   653
            this.processorIterator = processorIterator;
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 20615
diff changeset
   654
            this.procStateList = new ArrayList<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   655
        }
3764
f0077f165983 6548708: Annotation processing should free service loader if there are no processors
darcy
parents: 3656
diff changeset
   656
f0077f165983 6548708: Annotation processing should free service loader if there are no processors
darcy
parents: 3656
diff changeset
   657
        /**
f0077f165983 6548708: Annotation processing should free service loader if there are no processors
darcy
parents: 3656
diff changeset
   658
         * 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
   659
         */
f0077f165983 6548708: Annotation processing should free service loader if there are no processors
darcy
parents: 3656
diff changeset
   660
        public void close() {
f0077f165983 6548708: Annotation processing should free service loader if there are no processors
darcy
parents: 3656
diff changeset
   661
            if (processorIterator != null &&
f0077f165983 6548708: Annotation processing should free service loader if there are no processors
darcy
parents: 3656
diff changeset
   662
                processorIterator instanceof ServiceIterator) {
f0077f165983 6548708: Annotation processing should free service loader if there are no processors
darcy
parents: 3656
diff changeset
   663
                ((ServiceIterator) processorIterator).close();
f0077f165983 6548708: Annotation processing should free service loader if there are no processors
darcy
parents: 3656
diff changeset
   664
            }
f0077f165983 6548708: Annotation processing should free service loader if there are no processors
darcy
parents: 3656
diff changeset
   665
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   666
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   667
23810
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
   668
    private void discoverAndRunProcs(Set<TypeElement> annotationsPresent,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   669
                                     List<ClassSymbol> topLevelClasses,
06bc494ca11e Initial load
duke
parents:
diff changeset
   670
                                     List<PackageSymbol> packageInfoFiles) {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 20615
diff changeset
   671
        Map<String, TypeElement> unmatchedAnnotations = new HashMap<>(annotationsPresent.size());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   672
06bc494ca11e Initial load
duke
parents:
diff changeset
   673
        for(TypeElement a  : annotationsPresent) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   674
                unmatchedAnnotations.put(a.getQualifiedName().toString(),
06bc494ca11e Initial load
duke
parents:
diff changeset
   675
                                         a);
06bc494ca11e Initial load
duke
parents:
diff changeset
   676
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   677
06bc494ca11e Initial load
duke
parents:
diff changeset
   678
        // Give "*" processors a chance to match
06bc494ca11e Initial load
duke
parents:
diff changeset
   679
        if (unmatchedAnnotations.size() == 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
   680
            unmatchedAnnotations.put("", null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   681
06bc494ca11e Initial load
duke
parents:
diff changeset
   682
        DiscoveredProcessors.ProcessorStateIterator psi = discoveredProcs.iterator();
06bc494ca11e Initial load
duke
parents:
diff changeset
   683
        // TODO: Create proper argument values; need past round
06bc494ca11e Initial load
duke
parents:
diff changeset
   684
        // information to fill in this constructor.  Note that the 1
06bc494ca11e Initial load
duke
parents:
diff changeset
   685
        // st round of processing could be the last round if there
06bc494ca11e Initial load
duke
parents:
diff changeset
   686
        // were parse errors on the initial source files; however, we
06bc494ca11e Initial load
duke
parents:
diff changeset
   687
        // are not doing processing in that case.
06bc494ca11e Initial load
duke
parents:
diff changeset
   688
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 20615
diff changeset
   689
        Set<Element> rootElements = new LinkedHashSet<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   690
        rootElements.addAll(topLevelClasses);
06bc494ca11e Initial load
duke
parents:
diff changeset
   691
        rootElements.addAll(packageInfoFiles);
06bc494ca11e Initial load
duke
parents:
diff changeset
   692
        rootElements = Collections.unmodifiableSet(rootElements);
06bc494ca11e Initial load
duke
parents:
diff changeset
   693
06bc494ca11e Initial load
duke
parents:
diff changeset
   694
        RoundEnvironment renv = new JavacRoundEnvironment(false,
06bc494ca11e Initial load
duke
parents:
diff changeset
   695
                                                          false,
06bc494ca11e Initial load
duke
parents:
diff changeset
   696
                                                          rootElements,
06bc494ca11e Initial load
duke
parents:
diff changeset
   697
                                                          JavacProcessingEnvironment.this);
06bc494ca11e Initial load
duke
parents:
diff changeset
   698
06bc494ca11e Initial load
duke
parents:
diff changeset
   699
        while(unmatchedAnnotations.size() > 0 && psi.hasNext() ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   700
            ProcessorState ps = psi.next();
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 20615
diff changeset
   701
            Set<String>  matchedNames = new HashSet<>();
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 20615
diff changeset
   702
            Set<TypeElement> typeElements = new LinkedHashSet<>();
5003
fd0b30cdbe5c 6929645: Address various findbugs warnings in langtools
darcy
parents: 5001
diff changeset
   703
fd0b30cdbe5c 6929645: Address various findbugs warnings in langtools
darcy
parents: 5001
diff changeset
   704
            for (Map.Entry<String, TypeElement> entry: unmatchedAnnotations.entrySet()) {
fd0b30cdbe5c 6929645: Address various findbugs warnings in langtools
darcy
parents: 5001
diff changeset
   705
                String unmatchedAnnotationName = entry.getKey();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   706
                if (ps.annotationSupported(unmatchedAnnotationName) ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   707
                    matchedNames.add(unmatchedAnnotationName);
5003
fd0b30cdbe5c 6929645: Address various findbugs warnings in langtools
darcy
parents: 5001
diff changeset
   708
                    TypeElement te = entry.getValue();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   709
                    if (te != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   710
                        typeElements.add(te);
06bc494ca11e Initial load
duke
parents:
diff changeset
   711
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   712
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   713
06bc494ca11e Initial load
duke
parents:
diff changeset
   714
            if (matchedNames.size() > 0 || ps.contributed) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   715
                boolean processingResult = callProcessor(ps.processor, typeElements, renv);
06bc494ca11e Initial load
duke
parents:
diff changeset
   716
                ps.contributed = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   717
                ps.removeSupportedOptions(unmatchedProcessorOptions);
06bc494ca11e Initial load
duke
parents:
diff changeset
   718
06bc494ca11e Initial load
duke
parents:
diff changeset
   719
                if (printProcessorInfo || verbose) {
11053
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 10950
diff changeset
   720
                    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
   721
                            ps.processor.getClass().getName(),
79b7dee406cc 6966732: replace use of static Log.getLocalizedString with non-static alternative where possible
jjg
parents: 5847
diff changeset
   722
                            matchedNames.toString(),
79b7dee406cc 6966732: replace use of static Log.getLocalizedString with non-static alternative where possible
jjg
parents: 5847
diff changeset
   723
                            processingResult);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   724
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   725
06bc494ca11e Initial load
duke
parents:
diff changeset
   726
                if (processingResult) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   727
                    unmatchedAnnotations.keySet().removeAll(matchedNames);
06bc494ca11e Initial load
duke
parents:
diff changeset
   728
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   729
06bc494ca11e Initial load
duke
parents:
diff changeset
   730
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   731
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   732
        unmatchedAnnotations.remove("");
06bc494ca11e Initial load
duke
parents:
diff changeset
   733
06bc494ca11e Initial load
duke
parents:
diff changeset
   734
        if (lint && unmatchedAnnotations.size() > 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   735
            // Remove annotations processed by javac
06bc494ca11e Initial load
duke
parents:
diff changeset
   736
            unmatchedAnnotations.keySet().removeAll(platformAnnotations);
06bc494ca11e Initial load
duke
parents:
diff changeset
   737
            if (unmatchedAnnotations.size() > 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   738
                log.warning("proc.annotations.without.processors",
06bc494ca11e Initial load
duke
parents:
diff changeset
   739
                            unmatchedAnnotations.keySet());
06bc494ca11e Initial load
duke
parents:
diff changeset
   740
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   741
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   742
06bc494ca11e Initial load
duke
parents:
diff changeset
   743
        // Run contributing processors that haven't run yet
06bc494ca11e Initial load
duke
parents:
diff changeset
   744
        psi.runContributingProcs(renv);
06bc494ca11e Initial load
duke
parents:
diff changeset
   745
06bc494ca11e Initial load
duke
parents:
diff changeset
   746
        // Debugging
6721
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 6717
diff changeset
   747
        if (options.isSet("displayFilerState"))
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   748
            filer.displayState();
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
     * Computes the set of annotations on the symbol in question.
06bc494ca11e Initial load
duke
parents:
diff changeset
   753
     * Leave class public for external testing purposes.
06bc494ca11e Initial load
duke
parents:
diff changeset
   754
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   755
    public static class ComputeAnnotationSet extends
25690
b1dac768ab79 8050430: Provided new utility visitors supporting SourceVersion.RELEASE_9
darcy
parents: 24604
diff changeset
   756
        ElementScanner9<Set<TypeElement>, Set<TypeElement>> {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   757
        final Elements elements;
06bc494ca11e Initial load
duke
parents:
diff changeset
   758
06bc494ca11e Initial load
duke
parents:
diff changeset
   759
        public ComputeAnnotationSet(Elements elements) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   760
            super();
06bc494ca11e Initial load
duke
parents:
diff changeset
   761
            this.elements = elements;
06bc494ca11e Initial load
duke
parents:
diff changeset
   762
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   763
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   764
        @Override @DefinedBy(Api.LANGUAGE_MODEL)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   765
        public Set<TypeElement> visitPackage(PackageElement e, Set<TypeElement> p) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   766
            // Don't scan enclosed elements of a package
06bc494ca11e Initial load
duke
parents:
diff changeset
   767
            return p;
06bc494ca11e Initial load
duke
parents:
diff changeset
   768
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   769
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   770
        @Override @DefinedBy(Api.LANGUAGE_MODEL)
18669
99572d59c916 7162089: Add support for repeating annotations to javax.annotation.processing
darcy
parents: 17551
diff changeset
   771
        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
   772
            // 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
   773
            scan(e.getTypeParameters(), p);
23795
62509b72088e 8038080: annotation processors don't visit declaration parameter annotations
jfranck
parents: 22442
diff changeset
   774
            return super.visitType(e, p);
18669
99572d59c916 7162089: Add support for repeating annotations to javax.annotation.processing
darcy
parents: 17551
diff changeset
   775
        }
99572d59c916 7162089: Add support for repeating annotations to javax.annotation.processing
darcy
parents: 17551
diff changeset
   776
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   777
        @Override @DefinedBy(Api.LANGUAGE_MODEL)
18669
99572d59c916 7162089: Add support for repeating annotations to javax.annotation.processing
darcy
parents: 17551
diff changeset
   778
        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
   779
            // 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
   780
            scan(e.getTypeParameters(), p);
23795
62509b72088e 8038080: annotation processors don't visit declaration parameter annotations
jfranck
parents: 22442
diff changeset
   781
            return super.visitExecutable(e, p);
18669
99572d59c916 7162089: Add support for repeating annotations to javax.annotation.processing
darcy
parents: 17551
diff changeset
   782
        }
99572d59c916 7162089: Add support for repeating annotations to javax.annotation.processing
darcy
parents: 17551
diff changeset
   783
99572d59c916 7162089: Add support for repeating annotations to javax.annotation.processing
darcy
parents: 17551
diff changeset
   784
        void addAnnotations(Element e, Set<TypeElement> p) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   785
            for (AnnotationMirror annotationMirror :
06bc494ca11e Initial load
duke
parents:
diff changeset
   786
                     elements.getAllAnnotationMirrors(e) ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   787
                Element e2 = annotationMirror.getAnnotationType().asElement();
06bc494ca11e Initial load
duke
parents:
diff changeset
   788
                p.add((TypeElement) e2);
06bc494ca11e Initial load
duke
parents:
diff changeset
   789
            }
18669
99572d59c916 7162089: Add support for repeating annotations to javax.annotation.processing
darcy
parents: 17551
diff changeset
   790
        }
99572d59c916 7162089: Add support for repeating annotations to javax.annotation.processing
darcy
parents: 17551
diff changeset
   791
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   792
        @Override @DefinedBy(Api.LANGUAGE_MODEL)
18669
99572d59c916 7162089: Add support for repeating annotations to javax.annotation.processing
darcy
parents: 17551
diff changeset
   793
        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
   794
            addAnnotations(e, p);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   795
            return super.scan(e, p);
06bc494ca11e Initial load
duke
parents:
diff changeset
   796
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   797
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   798
06bc494ca11e Initial load
duke
parents:
diff changeset
   799
    private boolean callProcessor(Processor proc,
06bc494ca11e Initial load
duke
parents:
diff changeset
   800
                                         Set<? extends TypeElement> tes,
06bc494ca11e Initial load
duke
parents:
diff changeset
   801
                                         RoundEnvironment renv) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   802
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   803
            return proc.process(tes, renv);
24604
7f68545b5128 8041422: Split javac ClassReader into ClassReader+ClassFinder
jjg
parents: 24396
diff changeset
   804
        } catch (ClassFinder.BadClassFile ex) {
9077
6ee59e40b258 7031108: NPE in javac.jvm.ClassReader.findMethod in PackageElement.enclosedElements from AP in incr build
jjg
parents: 9071
diff changeset
   805
            log.error("proc.cant.access.1", ex.sym, ex.getDetailValue());
6ee59e40b258 7031108: NPE in javac.jvm.ClassReader.findMethod in PackageElement.enclosedElements from AP in incr build
jjg
parents: 9071
diff changeset
   806
            return false;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   807
        } catch (CompletionFailure ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   808
            StringWriter out = new StringWriter();
06bc494ca11e Initial load
duke
parents:
diff changeset
   809
            ex.printStackTrace(new PrintWriter(out));
169
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents: 10
diff changeset
   810
            log.error("proc.cant.access", ex.sym, ex.getDetailValue(), out.toString());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   811
            return false;
9071
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 9069
diff changeset
   812
        } catch (ClientCodeException e) {
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 9069
diff changeset
   813
            throw e;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   814
        } catch (Throwable t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   815
            throw new AnnotationProcessingError(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
   816
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   817
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   818
6159
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
   819
    /**
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
   820
     * Helper object for a single round of annotation processing.
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
   821
     */
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
   822
    class Round {
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
   823
        /** The round number. */
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
   824
        final int number;
14538
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   825
        /** The diagnostic handler for the round. */
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   826
        final Log.DeferredDiagnosticHandler deferredDiagnosticHandler;
6355
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
   827
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
   828
        /** The ASTs to be compiled. */
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
   829
        List<JCCompilationUnit> roots;
23810
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
   830
        /** 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
   831
        Set<JCCompilationUnit> treesToClean;
6355
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
   832
        /** The classes to be compiler that have were generated. */
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
   833
        Map<String, JavaFileObject> genClassFiles;
6159
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
   834
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
   835
        /** The set of annotations to be processed this round. */
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
   836
        Set<TypeElement> annotationsPresent;
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
   837
        /** The set of top level classes to be processed this round. */
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
   838
        List<ClassSymbol> topLevelClasses;
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
   839
        /** The set of package-info files to be processed this round. */
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
   840
        List<PackageSymbol> packageInfoFiles;
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
   841
6355
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
   842
        /** Create a round (common code). */
23810
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
   843
        private Round(int number, Set<JCCompilationUnit> treesToClean,
14538
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   844
                Log.DeferredDiagnosticHandler deferredDiagnosticHandler) {
6159
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
   845
            this.number = number;
6355
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
   846
14538
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   847
            if (number == 1) {
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   848
                Assert.checkNonNull(deferredDiagnosticHandler);
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   849
                this.deferredDiagnosticHandler = deferredDiagnosticHandler;
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   850
            } else {
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   851
                this.deferredDiagnosticHandler = new Log.DeferredDiagnosticHandler(log);
23810
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
   852
                compiler.setDeferredDiagnosticHandler(this.deferredDiagnosticHandler);
14538
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   853
            }
6159
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
   854
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
   855
            // the following will be populated as needed
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
   856
            topLevelClasses  = List.nil();
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
   857
            packageInfoFiles = List.nil();
23810
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
   858
            this.treesToClean = treesToClean;
6159
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
   859
        }
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
   860
6355
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
   861
        /** Create the first round. */
23810
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
   862
        Round(List<JCCompilationUnit> roots,
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
   863
              List<ClassSymbol> classSymbols,
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
   864
              Set<JCCompilationUnit> treesToClean,
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
   865
              Log.DeferredDiagnosticHandler deferredDiagnosticHandler) {
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
   866
            this(1, treesToClean, deferredDiagnosticHandler);
6355
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
   867
            this.roots = roots;
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 20615
diff changeset
   868
            genClassFiles = new HashMap<>();
6355
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
   869
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
   870
            // The reverse() in the following line is to maintain behavioural
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
   871
            // compatibility with the previous revision of the code. Strictly speaking,
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
   872
            // 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
   873
            topLevelClasses =
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
   874
                getTopLevelClasses(roots).prependList(classSymbols.reverse());
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
   875
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
   876
            packageInfoFiles = getPackageInfoFiles(roots);
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
   877
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
   878
            findAnnotationsPresent();
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
   879
        }
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
   880
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
   881
        /** Create a new round. */
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
   882
        private Round(Round prev,
6581
f58f0ce45802 6980707: Reduce use of IOException in JavaCompiler
jjg
parents: 6355
diff changeset
   883
                Set<JavaFileObject> newSourceFiles, Map<String,JavaFileObject> newClassFiles) {
23810
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
   884
            this(prev.number+1, prev.treesToClean, null);
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
   885
            prev.newRound();
6355
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
   886
            this.genClassFiles = prev.genClassFiles;
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
   887
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
   888
            List<JCCompilationUnit> parsedFiles = compiler.parseFiles(newSourceFiles);
23810
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
   889
            roots = prev.roots.appendList(parsedFiles);
6355
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
   890
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
   891
            // Check for errors after parsing
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
   892
            if (unrecoverableError())
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
   893
                return;
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
   894
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
   895
            enterClassFiles(genClassFiles);
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
   896
            List<ClassSymbol> newClasses = enterClassFiles(newClassFiles);
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
   897
            genClassFiles.putAll(newClassFiles);
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
   898
            enterTrees(roots);
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
   899
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
   900
            if (unrecoverableError())
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
   901
                return;
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
   902
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
   903
            topLevelClasses = join(
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
   904
                    getTopLevelClasses(parsedFiles),
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
   905
                    getTopLevelClassesFromClasses(newClasses));
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
   906
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
   907
            packageInfoFiles = join(
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
   908
                    getPackageInfoFiles(parsedFiles),
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
   909
                    getPackageInfoFilesFromClasses(newClasses));
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
   910
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
   911
            findAnnotationsPresent();
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
   912
        }
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
   913
6159
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
   914
        /** Create the next round to be used. */
6581
f58f0ce45802 6980707: Reduce use of IOException in JavaCompiler
jjg
parents: 6355
diff changeset
   915
        Round next(Set<JavaFileObject> newSourceFiles, Map<String, JavaFileObject> newClassFiles) {
23810
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
   916
            return new Round(this, newSourceFiles, newClassFiles);
6355
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
   917
        }
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
   918
23810
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
   919
        /** Prepare the compiler for the final compilation. */
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
   920
        void finalCompiler() {
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
   921
            newRound();
6159
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
   922
        }
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
   923
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
   924
        /** Return the number of errors found so far in this round.
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
   925
         * This may include uncoverable errors, such as parse errors,
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
   926
         * and transient errors, such as missing symbols. */
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
   927
        int errorCount() {
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
   928
            return compiler.errorCount();
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
   929
        }
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
   930
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
   931
        /** Return the number of warnings found so far in this round. */
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
   932
        int warningCount() {
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
   933
            return compiler.warningCount();
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
   934
        }
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
   935
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
   936
        /** Return whether or not an unrecoverable error has occurred. */
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
   937
        boolean unrecoverableError() {
6582
c7a4fb5a2f86 6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents: 6581
diff changeset
   938
            if (messager.errorRaised())
c7a4fb5a2f86 6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents: 6581
diff changeset
   939
                return true;
c7a4fb5a2f86 6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents: 6581
diff changeset
   940
14538
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   941
            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
   942
                switch (d.getKind()) {
c7a4fb5a2f86 6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents: 6581
diff changeset
   943
                    case WARNING:
c7a4fb5a2f86 6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents: 6581
diff changeset
   944
                        if (werror)
c7a4fb5a2f86 6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents: 6581
diff changeset
   945
                            return true;
c7a4fb5a2f86 6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents: 6581
diff changeset
   946
                        break;
c7a4fb5a2f86 6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents: 6581
diff changeset
   947
c7a4fb5a2f86 6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents: 6581
diff changeset
   948
                    case ERROR:
7076
c96fa26247c8 6994946: option to specify only syntax errors as unrecoverable
jjg
parents: 7072
diff changeset
   949
                        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
   950
                            return true;
c7a4fb5a2f86 6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents: 6581
diff changeset
   951
                        break;
c7a4fb5a2f86 6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents: 6581
diff changeset
   952
                }
c7a4fb5a2f86 6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents: 6581
diff changeset
   953
            }
c7a4fb5a2f86 6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents: 6581
diff changeset
   954
c7a4fb5a2f86 6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents: 6581
diff changeset
   955
            return false;
6159
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
   956
        }
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
   957
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
   958
        /** Find the set of annotations present in the set of top level
6355
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
   959
         *  classes and package info files to be processed this round. */
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
   960
        void findAnnotationsPresent() {
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
   961
            ComputeAnnotationSet annotationComputer = new ComputeAnnotationSet(elementUtils);
6159
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
   962
            // Use annotation processing to compute the set of annotations present
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 20615
diff changeset
   963
            annotationsPresent = new LinkedHashSet<>();
6159
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
   964
            for (ClassSymbol classSym : topLevelClasses)
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
   965
                annotationComputer.scan(classSym, annotationsPresent);
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
   966
            for (PackageSymbol pkgSym : packageInfoFiles)
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
   967
                annotationComputer.scan(pkgSym, annotationsPresent);
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
   968
        }
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
   969
6355
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
   970
        /** Enter a set of generated class files. */
6582
c7a4fb5a2f86 6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents: 6581
diff changeset
   971
        private List<ClassSymbol> enterClassFiles(Map<String, JavaFileObject> classFiles) {
6159
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
   972
            List<ClassSymbol> list = List.nil();
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
   973
6355
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
   974
            for (Map.Entry<String,JavaFileObject> entry : classFiles.entrySet()) {
6159
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
   975
                Name name = names.fromString(entry.getKey());
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
   976
                JavaFileObject file = entry.getValue();
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
   977
                if (file.getKind() != JavaFileObject.Kind.CLASS)
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
   978
                    throw new AssertionError(file);
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
   979
                ClassSymbol cs;
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
   980
                if (isPkgInfo(file, JavaFileObject.Kind.CLASS)) {
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
   981
                    Name packageName = Convert.packagePart(name);
22442
8fd30fc4e3a3 8029690: Move symbol creation from ClassReader to Symtab
jfranck
parents: 22163
diff changeset
   982
                    PackageSymbol p = symtab.enterPackage(packageName);
6159
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
   983
                    if (p.package_info == null)
22442
8fd30fc4e3a3 8029690: Move symbol creation from ClassReader to Symtab
jfranck
parents: 22163
diff changeset
   984
                        p.package_info = symtab.enterClass(Convert.shortName(name), p);
6159
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
   985
                    cs = p.package_info;
23810
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
   986
                    cs.reset();
6159
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
   987
                    if (cs.classfile == null)
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
   988
                        cs.classfile = file;
23810
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
   989
                    cs.completer = initialCompleter;
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
   990
                } else {
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
   991
                    cs = symtab.enterClass(name);
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
   992
                    cs.reset();
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
   993
                    cs.classfile = file;
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
   994
                    cs.completer = initialCompleter;
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
   995
                }
6159
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
   996
                list = list.prepend(cs);
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
            return list.reverse();
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
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
  1001
        /** 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
  1002
        private void enterTrees(List<JCCompilationUnit> roots) {
6159
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
  1003
            compiler.enterTrees(roots);
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
  1004
        }
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
  1005
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
  1006
        /** Run a processing round. */
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
  1007
        void run(boolean lastRound, boolean errorStatus) {
6355
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
  1008
            printRoundInfo(lastRound);
6159
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
  1009
12016
1990493b64db 7093891: support multiple task listeners
jjg
parents: 11316
diff changeset
  1010
            if (!taskListener.isEmpty())
6159
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
  1011
                taskListener.started(new TaskEvent(TaskEvent.Kind.ANNOTATION_PROCESSING_ROUND));
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
            try {
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
  1014
                if (lastRound) {
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
  1015
                    filer.setLastRound(true);
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
  1016
                    Set<Element> emptyRootElements = Collections.emptySet(); // immutable
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
  1017
                    RoundEnvironment renv = new JavacRoundEnvironment(true,
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
  1018
                            errorStatus,
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
  1019
                            emptyRootElements,
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
  1020
                            JavacProcessingEnvironment.this);
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
  1021
                    discoveredProcs.iterator().runContributingProcs(renv);
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
  1022
                } else {
23810
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
  1023
                    discoverAndRunProcs(annotationsPresent, topLevelClasses, packageInfoFiles);
6159
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
  1024
                }
20615
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents: 19651
diff changeset
  1025
            } 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
  1026
                // 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
  1027
                // 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
  1028
                // 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
  1029
                deferredDiagnosticHandler.reportDeferredDiagnostics();
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents: 19651
diff changeset
  1030
                log.popDiagnosticHandler(deferredDiagnosticHandler);
23810
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
  1031
                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
  1032
                throw t;
6159
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
  1033
            } finally {
12016
1990493b64db 7093891: support multiple task listeners
jjg
parents: 11316
diff changeset
  1034
                if (!taskListener.isEmpty())
6159
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
  1035
                    taskListener.finished(new TaskEvent(TaskEvent.Kind.ANNOTATION_PROCESSING_ROUND));
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
  1036
            }
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
  1037
        }
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
  1038
6582
c7a4fb5a2f86 6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents: 6581
diff changeset
  1039
        void showDiagnostics(boolean showAll) {
c7a4fb5a2f86 6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents: 6581
diff changeset
  1040
            Set<JCDiagnostic.Kind> kinds = EnumSet.allOf(JCDiagnostic.Kind.class);
c7a4fb5a2f86 6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents: 6581
diff changeset
  1041
            if (!showAll) {
c7a4fb5a2f86 6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents: 6581
diff changeset
  1042
                // suppress errors, which are all presumed to be transient resolve errors
c7a4fb5a2f86 6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents: 6581
diff changeset
  1043
                kinds.remove(JCDiagnostic.Kind.ERROR);
c7a4fb5a2f86 6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents: 6581
diff changeset
  1044
            }
14538
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
  1045
            deferredDiagnosticHandler.reportDeferredDiagnostics(kinds);
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
  1046
            log.popDiagnosticHandler(deferredDiagnosticHandler);
23810
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
  1047
            compiler.setDeferredDiagnosticHandler(null);
6582
c7a4fb5a2f86 6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents: 6581
diff changeset
  1048
        }
c7a4fb5a2f86 6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents: 6581
diff changeset
  1049
6159
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
  1050
        /** Print info about this round. */
6355
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
  1051
        private void printRoundInfo(boolean lastRound) {
6159
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
  1052
            if (printRounds || verbose) {
6355
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
  1053
                List<ClassSymbol> tlc = lastRound ? List.<ClassSymbol>nil() : topLevelClasses;
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
  1054
                Set<TypeElement> ap = lastRound ? Collections.<TypeElement>emptySet() : annotationsPresent;
11053
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 10950
diff changeset
  1055
                log.printLines("x.print.rounds",
6355
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
  1056
                        number,
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
  1057
                        "{" + tlc.toString(", ") + "}",
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
  1058
                        ap,
6159
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
  1059
                        lastRound);
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
  1060
            }
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
  1061
        }
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
  1062
23810
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
  1063
        /** 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
  1064
         * 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
  1065
         */
23810
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
  1066
        private void newRound() {
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
  1067
            //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
  1068
            for (Env<AttrContext> env : enter.getEnvs()) {
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
  1069
                treesToClean.add(env.toplevel);
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
  1070
            }
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
  1071
            for (JCCompilationUnit node : treesToClean) {
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
  1072
                treeCleaner.scan(node);
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
  1073
            }
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
  1074
            chk.newRound();
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
  1075
            enter.newRound();
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
  1076
            filer.newRound();
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
  1077
            messager.newRound();
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
  1078
            compiler.newRound();
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
  1079
            types.newRound();
6159
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
  1080
23810
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
  1081
            boolean foundError = false;
11316
4dcad625e72e 7111022: javac no long prints last round of processing
jjg
parents: 11314
diff changeset
  1082
23810
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
  1083
            for (ClassSymbol cs : symtab.classes.values()) {
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26781
diff changeset
  1084
                if (cs.kind == ERR) {
23810
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
  1085
                    foundError = true;
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
  1086
                    break;
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
  1087
                }
6159
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
  1088
            }
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
  1089
23810
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
  1090
            if (foundError) {
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
  1091
                for (ClassSymbol cs : symtab.classes.values()) {
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26781
diff changeset
  1092
                    if (cs.classfile != null || cs.kind == ERR) {
23810
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
  1093
                        cs.reset();
26781
a786b07c7b91 8048614: Add TypeMetadata to contain type annotations and other type information
emc
parents: 26266
diff changeset
  1094
                        cs.type = new ClassType(cs.type.getEnclosingType(), null, cs);
30066
d74c06a92bd8 8078600: Infinite loop when compiling annotations with -XDcompletionDeps
alundblad
parents: 29427
diff changeset
  1095
                        if (cs.isCompleted()) {
23810
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
  1096
                            cs.completer = initialCompleter;
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
  1097
                        }
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
  1098
                    }
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
  1099
                }
6159
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
  1100
            }
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
  1101
        }
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
  1102
    }
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
  1103
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1104
06bc494ca11e Initial load
duke
parents:
diff changeset
  1105
    // TODO: internal catch clauses?; catch and rethrow an annotation
06bc494ca11e Initial load
duke
parents:
diff changeset
  1106
    // processing error
23810
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
  1107
    public boolean doProcessing(List<JCCompilationUnit> roots,
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
  1108
                                List<ClassSymbol> classSymbols,
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
  1109
                                Iterable<? extends PackageSymbol> pckSymbols,
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
  1110
                                Log.DeferredDiagnosticHandler deferredDiagnosticHandler) {
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
  1111
        final Set<JCCompilationUnit> treesToClean =
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
  1112
                Collections.newSetFromMap(new IdentityHashMap<JCCompilationUnit, Boolean>());
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
  1113
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
  1114
        //fill already attributed implicit trees:
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
  1115
        for (Env<AttrContext> env : enter.getEnvs()) {
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
  1116
            treesToClean.add(env.toplevel);
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
  1117
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1118
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 20615
diff changeset
  1119
        Set<PackageSymbol> specifiedPackages = new LinkedHashSet<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1120
        for (PackageSymbol psym : pckSymbols)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1121
            specifiedPackages.add(psym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1122
        this.specifiedPackages = Collections.unmodifiableSet(specifiedPackages);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1123
23810
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
  1124
        Round round = new Round(roots, classSymbols, treesToClean, deferredDiagnosticHandler);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1125
6355
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
  1126
        boolean errorStatus;
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
  1127
        boolean moreToDo;
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
  1128
        do {
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
  1129
            // Run processors for round n
6159
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
  1130
            round.run(false, false);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1131
6355
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
  1132
            // Processors for round n have run to completion.
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
  1133
            // Check for errors and whether there is more work to do.
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
  1134
            errorStatus = round.unrecoverableError();
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
  1135
            moreToDo = moreToDo();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1136
6582
c7a4fb5a2f86 6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents: 6581
diff changeset
  1137
            round.showDiagnostics(errorStatus || showResolveErrors);
c7a4fb5a2f86 6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents: 6581
diff changeset
  1138
6355
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
  1139
            // Set up next round.
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
  1140
            // Copy mutable collections returned from filer.
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
  1141
            round = round.next(
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 20615
diff changeset
  1142
                    new LinkedHashSet<>(filer.getGeneratedSourceFileObjects()),
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 20615
diff changeset
  1143
                    new LinkedHashMap<>(filer.getGeneratedClasses()));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1144
6355
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
  1145
             // Check for errors during setup.
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
  1146
            if (round.unrecoverableError())
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
  1147
                errorStatus = true;
4871
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents: 3764
diff changeset
  1148
6355
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
  1149
        } while (moreToDo && !errorStatus);
6159
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
        // run last round
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
  1152
        round.run(true, errorStatus);
6582
c7a4fb5a2f86 6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents: 6581
diff changeset
  1153
        round.showDiagnostics(true);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1154
06bc494ca11e Initial load
duke
parents:
diff changeset
  1155
        filer.warnIfUnclosedFiles();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1156
        warnIfUnmatchedOptions();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1157
6355
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
  1158
        /*
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
  1159
         * If an annotation processor raises an error in a round,
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
  1160
         * that round runs to completion and one last round occurs.
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
  1161
         * The last round may also occur because no more source or
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
  1162
         * class files have been generated.  Therefore, if an error
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
  1163
         * was raised on either of the last *two* rounds, the compile
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
  1164
         * should exit with a nonzero exit code.  The current value of
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
  1165
         * errorStatus holds whether or not an error was raised on the
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
  1166
         * second to last round; errorRaised() gives the error status
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
  1167
         * of the last round.
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
  1168
         */
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
  1169
        if (messager.errorRaised()
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
  1170
                || werror && round.warningCount() > 0 && round.errorCount() > 0)
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
  1171
            errorStatus = true;
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
  1172
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
  1173
        Set<JavaFileObject> newSourceFiles =
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 20615
diff changeset
  1174
                new LinkedHashSet<>(filer.getGeneratedSourceFileObjects());
23810
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
  1175
        roots = round.roots;
6355
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
  1176
23810
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
  1177
        errorStatus = errorStatus || (compiler.errorCount() > 0);
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
  1178
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
  1179
        if (!errorStatus)
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
  1180
            round.finalCompiler();
6355
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
  1181
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
  1182
        if (newSourceFiles.size() > 0)
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
  1183
            roots = roots.appendList(compiler.parseFiles(newSourceFiles));
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
  1184
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
  1185
        errorStatus = errorStatus || (compiler.errorCount() > 0);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1186
06bc494ca11e Initial load
duke
parents:
diff changeset
  1187
        // Free resources
06bc494ca11e Initial load
duke
parents:
diff changeset
  1188
        this.close();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1189
12016
1990493b64db 7093891: support multiple task listeners
jjg
parents: 11316
diff changeset
  1190
        if (!taskListener.isEmpty())
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1191
            taskListener.finished(new TaskEvent(TaskEvent.Kind.ANNOTATION_PROCESSING));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1192
06bc494ca11e Initial load
duke
parents:
diff changeset
  1193
        if (errorStatus) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1194
            if (compiler.errorCount() == 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1195
                compiler.log.nerrors++;
23810
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
  1196
            return true;
6355
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
  1197
        }
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
  1198
14050
9bfad4b4b6a2 7196464: upgrade JavaCompiler.shouldStopPolicy to accomodate policies in face of error and no error
jjg
parents: 14044
diff changeset
  1199
        compiler.enterTreesIfNeeded(roots);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1200
23810
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
  1201
        return true;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1202
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1203
06bc494ca11e Initial load
duke
parents:
diff changeset
  1204
    private void warnIfUnmatchedOptions() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1205
        if (!unmatchedProcessorOptions.isEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1206
            log.warning("proc.unmatched.processor.options", unmatchedProcessorOptions.toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1207
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1208
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1209
06bc494ca11e Initial load
duke
parents:
diff changeset
  1210
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  1211
     * Free resources related to annotation processing.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1212
     */
6581
f58f0ce45802 6980707: Reduce use of IOException in JavaCompiler
jjg
parents: 6355
diff changeset
  1213
    public void close() {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1214
        filer.close();
3764
f0077f165983 6548708: Annotation processing should free service loader if there are no processors
darcy
parents: 3656
diff changeset
  1215
        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
  1216
            discoveredProcs.close();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1217
        discoveredProcs = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1218
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1219
06bc494ca11e Initial load
duke
parents:
diff changeset
  1220
    private List<ClassSymbol> getTopLevelClasses(List<? extends JCCompilationUnit> units) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1221
        List<ClassSymbol> classes = List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1222
        for (JCCompilationUnit unit : units) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1223
            for (JCTree node : unit.defs) {
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10815
diff changeset
  1224
                if (node.hasTag(JCTree.Tag.CLASSDEF)) {
6355
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
  1225
                    ClassSymbol sym = ((JCClassDecl) node).sym;
8032
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents: 8031
diff changeset
  1226
                    Assert.checkNonNull(sym);
6355
f01ebbf5a5f7 6975005: improve JavacProcessingEnvironment.Round abstraction
jjg
parents: 6162
diff changeset
  1227
                    classes = classes.prepend(sym);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1228
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1229
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1230
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1231
        return classes.reverse();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1232
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1233
4871
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents: 3764
diff changeset
  1234
    private List<ClassSymbol> getTopLevelClassesFromClasses(List<? extends ClassSymbol> syms) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents: 3764
diff changeset
  1235
        List<ClassSymbol> classes = List.nil();
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents: 3764
diff changeset
  1236
        for (ClassSymbol sym : syms) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents: 3764
diff changeset
  1237
            if (!isPkgInfo(sym)) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents: 3764
diff changeset
  1238
                classes = classes.prepend(sym);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents: 3764
diff changeset
  1239
            }
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents: 3764
diff changeset
  1240
        }
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents: 3764
diff changeset
  1241
        return classes.reverse();
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents: 3764
diff changeset
  1242
    }
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents: 3764
diff changeset
  1243
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1244
    private List<PackageSymbol> getPackageInfoFiles(List<? extends JCCompilationUnit> units) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1245
        List<PackageSymbol> packages = List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1246
        for (JCCompilationUnit unit : units) {
4871
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents: 3764
diff changeset
  1247
            if (isPkgInfo(unit.sourcefile, JavaFileObject.Kind.SOURCE)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1248
                packages = packages.prepend(unit.packge);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1249
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1250
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1251
        return packages.reverse();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1252
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1253
4871
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents: 3764
diff changeset
  1254
    private List<PackageSymbol> getPackageInfoFilesFromClasses(List<? extends ClassSymbol> syms) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents: 3764
diff changeset
  1255
        List<PackageSymbol> packages = List.nil();
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents: 3764
diff changeset
  1256
        for (ClassSymbol sym : syms) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents: 3764
diff changeset
  1257
            if (isPkgInfo(sym)) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents: 3764
diff changeset
  1258
                packages = packages.prepend((PackageSymbol) sym.owner);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents: 3764
diff changeset
  1259
            }
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents: 3764
diff changeset
  1260
        }
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents: 3764
diff changeset
  1261
        return packages.reverse();
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents: 3764
diff changeset
  1262
    }
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents: 3764
diff changeset
  1263
6159
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
  1264
    // avoid unchecked warning from use of varargs
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
  1265
    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
  1266
        return list1.appendList(list2);
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
  1267
    }
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6153
diff changeset
  1268
4871
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents: 3764
diff changeset
  1269
    private boolean isPkgInfo(JavaFileObject fo, JavaFileObject.Kind kind) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents: 3764
diff changeset
  1270
        return fo.isNameCompatible("package-info", kind);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents: 3764
diff changeset
  1271
    }
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents: 3764
diff changeset
  1272
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents: 3764
diff changeset
  1273
    private boolean isPkgInfo(ClassSymbol sym) {
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents: 3764
diff changeset
  1274
        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
  1275
    }
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents: 3764
diff changeset
  1276
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1277
    /*
06bc494ca11e Initial load
duke
parents:
diff changeset
  1278
     * Called retroactively to determine if a class loader was required,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1279
     * after we have failed to create one.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1280
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1281
    private boolean needClassLoader(String procNames, Iterable<? extends File> workingpath) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1282
        if (procNames != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1283
            return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1284
06bc494ca11e Initial load
duke
parents:
diff changeset
  1285
        URL[] urls = new URL[1];
06bc494ca11e Initial load
duke
parents:
diff changeset
  1286
        for(File pathElement : workingpath) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1287
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1288
                urls[0] = pathElement.toURI().toURL();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1289
                if (ServiceProxy.hasService(Processor.class, urls))
06bc494ca11e Initial load
duke
parents:
diff changeset
  1290
                    return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1291
            } catch (MalformedURLException ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1292
                throw new AssertionError(ex);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1293
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1294
            catch (ServiceProxy.ServiceConfigurationError e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1295
                log.error("proc.bad.config.file", e.getLocalizedMessage());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1296
                return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1297
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1298
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1299
06bc494ca11e Initial load
duke
parents:
diff changeset
  1300
        return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1301
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1302
23810
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
  1303
    class ImplicitCompleter implements Completer {
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
        private final JCCompilationUnit topLevel;
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
  1306
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
  1307
        public ImplicitCompleter(JCCompilationUnit topLevel) {
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
  1308
            this.topLevel = topLevel;
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
  1309
        }
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
  1310
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
  1311
        @Override public void complete(Symbol sym) throws CompletionFailure {
24604
7f68545b5128 8041422: Split javac ClassReader into ClassReader+ClassFinder
jjg
parents: 24396
diff changeset
  1312
            compiler.readSourceFile(topLevel, (ClassSymbol) sym);
23810
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
  1313
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1314
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1315
23810
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
  1316
    private final TreeScanner treeCleaner = new TreeScanner() {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1317
            public void scan(JCTree node) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1318
                super.scan(node);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1319
                if (node != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1320
                    node.type = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1321
            }
23810
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
  1322
            JCCompilationUnit topLevel;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1323
            public void visitTopLevel(JCCompilationUnit node) {
23810
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
  1324
                if (node.packge != null) {
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
  1325
                    if (node.packge.package_info != null) {
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
  1326
                        node.packge.package_info.reset();
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
  1327
                    }
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
  1328
                    node.packge.reset();
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
  1329
                }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1330
                node.packge = null;
23810
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
  1331
                topLevel = node;
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
  1332
                try {
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
  1333
                    super.visitTopLevel(node);
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
  1334
                } finally {
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
  1335
                    topLevel = null;
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
  1336
                }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1337
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1338
            public void visitClassDef(JCClassDecl node) {
23810
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
  1339
                if (node.sym != null) {
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
  1340
                    node.sym.reset();
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
  1341
                    node.sym.completer = new ImplicitCompleter(topLevel);
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23795
diff changeset
  1342
                }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1343
                node.sym = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1344
                super.visitClassDef(node);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1345
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1346
            public void visitMethodDef(JCMethodDecl node) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1347
                node.sym = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1348
                super.visitMethodDef(node);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1349
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1350
            public void visitVarDef(JCVariableDecl node) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1351
                node.sym = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1352
                super.visitVarDef(node);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1353
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1354
            public void visitNewClass(JCNewClass node) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1355
                node.constructor = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1356
                super.visitNewClass(node);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1357
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1358
            public void visitAssignop(JCAssignOp node) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1359
                node.operator = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1360
                super.visitAssignop(node);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1361
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1362
            public void visitUnary(JCUnary node) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1363
                node.operator = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1364
                super.visitUnary(node);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1365
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1366
            public void visitBinary(JCBinary node) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1367
                node.operator = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1368
                super.visitBinary(node);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1369
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1370
            public void visitSelect(JCFieldAccess node) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1371
                node.sym = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1372
                super.visitSelect(node);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1373
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1374
            public void visitIdent(JCIdent node) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1375
                node.sym = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1376
                super.visitIdent(node);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1377
            }
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 14801
diff changeset
  1378
            public void visitAnnotation(JCAnnotation node) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 14801
diff changeset
  1379
                node.attribute = null;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 14801
diff changeset
  1380
                super.visitAnnotation(node);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 14801
diff changeset
  1381
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1382
        };
06bc494ca11e Initial load
duke
parents:
diff changeset
  1383
06bc494ca11e Initial load
duke
parents:
diff changeset
  1384
06bc494ca11e Initial load
duke
parents:
diff changeset
  1385
    private boolean moreToDo() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1386
        return filer.newFiles();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1387
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1388
06bc494ca11e Initial load
duke
parents:
diff changeset
  1389
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  1390
     * {@inheritdoc}
06bc494ca11e Initial load
duke
parents:
diff changeset
  1391
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  1392
     * Command line options suitable for presenting to annotation
13844
56339cf983a3 7177970: fix issues in langtools doc comments
jjg
parents: 13841
diff changeset
  1393
     * processors.
56339cf983a3 7177970: fix issues in langtools doc comments
jjg
parents: 13841
diff changeset
  1394
     * {@literal "-Afoo=bar"} should be {@literal "-Afoo" => "bar"}.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1395
     */
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
  1396
    @DefinedBy(Api.ANNOTATION_PROCESSING)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1397
    public Map<String,String> getOptions() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1398
        return processorOptions;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1399
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1400
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
  1401
    @DefinedBy(Api.ANNOTATION_PROCESSING)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1402
    public Messager getMessager() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1403
        return messager;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1404
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1405
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
  1406
    @DefinedBy(Api.ANNOTATION_PROCESSING)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1407
    public Filer getFiler() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1408
        return filer;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1409
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1410
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
  1411
    @DefinedBy(Api.ANNOTATION_PROCESSING)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1412
    public JavacElements getElementUtils() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1413
        return elementUtils;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1414
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1415
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
  1416
    @DefinedBy(Api.ANNOTATION_PROCESSING)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1417
    public JavacTypes getTypeUtils() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1418
        return typeUtils;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1419
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1420
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
  1421
    @DefinedBy(Api.ANNOTATION_PROCESSING)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1422
    public SourceVersion getSourceVersion() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1423
        return Source.toSourceVersion(source);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1424
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1425
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
  1426
    @DefinedBy(Api.ANNOTATION_PROCESSING)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1427
    public Locale getLocale() {
1471
57506cdfb7b4 6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents: 1260
diff changeset
  1428
        return messages.getCurrentLocale();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1429
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1430
06bc494ca11e Initial load
duke
parents:
diff changeset
  1431
    public Set<Symbol.PackageSymbol> getSpecifiedPackages() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1432
        return specifiedPackages;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1433
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1434
4937
2fc03fb01efa 6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents: 4934
diff changeset
  1435
    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
  1436
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1437
    /**
4937
2fc03fb01efa 6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents: 4934
diff changeset
  1438
     * Convert import-style string for supported annotations into a
2fc03fb01efa 6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents: 4934
diff changeset
  1439
     * regex matching that string.  If the string is a valid
2fc03fb01efa 6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents: 4934
diff changeset
  1440
     * import-style string, return a regex that won't match anything.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1441
     */
4937
2fc03fb01efa 6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents: 4934
diff changeset
  1442
    private static Pattern importStringToPattern(String s, Processor p, Log log) {
29427
44f4e6905b67 8071851: Provide filtering of doclint checking based on packages
jlahoda
parents: 27224
diff changeset
  1443
        if (MatchingUtils.isValidImportString(s)) {
44f4e6905b67 8071851: Provide filtering of doclint checking based on packages
jlahoda
parents: 27224
diff changeset
  1444
            return MatchingUtils.validImportStringToPattern(s);
4937
2fc03fb01efa 6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents: 4934
diff changeset
  1445
        } else {
2fc03fb01efa 6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents: 4934
diff changeset
  1446
            log.warning("proc.malformed.supported.string", s, p.getClass().getName());
2fc03fb01efa 6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents: 4934
diff changeset
  1447
            return noMatches; // won't match any valid identifier
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1448
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1449
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1450
06bc494ca11e Initial load
duke
parents:
diff changeset
  1451
    /**
14548
aa687b312c97 8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents: 14538
diff changeset
  1452
     * For internal use only.  This method may be removed without warning.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1453
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1454
    public Context getContext() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1455
        return context;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1456
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1457
14548
aa687b312c97 8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents: 14538
diff changeset
  1458
    /**
aa687b312c97 8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents: 14538
diff changeset
  1459
     * 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
  1460
     */
aa687b312c97 8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents: 14538
diff changeset
  1461
    public ClassLoader getProcessorClassLoader() {
aa687b312c97 8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents: 14538
diff changeset
  1462
        return processorClassLoader;
aa687b312c97 8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents: 14538
diff changeset
  1463
    }
aa687b312c97 8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents: 14538
diff changeset
  1464
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1465
    public String toString() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1466
        return "javac ProcessingEnvironment";
06bc494ca11e Initial load
duke
parents:
diff changeset
  1467
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1468
06bc494ca11e Initial load
duke
parents:
diff changeset
  1469
    public static boolean isValidOptionName(String optionName) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1470
        for(String s : optionName.split("\\.", -1)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1471
            if (!SourceVersion.isIdentifier(s))
06bc494ca11e Initial load
duke
parents:
diff changeset
  1472
                return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1473
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1474
        return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1475
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1476
}