|
1 /* |
|
2 * Copyright (c) 2010, 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.util.*; |
|
25 import javax.annotation.processing.*; |
|
26 import javax.lang.model.SourceVersion; |
|
27 import static javax.lang.model.SourceVersion.*; |
|
28 import javax.lang.model.element.*; |
|
29 import javax.lang.model.util.*; |
|
30 |
|
31 /** |
|
32 * An abstract annotation processor tailored to javac regression testing. |
|
33 */ |
|
34 public abstract class JavacTestingAbstractProcessor extends AbstractProcessor { |
|
35 private static final Set<String> allAnnotations; |
|
36 |
|
37 static { |
|
38 Set<String> tmp = new HashSet<>(); |
|
39 tmp.add("*"); |
|
40 allAnnotations = Collections.unmodifiableSet(tmp); |
|
41 } |
|
42 |
|
43 protected Elements eltUtils; |
|
44 protected Elements elements; |
|
45 protected Types typeUtils; |
|
46 protected Types types; |
|
47 protected Filer filer; |
|
48 protected Messager messager; |
|
49 protected Map<String, String> options; |
|
50 |
|
51 /** |
|
52 * Constructor for subclasses to call. |
|
53 */ |
|
54 protected JavacTestingAbstractProcessor() { |
|
55 super(); |
|
56 } |
|
57 |
|
58 /** |
|
59 * Return the latest source version. Unless this method is |
|
60 * overridden, an {@code IllegalStateException} will be thrown if a |
|
61 * subclass has a {@code SupportedSourceVersion} annotation. |
|
62 */ |
|
63 @Override |
|
64 public SourceVersion getSupportedSourceVersion() { |
|
65 SupportedSourceVersion ssv = this.getClass().getAnnotation(SupportedSourceVersion.class); |
|
66 if (ssv != null) |
|
67 throw new IllegalStateException("SupportedSourceVersion annotation not supported here."); |
|
68 |
|
69 return SourceVersion.latest(); |
|
70 } |
|
71 |
|
72 /** |
|
73 * If the processor class is annotated with {@link |
|
74 * SupportedAnnotationTypes}, return an unmodifiable set with the |
|
75 * same set of strings as the annotation. If the class is not so |
|
76 * annotated, a one-element set containing {@code "*"} is returned |
|
77 * to indicate all annotations are processed. |
|
78 * |
|
79 * @return the names of the annotation types supported by this |
|
80 * processor, or an empty set if none |
|
81 */ |
|
82 @Override |
|
83 public Set<String> getSupportedAnnotationTypes() { |
|
84 SupportedAnnotationTypes sat = this.getClass().getAnnotation(SupportedAnnotationTypes.class); |
|
85 if (sat != null) |
|
86 return super.getSupportedAnnotationTypes(); |
|
87 else |
|
88 return allAnnotations; |
|
89 } |
|
90 |
|
91 @Override |
|
92 public void init(ProcessingEnvironment processingEnv) { |
|
93 super.init(processingEnv); |
|
94 elements = eltUtils = processingEnv.getElementUtils(); |
|
95 types = typeUtils = processingEnv.getTypeUtils(); |
|
96 filer = processingEnv.getFiler(); |
|
97 messager = processingEnv.getMessager(); |
|
98 options = processingEnv.getOptions(); |
|
99 } |
|
100 } |