src/share/classes/javax/annotation/processing/AbstractProcessor.java

Wed, 27 Apr 2016 01:34:52 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:34:52 +0800
changeset 0
959103a6100f
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/
changeset: 2573:53ca196be1ae
tag: jdk8u25-b17

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

mercurial