JavacTestingAbstractProcessor.java revision 4104:4012b3f11f0d
1219974Smav/*
2219974Smav * Copyright (c) 2010, 2015, Oracle and/or its affiliates. All rights reserved.
3219974Smav * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4219974Smav *
5219974Smav * This code is free software; you can redistribute it and/or modify it
6219974Smav * under the terms of the GNU General Public License version 2 only, as
7219974Smav * published by the Free Software Foundation.
8219974Smav *
9219974Smav * This code is distributed in the hope that it will be useful, but WITHOUT
10219974Smav * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11219974Smav * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12219974Smav * version 2 for more details (a copy is included in the LICENSE file that
13219974Smav * accompanied this code).
14219974Smav *
15219974Smav * You should have received a copy of the GNU General Public License version
16219974Smav * 2 along with this work; if not, write to the Free Software Foundation,
17219974Smav * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18219974Smav *
19219974Smav * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20219974Smav * or visit www.oracle.com if you need additional information or have any
21219974Smav * questions.
22219974Smav */
23219974Smav
24219974Smavimport java.util.*;
25219974Smavimport javax.annotation.processing.*;
26219974Smavimport javax.lang.model.SourceVersion;
27219974Smavimport javax.lang.model.util.*;
28219974Smavimport static javax.lang.model.SourceVersion.*;
29219974Smav
30219974Smav/**
31219974Smav * An abstract annotation processor tailored to {@code javac} regression testing.
32219974Smav */
33219974Smavpublic abstract class JavacTestingAbstractProcessor extends AbstractProcessor {
34219974Smav    private static final Set<String> allAnnotations;
35219974Smav
36219974Smav    static {
37219974Smav        Set<String> tmp = new HashSet<>();
38219974Smav        tmp.add("*");
39219974Smav        allAnnotations = Collections.unmodifiableSet(tmp);
40219974Smav    }
41219974Smav
42219974Smav    protected Elements eltUtils;
43219974Smav    protected Elements elements;
44219974Smav    protected Types    typeUtils;
45219974Smav    protected Types    types;
46219974Smav    protected Filer    filer;
47240465Smav    protected Messager messager;
48219974Smav    protected Map<String, String> options;
49219974Smav
50219974Smav    /**
51219974Smav     * Constructor for subclasses to call.
52219974Smav     */
53219974Smav    protected JavacTestingAbstractProcessor() {
54219974Smav        super();
55219974Smav    }
56219974Smav
57219974Smav    /**
58219974Smav     * Return the latest source version. Unless this method is
59219974Smav     * overridden, an {@code IllegalStateException} will be thrown if a
60219974Smav     * subclass has a {@code SupportedSourceVersion} annotation.
61219974Smav     */
62219974Smav    @Override
63219974Smav    public SourceVersion getSupportedSourceVersion() {
64219974Smav        SupportedSourceVersion ssv = this.getClass().getAnnotation(SupportedSourceVersion.class);
65219974Smav        if (ssv != null)
66219974Smav            throw new IllegalStateException("SupportedSourceVersion annotation not supported here.");
67219974Smav
68219974Smav        return SourceVersion.latest();
69219974Smav    }
70219974Smav
71219974Smav    /**
72219974Smav     * If the processor class is annotated with {@link
73219974Smav     * SupportedAnnotationTypes}, return an unmodifiable set with the
74219974Smav     * same set of strings as the annotation.  If the class is not so
75219974Smav     * annotated, a one-element set containing {@code "*"} is returned
76219974Smav     * to indicate all annotations are processed.
77219974Smav     *
78219974Smav     * @return the names of the annotation types supported by this
79219974Smav     * processor, or an empty set if none
80219974Smav     */
81219974Smav    @Override
82219974Smav    public Set<String> getSupportedAnnotationTypes() {
83219974Smav        SupportedAnnotationTypes sat = this.getClass().getAnnotation(SupportedAnnotationTypes.class);
84219974Smav        if (sat != null)
85219974Smav            return super.getSupportedAnnotationTypes();
86219974Smav        else
87219974Smav            return allAnnotations;
88219974Smav    }
89219974Smav
90219974Smav    @Override
91219974Smav    public void init(ProcessingEnvironment processingEnv) {
92219974Smav        super.init(processingEnv);
93219974Smav        elements = eltUtils  = processingEnv.getElementUtils();
94219974Smav        types = typeUtils = processingEnv.getTypeUtils();
95219974Smav        filer     = processingEnv.getFiler();
96219974Smav        messager  = processingEnv.getMessager();
97219974Smav        options   = processingEnv.getOptions();
98219974Smav    }
99219974Smav
100219974Smav    protected void addExports(String moduleName, String... packageNames) {
101219974Smav        for (String packageName : packageNames) {
102219974Smav            try {
103219974Smav                ModuleLayer layer = ModuleLayer.boot();
104219974Smav                Optional<Module> m = layer.findModule(moduleName);
105219974Smav                if (!m.isPresent())
106219974Smav                    throw new Error("module not found: " + moduleName);
107219974Smav                m.get().addExports(packageName, getClass().getModule());
108219974Smav            } catch (Exception e) {
109219974Smav                throw new Error("failed to add exports for " + moduleName + "/" + packageName);
110219974Smav            }
111219974Smav        }
112219974Smav    }
113219974Smav
114219974Smav    /*
115219974Smav     * The set of visitors below will directly extend the most recent
116219974Smav     * corresponding platform visitor type.
117219974Smav     */
118219974Smav
119219974Smav    @SupportedSourceVersion(RELEASE_9)
120219974Smav    public static abstract class AbstractAnnotationValueVisitor<R, P> extends AbstractAnnotationValueVisitor9<R, P> {
121219974Smav
122219974Smav        /**
123219974Smav         * Constructor for concrete subclasses to call.
124219974Smav         */
125219974Smav        protected AbstractAnnotationValueVisitor() {
126219974Smav            super();
127219974Smav        }
128219974Smav    }
129219974Smav
130219974Smav    @SupportedSourceVersion(RELEASE_9)
131219974Smav    public static abstract class AbstractElementVisitor<R, P> extends AbstractElementVisitor9<R, P> {
132219974Smav        /**
133219974Smav         * Constructor for concrete subclasses to call.
134219974Smav         */
135219974Smav        protected AbstractElementVisitor(){
136240465Smav            super();
137260385Sscottl        }
138260385Sscottl    }
139219974Smav
140219974Smav    @SupportedSourceVersion(RELEASE_9)
141219974Smav    public static abstract class AbstractTypeVisitor<R, P> extends AbstractTypeVisitor9<R, P> {
142219974Smav        /**
143219974Smav         * Constructor for concrete subclasses to call.
144219974Smav         */
145219974Smav        protected AbstractTypeVisitor() {
146219974Smav            super();
147219974Smav        }
148219974Smav    }
149219974Smav
150219974Smav    @SupportedSourceVersion(RELEASE_9)
151219974Smav    public static class ElementKindVisitor<R, P> extends ElementKindVisitor9<R, P> {
152219974Smav        /**
153219974Smav         * Constructor for concrete subclasses; uses {@code null} for the
154219974Smav         * default value.
155219974Smav         */
156219974Smav        protected ElementKindVisitor() {
157219974Smav            super(null);
158219974Smav        }
159219974Smav
160219974Smav        /**
161219974Smav         * Constructor for concrete subclasses; uses the argument for the
162219974Smav         * default value.
163219974Smav         *
164219974Smav         * @param defaultValue the value to assign to {@link #DEFAULT_VALUE}
165219974Smav         */
166219974Smav        protected ElementKindVisitor(R defaultValue) {
167219974Smav            super(defaultValue);
168219974Smav        }
169219974Smav    }
170219974Smav
171219974Smav    @SupportedSourceVersion(RELEASE_9)
172219974Smav    public static class ElementScanner<R, P> extends ElementScanner9<R, P> {
173219974Smav        /**
174219974Smav         * Constructor for concrete subclasses; uses {@code null} for the
175219974Smav         * default value.
176219974Smav         */
177219974Smav        protected ElementScanner(){
178219974Smav            super(null);
179219974Smav        }
180219974Smav
181219974Smav        /**
182219974Smav         * Constructor for concrete subclasses; uses the argument for the
183219974Smav         * default value.
184219974Smav         */
185219974Smav        protected ElementScanner(R defaultValue){
186219974Smav            super(defaultValue);
187219974Smav        }
188219974Smav    }
189219974Smav
190234603Smav    @SupportedSourceVersion(RELEASE_9)
191219974Smav    public static class SimpleAnnotationValueVisitor<R, P> extends SimpleAnnotationValueVisitor9<R, P> {
192219974Smav        /**
193219974Smav         * Constructor for concrete subclasses; uses {@code null} for the
194219974Smav         * default value.
195219974Smav         */
196219974Smav        protected SimpleAnnotationValueVisitor() {
197219974Smav            super(null);
198219974Smav        }
199219974Smav
200219974Smav        /**
201219974Smav         * Constructor for concrete subclasses; uses the argument for the
202219974Smav         * default value.
203219974Smav         *
204219974Smav         * @param defaultValue the value to assign to {@link #DEFAULT_VALUE}
205219974Smav         */
206219974Smav        protected SimpleAnnotationValueVisitor(R defaultValue) {
207219974Smav            super(defaultValue);
208219974Smav        }
209219974Smav    }
210219974Smav
211219974Smav    @SupportedSourceVersion(RELEASE_9)
212219974Smav    public static class SimpleElementVisitor<R, P> extends SimpleElementVisitor9<R, P> {
213219974Smav        /**
214219974Smav         * Constructor for concrete subclasses; uses {@code null} for the
215219974Smav         * default value.
216219974Smav         */
217219974Smav        protected SimpleElementVisitor(){
218219974Smav            super(null);
219219974Smav        }
220219974Smav
221219974Smav        /**
222219974Smav         * Constructor for concrete subclasses; uses the argument for the
223219974Smav         * default value.
224219974Smav         *
225219974Smav         * @param defaultValue the value to assign to {@link #DEFAULT_VALUE}
226219974Smav         */
227219974Smav        protected SimpleElementVisitor(R defaultValue){
228219974Smav            super(defaultValue);
229219974Smav        }
230219974Smav    }
231219974Smav
232219974Smav    @SupportedSourceVersion(RELEASE_9)
233219974Smav    public static class SimpleTypeVisitor<R, P> extends SimpleTypeVisitor9<R, P> {
234219974Smav        /**
235219974Smav         * Constructor for concrete subclasses; uses {@code null} for the
236219974Smav         * default value.
237219974Smav         */
238219974Smav        protected SimpleTypeVisitor(){
239219974Smav            super(null);
240219974Smav        }
241219974Smav
242219974Smav        /**
243219974Smav         * Constructor for concrete subclasses; uses the argument for the
244219974Smav         * default value.
245219974Smav         *
246219974Smav         * @param defaultValue the value to assign to {@link #DEFAULT_VALUE}
247219974Smav         */
248219974Smav        protected SimpleTypeVisitor(R defaultValue){
249219974Smav            super(defaultValue);
250219974Smav        }
251219974Smav    }
252219974Smav
253219974Smav    @SupportedSourceVersion(RELEASE_9)
254219974Smav    public static class TypeKindVisitor<R, P> extends TypeKindVisitor9<R, P> {
255219974Smav        /**
256219974Smav         * Constructor for concrete subclasses to call; uses {@code null}
257219974Smav         * for the default value.
258219974Smav         */
259219974Smav        protected TypeKindVisitor() {
260219974Smav            super(null);
261219974Smav        }
262219974Smav
263219974Smav        /**
264219974Smav         * Constructor for concrete subclasses to call; uses the argument
265219974Smav         * for the default value.
266219974Smav         *
267219974Smav         * @param defaultValue the value to assign to {@link #DEFAULT_VALUE}
268219974Smav         */
269219974Smav        protected TypeKindVisitor(R defaultValue) {
270219974Smav            super(defaultValue);
271219974Smav        }
272219974Smav    }
273219974Smav}
274219974Smav