duke@1: /*
duke@1: * Copyright 2005-2006 Sun Microsystems, Inc. All Rights Reserved.
duke@1: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1: *
duke@1: * This code is free software; you can redistribute it and/or modify it
duke@1: * under the terms of the GNU General Public License version 2 only, as
duke@1: * published by the Free Software Foundation. Sun designates this
duke@1: * particular file as subject to the "Classpath" exception as provided
duke@1: * by Sun in the LICENSE file that accompanied this code.
duke@1: *
duke@1: * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1: * version 2 for more details (a copy is included in the LICENSE file that
duke@1: * accompanied this code).
duke@1: *
duke@1: * You should have received a copy of the GNU General Public License version
duke@1: * 2 along with this work; if not, write to the Free Software Foundation,
duke@1: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1: *
duke@1: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@1: * CA 95054 USA or visit www.sun.com if you need additional information or
duke@1: * have any questions.
duke@1: */
duke@1:
duke@1: package javax.annotation.processing;
duke@1:
duke@1: import java.util.Set;
duke@1: import java.util.HashSet;
duke@1: import java.util.Collections;
duke@1: import javax.lang.model.element.*;
duke@1: import javax.lang.model.SourceVersion;
duke@1: import javax.tools.Diagnostic;
duke@1:
duke@1: /**
duke@1: * An abstract annotation processor designed to be a convenient
duke@1: * superclass for most concrete annotation processors. This class
duke@1: * examines annotation values to compute the {@linkplain
duke@1: * #getSupportedOptions options}, {@linkplain
duke@1: * #getSupportedAnnotationTypes annotations}, and {@linkplain
duke@1: * #getSupportedSourceVersion source version} supported by its
duke@1: * subtypes.
duke@1: *
duke@1: *
The getter methods may {@linkplain Messager#printMessage issue
duke@1: * warnings} about noteworthy conditions using the facilities available
duke@1: * after the processor has been {@linkplain #isInitialized
duke@1: * initialized}.
duke@1: *
duke@1: *
Subclasses are free to override the implementation and
duke@1: * specification of any of the methods in this class as long as the
duke@1: * general {@link javax.annotation.processing.Processor Processor}
duke@1: * contract for that method is obeyed.
duke@1: *
duke@1: * @author Joseph D. Darcy
duke@1: * @author Scott Seligman
duke@1: * @author Peter von der Ahé
duke@1: * @since 1.6
duke@1: */
duke@1: public abstract class AbstractProcessor implements Processor {
duke@1: /**
duke@1: * Processing environment providing by the tool framework.
duke@1: */
duke@1: protected ProcessingEnvironment processingEnv;
duke@1: private boolean initialized = false;
duke@1:
duke@1: /**
duke@1: * Constructor for subclasses to call.
duke@1: */
duke@1: protected AbstractProcessor() {}
duke@1:
duke@1: /**
duke@1: * If the processor class is annotated with {@link
duke@1: * SupportedOptions}, return an unmodifiable set with the same set
duke@1: * of strings as the annotation. If the class is not so
duke@1: * annotated, an empty set is returned.
duke@1: *
duke@1: * @return the options recognized by this processor, or an empty
duke@1: * set if none
duke@1: */
duke@1: public Set getSupportedOptions() {
duke@1: SupportedOptions so = this.getClass().getAnnotation(SupportedOptions.class);
duke@1: if (so == null)
duke@1: return Collections.emptySet();
duke@1: else
duke@1: return arrayToSet(so.value());
duke@1: }
duke@1:
duke@1: /**
duke@1: * If the processor class is annotated with {@link
duke@1: * SupportedAnnotationTypes}, return an unmodifiable set with the
duke@1: * same set of strings as the annotation. If the class is not so
duke@1: * annotated, an empty set is returned.
duke@1: *
duke@1: * @return the names of the annotation types supported by this
duke@1: * processor, or an empty set if none
duke@1: */
duke@1: public Set getSupportedAnnotationTypes() {
duke@1: SupportedAnnotationTypes sat = this.getClass().getAnnotation(SupportedAnnotationTypes.class);
duke@1: if (sat == null) {
duke@1: if (isInitialized())
duke@1: processingEnv.getMessager().printMessage(Diagnostic.Kind.WARNING,
duke@1: "No SupportedAnnotationTypes annotation " +
duke@1: "found on " + this.getClass().getName() +
duke@1: ", returning an empty set.");
duke@1: return Collections.emptySet();
duke@1: }
duke@1: else
duke@1: return arrayToSet(sat.value());
duke@1: }
duke@1:
duke@1: /**
duke@1: * If the processor class is annotated with {@link
duke@1: * SupportedSourceVersion}, return the source version in the
duke@1: * annotation. If the class is not so annotated, {@link
duke@1: * SourceVersion#RELEASE_6} is returned.
duke@1: *
duke@1: * @return the latest source version supported by this processor
duke@1: */
duke@1: public SourceVersion getSupportedSourceVersion() {
duke@1: SupportedSourceVersion ssv = this.getClass().getAnnotation(SupportedSourceVersion.class);
duke@1: SourceVersion sv = null;
duke@1: if (ssv == null) {
duke@1: sv = SourceVersion.RELEASE_6;
duke@1: if (isInitialized())
duke@1: processingEnv.getMessager().printMessage(Diagnostic.Kind.WARNING,
duke@1: "No SupportedSourceVersion annotation " +
duke@1: "found on " + this.getClass().getName() +
duke@1: ", returning " + sv + ".");
duke@1: } else
duke@1: sv = ssv.value();
duke@1: return sv;
duke@1: }
duke@1:
duke@1:
duke@1: /**
duke@1: * Initializes the processor with the processing environment by
duke@1: * setting the {@code processingEnv} field to the value of the
duke@1: * {@code processingEnv} argument. An {@code
duke@1: * IllegalStateException} will be thrown if this method is called
duke@1: * more than once on the same object.
duke@1: *
duke@1: * @param processingEnv environment to access facilities the tool framework
duke@1: * provides to the processor
duke@1: * @throws IllegalStateException if this method is called more than once.
duke@1: */
duke@1: public synchronized void init(ProcessingEnvironment processingEnv) {
duke@1: if (initialized)
duke@1: throw new IllegalStateException("Cannot call init more than once.");
duke@1: if (processingEnv == null)
duke@1: throw new NullPointerException("Tool provided null ProcessingEnvironment");
duke@1:
duke@1: this.processingEnv = processingEnv;
duke@1: initialized = true;
duke@1: }
duke@1:
duke@1: /**
duke@1: * {@inheritDoc}
duke@1: */
duke@1: public abstract boolean process(Set extends TypeElement> annotations,
duke@1: RoundEnvironment roundEnv);
duke@1:
duke@1: /**
duke@1: * Returns an empty iterable of completions.
duke@1: *
duke@1: * @param element {@inheritDoc}
duke@1: * @param annotation {@inheritDoc}
duke@1: * @param member {@inheritDoc}
duke@1: * @param userText {@inheritDoc}
duke@1: */
duke@1: public Iterable extends Completion> getCompletions(Element element,
duke@1: AnnotationMirror annotation,
duke@1: ExecutableElement member,
duke@1: String userText) {
duke@1: return Collections.emptyList();
duke@1: }
duke@1:
duke@1: /**
duke@1: * Returns {@code true} if this object has been {@linkplain #init
duke@1: * initialized}, {@code false} otherwise.
duke@1: *
duke@1: * @return {@code true} if this object has been initialized,
duke@1: * {@code false} otherwise.
duke@1: */
duke@1: protected synchronized boolean isInitialized() {
duke@1: return initialized;
duke@1: }
duke@1:
duke@1: private static Set arrayToSet(String[] array) {
duke@1: assert array != null;
duke@1: Set set = new HashSet(array.length);
duke@1: for (String s : array)
duke@1: set.add(s);
duke@1: return Collections.unmodifiableSet(set);
duke@1: }
duke@1: }