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