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.*;
36
public abstract class JavacTestingAbstractProcessor extends AbstractProcessor {
37
private static final Set<String> allAnnotations = Set.of("*");
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;
50
protected JavacTestingAbstractProcessor() {
60
public SourceVersion getSupportedSourceVersion() {
61
SupportedSourceVersion ssv = this.getClass().getAnnotation(SupportedSourceVersion.class);
63
throw new IllegalStateException("SupportedSourceVersion annotation not supported here.");
65
return SourceVersion.latest();
79
public Set<String> getSupportedAnnotationTypes() {
80
SupportedAnnotationTypes sat = this.getClass().getAnnotation(SupportedAnnotationTypes.class);
82
return super.getSupportedAnnotationTypes();
84
return allAnnotations;
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();
97
protected void addExports(String moduleName, String... packageNames) {
98
for (String packageName : packageNames) {
100
ModuleLayer layer = ModuleLayer.boot();
101
Optional<Module> m = layer.findModule(moduleName);
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);
116
@SupportedSourceVersion(RELEASE_24)
117
@SuppressWarnings("preview")
118
public static abstract class AbstractAnnotationValueVisitor<R, P> extends AbstractAnnotationValueVisitorPreview<R, P> {
123
protected AbstractAnnotationValueVisitor() {
128
@SupportedSourceVersion(RELEASE_24)
129
@SuppressWarnings("preview")
130
public static abstract class AbstractElementVisitor<R, P> extends AbstractElementVisitorPreview<R, P> {
134
protected AbstractElementVisitor(){
139
@SupportedSourceVersion(RELEASE_24)
140
@SuppressWarnings("preview")
141
public static abstract class AbstractTypeVisitor<R, P> extends AbstractTypeVisitorPreview<R, P> {
145
protected AbstractTypeVisitor() {
150
@SupportedSourceVersion(RELEASE_24)
151
@SuppressWarnings("preview")
152
public static class ElementKindVisitor<R, P> extends ElementKindVisitorPreview<R, P> {
157
protected ElementKindVisitor() {
167
protected ElementKindVisitor(R defaultValue) {
172
@SupportedSourceVersion(RELEASE_24)
173
@SuppressWarnings("preview")
174
public static class ElementScanner<R, P> extends ElementScannerPreview<R, P> {
179
protected ElementScanner(){
187
protected ElementScanner(R defaultValue){
192
@SupportedSourceVersion(RELEASE_24)
193
@SuppressWarnings("preview")
194
public static class SimpleAnnotationValueVisitor<R, P> extends SimpleAnnotationValueVisitorPreview<R, P> {
199
protected SimpleAnnotationValueVisitor() {
209
protected SimpleAnnotationValueVisitor(R defaultValue) {
214
@SupportedSourceVersion(RELEASE_24)
215
@SuppressWarnings("preview")
216
public static class SimpleElementVisitor<R, P> extends SimpleElementVisitorPreview<R, P> {
221
protected SimpleElementVisitor(){
231
protected SimpleElementVisitor(R defaultValue){
236
@SupportedSourceVersion(RELEASE_24)
237
@SuppressWarnings("preview")
238
public static class SimpleTypeVisitor<R, P> extends SimpleTypeVisitorPreview<R, P> {
243
protected SimpleTypeVisitor(){
253
protected SimpleTypeVisitor(R defaultValue){
258
@SupportedSourceVersion(RELEASE_24)
259
@SuppressWarnings("preview")
260
public static class TypeKindVisitor<R, P> extends TypeKindVisitorPreview<R, P> {
265
protected TypeKindVisitor() {
275
protected TypeKindVisitor(R defaultValue) {
285
public static class VacuousElements implements Elements {
286
public VacuousElements() {}
289
public PackageElement getPackageElement(CharSequence name) {return null;}
292
public TypeElement getTypeElement(CharSequence name) {return null;}
295
public Map<? extends ExecutableElement, ? extends AnnotationValue>
296
getElementValuesWithDefaults(AnnotationMirror a) {return null;}
298
public String getDocComment(Element e) {return null;}
301
public boolean isDeprecated(Element e) {return false;}
304
public Name getBinaryName(TypeElement type) {return null;}
307
public PackageElement getPackageOf(Element e) {return null;}
310
public List<? extends Element> getAllMembers(TypeElement type) {return null;}
313
public List<? extends AnnotationMirror> getAllAnnotationMirrors(Element e) {return null;}
316
public boolean hides(Element hider, Element hidden) {return false;}
319
public boolean overrides(ExecutableElement overrider,
320
ExecutableElement overridden,
321
TypeElement type) {return false;}
324
public String getConstantExpression(Object value) {return null;}
327
public void printElements(Writer w, Element... elements) {}
330
public Name getName(CharSequence cs) {return null;}
333
public boolean isFunctionalInterface(TypeElement type) {return false;}
341
public static class VacuousTypes implements Types {
342
public VacuousTypes() {}
345
public Element asElement(TypeMirror t) {return null;}
348
public boolean isSameType(TypeMirror t1, TypeMirror t2) {return false;}
351
public boolean isSubtype(TypeMirror t1, TypeMirror t2) {return false;};
354
public boolean isAssignable(TypeMirror t1, TypeMirror t2) {return false;};
357
public boolean contains(TypeMirror t1, TypeMirror t2) {return false;};
360
public boolean isSubsignature(ExecutableType m1, ExecutableType m2) {return false;}
363
public List<? extends TypeMirror> directSupertypes(TypeMirror t) {return null;}
366
public TypeMirror erasure(TypeMirror t) {return null;}
369
public TypeElement boxedClass(PrimitiveType p) {return null;}
372
public PrimitiveType unboxedType(TypeMirror t) {return null;}
375
public TypeMirror capture(TypeMirror t) {return null;}
378
public PrimitiveType getPrimitiveType(TypeKind kind) {return null;}
381
public NullType getNullType() {return null;}
384
public NoType getNoType(TypeKind kind) {return null;}
387
public ArrayType getArrayType(TypeMirror componentType) {return null;}
390
public WildcardType getWildcardType(TypeMirror extendsBound,
391
TypeMirror superBound) {return null;}
394
public DeclaredType getDeclaredType(TypeElement typeElem, TypeMirror... typeArgs) {return null;}
398
public DeclaredType getDeclaredType(DeclaredType containing,
399
TypeElement typeElem, TypeMirror... typeArgs) {return null;}
402
public TypeMirror asMemberOf(DeclaredType containing, Element element) {return null;}