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 import static javax.lang.model.SourceVersion.*; 29 30 /** 31 * An abstract annotation processor tailored to javac regression testing. 32 */ 33 public abstract class JavacTestingAbstractProcessor extends AbstractProcessor { 34 private static final Set<String> allAnnotations; 35 36 static { 37 Set<String> tmp = new HashSet<>(); 38 tmp.add("*"); 39 allAnnotations = Collections.unmodifiableSet(tmp); 40 } 41 42 protected Elements eltUtils; 43 protected Elements elements; 44 protected Types typeUtils; 45 protected Types types; 46 protected Filer filer; 47 protected Messager messager; 48 protected Map<String, String> options; 49 50 /** 51 * Constructor for subclasses to call. 52 */ 53 protected JavacTestingAbstractProcessor() { 54 super(); 55 } 56 57 /** 58 * Return the latest source version. Unless this method is 59 * overridden, an {@code IllegalStateException} will be thrown if a 60 * subclass has a {@code SupportedSourceVersion} annotation. 61 */ 62 @Override 63 public SourceVersion getSupportedSourceVersion() { 64 SupportedSourceVersion ssv = this.getClass().getAnnotation(SupportedSourceVersion.class); 65 if (ssv != null) 66 throw new IllegalStateException("SupportedSourceVersion annotation not supported here."); 67 68 return SourceVersion.latest(); 69 } 70 71 /** 72 * If the processor class is annotated with {@link 73 * SupportedAnnotationTypes}, return an unmodifiable set with the 74 * same set of strings as the annotation. If the class is not so 75 * annotated, a one-element set containing {@code "*"} is returned 76 * to indicate all annotations are processed. 77 * 78 * @return the names of the annotation types supported by this 79 * processor, or an empty set if none 80 */ 81 @Override 82 public Set<String> getSupportedAnnotationTypes() { 83 SupportedAnnotationTypes sat = this.getClass().getAnnotation(SupportedAnnotationTypes.class); 84 if (sat != null) 85 return super.getSupportedAnnotationTypes(); 86 else 87 return allAnnotations; 88 } 89 90 @Override 91 public void init(ProcessingEnvironment processingEnv) { 92 super.init(processingEnv); 93 elements = eltUtils = processingEnv.getElementUtils(); 94 types = typeUtils = processingEnv.getTypeUtils(); 95 filer = processingEnv.getFiler(); 96 messager = processingEnv.getMessager(); 97 options = processingEnv.getOptions(); 98 } 99 100 /* 101 * The set of "" utility visitors below will directly extend 102 * the most recent corresponding platform visitor type. 103 */ 104 105 @SupportedSourceVersion(RELEASE_8) 106 public static abstract class AbstractAnnotationValueVisitor<R, P> extends AbstractAnnotationValueVisitor8<R, P> { 107 108 /** 109 * Constructor for concrete subclasses to call. 110 */ 111 protected AbstractAnnotationValueVisitor() { 112 super(); 113 } 114 } 115 116 @SupportedSourceVersion(RELEASE_8) 117 public static abstract class AbstractElementVisitor<R, P> extends AbstractElementVisitor8<R, P> { 118 /** 119 * Constructor for concrete subclasses to call. 120 */ 121 protected AbstractElementVisitor(){ 122 super(); 123 } 124 } 125 126 @SupportedSourceVersion(RELEASE_8) 127 public static abstract class AbstractTypeVisitor<R, P> extends AbstractTypeVisitor8<R, P> { 128 /** 129 * Constructor for concrete subclasses to call. 130 */ 131 protected AbstractTypeVisitor() { 132 super(); 133 } 134 } 135 136 @SupportedSourceVersion(RELEASE_8) 137 public static class ElementKindVisitor<R, P> extends ElementKindVisitor8<R, P> { 138 /** 139 * Constructor for concrete subclasses; uses {@code null} for the 140 * default value. 141 */ 142 protected ElementKindVisitor() { 143 super(null); 144 } 145 146 /** 147 * Constructor for concrete subclasses; uses the argument for the 148 * default value. 149 * 150 * @param defaultValue the value to assign to {@link #DEFAULT_VALUE} 151 */ 152 protected ElementKindVisitor(R defaultValue) { 153 super(defaultValue); 154 } 155 } 156 157 @SupportedSourceVersion(RELEASE_8) 158 public static class ElementScanner<R, P> extends ElementScanner8<R, P> { 159 /** 160 * Constructor for concrete subclasses; uses {@code null} for the 161 * default value. 162 */ 163 protected ElementScanner(){ 164 super(null); 165 } 166 167 /** 168 * Constructor for concrete subclasses; uses the argument for the 169 * default value. 170 */ 171 protected ElementScanner(R defaultValue){ 172 super(defaultValue); 173 } 174 } 175 176 @SupportedSourceVersion(RELEASE_8) 177 public static class SimpleAnnotationValueVisitor<R, P> extends SimpleAnnotationValueVisitor8<R, P> { 178 /** 179 * Constructor for concrete subclasses; uses {@code null} for the 180 * default value. 181 */ 182 protected SimpleAnnotationValueVisitor() { 183 super(null); 184 } 185 186 /** 187 * Constructor for concrete subclasses; uses the argument for the 188 * default value. 189 * 190 * @param defaultValue the value to assign to {@link #DEFAULT_VALUE} 191 */ 192 protected SimpleAnnotationValueVisitor(R defaultValue) { 193 super(defaultValue); 194 } 195 } 196 197 @SupportedSourceVersion(RELEASE_8) 198 public static class SimpleElementVisitor<R, P> extends SimpleElementVisitor8<R, P> { 199 /** 200 * Constructor for concrete subclasses; uses {@code null} for the 201 * default value. 202 */ 203 protected SimpleElementVisitor(){ 204 super(null); 205 } 206 207 /** 208 * Constructor for concrete subclasses; uses the argument for the 209 * default value. 210 * 211 * @param defaultValue the value to assign to {@link #DEFAULT_VALUE} 212 */ 213 protected SimpleElementVisitor(R defaultValue){ 214 super(defaultValue); 215 } 216 } 217 218 @SupportedSourceVersion(RELEASE_8) 219 public static class SimpleTypeVisitor<R, P> extends SimpleTypeVisitor8<R, P> { 220 /** 221 * Constructor for concrete subclasses; uses {@code null} for the 222 * default value. 223 */ 224 protected SimpleTypeVisitor(){ 225 super(null); 226 } 227 228 /** 229 * Constructor for concrete subclasses; uses the argument for the 230 * default value. 231 * 232 * @param defaultValue the value to assign to {@link #DEFAULT_VALUE} 233 */ 234 protected SimpleTypeVisitor(R defaultValue){ 235 super(defaultValue); 236 } 237 } 238 239 @SupportedSourceVersion(RELEASE_8) 240 public static class TypeKindVisitor<R, P> extends TypeKindVisitor8<R, P> { 241 /** 242 * Constructor for concrete subclasses to call; uses {@code null} 243 * for the default value. 244 */ 245 protected TypeKindVisitor() { 246 super(null); 247 } 248 249 /** 250 * Constructor for concrete subclasses to call; uses the argument 251 * for the default value. 252 * 253 * @param defaultValue the value to assign to {@link #DEFAULT_VALUE} 254 */ 255 protected TypeKindVisitor(R defaultValue) { 256 super(defaultValue); 257 } 258 } 259 }