1 /*
   2  * Copyright (c) 2010, 2011, 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 
  24 import java.util.*;
  25 import javax.annotation.processing.*;
  26 import javax.lang.model.SourceVersion;
  27 import javax.lang.model.util.*;
  28 
  29 /**
  30  * An abstract annotation processor tailored to javac regression testing.
  31  */
  32 public abstract class JavacTestingAbstractProcessor extends AbstractProcessor {
  33     private static final Set<String> allAnnotations;
  34 
  35     static {
  36         Set<String> tmp = new HashSet<>();
  37         tmp.add("*");
  38         allAnnotations = Collections.unmodifiableSet(tmp);
  39     }
  40 
  41     protected Elements eltUtils;
  42     protected Elements elements;
  43     protected Types    typeUtils;
  44     protected Types    types;
  45     protected Filer    filer;
  46     protected Messager messager;
  47     protected Map<String, String> options;
  48 
  49     /**
  50      * Constructor for subclasses to call.
  51      */
  52     protected JavacTestingAbstractProcessor() {
  53         super();
  54     }
  55 
  56     /**
  57      * Return the latest source version. Unless this method is
  58      * overridden, an {@code IllegalStateException} will be thrown if a
  59      * subclass has a {@code SupportedSourceVersion} annotation.
  60      */
  61     @Override
  62     public SourceVersion getSupportedSourceVersion() {
  63         SupportedSourceVersion ssv = this.getClass().getAnnotation(SupportedSourceVersion.class);
  64         if (ssv != null)
  65             throw new IllegalStateException("SupportedSourceVersion annotation not supported here.");
  66 
  67         return SourceVersion.latest();
  68     }
  69 
  70     /**
  71      * If the processor class is annotated with {@link
  72      * SupportedAnnotationTypes}, return an unmodifiable set with the
  73      * same set of strings as the annotation.  If the class is not so
  74      * annotated, a one-element set containing {@code "*"} is returned
  75      * to indicate all annotations are processed.
  76      *
  77      * @return the names of the annotation types supported by this
  78      * processor, or an empty set if none
  79      */
  80     @Override
  81     public Set<String> getSupportedAnnotationTypes() {
  82         SupportedAnnotationTypes sat = this.getClass().getAnnotation(SupportedAnnotationTypes.class);
  83         if (sat != null)
  84             return super.getSupportedAnnotationTypes();
  85         else
  86             return allAnnotations;
  87     }
  88 
  89     @Override
  90     public void init(ProcessingEnvironment processingEnv) {
  91         super.init(processingEnv);
  92         elements = eltUtils  = processingEnv.getElementUtils();
  93         types = typeUtils = processingEnv.getTypeUtils();
  94         filer     = processingEnv.getFiler();
  95         messager  = processingEnv.getMessager();
  96         options   = processingEnv.getOptions();
  97     }
  98 }