darcy@699: /* jjg@932: * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved. darcy@699: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. darcy@699: * darcy@699: * This code is free software; you can redistribute it and/or modify it darcy@699: * under the terms of the GNU General Public License version 2 only, as darcy@699: * published by the Free Software Foundation. darcy@699: * darcy@699: * This code is distributed in the hope that it will be useful, but WITHOUT darcy@699: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or darcy@699: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License darcy@699: * version 2 for more details (a copy is included in the LICENSE file that darcy@699: * accompanied this code). darcy@699: * darcy@699: * You should have received a copy of the GNU General Public License version darcy@699: * 2 along with this work; if not, write to the Free Software Foundation, darcy@699: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. darcy@699: * darcy@699: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA darcy@699: * or visit www.oracle.com if you need additional information or have any darcy@699: * questions. darcy@699: */ darcy@699: darcy@699: import java.util.*; darcy@699: import javax.annotation.processing.*; darcy@699: import javax.lang.model.SourceVersion; darcy@699: import javax.lang.model.util.*; darcy@1054: import static javax.lang.model.SourceVersion.*; darcy@699: darcy@699: /** darcy@699: * An abstract annotation processor tailored to javac regression testing. darcy@699: */ darcy@699: public abstract class JavacTestingAbstractProcessor extends AbstractProcessor { darcy@699: private static final Set allAnnotations; darcy@699: darcy@699: static { darcy@699: Set tmp = new HashSet<>(); darcy@699: tmp.add("*"); darcy@699: allAnnotations = Collections.unmodifiableSet(tmp); darcy@699: } darcy@699: darcy@699: protected Elements eltUtils; darcy@699: protected Elements elements; darcy@699: protected Types typeUtils; darcy@699: protected Types types; darcy@699: protected Filer filer; darcy@699: protected Messager messager; darcy@699: protected Map options; darcy@699: darcy@699: /** darcy@699: * Constructor for subclasses to call. darcy@699: */ darcy@699: protected JavacTestingAbstractProcessor() { darcy@699: super(); darcy@699: } darcy@699: darcy@699: /** darcy@699: * Return the latest source version. Unless this method is darcy@699: * overridden, an {@code IllegalStateException} will be thrown if a darcy@699: * subclass has a {@code SupportedSourceVersion} annotation. darcy@699: */ darcy@699: @Override darcy@699: public SourceVersion getSupportedSourceVersion() { darcy@699: SupportedSourceVersion ssv = this.getClass().getAnnotation(SupportedSourceVersion.class); darcy@699: if (ssv != null) darcy@699: throw new IllegalStateException("SupportedSourceVersion annotation not supported here."); darcy@699: darcy@699: return SourceVersion.latest(); darcy@699: } darcy@699: darcy@699: /** darcy@699: * If the processor class is annotated with {@link darcy@699: * SupportedAnnotationTypes}, return an unmodifiable set with the darcy@699: * same set of strings as the annotation. If the class is not so darcy@699: * annotated, a one-element set containing {@code "*"} is returned darcy@699: * to indicate all annotations are processed. darcy@699: * darcy@699: * @return the names of the annotation types supported by this darcy@699: * processor, or an empty set if none darcy@699: */ darcy@699: @Override darcy@699: public Set getSupportedAnnotationTypes() { darcy@699: SupportedAnnotationTypes sat = this.getClass().getAnnotation(SupportedAnnotationTypes.class); darcy@699: if (sat != null) darcy@699: return super.getSupportedAnnotationTypes(); darcy@699: else darcy@699: return allAnnotations; darcy@699: } darcy@699: darcy@699: @Override darcy@699: public void init(ProcessingEnvironment processingEnv) { darcy@699: super.init(processingEnv); darcy@699: elements = eltUtils = processingEnv.getElementUtils(); darcy@699: types = typeUtils = processingEnv.getTypeUtils(); darcy@699: filer = processingEnv.getFiler(); darcy@699: messager = processingEnv.getMessager(); darcy@699: options = processingEnv.getOptions(); darcy@699: } darcy@1054: darcy@1054: /* darcy@1054: * The set of visitors below will directly extend the most recent darcy@1054: * corresponding platform visitor type. darcy@1054: */ darcy@1054: darcy@1054: @SupportedSourceVersion(RELEASE_8) darcy@1054: public static abstract class AbstractAnnotationValueVisitor extends AbstractAnnotationValueVisitor8 { darcy@1054: darcy@1054: /** darcy@1054: * Constructor for concrete subclasses to call. darcy@1054: */ darcy@1054: protected AbstractAnnotationValueVisitor() { darcy@1054: super(); darcy@1054: } darcy@1054: } darcy@1054: darcy@1054: @SupportedSourceVersion(RELEASE_8) darcy@1054: public static abstract class AbstractElementVisitor extends AbstractElementVisitor8 { darcy@1054: /** darcy@1054: * Constructor for concrete subclasses to call. darcy@1054: */ darcy@1054: protected AbstractElementVisitor(){ darcy@1054: super(); darcy@1054: } darcy@1054: } darcy@1054: darcy@1054: @SupportedSourceVersion(RELEASE_8) darcy@1054: public static abstract class AbstractTypeVisitor extends AbstractTypeVisitor8 { darcy@1054: /** darcy@1054: * Constructor for concrete subclasses to call. darcy@1054: */ darcy@1054: protected AbstractTypeVisitor() { darcy@1054: super(); darcy@1054: } darcy@1054: } darcy@1054: darcy@1054: @SupportedSourceVersion(RELEASE_8) darcy@1054: public static class ElementKindVisitor extends ElementKindVisitor8 { darcy@1054: /** darcy@1054: * Constructor for concrete subclasses; uses {@code null} for the darcy@1054: * default value. darcy@1054: */ darcy@1054: protected ElementKindVisitor() { darcy@1054: super(null); darcy@1054: } darcy@1054: darcy@1054: /** darcy@1054: * Constructor for concrete subclasses; uses the argument for the darcy@1054: * default value. darcy@1054: * darcy@1054: * @param defaultValue the value to assign to {@link #DEFAULT_VALUE} darcy@1054: */ darcy@1054: protected ElementKindVisitor(R defaultValue) { darcy@1054: super(defaultValue); darcy@1054: } darcy@1054: } darcy@1054: darcy@1054: @SupportedSourceVersion(RELEASE_8) darcy@1054: public static class ElementScanner extends ElementScanner8 { darcy@1054: /** darcy@1054: * Constructor for concrete subclasses; uses {@code null} for the darcy@1054: * default value. darcy@1054: */ darcy@1054: protected ElementScanner(){ darcy@1054: super(null); darcy@1054: } darcy@1054: darcy@1054: /** darcy@1054: * Constructor for concrete subclasses; uses the argument for the darcy@1054: * default value. darcy@1054: */ darcy@1054: protected ElementScanner(R defaultValue){ darcy@1054: super(defaultValue); darcy@1054: } darcy@1054: } darcy@1054: darcy@1054: @SupportedSourceVersion(RELEASE_8) darcy@1054: public static class SimpleAnnotationValueVisitor extends SimpleAnnotationValueVisitor8 { darcy@1054: /** darcy@1054: * Constructor for concrete subclasses; uses {@code null} for the darcy@1054: * default value. darcy@1054: */ darcy@1054: protected SimpleAnnotationValueVisitor() { darcy@1054: super(null); darcy@1054: } darcy@1054: darcy@1054: /** darcy@1054: * Constructor for concrete subclasses; uses the argument for the darcy@1054: * default value. darcy@1054: * darcy@1054: * @param defaultValue the value to assign to {@link #DEFAULT_VALUE} darcy@1054: */ darcy@1054: protected SimpleAnnotationValueVisitor(R defaultValue) { darcy@1054: super(defaultValue); darcy@1054: } darcy@1054: } darcy@1054: darcy@1054: @SupportedSourceVersion(RELEASE_8) darcy@1054: public static class SimpleElementVisitor extends SimpleElementVisitor8 { darcy@1054: /** darcy@1054: * Constructor for concrete subclasses; uses {@code null} for the darcy@1054: * default value. darcy@1054: */ darcy@1054: protected SimpleElementVisitor(){ darcy@1054: super(null); darcy@1054: } darcy@1054: darcy@1054: /** darcy@1054: * Constructor for concrete subclasses; uses the argument for the darcy@1054: * default value. darcy@1054: * darcy@1054: * @param defaultValue the value to assign to {@link #DEFAULT_VALUE} darcy@1054: */ darcy@1054: protected SimpleElementVisitor(R defaultValue){ darcy@1054: super(defaultValue); darcy@1054: } darcy@1054: } darcy@1054: darcy@1054: @SupportedSourceVersion(RELEASE_8) darcy@1054: public static class SimpleTypeVisitor extends SimpleTypeVisitor8 { darcy@1054: /** darcy@1054: * Constructor for concrete subclasses; uses {@code null} for the darcy@1054: * default value. darcy@1054: */ darcy@1054: protected SimpleTypeVisitor(){ darcy@1054: super(null); darcy@1054: } darcy@1054: darcy@1054: /** darcy@1054: * Constructor for concrete subclasses; uses the argument for the darcy@1054: * default value. darcy@1054: * darcy@1054: * @param defaultValue the value to assign to {@link #DEFAULT_VALUE} darcy@1054: */ darcy@1054: protected SimpleTypeVisitor(R defaultValue){ darcy@1054: super(defaultValue); darcy@1054: } darcy@1054: } darcy@1054: darcy@1054: @SupportedSourceVersion(RELEASE_8) darcy@1054: public static class TypeKindVisitor extends TypeKindVisitor8 { darcy@1054: /** darcy@1054: * Constructor for concrete subclasses to call; uses {@code null} darcy@1054: * for the default value. darcy@1054: */ darcy@1054: protected TypeKindVisitor() { darcy@1054: super(null); darcy@1054: } darcy@1054: darcy@1054: /** darcy@1054: * Constructor for concrete subclasses to call; uses the argument darcy@1054: * for the default value. darcy@1054: * darcy@1054: * @param defaultValue the value to assign to {@link #DEFAULT_VALUE} darcy@1054: */ darcy@1054: protected TypeKindVisitor(R defaultValue) { darcy@1054: super(defaultValue); darcy@1054: } darcy@1054: } darcy@699: }