1 /*
2  * Copyright (c) 2010, 2019, 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 javax.lang.model.util.*;
28 import static javax.lang.model.SourceVersion.*;
29 
30 /**
31  * An abstract annotation processor tailored to {@code javac} regression testing.
32  */
33 public abstract class JavacTestingAbstractProcessor extends AbstractProcessor {
34     private static final Set<String> allAnnotations = Set.of("*");
35 
36     protected Elements eltUtils;
37     protected Elements elements;
38     protected Types    typeUtils;
39     protected Types    types;
40     protected Filer    filer;
41     protected Messager messager;
42     protected Map<String, String> options;
43 
44     /**
45      * Constructor for subclasses to call.
46      */
JavacTestingAbstractProcessor()47     protected JavacTestingAbstractProcessor() {
48         super();
49     }
50 
51     /**
52      * Return the latest source version. Unless this method is
53      * overridden, an {@code IllegalStateException} will be thrown if a
54      * subclass has a {@code SupportedSourceVersion} annotation.
55      */
56     @Override
getSupportedSourceVersion()57     public SourceVersion getSupportedSourceVersion() {
58         SupportedSourceVersion ssv = this.getClass().getAnnotation(SupportedSourceVersion.class);
59         if (ssv != null)
60             throw new IllegalStateException("SupportedSourceVersion annotation not supported here.");
61 
62         return SourceVersion.latest();
63     }
64 
65     /**
66      * If the processor class is annotated with {@link
67      * SupportedAnnotationTypes}, return an unmodifiable set with the
68      * same set of strings as the annotation.  If the class is not so
69      * annotated, a one-element set containing {@code "*"} is returned
70      * to indicate all annotations are processed.
71      *
72      * @return the names of the annotation types supported by this
73      * processor, or an empty set if none
74      */
75     @Override
getSupportedAnnotationTypes()76     public Set<String> getSupportedAnnotationTypes() {
77         SupportedAnnotationTypes sat = this.getClass().getAnnotation(SupportedAnnotationTypes.class);
78         if (sat != null)
79             return super.getSupportedAnnotationTypes();
80         else
81             return allAnnotations;
82     }
83 
84     @Override
init(ProcessingEnvironment processingEnv)85     public void init(ProcessingEnvironment processingEnv) {
86         super.init(processingEnv);
87         elements = eltUtils  = processingEnv.getElementUtils();
88         types = typeUtils = processingEnv.getTypeUtils();
89         filer     = processingEnv.getFiler();
90         messager  = processingEnv.getMessager();
91         options   = processingEnv.getOptions();
92     }
93 
addExports(String moduleName, String... packageNames)94     protected void addExports(String moduleName, String... packageNames) {
95         for (String packageName : packageNames) {
96             try {
97                 ModuleLayer layer = ModuleLayer.boot();
98                 Optional<Module> m = layer.findModule(moduleName);
99                 if (!m.isPresent())
100                     throw new Error("module not found: " + moduleName);
101                 m.get().addExports(packageName, getClass().getModule());
102             } catch (Exception e) {
103                 throw new Error("failed to add exports for " + moduleName + "/" + packageName);
104             }
105         }
106     }
107 
108     /*
109      * The set of visitors below will directly extend the most recent
110      * corresponding platform visitor type.
111      */
112 
113     @SupportedSourceVersion(RELEASE_15)
114     public static abstract class AbstractAnnotationValueVisitor<R, P> extends AbstractAnnotationValueVisitor14<R, P> {
115 
116         /**
117          * Constructor for concrete subclasses to call.
118          */
AbstractAnnotationValueVisitor()119         protected AbstractAnnotationValueVisitor() {
120             super();
121         }
122     }
123 
124     @SupportedSourceVersion(RELEASE_15)
125     public static abstract class AbstractElementVisitor<R, P> extends AbstractElementVisitor14<R, P> {
126         /**
127          * Constructor for concrete subclasses to call.
128          */
AbstractElementVisitor()129         protected AbstractElementVisitor(){
130             super();
131         }
132     }
133 
134     @SupportedSourceVersion(RELEASE_15)
135     public static abstract class AbstractTypeVisitor<R, P> extends AbstractTypeVisitor14<R, P> {
136         /**
137          * Constructor for concrete subclasses to call.
138          */
AbstractTypeVisitor()139         protected AbstractTypeVisitor() {
140             super();
141         }
142     }
143 
144     @SupportedSourceVersion(RELEASE_14)
145     public static class ElementKindVisitor<R, P> extends ElementKindVisitor14<R, P> {
146         /**
147          * Constructor for concrete subclasses; uses {@code null} for the
148          * default value.
149          */
ElementKindVisitor()150         protected ElementKindVisitor() {
151             super(null);
152         }
153 
154         /**
155          * Constructor for concrete subclasses; uses the argument for the
156          * default value.
157          *
158          * @param defaultValue the value to assign to {@link #DEFAULT_VALUE}
159          */
ElementKindVisitor(R defaultValue)160         protected ElementKindVisitor(R defaultValue) {
161             super(defaultValue);
162         }
163     }
164 
165     @SupportedSourceVersion(RELEASE_15)
166     public static class ElementScanner<R, P> extends ElementScanner14<R, P> {
167         /**
168          * Constructor for concrete subclasses; uses {@code null} for the
169          * default value.
170          */
ElementScanner()171         protected ElementScanner(){
172             super(null);
173         }
174 
175         /**
176          * Constructor for concrete subclasses; uses the argument for the
177          * default value.
178          */
ElementScanner(R defaultValue)179         protected ElementScanner(R defaultValue){
180             super(defaultValue);
181         }
182     }
183 
184     @SupportedSourceVersion(RELEASE_15)
185     public static class SimpleAnnotationValueVisitor<R, P> extends SimpleAnnotationValueVisitor14<R, P> {
186         /**
187          * Constructor for concrete subclasses; uses {@code null} for the
188          * default value.
189          */
SimpleAnnotationValueVisitor()190         protected SimpleAnnotationValueVisitor() {
191             super(null);
192         }
193 
194         /**
195          * Constructor for concrete subclasses; uses the argument for the
196          * default value.
197          *
198          * @param defaultValue the value to assign to {@link #DEFAULT_VALUE}
199          */
SimpleAnnotationValueVisitor(R defaultValue)200         protected SimpleAnnotationValueVisitor(R defaultValue) {
201             super(defaultValue);
202         }
203     }
204 
205     @SupportedSourceVersion(RELEASE_15)
206     public static class SimpleElementVisitor<R, P> extends SimpleElementVisitor14<R, P> {
207         /**
208          * Constructor for concrete subclasses; uses {@code null} for the
209          * default value.
210          */
SimpleElementVisitor()211         protected SimpleElementVisitor(){
212             super(null);
213         }
214 
215         /**
216          * Constructor for concrete subclasses; uses the argument for the
217          * default value.
218          *
219          * @param defaultValue the value to assign to {@link #DEFAULT_VALUE}
220          */
SimpleElementVisitor(R defaultValue)221         protected SimpleElementVisitor(R defaultValue){
222             super(defaultValue);
223         }
224     }
225 
226     @SupportedSourceVersion(RELEASE_15)
227     public static class SimpleTypeVisitor<R, P> extends SimpleTypeVisitor14<R, P> {
228         /**
229          * Constructor for concrete subclasses; uses {@code null} for the
230          * default value.
231          */
SimpleTypeVisitor()232         protected SimpleTypeVisitor(){
233             super(null);
234         }
235 
236         /**
237          * Constructor for concrete subclasses; uses the argument for the
238          * default value.
239          *
240          * @param defaultValue the value to assign to {@link #DEFAULT_VALUE}
241          */
SimpleTypeVisitor(R defaultValue)242         protected SimpleTypeVisitor(R defaultValue){
243             super(defaultValue);
244         }
245     }
246 
247     @SupportedSourceVersion(RELEASE_15)
248     public static class TypeKindVisitor<R, P> extends TypeKindVisitor14<R, P> {
249         /**
250          * Constructor for concrete subclasses to call; uses {@code null}
251          * for the default value.
252          */
TypeKindVisitor()253         protected TypeKindVisitor() {
254             super(null);
255         }
256 
257         /**
258          * Constructor for concrete subclasses to call; uses the argument
259          * for the default value.
260          *
261          * @param defaultValue the value to assign to {@link #DEFAULT_VALUE}
262          */
TypeKindVisitor(R defaultValue)263         protected TypeKindVisitor(R defaultValue) {
264             super(defaultValue);
265         }
266     }
267 }
268