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

Tue, 26 Mar 2013 17:17:14 -0700

author
darcy
date
Tue, 26 Mar 2013 17:17:14 -0700
changeset 1664
330b35b27e68
parent 554
9d9f26857129
child 1876
1908e86ee49a
permissions
-rw-r--r--

7041251: Use j.u.Objects utility methods in langtools
Reviewed-by: jjg

duke@1 1 /*
darcy@1664 2 * Copyright (c) 2005, 2013, Oracle and/or its affiliates. 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
ohair@554 7 * published by the Free Software Foundation. Oracle designates this
duke@1 8 * particular file as subject to the "Classpath" exception as provided
ohair@554 9 * by Oracle 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 *
ohair@554 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554 22 * or visit www.oracle.com if you need additional information or have any
ohair@554 23 * 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;
darcy@1664 31 import java.util.Objects;
duke@1 32 import javax.lang.model.element.*;
duke@1 33 import javax.lang.model.SourceVersion;
duke@1 34 import javax.tools.Diagnostic;
duke@1 35
duke@1 36 /**
duke@1 37 * An abstract annotation processor designed to be a convenient
duke@1 38 * superclass for most concrete annotation processors. This class
duke@1 39 * examines annotation values to compute the {@linkplain
duke@1 40 * #getSupportedOptions options}, {@linkplain
duke@1 41 * #getSupportedAnnotationTypes annotations}, and {@linkplain
duke@1 42 * #getSupportedSourceVersion source version} supported by its
duke@1 43 * subtypes.
duke@1 44 *
duke@1 45 * <p>The getter methods may {@linkplain Messager#printMessage issue
duke@1 46 * warnings} about noteworthy conditions using the facilities available
duke@1 47 * after the processor has been {@linkplain #isInitialized
duke@1 48 * initialized}.
duke@1 49 *
duke@1 50 * <p>Subclasses are free to override the implementation and
duke@1 51 * specification of any of the methods in this class as long as the
duke@1 52 * general {@link javax.annotation.processing.Processor Processor}
duke@1 53 * contract for that method is obeyed.
duke@1 54 *
duke@1 55 * @author Joseph D. Darcy
duke@1 56 * @author Scott Seligman
duke@1 57 * @author Peter von der Ah&eacute;
duke@1 58 * @since 1.6
duke@1 59 */
duke@1 60 public abstract class AbstractProcessor implements Processor {
duke@1 61 /**
duke@1 62 * Processing environment providing by the tool framework.
duke@1 63 */
duke@1 64 protected ProcessingEnvironment processingEnv;
duke@1 65 private boolean initialized = false;
duke@1 66
duke@1 67 /**
duke@1 68 * Constructor for subclasses to call.
duke@1 69 */
duke@1 70 protected AbstractProcessor() {}
duke@1 71
duke@1 72 /**
duke@1 73 * If the processor class is annotated with {@link
duke@1 74 * SupportedOptions}, return an unmodifiable set with the same set
duke@1 75 * of strings as the annotation. If the class is not so
duke@1 76 * annotated, an empty set is returned.
duke@1 77 *
duke@1 78 * @return the options recognized by this processor, or an empty
duke@1 79 * set if none
duke@1 80 */
duke@1 81 public Set<String> getSupportedOptions() {
duke@1 82 SupportedOptions so = this.getClass().getAnnotation(SupportedOptions.class);
duke@1 83 if (so == null)
duke@1 84 return Collections.emptySet();
duke@1 85 else
duke@1 86 return arrayToSet(so.value());
duke@1 87 }
duke@1 88
duke@1 89 /**
duke@1 90 * If the processor class is annotated with {@link
duke@1 91 * SupportedAnnotationTypes}, return an unmodifiable set with the
duke@1 92 * same set of strings as the annotation. If the class is not so
duke@1 93 * annotated, an empty set is returned.
duke@1 94 *
duke@1 95 * @return the names of the annotation types supported by this
duke@1 96 * processor, or an empty set if none
duke@1 97 */
duke@1 98 public Set<String> getSupportedAnnotationTypes() {
duke@1 99 SupportedAnnotationTypes sat = this.getClass().getAnnotation(SupportedAnnotationTypes.class);
duke@1 100 if (sat == null) {
duke@1 101 if (isInitialized())
duke@1 102 processingEnv.getMessager().printMessage(Diagnostic.Kind.WARNING,
duke@1 103 "No SupportedAnnotationTypes annotation " +
duke@1 104 "found on " + this.getClass().getName() +
duke@1 105 ", returning an empty set.");
duke@1 106 return Collections.emptySet();
duke@1 107 }
duke@1 108 else
duke@1 109 return arrayToSet(sat.value());
duke@1 110 }
duke@1 111
duke@1 112 /**
duke@1 113 * If the processor class is annotated with {@link
duke@1 114 * SupportedSourceVersion}, return the source version in the
duke@1 115 * annotation. If the class is not so annotated, {@link
duke@1 116 * SourceVersion#RELEASE_6} is returned.
duke@1 117 *
duke@1 118 * @return the latest source version supported by this processor
duke@1 119 */
duke@1 120 public SourceVersion getSupportedSourceVersion() {
duke@1 121 SupportedSourceVersion ssv = this.getClass().getAnnotation(SupportedSourceVersion.class);
duke@1 122 SourceVersion sv = null;
duke@1 123 if (ssv == null) {
duke@1 124 sv = SourceVersion.RELEASE_6;
duke@1 125 if (isInitialized())
duke@1 126 processingEnv.getMessager().printMessage(Diagnostic.Kind.WARNING,
duke@1 127 "No SupportedSourceVersion annotation " +
duke@1 128 "found on " + this.getClass().getName() +
duke@1 129 ", returning " + sv + ".");
duke@1 130 } else
duke@1 131 sv = ssv.value();
duke@1 132 return sv;
duke@1 133 }
duke@1 134
duke@1 135
duke@1 136 /**
duke@1 137 * Initializes the processor with the processing environment by
duke@1 138 * setting the {@code processingEnv} field to the value of the
duke@1 139 * {@code processingEnv} argument. An {@code
duke@1 140 * IllegalStateException} will be thrown if this method is called
duke@1 141 * more than once on the same object.
duke@1 142 *
duke@1 143 * @param processingEnv environment to access facilities the tool framework
duke@1 144 * provides to the processor
duke@1 145 * @throws IllegalStateException if this method is called more than once.
duke@1 146 */
duke@1 147 public synchronized void init(ProcessingEnvironment processingEnv) {
duke@1 148 if (initialized)
duke@1 149 throw new IllegalStateException("Cannot call init more than once.");
darcy@1664 150 Objects.requireNonNull(processingEnv, "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