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