001 /*
002 * Copyright 2005-2006 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package javax.annotation.processing;
027
028 import java.util.Set;
029 import java.util.HashSet;
030 import java.util.Collections;
031 import javax.lang.model.element.*;
032 import javax.lang.model.SourceVersion;
033 import javax.tools.Diagnostic;
034
035 /**
036 * An abstract annotation processor designed to be a convenient
037 * superclass for most concrete annotation processors. This class
038 * examines annotation values to compute the {@linkplain
039 * #getSupportedOptions options}, {@linkplain
040 * #getSupportedAnnotationTypes annotations}, and {@linkplain
041 * #getSupportedSourceVersion source version} supported by its
042 * subtypes.
043 *
044 * <p>The getter methods may {@linkplain Messager#printMessage issue
045 * warnings} about noteworthy conditions using the facilities available
046 * after the processor has been {@linkplain #isInitialized
047 * initialized}.
048 *
049 * <p>Subclasses are free to override the implementation and
050 * specification of any of the methods in this class as long as the
051 * general {@link javax.annotation.processing.Processor Processor}
052 * contract for that method is obeyed.
053 *
054 * @author Joseph D. Darcy
055 * @author Scott Seligman
056 * @author Peter von der Ahé
057 * @since 1.6
058 */
059 public abstract class AbstractProcessor implements Processor {
060 /**
061 * Processing environment providing by the tool framework.
062 */
063 protected ProcessingEnvironment processingEnv;
064 private boolean initialized = false;
065
066 /**
067 * Constructor for subclasses to call.
068 */
069 protected AbstractProcessor() {}
070
071 /**
072 * If the processor class is annotated with {@link
073 * SupportedOptions}, return an unmodifiable set with the same set
074 * of strings as the annotation. If the class is not so
075 * annotated, an empty set is returned.
076 *
077 * @return the options recognized by this processor, or an empty
078 * set if none
079 */
080 public Set<String> getSupportedOptions() {
081 SupportedOptions so = this.getClass().getAnnotation(SupportedOptions.class);
082 if (so == null)
083 return Collections.emptySet();
084 else
085 return arrayToSet(so.value());
086 }
087
088 /**
089 * If the processor class is annotated with {@link
090 * SupportedAnnotationTypes}, return an unmodifiable set with the
091 * same set of strings as the annotation. If the class is not so
092 * annotated, an empty set is returned.
093 *
094 * @return the names of the annotation types supported by this
095 * processor, or an empty set if none
096 */
097 public Set<String> getSupportedAnnotationTypes() {
098 SupportedAnnotationTypes sat = this.getClass().getAnnotation(SupportedAnnotationTypes.class);
099 if (sat == null) {
100 if (isInitialized())
101 processingEnv.getMessager().printMessage(Diagnostic.Kind.WARNING,
102 "No SupportedAnnotationTypes annotation " +
103 "found on " + this.getClass().getName() +
104 ", returning an empty set.");
105 return Collections.emptySet();
106 }
107 else
108 return arrayToSet(sat.value());
109 }
110
111 /**
112 * If the processor class is annotated with {@link
113 * SupportedSourceVersion}, return the source version in the
114 * annotation. If the class is not so annotated, {@link
115 * SourceVersion#RELEASE_6} is returned.
116 *
117 * @return the latest source version supported by this processor
118 */
119 public SourceVersion getSupportedSourceVersion() {
120 SupportedSourceVersion ssv = this.getClass().getAnnotation(SupportedSourceVersion.class);
121 SourceVersion sv = null;
122 if (ssv == null) {
123 sv = SourceVersion.RELEASE_6;
124 if (isInitialized())
125 processingEnv.getMessager().printMessage(Diagnostic.Kind.WARNING,
126 "No SupportedSourceVersion annotation " +
127 "found on " + this.getClass().getName() +
128 ", returning " + sv + ".");
129 } else
130 sv = ssv.value();
131 return sv;
132 }
133
134
135 /**
136 * Initializes the processor with the processing environment by
137 * setting the {@code processingEnv} field to the value of the
138 * {@code processingEnv} argument. An {@code
139 * IllegalStateException} will be thrown if this method is called
140 * more than once on the same object.
141 *
142 * @param processingEnv environment to access facilities the tool framework
143 * provides to the processor
144 * @throws IllegalStateException if this method is called more than once.
145 */
146 public synchronized void init(ProcessingEnvironment processingEnv) {
147 if (initialized)
148 throw new IllegalStateException("Cannot call init more than once.");
149 if (processingEnv == null)
150 throw new NullPointerException("Tool provided null ProcessingEnvironment");
151
152 this.processingEnv = processingEnv;
153 initialized = true;
154 }
155
156 /**
157 * {@inheritDoc}
158 */
159 public abstract boolean process(Set<? extends TypeElement> annotations,
160 RoundEnvironment roundEnv);
161
162 /**
163 * Returns an empty iterable of completions.
164 *
165 * @param element {@inheritDoc}
166 * @param annotation {@inheritDoc}
167 * @param member {@inheritDoc}
168 * @param userText {@inheritDoc}
169 */
170 public Iterable<? extends Completion> getCompletions(Element element,
171 AnnotationMirror annotation,
172 ExecutableElement member,
173 String userText) {
174 return Collections.emptyList();
175 }
176
177 /**
178 * Returns {@code true} if this object has been {@linkplain #init
179 * initialized}, {@code false} otherwise.
180 *
181 * @return {@code true} if this object has been initialized,
182 * {@code false} otherwise.
183 */
184 protected synchronized boolean isInitialized() {
185 return initialized;
186 }
187
188 private static Set<String> arrayToSet(String[] array) {
189 assert array != null;
190 Set<String> set = new HashSet<String>(array.length);
191 for (String s : array)
192 set.add(s);
193 return Collections.unmodifiableSet(set);
194 }
195 }