1 import java.util.*;
   2 import javax.annotation.processing.*;
   3 import javax.lang.model.SourceVersion;
   4 import static javax.lang.model.SourceVersion.*;
   5 import javax.lang.model.element.*;
   6 import javax.lang.model.util.*;
   7 
   8 /**
   9  * An abstract annotation processor tailored to javac regression testing.
  10  */
  11 public abstract class JavacTestingAbstractProcessor extends AbstractProcessor {
  12     private static final Set<String> allAnnotations;
  13 
  14     static { 
  15         Set<String> tmp = new HashSet<>();
  16         tmp.add("*");
  17         allAnnotations = Collections.unmodifiableSet(tmp);
  18     }
  19 
  20     protected Elements eltUtils;
  21     protected Elements elements;
  22     protected Types    typeUtils;
  23     protected Types    types;
  24     protected Filer    filer;
  25     protected Messager messager;
  26     protected Map<String, String> options;
  27 
  28     /**
  29      * Constructor for subclasses to call.
  30      */
  31     protected JavacTestingAbstractProcessor() {
  32         super();
  33     }
  34 
  35     /**
  36      * Return the latest source version. Unless this method is
  37      * overridden, an {@code IllegalStateException} will be thrown if a
  38      * subclass has a {@code SupportedSourceVersion} annotation.
  39      */
  40     @Override
  41     public SourceVersion getSupportedSourceVersion() {
  42         SupportedSourceVersion ssv = this.getClass().getAnnotation(SupportedSourceVersion.class);
  43         if (ssv != null)
  44             throw new IllegalStateException("SupportedSourceVersion annotation not supported here.");
  45 
  46         return SourceVersion.latest();
  47     }
  48 
  49     /**
  50      * If the processor class is annotated with {@link
  51      * SupportedAnnotationTypes}, return an unmodifiable set with the
  52      * same set of strings as the annotation.  If the class is not so
  53      * annotated, a one-element set containing {@code "*"} is returned
  54      * to indicate all annotations are processed.
  55      *
  56      * @return the names of the annotation types supported by this
  57      * processor, or an empty set if none
  58      */
  59     @Override
  60     public Set<String> getSupportedAnnotationTypes() {
  61         SupportedAnnotationTypes sat = this.getClass().getAnnotation(SupportedAnnotationTypes.class);
  62         if (sat != null)
  63             return super.getSupportedAnnotationTypes();
  64         else
  65             return allAnnotations;
  66     }
  67 
  68     @Override
  69     public void init(ProcessingEnvironment processingEnv) {
  70         super.init(processingEnv);
  71         elements = eltUtils  = processingEnv.getElementUtils();
  72         types = typeUtils = processingEnv.getTypeUtils();
  73         filer     = processingEnv.getFiler();
  74         messager  = processingEnv.getMessager();
  75         options   = processingEnv.getOptions();
  76     }
  77 }