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