-
Notifications
You must be signed in to change notification settings - Fork 1
/
FullQualifier.java
91 lines (84 loc) · 3 KB
/
FullQualifier.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import java.io.*;
import java.util.*;
import javax.annotation.processing.*;
import javax.lang.model.*;
import javax.lang.model.element.*;
import com.sun.source.tree.*;
import com.sun.source.util.*;
import com.sun.tools.javac.model.*;
import com.sun.tools.javac.processing.*;
import com.sun.tools.javac.tree.*;
import com.sun.tools.javac.util.*;
@SupportedAnnotationTypes("*")
@SupportedOptions("replaces")
@SupportedSourceVersion(SourceVersion.RELEASE_17)
public class FullQualifier extends AbstractProcessor {
private Trees trees;
private boolean imported = false;
@Override
public synchronized void init(ProcessingEnvironment env) {
super.init(env);
trees = Trees.instance(env);
Context context = ((JavacProcessingEnvironment) env).getContext();
TreeMaker.instance(context);
JavacElements.instance(context);
}
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
if (!roundEnv.processingOver()) {
Set<? extends Element> elements = roundEnv.getRootElements();
for (Element each : elements) {
if (each.getKind() == ElementKind.CLASS) {
JCTree tree = (JCTree) trees.getTree(each);
if (!imported) {
imported = true;
TreePath path = trees.getPath(each);
java.util.List<? extends ImportTree> imports = path.getCompilationUnit().getImports();
for (Object o : imports) {
System.out.print(o);
}
}
StringWriter stringWriter = new StringWriter();
try {
new FullQualifierPrettyPrinter(stringWriter, false).printExpr(tree);
} catch (IOException e) {
throw new AssertionError(e);
}
System.out.println(stringWriter);
}
}
// even with -proc:only the javac does some semantic checking
// to avoid that (we compile the generated files anyway in the next step), exit in a clean way
System.exit(0);
}
return false;
}
private static class FullQualifierPrettyPrinter extends JavaSourcePrettyPrinter {
public FullQualifierPrettyPrinter(Writer out, boolean sourceOutput) {
super(out, sourceOutput);
}
@Override
public void visitAnnotation(JCTree.JCAnnotation tree) {
try {
print("@");
String before = tree.annotationType.toString();
switch (before) {
case "After", "Before", "Ignore", "FixMethodOrder", "Rule", "ClassRule", "Test" -> print("org.junit.");
case "RunWith" -> print("org.junit.runner.");
case "Parameters" -> print("org.junit.runners.Parameterized.");
case "CompareInterface", "Ex", "Exercises", "Forbidden", "InitializeOnce", "NotForbidden", "Points", "Replace", "SafeCallers", "SecretClass" -> print("tester.annotations.");
}
printExpr(tree.annotationType);
print("(");
if (before.equals("RunWith") && tree.args.head.toString().equals("value = Parameterized.class")) {
print("value = org.junit.runners.Parameterized.class");
} else {
printExprs(tree.args);
}
print(")");
} catch (IOException e) {
throw new Error("something failed while pretty printing annotations: " + e);
}
}
}
}