test/tools/javac/lib/JavacTestingAbstractProcessor.java

Wed, 29 Sep 2010 23:27:57 -0700

author
darcy
date
Wed, 29 Sep 2010 23:27:57 -0700
changeset 699
d2aaaec153e8
child 932
edf03ca74991
permissions
-rw-r--r--

6983738: Use a JavacTestingAbstractProcessor
Reviewed-by: jjg

darcy@699 1 /*
darcy@699 2 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
darcy@699 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
darcy@699 4 *
darcy@699 5 * This code is free software; you can redistribute it and/or modify it
darcy@699 6 * under the terms of the GNU General Public License version 2 only, as
darcy@699 7 * published by the Free Software Foundation.
darcy@699 8 *
darcy@699 9 * This code is distributed in the hope that it will be useful, but WITHOUT
darcy@699 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
darcy@699 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
darcy@699 12 * version 2 for more details (a copy is included in the LICENSE file that
darcy@699 13 * accompanied this code).
darcy@699 14 *
darcy@699 15 * You should have received a copy of the GNU General Public License version
darcy@699 16 * 2 along with this work; if not, write to the Free Software Foundation,
darcy@699 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
darcy@699 18 *
darcy@699 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
darcy@699 20 * or visit www.oracle.com if you need additional information or have any
darcy@699 21 * questions.
darcy@699 22 */
darcy@699 23
darcy@699 24 import java.util.*;
darcy@699 25 import javax.annotation.processing.*;
darcy@699 26 import javax.lang.model.SourceVersion;
darcy@699 27 import static javax.lang.model.SourceVersion.*;
darcy@699 28 import javax.lang.model.element.*;
darcy@699 29 import javax.lang.model.util.*;
darcy@699 30
darcy@699 31 /**
darcy@699 32 * An abstract annotation processor tailored to javac regression testing.
darcy@699 33 */
darcy@699 34 public abstract class JavacTestingAbstractProcessor extends AbstractProcessor {
darcy@699 35 private static final Set<String> allAnnotations;
darcy@699 36
darcy@699 37 static {
darcy@699 38 Set<String> tmp = new HashSet<>();
darcy@699 39 tmp.add("*");
darcy@699 40 allAnnotations = Collections.unmodifiableSet(tmp);
darcy@699 41 }
darcy@699 42
darcy@699 43 protected Elements eltUtils;
darcy@699 44 protected Elements elements;
darcy@699 45 protected Types typeUtils;
darcy@699 46 protected Types types;
darcy@699 47 protected Filer filer;
darcy@699 48 protected Messager messager;
darcy@699 49 protected Map<String, String> options;
darcy@699 50
darcy@699 51 /**
darcy@699 52 * Constructor for subclasses to call.
darcy@699 53 */
darcy@699 54 protected JavacTestingAbstractProcessor() {
darcy@699 55 super();
darcy@699 56 }
darcy@699 57
darcy@699 58 /**
darcy@699 59 * Return the latest source version. Unless this method is
darcy@699 60 * overridden, an {@code IllegalStateException} will be thrown if a
darcy@699 61 * subclass has a {@code SupportedSourceVersion} annotation.
darcy@699 62 */
darcy@699 63 @Override
darcy@699 64 public SourceVersion getSupportedSourceVersion() {
darcy@699 65 SupportedSourceVersion ssv = this.getClass().getAnnotation(SupportedSourceVersion.class);
darcy@699 66 if (ssv != null)
darcy@699 67 throw new IllegalStateException("SupportedSourceVersion annotation not supported here.");
darcy@699 68
darcy@699 69 return SourceVersion.latest();
darcy@699 70 }
darcy@699 71
darcy@699 72 /**
darcy@699 73 * If the processor class is annotated with {@link
darcy@699 74 * SupportedAnnotationTypes}, return an unmodifiable set with the
darcy@699 75 * same set of strings as the annotation. If the class is not so
darcy@699 76 * annotated, a one-element set containing {@code "*"} is returned
darcy@699 77 * to indicate all annotations are processed.
darcy@699 78 *
darcy@699 79 * @return the names of the annotation types supported by this
darcy@699 80 * processor, or an empty set if none
darcy@699 81 */
darcy@699 82 @Override
darcy@699 83 public Set<String> getSupportedAnnotationTypes() {
darcy@699 84 SupportedAnnotationTypes sat = this.getClass().getAnnotation(SupportedAnnotationTypes.class);
darcy@699 85 if (sat != null)
darcy@699 86 return super.getSupportedAnnotationTypes();
darcy@699 87 else
darcy@699 88 return allAnnotations;
darcy@699 89 }
darcy@699 90
darcy@699 91 @Override
darcy@699 92 public void init(ProcessingEnvironment processingEnv) {
darcy@699 93 super.init(processingEnv);
darcy@699 94 elements = eltUtils = processingEnv.getElementUtils();
darcy@699 95 types = typeUtils = processingEnv.getTypeUtils();
darcy@699 96 filer = processingEnv.getFiler();
darcy@699 97 messager = processingEnv.getMessager();
darcy@699 98 options = processingEnv.getOptions();
darcy@699 99 }
darcy@699 100 }

mercurial