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