Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/src/share/classes/javax/annotation/processing/AbstractProcessor.java
38911 views
/*1* Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.annotation.processing;2627import java.util.Set;28import java.util.HashSet;29import java.util.Collections;30import java.util.Objects;31import javax.lang.model.element.*;32import javax.lang.model.SourceVersion;33import javax.tools.Diagnostic;3435/**36* An abstract annotation processor designed to be a convenient37* superclass for most concrete annotation processors. This class38* examines annotation values to compute the {@linkplain39* #getSupportedOptions options}, {@linkplain40* #getSupportedAnnotationTypes annotation types}, and {@linkplain41* #getSupportedSourceVersion source version} supported by its42* subtypes.43*44* <p>The getter methods may {@linkplain Messager#printMessage issue45* warnings} about noteworthy conditions using the facilities available46* after the processor has been {@linkplain #isInitialized47* initialized}.48*49* <p>Subclasses are free to override the implementation and50* specification of any of the methods in this class as long as the51* general {@link javax.annotation.processing.Processor Processor}52* contract for that method is obeyed.53*54* @author Joseph D. Darcy55* @author Scott Seligman56* @author Peter von der Ahé57* @since 1.658*/59public abstract class AbstractProcessor implements Processor {60/**61* Processing environment providing by the tool framework.62*/63protected ProcessingEnvironment processingEnv;64private boolean initialized = false;6566/**67* Constructor for subclasses to call.68*/69protected AbstractProcessor() {}7071/**72* If the processor class is annotated with {@link73* SupportedOptions}, return an unmodifiable set with the same set74* of strings as the annotation. If the class is not so75* annotated, an empty set is returned.76*77* @return the options recognized by this processor, or an empty78* set if none79*/80public Set<String> getSupportedOptions() {81SupportedOptions so = this.getClass().getAnnotation(SupportedOptions.class);82if (so == null)83return Collections.emptySet();84else85return arrayToSet(so.value());86}8788/**89* If the processor class is annotated with {@link90* SupportedAnnotationTypes}, return an unmodifiable set with the91* same set of strings as the annotation. If the class is not so92* annotated, an empty set is returned.93*94* @return the names of the annotation types supported by this95* processor, or an empty set if none96*/97public Set<String> getSupportedAnnotationTypes() {98SupportedAnnotationTypes sat = this.getClass().getAnnotation(SupportedAnnotationTypes.class);99if (sat == null) {100if (isInitialized())101processingEnv.getMessager().printMessage(Diagnostic.Kind.WARNING,102"No SupportedAnnotationTypes annotation " +103"found on " + this.getClass().getName() +104", returning an empty set.");105return Collections.emptySet();106}107else108return arrayToSet(sat.value());109}110111/**112* If the processor class is annotated with {@link113* SupportedSourceVersion}, return the source version in the114* annotation. If the class is not so annotated, {@link115* SourceVersion#RELEASE_6} is returned.116*117* @return the latest source version supported by this processor118*/119public SourceVersion getSupportedSourceVersion() {120SupportedSourceVersion ssv = this.getClass().getAnnotation(SupportedSourceVersion.class);121SourceVersion sv = null;122if (ssv == null) {123sv = SourceVersion.RELEASE_6;124if (isInitialized())125processingEnv.getMessager().printMessage(Diagnostic.Kind.WARNING,126"No SupportedSourceVersion annotation " +127"found on " + this.getClass().getName() +128", returning " + sv + ".");129} else130sv = ssv.value();131return sv;132}133134135/**136* Initializes the processor with the processing environment by137* setting the {@code processingEnv} field to the value of the138* {@code processingEnv} argument. An {@code139* IllegalStateException} will be thrown if this method is called140* more than once on the same object.141*142* @param processingEnv environment to access facilities the tool framework143* provides to the processor144* @throws IllegalStateException if this method is called more than once.145*/146public synchronized void init(ProcessingEnvironment processingEnv) {147if (initialized)148throw new IllegalStateException("Cannot call init more than once.");149Objects.requireNonNull(processingEnv, "Tool provided null ProcessingEnvironment");150151this.processingEnv = processingEnv;152initialized = true;153}154155/**156* {@inheritDoc}157*/158public abstract boolean process(Set<? extends TypeElement> annotations,159RoundEnvironment roundEnv);160161/**162* Returns an empty iterable of completions.163*164* @param element {@inheritDoc}165* @param annotation {@inheritDoc}166* @param member {@inheritDoc}167* @param userText {@inheritDoc}168*/169public Iterable<? extends Completion> getCompletions(Element element,170AnnotationMirror annotation,171ExecutableElement member,172String userText) {173return Collections.emptyList();174}175176/**177* Returns {@code true} if this object has been {@linkplain #init178* initialized}, {@code false} otherwise.179*180* @return {@code true} if this object has been initialized,181* {@code false} otherwise.182*/183protected synchronized boolean isInitialized() {184return initialized;185}186187private static Set<String> arrayToSet(String[] array) {188assert array != null;189Set<String> set = new HashSet<String>(array.length);190for (String s : array)191set.add(s);192return Collections.unmodifiableSet(set);193}194}195196197