KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > annotation > processing > AbstractProcessor


1 /*
2  * @(#)AbstractProcessor.java 1.8 06/07/17
3  *
4  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package javax.annotation.processing;
9
10 import java.util.Set JavaDoc;
11 import java.util.HashSet JavaDoc;
12 import java.util.Collections JavaDoc;
13 import javax.lang.model.element.*;
14 import javax.lang.model.SourceVersion;
15 import javax.tools.Diagnostic;
16
17 /**
18  * An abstract annotation processor designed to be a convenient
19  * superclass for most concrete annotation processors. This class
20  * examines annotation values to compute the {@linkplain
21  * #getSupportedOptions options}, {@linkplain
22  * #getSupportedAnnotationTypes annotations}, and {@linkplain
23  * #getSupportedSourceVersion source version} supported by its
24  * subtypes.
25  *
26  * <p>The getter methods may {@linkplain Messager#printMessage issue
27  * warnings} about noteworthy conditions using the facilities available
28  * after the processor has been {@linkplain #isInitialized
29  * initialized}.
30  *
31  * <p>Subclasses are free to override the implementation and
32  * specification of any of the methods in this class as long as the
33  * general {@link javax.annotation.processing.Processor Processor}
34  * contract for that method is obeyed.
35  *
36  * @author Joseph D. Darcy
37  * @author Scott Seligman
38  * @author Peter von der Ah&eacute;
39  * @version 1.8 06/07/17
40  * @since 1.6
41  */

42 public abstract class AbstractProcessor implements Processor {
43     /**
44      * Processing environment providing by the tool framework.
45      */

46     protected ProcessingEnvironment processingEnv;
47     private boolean initialized = false;
48
49     /**
50      * Constructor for subclasses to call.
51      */

52     protected AbstractProcessor() {}
53
54     /**
55      * If the processor class is annotated with {@link
56      * SupportedOptions}, return an unmodifiable set with the same set
57      * of strings as the annotation. If the class is not so
58      * annotated, an empty set is returned.
59      *
60      * @return the options recognized by this processor, or an empty
61      * set if none
62      */

63     public Set JavaDoc<String JavaDoc> getSupportedOptions() {
64     SupportedOptions so = this.getClass().getAnnotation(SupportedOptions.class);
65     if (so == null)
66         return Collections.emptySet();
67     else
68         return arrayToSet(so.value());
69     }
70
71     /**
72      * If the processor class is annotated with {@link
73      * SupportedAnnotationTypes}, return an unmodifiable set with the
74      * same set of strings as the annotation. If the class is not so
75      * annotated, an empty set is returned.
76      *
77      * @return the names of the annotation types supported by this
78      * processor, or an empty set if none
79      */

80     public Set JavaDoc<String JavaDoc> getSupportedAnnotationTypes() {
81         SupportedAnnotationTypes sat = this.getClass().getAnnotation(SupportedAnnotationTypes.class);
82         if (sat == null) {
83         if (isInitialized())
84             processingEnv.getMessager().printMessage(Diagnostic.Kind.WARNING,
85                                  "No SupportedAnnotationTypes annotation " +
86                                  "found on " + this.getClass().getName() +
87                                  ", returning an empty set.");
88         return Collections.emptySet();
89         }
90         else
91         return arrayToSet(sat.value());
92     }
93
94     /**
95      * If the processor class is annotated with {@link
96      * SupportedSourceVersion}, return the source version in the
97      * annotation. If the class is not so annotated, {@link
98      * SourceVersion#RELEASE_6} is returned.
99      *
100      * @return the latest source version supported by this processor
101      */

102     public SourceVersion getSupportedSourceVersion() {
103     SupportedSourceVersion ssv = this.getClass().getAnnotation(SupportedSourceVersion.class);
104     SourceVersion sv = null;
105     if (ssv == null) {
106         sv = SourceVersion.RELEASE_6;
107         if (isInitialized())
108         processingEnv.getMessager().printMessage(Diagnostic.Kind.WARNING,
109                              "No SupportedSourceVersion annotation " +
110                              "found on " + this.getClass().getName() +
111                              ", returning " + sv + ".");
112     } else
113         sv = ssv.value();
114     return sv;
115     }
116
117     
118     /**
119      * Initializes the processor with the processing environment by
120      * setting the {@code processingEnv} field to the value of the
121      * {@code processingEnv} argument. An {@code
122      * IllegalStateException} will be thrown if this method is called
123      * more than once on the same object.
124      *
125      * @param processingEnv environment to access facilities the tool framework
126      * provides to the processor
127      * @throws IllegalStateException if this method is called more than once.
128      */

129     public synchronized void init(ProcessingEnvironment processingEnv) {
130     if (initialized)
131         throw new IllegalStateException JavaDoc("Cannot call init more than once.");
132     if (processingEnv == null)
133         throw new NullPointerException JavaDoc("Tool provided null ProcessingEnvironment");
134
135     this.processingEnv = processingEnv;
136     initialized = true;
137     }
138
139     /**
140      * {@inheritDoc}
141      */

142     public abstract boolean process(Set JavaDoc<? extends TypeElement> annotations,
143                     RoundEnvironment roundEnv);
144
145     /**
146      * Returns an empty iterable of completions.
147      *
148      * @param element {@inheritDoc}
149      * @param annotation {@inheritDoc}
150      * @param member {@inheritDoc}
151      * @param userText {@inheritDoc}
152      */

153     public Iterable JavaDoc<? extends Completion> getCompletions(Element element,
154                              AnnotationMirror annotation,
155                              ExecutableElement member,
156                              String JavaDoc userText) {
157     return Collections.emptyList();
158     }
159
160     /**
161      * Returns {@code true} if this object has been {@linkplain #init
162      * initialized}, {@code false} otherwise.
163      *
164      * @return {@code true} if this object has been initialized,
165      * {@code false} otherwise.
166      */

167     protected synchronized boolean isInitialized() {
168     return initialized;
169     }
170
171     private static Set JavaDoc<String JavaDoc> arrayToSet(String JavaDoc[] array) {
172     assert array != null;
173     Set JavaDoc<String JavaDoc> set = new HashSet JavaDoc<String JavaDoc>(array.length);
174     for (String JavaDoc s : array)
175         set.add(s);
176     return Collections.unmodifiableSet(set);
177     }
178 }
179
Popular Tags