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

Sat, 01 Dec 2007 00:00:00 +0000

author
duke
date
Sat, 01 Dec 2007 00:00:00 +0000
changeset 1
9a66ca7c79fa
child 554
9d9f26857129
permissions
-rw-r--r--

Initial load

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

mercurial