JavacTestingAbstractProcessor.java revision 698:d2aaaec153e8
13033Salexp/*
23033Salexp * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
33033Salexp * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
43033Salexp *
53033Salexp * This code is free software; you can redistribute it and/or modify it
63033Salexp * under the terms of the GNU General Public License version 2 only, as
73033Salexp * published by the Free Software Foundation.
83033Salexp *
93033Salexp * This code is distributed in the hope that it will be useful, but WITHOUT
103033Salexp * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
113033Salexp * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
123033Salexp * version 2 for more details (a copy is included in the LICENSE file that
133033Salexp * accompanied this code).
143033Salexp *
153033Salexp * You should have received a copy of the GNU General Public License version
163033Salexp * 2 along with this work; if not, write to the Free Software Foundation,
173033Salexp * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
183033Salexp *
193033Salexp * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
203033Salexp * or visit www.oracle.com if you need additional information or have any
213033Salexp * questions.
223033Salexp */
233033Salexp
243033Salexpimport java.util.*;
253033Salexpimport javax.annotation.processing.*;
2615235Sgoetzimport javax.lang.model.SourceVersion;
273033Salexpimport static javax.lang.model.SourceVersion.*;
283033Salexpimport javax.lang.model.element.*;
293033Salexpimport javax.lang.model.util.*;
303033Salexp
313033Salexp/**
323033Salexp * An abstract annotation processor tailored to javac regression testing.
333033Salexp */
343033Salexppublic abstract class JavacTestingAbstractProcessor extends AbstractProcessor {
353033Salexp    private static final Set<String> allAnnotations;
363033Salexp
373033Salexp    static {
383033Salexp        Set<String> tmp = new HashSet<>();
393033Salexp        tmp.add("*");
403033Salexp        allAnnotations = Collections.unmodifiableSet(tmp);
413033Salexp    }
423033Salexp
433033Salexp    protected Elements eltUtils;
443033Salexp    protected Elements elements;
453033Salexp    protected Types    typeUtils;
463033Salexp    protected Types    types;
473033Salexp    protected Filer    filer;
483033Salexp    protected Messager messager;
493033Salexp    protected Map<String, String> options;
503033Salexp
513033Salexp    /**
523033Salexp     * Constructor for subclasses to call.
533033Salexp     */
543033Salexp    protected JavacTestingAbstractProcessor() {
553033Salexp        super();
563033Salexp    }
573033Salexp
583033Salexp    /**
593033Salexp     * Return the latest source version. Unless this method is
603033Salexp     * overridden, an {@code IllegalStateException} will be thrown if a
613033Salexp     * subclass has a {@code SupportedSourceVersion} annotation.
623033Salexp     */
633033Salexp    @Override
643033Salexp    public SourceVersion getSupportedSourceVersion() {
6511111Syan        SupportedSourceVersion ssv = this.getClass().getAnnotation(SupportedSourceVersion.class);
663033Salexp        if (ssv != null)
673033Salexp            throw new IllegalStateException("SupportedSourceVersion annotation not supported here.");
683033Salexp
693033Salexp        return SourceVersion.latest();
703033Salexp    }
713033Salexp
723033Salexp    /**
733033Salexp     * If the processor class is annotated with {@link
743033Salexp     * SupportedAnnotationTypes}, return an unmodifiable set with the
753033Salexp     * same set of strings as the annotation.  If the class is not so
763033Salexp     * annotated, a one-element set containing {@code "*"} is returned
773033Salexp     * to indicate all annotations are processed.
783033Salexp     *
793033Salexp     * @return the names of the annotation types supported by this
8011111Syan     * processor, or an empty set if none
813033Salexp     */
823033Salexp    @Override
833033Salexp    public Set<String> getSupportedAnnotationTypes() {
843033Salexp        SupportedAnnotationTypes sat = this.getClass().getAnnotation(SupportedAnnotationTypes.class);
853033Salexp        if (sat != null)
863033Salexp            return super.getSupportedAnnotationTypes();
873033Salexp        else
883033Salexp            return allAnnotations;
893033Salexp    }
903033Salexp
913033Salexp    @Override
923033Salexp    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}
101