1 /*
2 * Copyright (c) 2010, 2025, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24 import java.io.Writer;
25 import java.util.*;
26 import javax.annotation.processing.*;
27 import javax.lang.model.SourceVersion;
28 import javax.lang.model.element.*;
29 import javax.lang.model.type.*;
30 import javax.lang.model.util.*;
31 import static javax.lang.model.SourceVersion.*;
32
33 /**
34 * An abstract annotation processor tailored to {@code javac} regression testing.
35 */
36 public abstract class JavacTestingAbstractProcessor extends AbstractProcessor {
37 private static final Set<String> allAnnotations = Set.of("*");
38
39 protected Elements eltUtils;
40 protected Elements elements;
41 protected Types typeUtils;
42 protected Types types;
43 protected Filer filer;
44 protected Messager messager;
45 protected Map<String, String> options;
46
47 /**
48 * Constructor for subclasses to call.
49 */
50 protected JavacTestingAbstractProcessor() {
51 super();
52 }
53
54 /**
55 * Return the latest source version. Unless this method is
56 * overridden, an {@code IllegalStateException} will be thrown if a
57 * subclass has a {@code SupportedSourceVersion} annotation.
58 */
59 @Override
60 public SourceVersion getSupportedSourceVersion() {
61 SupportedSourceVersion ssv = this.getClass().getAnnotation(SupportedSourceVersion.class);
62 if (ssv != null)
63 throw new IllegalStateException("SupportedSourceVersion annotation not supported here.");
64
65 return SourceVersion.latest();
66 }
67
68 /**
69 * If the processor class is annotated with {@link
70 * SupportedAnnotationTypes}, return an unmodifiable set with the
71 * same set of strings as the annotation. If the class is not so
72 * annotated, a one-element set containing {@code "*"} is returned
73 * to indicate all annotations are processed.
74 *
75 * @return the names of the annotation types supported by this
76 * processor, or an empty set if none
77 */
78 @Override
79 public Set<String> getSupportedAnnotationTypes() {
80 SupportedAnnotationTypes sat = this.getClass().getAnnotation(SupportedAnnotationTypes.class);
81 if (sat != null)
82 return super.getSupportedAnnotationTypes();
83 else
84 return allAnnotations;
85 }
86
87 @Override
88 public void init(ProcessingEnvironment processingEnv) {
89 super.init(processingEnv);
90 elements = eltUtils = processingEnv.getElementUtils();
91 types = typeUtils = processingEnv.getTypeUtils();
92 filer = processingEnv.getFiler();
93 messager = processingEnv.getMessager();
94 options = processingEnv.getOptions();
95 }
96
97 protected void addExports(String moduleName, String... packageNames) {
98 for (String packageName : packageNames) {
99 try {
100 ModuleLayer layer = ModuleLayer.boot();
101 // removing dependency on java.util.Optional, valhalla only, to avoid VM warnings
102 Module m = layer.findModule(moduleName).get();
103 if (m == null)
104 throw new Error("module not found: " + moduleName);
105 m.addExports(packageName, getClass().getModule());
106 } catch (Exception e) {
107 throw new Error("failed to add exports for " + moduleName + "/" + packageName);
108 }
109 }
110 }
111
112 /*
113 * The set of visitors below will directly extend the most recent
114 * corresponding platform visitor type.
115 */
116
117 @SupportedSourceVersion(RELEASE_26)
118 @SuppressWarnings("preview")
119 public static abstract class AbstractAnnotationValueVisitor<R, P> extends AbstractAnnotationValueVisitorPreview<R, P> {
120
121 /**
122 * Constructor for concrete subclasses to call.
123 */
124 protected AbstractAnnotationValueVisitor() {
125 super();
126 }
127 }
128
129 @SupportedSourceVersion(RELEASE_26)
130 @SuppressWarnings("preview")
131 public static abstract class AbstractElementVisitor<R, P> extends AbstractElementVisitorPreview<R, P> {
132 /**
133 * Constructor for concrete subclasses to call.
134 */
135 protected AbstractElementVisitor(){
136 super();
137 }
138 }
139
140 @SupportedSourceVersion(RELEASE_26)
141 @SuppressWarnings("preview")
142 public static abstract class AbstractTypeVisitor<R, P> extends AbstractTypeVisitorPreview<R, P> {
143 /**
144 * Constructor for concrete subclasses to call.
145 */
146 protected AbstractTypeVisitor() {
147 super();
148 }
149 }
150
151 @SupportedSourceVersion(RELEASE_26)
152 @SuppressWarnings("preview")
153 public static class ElementKindVisitor<R, P> extends ElementKindVisitorPreview<R, P> {
154 /**
155 * Constructor for concrete subclasses; uses {@code null} for the
156 * default value.
157 */
158 protected ElementKindVisitor() {
159 super(null);
160 }
161
162 /**
163 * Constructor for concrete subclasses; uses the argument for the
164 * default value.
165 *
166 * @param defaultValue the value to assign to {@link #DEFAULT_VALUE}
167 */
168 protected ElementKindVisitor(R defaultValue) {
169 super(defaultValue);
170 }
171 }
172
173 @SupportedSourceVersion(RELEASE_26)
174 @SuppressWarnings("preview")
175 public static class ElementScanner<R, P> extends ElementScannerPreview<R, P> {
176 /**
177 * Constructor for concrete subclasses; uses {@code null} for the
178 * default value.
179 */
180 protected ElementScanner(){
181 super(null);
182 }
183
184 /**
185 * Constructor for concrete subclasses; uses the argument for the
186 * default value.
187 */
188 protected ElementScanner(R defaultValue){
189 super(defaultValue);
190 }
191 }
192
193 @SupportedSourceVersion(RELEASE_26)
194 @SuppressWarnings("preview")
195 public static class SimpleAnnotationValueVisitor<R, P> extends SimpleAnnotationValueVisitorPreview<R, P> {
196 /**
197 * Constructor for concrete subclasses; uses {@code null} for the
198 * default value.
199 */
200 protected SimpleAnnotationValueVisitor() {
201 super(null);
202 }
203
204 /**
205 * Constructor for concrete subclasses; uses the argument for the
206 * default value.
207 *
208 * @param defaultValue the value to assign to {@link #DEFAULT_VALUE}
209 */
210 protected SimpleAnnotationValueVisitor(R defaultValue) {
211 super(defaultValue);
212 }
213 }
214
215 @SupportedSourceVersion(RELEASE_26)
216 @SuppressWarnings("preview")
217 public static class SimpleElementVisitor<R, P> extends SimpleElementVisitorPreview<R, P> {
218 /**
219 * Constructor for concrete subclasses; uses {@code null} for the
220 * default value.
221 */
222 protected SimpleElementVisitor(){
223 super(null);
224 }
225
226 /**
227 * Constructor for concrete subclasses; uses the argument for the
228 * default value.
229 *
230 * @param defaultValue the value to assign to {@link #DEFAULT_VALUE}
231 */
232 protected SimpleElementVisitor(R defaultValue){
233 super(defaultValue);
234 }
235 }
236
237 @SupportedSourceVersion(RELEASE_26)
238 @SuppressWarnings("preview")
239 public static class SimpleTypeVisitor<R, P> extends SimpleTypeVisitorPreview<R, P> {
240 /**
241 * Constructor for concrete subclasses; uses {@code null} for the
242 * default value.
243 */
244 protected SimpleTypeVisitor(){
245 super(null);
246 }
247
248 /**
249 * Constructor for concrete subclasses; uses the argument for the
250 * default value.
251 *
252 * @param defaultValue the value to assign to {@link #DEFAULT_VALUE}
253 */
254 protected SimpleTypeVisitor(R defaultValue){
255 super(defaultValue);
256 }
257 }
258
259 @SupportedSourceVersion(RELEASE_26)
260 @SuppressWarnings("preview")
261 public static class TypeKindVisitor<R, P> extends TypeKindVisitorPreview<R, P> {
262 /**
263 * Constructor for concrete subclasses to call; uses {@code null}
264 * for the default value.
265 */
266 protected TypeKindVisitor() {
267 super(null);
268 }
269
270 /**
271 * Constructor for concrete subclasses to call; uses the argument
272 * for the default value.
273 *
274 * @param defaultValue the value to assign to {@link #DEFAULT_VALUE}
275 */
276 protected TypeKindVisitor(R defaultValue) {
277 super(defaultValue);
278 }
279 }
280
281 /**
282 * Vacuous implementation of javax.lang.model.util.Elements to aid
283 * in test development. Methods with defaults in the interface are
284 * *not* overridden to allow them to be tested.
285 */
286 public static class VacuousElements implements Elements {
287 public VacuousElements() {}
288
289 @Override
290 public PackageElement getPackageElement(CharSequence name) {return null;}
291
292 @Override
293 public TypeElement getTypeElement(CharSequence name) {return null;}
294
295 @Override
296 public Map<? extends ExecutableElement, ? extends AnnotationValue>
297 getElementValuesWithDefaults(AnnotationMirror a) {return null;}
298 @Override
299 public String getDocComment(Element e) {return null;}
300
301 @Override
302 public boolean isDeprecated(Element e) {return false;}
303
304 @Override
305 public Name getBinaryName(TypeElement type) {return null;}
306
307 @Override
308 public PackageElement getPackageOf(Element e) {return null;}
309
310 @Override
311 public List<? extends Element> getAllMembers(TypeElement type) {return null;}
312
313 @Override
314 public List<? extends AnnotationMirror> getAllAnnotationMirrors(Element e) {return null;}
315
316 @Override
317 public boolean hides(Element hider, Element hidden) {return false;}
318
319 @Override
320 public boolean overrides(ExecutableElement overrider,
321 ExecutableElement overridden,
322 TypeElement type) {return false;}
323
324 @Override
325 public String getConstantExpression(Object value) {return null;}
326
327 @Override
328 public void printElements(Writer w, Element... elements) {}
329
330 @Override
331 public Name getName(CharSequence cs) {return null;}
332
333 @Override
334 public boolean isFunctionalInterface(TypeElement type) {return false;}
335 }
336
337 /**
338 * Vacuous implementation of javax.lang.model.util.Types to aid
339 * in test development. Methods with defaults in the interface are
340 * *not* overridden to allow them to be tested.
341 */
342 public static class VacuousTypes implements Types {
343 public VacuousTypes() {}
344
345 @Override
346 public Element asElement(TypeMirror t) {return null;}
347
348 @Override
349 public boolean isSameType(TypeMirror t1, TypeMirror t2) {return false;}
350
351 @Override
352 public boolean isSubtype(TypeMirror t1, TypeMirror t2) {return false;};
353
354 @Override
355 public boolean isAssignable(TypeMirror t1, TypeMirror t2) {return false;};
356
357 @Override
358 public boolean contains(TypeMirror t1, TypeMirror t2) {return false;};
359
360 @Override
361 public boolean isSubsignature(ExecutableType m1, ExecutableType m2) {return false;}
362
363 @Override
364 public List<? extends TypeMirror> directSupertypes(TypeMirror t) {return null;}
365
366 @Override
367 public TypeMirror erasure(TypeMirror t) {return null;}
368
369 @Override
370 public TypeElement boxedClass(PrimitiveType p) {return null;}
371
372 @Override
373 public PrimitiveType unboxedType(TypeMirror t) {return null;}
374
375 @Override
376 public TypeMirror capture(TypeMirror t) {return null;}
377
378 @Override
379 public PrimitiveType getPrimitiveType(TypeKind kind) {return null;}
380
381 @Override
382 public NullType getNullType() {return null;}
383
384 @Override
385 public NoType getNoType(TypeKind kind) {return null;}
386
387 @Override
388 public ArrayType getArrayType(TypeMirror componentType) {return null;}
389
390 @Override
391 public WildcardType getWildcardType(TypeMirror extendsBound,
392 TypeMirror superBound) {return null;}
393
394 @Override
395 public DeclaredType getDeclaredType(TypeElement typeElem, TypeMirror... typeArgs) {return null;}
396
397
398 @Override
399 public DeclaredType getDeclaredType(DeclaredType containing,
400 TypeElement typeElem, TypeMirror... typeArgs) {return null;}
401
402 @Override
403 public TypeMirror asMemberOf(DeclaredType containing, Element element) {return null;}
404 }
405 }
--- EOF ---