langtools/src/share/classes/javax/annotation/processing/AbstractProcessor.java
author briangoetz
Wed, 18 Dec 2013 16:05:18 -0500
changeset 22163 3651128c74eb
parent 18669 99572d59c916
permissions -rw-r--r--
8030244: Update langtools to use Diamond Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
16795
04f5c8373776 7041251: Use j.u.Objects utility methods in langtools
darcy
parents: 5520
diff changeset
     2
 * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    20
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
    23
 * questions.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
package javax.annotation.processing;
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
import java.util.Set;
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
import java.util.HashSet;
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import java.util.Collections;
16795
04f5c8373776 7041251: Use j.u.Objects utility methods in langtools
darcy
parents: 5520
diff changeset
    31
import java.util.Objects;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
import javax.lang.model.element.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
import javax.lang.model.SourceVersion;
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
import javax.tools.Diagnostic;
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
 * An abstract annotation processor designed to be a convenient
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
 * superclass for most concrete annotation processors.  This class
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
 * examines annotation values to compute the {@linkplain
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
 * #getSupportedOptions options}, {@linkplain
18669
99572d59c916 7162089: Add support for repeating annotations to javax.annotation.processing
darcy
parents: 16795
diff changeset
    41
 * #getSupportedAnnotationTypes annotation types}, and {@linkplain
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
 * #getSupportedSourceVersion source version} supported by its
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
 * subtypes.
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
 * <p>The getter methods may {@linkplain Messager#printMessage issue
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
 * warnings} about noteworthy conditions using the facilities available
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
 * after the processor has been {@linkplain #isInitialized
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
 * initialized}.
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
 * <p>Subclasses are free to override the implementation and
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
 * specification of any of the methods in this class as long as the
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
 * general {@link javax.annotation.processing.Processor Processor}
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
 * contract for that method is obeyed.
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
 * @author Joseph D. Darcy
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
 * @author Scott Seligman
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
 * @author Peter von der Ah&eacute;
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
 * @since 1.6
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
public abstract class AbstractProcessor implements Processor {
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
     * Processing environment providing by the tool framework.
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
    protected ProcessingEnvironment processingEnv;
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
    private boolean initialized = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
     * Constructor for subclasses to call.
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
    protected AbstractProcessor() {}
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
     * If the processor class is annotated with {@link
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
     * SupportedOptions}, return an unmodifiable set with the same set
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
     * of strings as the annotation.  If the class is not so
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
     * annotated, an empty set is returned.
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
     * @return the options recognized by this processor, or an empty
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
     * set if none
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
    public Set<String> getSupportedOptions() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
        SupportedOptions so = this.getClass().getAnnotation(SupportedOptions.class);
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
        if  (so == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
            return Collections.emptySet();
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
        else
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
            return arrayToSet(so.value());
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
     * If the processor class is annotated with {@link
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
     * SupportedAnnotationTypes}, return an unmodifiable set with the
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
     * same set of strings as the annotation.  If the class is not so
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
     * annotated, an empty set is returned.
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
     * @return the names of the annotation types supported by this
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
     * processor, or an empty set if none
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
    public Set<String> getSupportedAnnotationTypes() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
            SupportedAnnotationTypes sat = this.getClass().getAnnotation(SupportedAnnotationTypes.class);
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
            if  (sat == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
                if (isInitialized())
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
                    processingEnv.getMessager().printMessage(Diagnostic.Kind.WARNING,
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
                                                             "No SupportedAnnotationTypes annotation " +
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
                                                             "found on " + this.getClass().getName() +
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
                                                             ", returning an empty set.");
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
                return Collections.emptySet();
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
            else
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
                return arrayToSet(sat.value());
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
     * If the processor class is annotated with {@link
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
     * SupportedSourceVersion}, return the source version in the
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
     * annotation.  If the class is not so annotated, {@link
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
     * SourceVersion#RELEASE_6} is returned.
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
     * @return the latest source version supported by this processor
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
    public SourceVersion getSupportedSourceVersion() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
        SupportedSourceVersion ssv = this.getClass().getAnnotation(SupportedSourceVersion.class);
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
        SourceVersion sv = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
        if (ssv == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
            sv = SourceVersion.RELEASE_6;
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
            if (isInitialized())
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
                processingEnv.getMessager().printMessage(Diagnostic.Kind.WARNING,
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
                                                         "No SupportedSourceVersion annotation " +
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
                                                         "found on " + this.getClass().getName() +
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
                                                         ", returning " + sv + ".");
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
        } else
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
            sv = ssv.value();
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
        return sv;
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
     * Initializes the processor with the processing environment by
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
     * setting the {@code processingEnv} field to the value of the
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
     * {@code processingEnv} argument.  An {@code
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
     * IllegalStateException} will be thrown if this method is called
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
     * more than once on the same object.
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
     * @param processingEnv environment to access facilities the tool framework
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
     * provides to the processor
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
     * @throws IllegalStateException if this method is called more than once.
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
    public synchronized void init(ProcessingEnvironment processingEnv) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
        if (initialized)
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
            throw new IllegalStateException("Cannot call init more than once.");
16795
04f5c8373776 7041251: Use j.u.Objects utility methods in langtools
darcy
parents: 5520
diff changeset
   150
        Objects.requireNonNull(processingEnv, "Tool provided null ProcessingEnvironment");
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
        this.processingEnv = processingEnv;
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
        initialized = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
     * {@inheritDoc}
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
    public abstract boolean process(Set<? extends TypeElement> annotations,
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
                                    RoundEnvironment roundEnv);
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
     * Returns an empty iterable of completions.
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
     * @param element {@inheritDoc}
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
     * @param annotation {@inheritDoc}
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
     * @param member {@inheritDoc}
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
     * @param userText {@inheritDoc}
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
    public Iterable<? extends Completion> getCompletions(Element element,
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
                                                         AnnotationMirror annotation,
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
                                                         ExecutableElement member,
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
                                                         String userText) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
        return Collections.emptyList();
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
     * Returns {@code true} if this object has been {@linkplain #init
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
     * initialized}, {@code false} otherwise.
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
     * @return {@code true} if this object has been initialized,
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
     * {@code false} otherwise.
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
    protected synchronized boolean isInitialized() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
        return initialized;
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
    private static Set<String> arrayToSet(String[] array) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
        assert array != null;
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 18669
diff changeset
   190
        Set<String> set = new HashSet<>(array.length);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
        for (String s : array)
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
            set.add(s);
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
        return Collections.unmodifiableSet(set);
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
}