Path: blob/master/src/java.compiler/share/classes/javax/annotation/processing/AbstractProcessor.java
41159 views
/*1* Copyright (c) 2005, 2020, 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.List;28import java.util.Set;29import java.util.HashSet;30import java.util.Collections;31import java.util.Objects;32import javax.lang.model.element.*;33import javax.lang.model.SourceVersion;34import javax.tools.Diagnostic;3536/**37* An abstract annotation processor designed to be a convenient38* superclass for most concrete annotation processors. This class39* examines annotation values to compute the {@linkplain40* #getSupportedOptions options}, {@linkplain41* #getSupportedAnnotationTypes annotation interfaces}, and42* {@linkplain #getSupportedSourceVersion source version} supported by43* its subtypes.44*45* <p>The getter methods may {@linkplain Messager#printMessage issue46* warnings} about noteworthy conditions using the facilities available47* after the processor has been {@linkplain #isInitialized48* initialized}.49*50* <p>Subclasses are free to override the implementation and51* specification of any of the methods in this class as long as the52* general {@link javax.annotation.processing.Processor Processor}53* contract for that method is obeyed.54*55* @author Joseph D. Darcy56* @author Scott Seligman57* @author Peter von der Ahé58* @since 1.659*/60public abstract class AbstractProcessor implements Processor {61/**62* Processing environment providing by the tool framework.63*/64protected ProcessingEnvironment processingEnv;65private boolean initialized = false;6667/**68* Constructor for subclasses to call.69*/70protected AbstractProcessor() {}7172/**73* If the processor class is annotated with {@link74* SupportedOptions}, return an unmodifiable set with the same set75* of strings as the annotation. If the class is not so76* annotated, an empty set is returned.77*78* @return the options recognized by this processor, or an empty79* set if none80*/81public Set<String> getSupportedOptions() {82SupportedOptions so = this.getClass().getAnnotation(SupportedOptions.class);83return (so == null) ?84Set.of() :85arrayToSet(so.value(), false, "option value", "@SupportedOptions");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* If the {@linkplain ProcessingEnvironment#getSourceVersion source95* version} does not support modules, in other words if it is less96* than or equal to {@link SourceVersion#RELEASE_8 RELEASE_8},97* then any leading {@linkplain Processor#getSupportedAnnotationTypes98* module prefixes} are stripped from the names.99*100* @return the names of the annotation interfaces supported by101* this processor, or an empty set if none102*/103public Set<String> getSupportedAnnotationTypes() {104SupportedAnnotationTypes sat = this.getClass().getAnnotation(SupportedAnnotationTypes.class);105boolean initialized = isInitialized();106if (sat == null) {107if (initialized)108processingEnv.getMessager().printMessage(Diagnostic.Kind.WARNING,109"No SupportedAnnotationTypes annotation " +110"found on " + this.getClass().getName() +111", returning an empty set.");112return Set.of();113} else {114boolean stripModulePrefixes =115initialized &&116processingEnv.getSourceVersion().compareTo(SourceVersion.RELEASE_8) <= 0;117return arrayToSet(sat.value(), stripModulePrefixes,118"annotation type", "@SupportedAnnotationTypes");119}120}121122/**123* If the processor class is annotated with {@link124* SupportedSourceVersion}, return the source version in the125* annotation. If the class is not so annotated, {@link126* SourceVersion#RELEASE_6} is returned.127*128* @return the latest source version supported by this processor129*/130public SourceVersion getSupportedSourceVersion() {131SupportedSourceVersion ssv = this.getClass().getAnnotation(SupportedSourceVersion.class);132SourceVersion sv = null;133if (ssv == null) {134sv = SourceVersion.RELEASE_6;135if (isInitialized())136processingEnv.getMessager().printMessage(Diagnostic.Kind.WARNING,137"No SupportedSourceVersion annotation " +138"found on " + this.getClass().getName() +139", returning " + sv + ".");140} else141sv = ssv.value();142return sv;143}144145146/**147* Initializes the processor with the processing environment by148* setting the {@code processingEnv} field to the value of the149* {@code processingEnv} argument. An {@code150* IllegalStateException} will be thrown if this method is called151* more than once on the same object.152*153* @param processingEnv environment to access facilities the tool framework154* provides to the processor155* @throws IllegalStateException if this method is called more than once.156*/157public synchronized void init(ProcessingEnvironment processingEnv) {158if (initialized)159throw new IllegalStateException("Cannot call init more than once.");160Objects.requireNonNull(processingEnv, "Tool provided null ProcessingEnvironment");161162this.processingEnv = processingEnv;163initialized = true;164}165166/**167* {@inheritDoc}168*/169public abstract boolean process(Set<? extends TypeElement> annotations,170RoundEnvironment roundEnv);171172/**173* {@return an empty iterable of completions}174*175* @param element {@inheritDoc}176* @param annotation {@inheritDoc}177* @param member {@inheritDoc}178* @param userText {@inheritDoc}179*/180public Iterable<? extends Completion> getCompletions(Element element,181AnnotationMirror annotation,182ExecutableElement member,183String userText) {184return List.of();185}186187/**188* {@return {@code true} if this object has been {@linkplain #init189* initialized}, {@code false} otherwise}190*/191protected synchronized boolean isInitialized() {192return initialized;193}194195private Set<String> arrayToSet(String[] array,196boolean stripModulePrefixes,197String contentType,198String annotationName) {199assert array != null;200Set<String> set = new HashSet<>();201for (String s : array) {202boolean stripped = false;203if (stripModulePrefixes) {204int index = s.indexOf('/');205if (index != -1) {206s = s.substring(index + 1);207stripped = true;208}209}210boolean added = set.add(s);211// Don't issue a duplicate warning when the module name is212// stripped off to avoid spurious warnings in a case like213// "foo/a.B", "bar/a.B".214if (!added && !stripped && isInitialized() ) {215processingEnv.getMessager().printMessage(Diagnostic.Kind.WARNING,216"Duplicate " + contentType +217" ``" + s + "'' for processor " +218this.getClass().getName() +219" in its " + annotationName +220"annotation.");221}222}223return Collections.unmodifiableSet(set);224}225}226227228