1 /* 2 * Copyright (c) 2010, 2024, 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.io.Writer; 25 import java.util.*; 26 import javax.annotation.processing.*; 27 import javax.lang.model.SourceVersion; 28 import javax.lang.model.element.*; 29 import javax.lang.model.type.*; 30 import javax.lang.model.util.*; 31 import static javax.lang.model.SourceVersion.*; 32 33 /** 34 * An abstract annotation processor tailored to {@code javac} regression testing. 35 */ 36 public abstract class JavacTestingAbstractProcessor extends AbstractProcessor { 37 private static final Set<String> allAnnotations = Set.of("*"); 38 39 protected Elements eltUtils; 40 protected Elements elements; 41 protected Types typeUtils; 42 protected Types types; 43 protected Filer filer; 44 protected Messager messager; 45 protected Map<String, String> options; 46 47 /** 48 * Constructor for subclasses to call. 49 */ 50 protected JavacTestingAbstractProcessor() { 51 super(); 52 } 53 54 /** 55 * Return the latest source version. Unless this method is 56 * overridden, an {@code IllegalStateException} will be thrown if a 57 * subclass has a {@code SupportedSourceVersion} annotation. 58 */ 59 @Override 60 public SourceVersion getSupportedSourceVersion() { 61 SupportedSourceVersion ssv = this.getClass().getAnnotation(SupportedSourceVersion.class); 62 if (ssv != null) 63 throw new IllegalStateException("SupportedSourceVersion annotation not supported here."); 64 65 return SourceVersion.latest(); 66 } 67 68 /** 69 * If the processor class is annotated with {@link 70 * SupportedAnnotationTypes}, return an unmodifiable set with the 71 * same set of strings as the annotation. If the class is not so 72 * annotated, a one-element set containing {@code "*"} is returned 73 * to indicate all annotations are processed. 74 * 75 * @return the names of the annotation types supported by this 76 * processor, or an empty set if none 77 */ 78 @Override 79 public Set<String> getSupportedAnnotationTypes() { 80 SupportedAnnotationTypes sat = this.getClass().getAnnotation(SupportedAnnotationTypes.class); 81 if (sat != null) 82 return super.getSupportedAnnotationTypes(); 83 else 84 return allAnnotations; 85 } 86 87 @Override 88 public void init(ProcessingEnvironment processingEnv) { 89 super.init(processingEnv); 90 elements = eltUtils = processingEnv.getElementUtils(); 91 types = typeUtils = processingEnv.getTypeUtils(); 92 filer = processingEnv.getFiler(); 93 messager = processingEnv.getMessager(); 94 options = processingEnv.getOptions(); 95 } 96 97 protected void addExports(String moduleName, String... packageNames) { 98 for (String packageName : packageNames) { 99 try { 100 ModuleLayer layer = ModuleLayer.boot(); 101 // removing dependency on java.util.Optional, valhalla only, to avoid VM warnings 102 Module m = layer.findModule(moduleName).get(); 103 if (m == null) 104 throw new Error("module not found: " + moduleName); 105 m.addExports(packageName, getClass().getModule()); 106 } catch (Exception e) { 107 throw new Error("failed to add exports for " + moduleName + "/" + packageName); 108 } 109 } 110 } 111 112 /* 113 * The set of visitors below will directly extend the most recent 114 * corresponding platform visitor type. 115 */ 116 117 @SupportedSourceVersion(RELEASE_23) 118 public static abstract class AbstractAnnotationValueVisitor<R, P> extends AbstractAnnotationValueVisitor14<R, P> { 119 120 /** 121 * Constructor for concrete subclasses to call. 122 */ 123 protected AbstractAnnotationValueVisitor() { 124 super(); 125 } 126 } 127 128 @SupportedSourceVersion(RELEASE_23) 129 public static abstract class AbstractElementVisitor<R, P> extends AbstractElementVisitor14<R, P> { 130 /** 131 * Constructor for concrete subclasses to call. 132 */ 133 protected AbstractElementVisitor(){ 134 super(); 135 } 136 } 137 138 @SupportedSourceVersion(RELEASE_23) 139 public static abstract class AbstractTypeVisitor<R, P> extends AbstractTypeVisitor14<R, P> { 140 /** 141 * Constructor for concrete subclasses to call. 142 */ 143 protected AbstractTypeVisitor() { 144 super(); 145 } 146 } 147 148 @SupportedSourceVersion(RELEASE_23) 149 public static class ElementKindVisitor<R, P> extends ElementKindVisitor14<R, P> { 150 /** 151 * Constructor for concrete subclasses; uses {@code null} for the 152 * default value. 153 */ 154 protected ElementKindVisitor() { 155 super(null); 156 } 157 158 /** 159 * Constructor for concrete subclasses; uses the argument for the 160 * default value. 161 * 162 * @param defaultValue the value to assign to {@link #DEFAULT_VALUE} 163 */ 164 protected ElementKindVisitor(R defaultValue) { 165 super(defaultValue); 166 } 167 } 168 169 @SupportedSourceVersion(RELEASE_23) 170 public static class ElementScanner<R, P> extends ElementScanner14<R, P> { 171 /** 172 * Constructor for concrete subclasses; uses {@code null} for the 173 * default value. 174 */ 175 protected ElementScanner(){ 176 super(null); 177 } 178 179 /** 180 * Constructor for concrete subclasses; uses the argument for the 181 * default value. 182 */ 183 protected ElementScanner(R defaultValue){ 184 super(defaultValue); 185 } 186 } 187 188 @SupportedSourceVersion(RELEASE_23) 189 public static class SimpleAnnotationValueVisitor<R, P> extends SimpleAnnotationValueVisitor14<R, P> { 190 /** 191 * Constructor for concrete subclasses; uses {@code null} for the 192 * default value. 193 */ 194 protected SimpleAnnotationValueVisitor() { 195 super(null); 196 } 197 198 /** 199 * Constructor for concrete subclasses; uses the argument for the 200 * default value. 201 * 202 * @param defaultValue the value to assign to {@link #DEFAULT_VALUE} 203 */ 204 protected SimpleAnnotationValueVisitor(R defaultValue) { 205 super(defaultValue); 206 } 207 } 208 209 @SupportedSourceVersion(RELEASE_23) 210 public static class SimpleElementVisitor<R, P> extends SimpleElementVisitor14<R, P> { 211 /** 212 * Constructor for concrete subclasses; uses {@code null} for the 213 * default value. 214 */ 215 protected SimpleElementVisitor(){ 216 super(null); 217 } 218 219 /** 220 * Constructor for concrete subclasses; uses the argument for the 221 * default value. 222 * 223 * @param defaultValue the value to assign to {@link #DEFAULT_VALUE} 224 */ 225 protected SimpleElementVisitor(R defaultValue){ 226 super(defaultValue); 227 } 228 } 229 230 @SupportedSourceVersion(RELEASE_23) 231 public static class SimpleTypeVisitor<R, P> extends SimpleTypeVisitor14<R, P> { 232 /** 233 * Constructor for concrete subclasses; uses {@code null} for the 234 * default value. 235 */ 236 protected SimpleTypeVisitor(){ 237 super(null); 238 } 239 240 /** 241 * Constructor for concrete subclasses; uses the argument for the 242 * default value. 243 * 244 * @param defaultValue the value to assign to {@link #DEFAULT_VALUE} 245 */ 246 protected SimpleTypeVisitor(R defaultValue){ 247 super(defaultValue); 248 } 249 } 250 251 @SupportedSourceVersion(RELEASE_23) 252 public static class TypeKindVisitor<R, P> extends TypeKindVisitor14<R, P> { 253 /** 254 * Constructor for concrete subclasses to call; uses {@code null} 255 * for the default value. 256 */ 257 protected TypeKindVisitor() { 258 super(null); 259 } 260 261 /** 262 * Constructor for concrete subclasses to call; uses the argument 263 * for the default value. 264 * 265 * @param defaultValue the value to assign to {@link #DEFAULT_VALUE} 266 */ 267 protected TypeKindVisitor(R defaultValue) { 268 super(defaultValue); 269 } 270 } 271 272 /** 273 * Vacuous implementation of javax.lang.model.util.Elements to aid 274 * in test development. Methods with defaults in the interface are 275 * *not* overridden to allow them to be tested. 276 */ 277 public static class VacuousElements implements Elements { 278 public VacuousElements() {} 279 280 @Override 281 public PackageElement getPackageElement(CharSequence name) {return null;} 282 283 @Override 284 public TypeElement getTypeElement(CharSequence name) {return null;} 285 286 @Override 287 public Map<? extends ExecutableElement, ? extends AnnotationValue> 288 getElementValuesWithDefaults(AnnotationMirror a) {return null;} 289 @Override 290 public String getDocComment(Element e) {return null;} 291 292 @Override 293 public boolean isDeprecated(Element e) {return false;} 294 295 @Override 296 public Name getBinaryName(TypeElement type) {return null;} 297 298 @Override 299 public PackageElement getPackageOf(Element e) {return null;} 300 301 @Override 302 public List<? extends Element> getAllMembers(TypeElement type) {return null;} 303 304 @Override 305 public List<? extends AnnotationMirror> getAllAnnotationMirrors(Element e) {return null;} 306 307 @Override 308 public boolean hides(Element hider, Element hidden) {return false;} 309 310 @Override 311 public boolean overrides(ExecutableElement overrider, 312 ExecutableElement overridden, 313 TypeElement type) {return false;} 314 315 @Override 316 public String getConstantExpression(Object value) {return null;} 317 318 @Override 319 public void printElements(Writer w, Element... elements) {} 320 321 @Override 322 public Name getName(CharSequence cs) {return null;} 323 324 @Override 325 public boolean isFunctionalInterface(TypeElement type) {return false;} 326 } 327 328 /** 329 * Vacuous implementation of javax.lang.model.util.Types to aid 330 * in test development. Methods with defaults in the interface are 331 * *not* overridden to allow them to be tested. 332 */ 333 public static class VacuousTypes implements Types { 334 public VacuousTypes() {} 335 336 @Override 337 public Element asElement(TypeMirror t) {return null;} 338 339 @Override 340 public boolean isSameType(TypeMirror t1, TypeMirror t2) {return false;} 341 342 @Override 343 public boolean isSubtype(TypeMirror t1, TypeMirror t2) {return false;}; 344 345 @Override 346 public boolean isAssignable(TypeMirror t1, TypeMirror t2) {return false;}; 347 348 @Override 349 public boolean contains(TypeMirror t1, TypeMirror t2) {return false;}; 350 351 @Override 352 public boolean isSubsignature(ExecutableType m1, ExecutableType m2) {return false;} 353 354 @Override 355 public List<? extends TypeMirror> directSupertypes(TypeMirror t) {return null;} 356 357 @Override 358 public TypeMirror erasure(TypeMirror t) {return null;} 359 360 @Override 361 public TypeElement boxedClass(PrimitiveType p) {return null;} 362 363 @Override 364 public PrimitiveType unboxedType(TypeMirror t) {return null;} 365 366 @Override 367 public TypeMirror capture(TypeMirror t) {return null;} 368 369 @Override 370 public PrimitiveType getPrimitiveType(TypeKind kind) {return null;} 371 372 @Override 373 public NullType getNullType() {return null;} 374 375 @Override 376 public NoType getNoType(TypeKind kind) {return null;} 377 378 @Override 379 public ArrayType getArrayType(TypeMirror componentType) {return null;} 380 381 @Override 382 public WildcardType getWildcardType(TypeMirror extendsBound, 383 TypeMirror superBound) {return null;} 384 385 @Override 386 public DeclaredType getDeclaredType(TypeElement typeElem, TypeMirror... typeArgs) {return null;} 387 388 389 @Override 390 public DeclaredType getDeclaredType(DeclaredType containing, 391 TypeElement typeElem, TypeMirror... typeArgs) {return null;} 392 393 @Override 394 public TypeMirror asMemberOf(DeclaredType containing, Element element) {return null;} 395 } 396 }