aboutsummaryrefslogtreecommitdiff
path: root/src/lombok/javac/JavacASTVisitor.java
blob: 714ae2949a288e0256d5a3e9f12c8b95fc081dd0 (plain)
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package lombok.javac;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;

import lombok.javac.JavacAST.Node;

import com.sun.tools.javac.code.Flags;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.JCTree.JCAnnotation;
import com.sun.tools.javac.tree.JCTree.JCBlock;
import com.sun.tools.javac.tree.JCTree.JCClassDecl;
import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
import com.sun.tools.javac.tree.JCTree.JCMethodDecl;
import com.sun.tools.javac.tree.JCTree.JCVariableDecl;

public interface JavacASTVisitor {
	/**
	 * Called at the very beginning and end.
	 */
	void visitCompilationUnit(Node top, JCCompilationUnit unit);
	void endVisitCompilationUnit(Node top, JCCompilationUnit unit);
	
	/**
	 * Called when visiting a type (a class, interface, annotation, enum, etcetera).
	 */
	void visitType(Node typeNode, JCClassDecl type);
	void visitAnnotationOnType(JCClassDecl type, Node annotationNode, JCAnnotation annotation);
	void endVisitType(Node typeNode, JCClassDecl type);
	
	/**
	 * Called when visiting a field of a class.
	 */
	void visitField(Node fieldNode, JCVariableDecl field);
	void visitAnnotationOnField(JCVariableDecl field, Node annotationNode, JCAnnotation annotation);
	void endVisitField(Node fieldNode, JCVariableDecl field);
	
	/**
	 * Called for static and instance initializers. You can tell the difference via the isStatic() method.
	 */
	void visitInitializer(Node initializerNode, JCBlock initializer);
	void endVisitInitializer(Node initializerNode, JCBlock initializer);
	
	/**
	 * Called for both methods and constructors.
	 */
	void visitMethod(Node methodNode, JCMethodDecl method);
	void visitAnnotationOnMethod(JCMethodDecl method, Node annotationNode, JCAnnotation annotation);
	void endVisitMethod(Node methodNode, JCMethodDecl method);
	
	/**
	 * Visits a method argument.
	 */
	void visitMethodArgument(Node argumentNode, JCVariableDecl argument, JCMethodDecl method);
	void visitAnnotationOnMethodArgument(JCVariableDecl argument, JCMethodDecl method, Node annotationNode, JCAnnotation annotation);
	void endVisitMethodArgument(Node argumentNode, JCVariableDecl argument, JCMethodDecl method);
	
	/**
	 * Visits a local declaration - that is, something like 'int x = 10;' on the method level. Also called
	 * for method parameters.
	 */
	void visitLocal(Node localNode, JCVariableDecl local);
	void visitAnnotationOnLocal(JCVariableDecl local, Node annotationNode, JCAnnotation annotation);
	void endVisitLocal(Node localNode, JCVariableDecl local);
	
	/**
	 * Visits a statement that isn't any of the other visit methods (e.g. JCClassDecl).
	 * The statement object is guaranteed to be either a JCStatement or a JCExpression.
	 */
	void visitStatement(Node statementNode, JCTree statement);
	void endVisitStatement(Node statementNode, JCTree statement);
	
	public static class Printer implements JavacASTVisitor {
		private final PrintStream out;
		
		public Printer() {
			this(System.out);
		}
		
		public Printer(File file) throws FileNotFoundException {
			this(new PrintStream(file));
		}
		
		public Printer(PrintStream out) {
			this.out = out;
		}
		
		int indent = 0;
		private void print(String text, Object... params) {
			StringBuilder sb = new StringBuilder();
			for ( int i = 0 ; i < indent ; i++ ) sb.append("  ");
			out.printf(sb.append(text).append('\n').toString(), params);
			out.flush();
		}
		
		@Override public void visitCompilationUnit(Node Node, JCCompilationUnit unit) {
			out.println("---------------------------------------------------------");
			
			print("<CU %s>", Node.getFileName());
			indent++;
		}
		
		@Override public void endVisitCompilationUnit(Node node, JCCompilationUnit unit) {
			indent--;
			print("</CUD>");
		}
		
		@Override public void visitType(Node node, JCClassDecl type) {
			print("<TYPE %s>", type.name);
			indent++;
		}
		
		@Override public void visitAnnotationOnType(JCClassDecl type, Node node, JCAnnotation annotation) {
			print("<ANNOTATION: %s />", annotation);
		}
		
		@Override public void endVisitType(Node node, JCClassDecl type) {
			indent--;
			print("</TYPE %s>", type.name);
		}
		
		@Override public void visitInitializer(Node node, JCBlock initializer) {
			print("<%s INITIALIZER>",
					initializer.isStatic() ? "static" : "instance");
			indent++;
		}
		
		@Override public void endVisitInitializer(Node node, JCBlock initializer) {
			indent--;
			print("</%s INITIALIZER>", initializer.isStatic() ? "static" : "instance");
		}
		
		@Override public void visitField(Node node, JCVariableDecl field) {
			print("<FIELD %s %s>", field.vartype, field.name);
			indent++;
		}
		
		@Override public void visitAnnotationOnField(JCVariableDecl field, Node node, JCAnnotation annotation) {
			print("<ANNOTATION: %s />", annotation);
		}
		
		@Override public void endVisitField(Node node, JCVariableDecl field) {
			indent--;
			print("</FIELD %s %s>", field.vartype, field.name);
		}
		
		@Override public void visitMethod(Node node, JCMethodDecl method) {
			final String type;
			if ( method.name.contentEquals("<init>") ) {
				if ( (method.mods.flags & Flags.GENERATEDCONSTR) != 0 ) {
					type = "DEFAULTCONSTRUCTOR";
				} else type = "CONSTRUCTOR";
			} else type = "METHOD";
			print("<%s %s> returns: %s", type, method.name, method.restype);
			indent++;
		}
		
		@Override public void visitAnnotationOnMethod(JCMethodDecl method, Node node, JCAnnotation annotation) {
			print("<ANNOTATION: %s />", annotation);
		}
		
		@Override public void endVisitMethod(Node node, JCMethodDecl method) {
			indent--;
			print("</%s %s>", "XMETHOD", method.name);
		}
		
		@Override public void visitMethodArgument(Node node, JCVariableDecl arg, JCMethodDecl method) {
			print("<METHODARG %s %s>", arg.vartype, arg.name);
			indent++;
		}
		
		@Override public void visitAnnotationOnMethodArgument(JCVariableDecl arg, JCMethodDecl method, Node nodeAnnotation, JCAnnotation annotation) {
			print("<ANNOTATION: %s />", annotation);
		}
		
		@Override public void endVisitMethodArgument(Node node, JCVariableDecl arg, JCMethodDecl method) {
			indent--;
			print("</METHODARG %s %s>", arg.vartype, arg.name);
		}
		
		@Override public void visitLocal(Node node, JCVariableDecl local) {
			print("<LOCAL %s %s>", local.vartype, local.name);
			indent++;
		}
		
		@Override public void visitAnnotationOnLocal(JCVariableDecl local, Node node, JCAnnotation annotation) {
			print("<ANNOTATION: %s />", annotation);
		}
		
		@Override public void endVisitLocal(Node node, JCVariableDecl local) {
			indent--;
			print("</LOCAL %s %s>", local.vartype, local.name);
		}
		
		@Override public void visitStatement(Node node, JCTree statement) {
			print("<%s>", statement.getClass());
			indent++;
			print("%s", statement);
		}
		
		@Override public void endVisitStatement(Node node, JCTree statement) {
			indent--;
			print("</%s>", statement.getClass());
		}
	}
}