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 Optional<Module> m = layer.findModule(moduleName); 102 if (!m.isPresent()) 103 throw new Error("module not found: " + moduleName); 104 m.get().addExports(packageName, getClass().getModule()); 105 } catch (Exception e) { 106 throw new Error("failed to add exports for " + moduleName + "/" + packageName); 107 } 108 } 109 } 110 111 /* 112 * The set of visitors below will directly extend the most recent 113 * corresponding platform visitor type. 114 */ 115 116 @SupportedSourceVersion(RELEASE_23) 117 public static abstract class AbstractAnnotationValueVisitor<R, P> extends AbstractAnnotationValueVisitor14<R, P> { 118 119 /** 120 * Constructor for concrete subclasses to call. 121 */ 122 protected AbstractAnnotationValueVisitor() { 123 super(); 124 } 125 } 126 127 @SupportedSourceVersion(RELEASE_23) 128 public static abstract class AbstractElementVisitor<R, P> extends AbstractElementVisitor14<R, P> { 129 /** 130 * Constructor for concrete subclasses to call. 131 */ 132 protected AbstractElementVisitor(){ 133 super(); 134 } 135 } 136 137 @SupportedSourceVersion(RELEASE_23) 138 public static abstract class AbstractTypeVisitor<R, P> extends AbstractTypeVisitor14<R, P> { 139 /** 140 * Constructor for concrete subclasses to call. 141 */ 142 protected AbstractTypeVisitor() { 143 super(); 144 } 145 } 146 147 @SupportedSourceVersion(RELEASE_23) 148 public static class ElementKindVisitor<R, P> extends ElementKindVisitor14<R, P> { 149 /** 150 * Constructor for concrete subclasses; uses {@code null} for the 151 * default value. 152 */ 153 protected ElementKindVisitor() { 154 super(null); 155 } 156 157 /** 158 * Constructor for concrete subclasses; uses the argument for the 159 * default value. 160 * 161 * @param defaultValue the value to assign to {@link #DEFAULT_VALUE} 162 */ 163 protected ElementKindVisitor(R defaultValue) { 164 super(defaultValue); 165 } 166 } 167 168 @SupportedSourceVersion(RELEASE_23) 169 public static class ElementScanner<R, P> extends ElementScanner14<R, P> { 170 /** 171 * Constructor for concrete subclasses; uses {@code null} for the 172 * default value. 173 */ 174 protected ElementScanner(){ 175 super(null); 176 } 177 178 /** 179 * Constructor for concrete subclasses; uses the argument for the 180 * default value. 181 */ 182 protected ElementScanner(R defaultValue){ 183 super(defaultValue); 184 } 185 } 186 187 @SupportedSourceVersion(RELEASE_23) 188 public static class SimpleAnnotationValueVisitor<R, P> extends SimpleAnnotationValueVisitor14<R, P> { 189 /** 190 * Constructor for concrete subclasses; uses {@code null} for the 191 * default value. 192 */ 193 protected SimpleAnnotationValueVisitor() { 194 super(null); 195 } 196 197 /** 198 * Constructor for concrete subclasses; uses the argument for the 199 * default value. 200 * 201 * @param defaultValue the value to assign to {@link #DEFAULT_VALUE} 202 */ 203 protected SimpleAnnotationValueVisitor(R defaultValue) { 204 super(defaultValue); 205 } 206 } 207 208 @SupportedSourceVersion(RELEASE_23) 209 public static class SimpleElementVisitor<R, P> extends SimpleElementVisitor14<R, P> { 210 /** 211 * Constructor for concrete subclasses; uses {@code null} for the 212 * default value. 213 */ 214 protected SimpleElementVisitor(){ 215 super(null); 216 } 217 218 /** 219 * Constructor for concrete subclasses; uses the argument for the 220 * default value. 221 * 222 * @param defaultValue the value to assign to {@link #DEFAULT_VALUE} 223 */ 224 protected SimpleElementVisitor(R defaultValue){ 225 super(defaultValue); 226 } 227 } 228 229 @SupportedSourceVersion(RELEASE_23) 230 public static class SimpleTypeVisitor<R, P> extends SimpleTypeVisitor14<R, P> { 231 /** 232 * Constructor for concrete subclasses; uses {@code null} for the 233 * default value. 234 */ 235 protected SimpleTypeVisitor(){ 236 super(null); 237 } 238 239 /** 240 * Constructor for concrete subclasses; uses the argument for the 241 * default value. 242 * 243 * @param defaultValue the value to assign to {@link #DEFAULT_VALUE} 244 */ 245 protected SimpleTypeVisitor(R defaultValue){ 246 super(defaultValue); 247 } 248 } 249 250 @SupportedSourceVersion(RELEASE_23) 251 public static class TypeKindVisitor<R, P> extends TypeKindVisitor14<R, P> { 252 /** 253 * Constructor for concrete subclasses to call; uses {@code null} 254 * for the default value. 255 */ 256 protected TypeKindVisitor() { 257 super(null); 258 } 259 260 /** 261 * Constructor for concrete subclasses to call; uses the argument 262 * for the default value. 263 * 264 * @param defaultValue the value to assign to {@link #DEFAULT_VALUE} 265 */ 266 protected TypeKindVisitor(R defaultValue) { 267 super(defaultValue); 268 } 269 } 270 271 /** 272 * Vacuous implementation of javax.lang.model.util.Elements to aid 273 * in test development. Methods with defaults in the interface are 274 * *not* overridden to allow them to be tested. 275 */ 276 public static class VacuousElements implements Elements { 277 public VacuousElements() {} 278 279 @Override 280 public PackageElement getPackageElement(CharSequence name) {return null;} 281 282 @Override 283 public TypeElement getTypeElement(CharSequence name) {return null;} 284 285 @Override 286 public Map<? extends ExecutableElement, ? extends AnnotationValue> 287 getElementValuesWithDefaults(AnnotationMirror a) {return null;} 288 @Override 289 public String getDocComment(Element e) {return null;} 290 291 @Override 292 public boolean isDeprecated(Element e) {return false;} 293 294 @Override 295 public Name getBinaryName(TypeElement type) {return null;} 296 297 @Override 298 public PackageElement getPackageOf(Element e) {return null;} 299 300 @Override 301 public List<? extends Element> getAllMembers(TypeElement type) {return null;} 302 303 @Override 304 public List<? extends AnnotationMirror> getAllAnnotationMirrors(Element e) {return null;} 305 306 @Override 307 public boolean hides(Element hider, Element hidden) {return false;} 308 309 @Override 310 public boolean overrides(ExecutableElement overrider, 311 ExecutableElement overridden, 312 TypeElement type) {return false;} 313 314 @Override 315 public String getConstantExpression(Object value) {return null;} 316 317 @Override 318 public void printElements(Writer w, Element... elements) {} 319 320 @Override 321 public Name getName(CharSequence cs) {return null;} 322 323 @Override 324 public boolean isFunctionalInterface(TypeElement type) {return false;} 325 } 326 327 /** 328 * Vacuous implementation of javax.lang.model.util.Types to aid 329 * in test development. Methods with defaults in the interface are 330 * *not* overridden to allow them to be tested. 331 */ 332 public static class VacuousTypes implements Types { 333 public VacuousTypes() {} 334 335 @Override 336 public Element asElement(TypeMirror t) {return null;} 337 338 @Override 339 public boolean isSameType(TypeMirror t1, TypeMirror t2) {return false;} 340 341 @Override 342 public boolean isSubtype(TypeMirror t1, TypeMirror t2) {return false;}; 343 344 @Override 345 public boolean isAssignable(TypeMirror t1, TypeMirror t2) {return false;}; 346 347 @Override 348 public boolean contains(TypeMirror t1, TypeMirror t2) {return false;}; 349 350 @Override 351 public boolean isSubsignature(ExecutableType m1, ExecutableType m2) {return false;} 352 353 @Override 354 public List<? extends TypeMirror> directSupertypes(TypeMirror t) {return null;} 355 356 @Override 357 public TypeMirror erasure(TypeMirror t) {return null;} 358 359 @Override 360 public TypeElement boxedClass(PrimitiveType p) {return null;} 361 362 @Override 363 public PrimitiveType unboxedType(TypeMirror t) {return null;} 364 365 @Override 366 public TypeMirror capture(TypeMirror t) {return null;} 367 368 @Override 369 public PrimitiveType getPrimitiveType(TypeKind kind) {return null;} 370 371 @Override 372 public NullType getNullType() {return null;} 373 374 @Override 375 public NoType getNoType(TypeKind kind) {return null;} 376 377 @Override 378 public ArrayType getArrayType(TypeMirror componentType) {return null;} 379 380 @Override 381 public WildcardType getWildcardType(TypeMirror extendsBound, 382 TypeMirror superBound) {return null;} 383 384 @Override 385 public DeclaredType getDeclaredType(TypeElement typeElem, TypeMirror... typeArgs) {return null;} 386 387 388 @Override 389 public DeclaredType getDeclaredType(DeclaredType containing, 390 TypeElement typeElem, TypeMirror... typeArgs) {return null;} 391 392 @Override 393 public TypeMirror asMemberOf(DeclaredType containing, Element element) {return null;} 394 } 395 }