aboutsummaryrefslogtreecommitdiff
path: root/original_panno/com/hanhuy/panno/processing/ASTTreeToSource.java
blob: 2e7dc641193c10141b35392736ff1eb7a4625d5c (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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
 * Copyright 2007 Perry Nguyen <pfnguyen@hanhuy.com> Licensed under the Apache
 * License, Version 2.0 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.hanhuy.panno.processing;

import java.util.Map;
import java.util.Stack;

import javax.annotation.processing.Messager;
import javax.lang.model.element.Modifier;
import javax.tools.Diagnostic;

import com.hanhuy.panno.Property;
import com.sun.source.tree.AnnotationTree;
import com.sun.source.tree.BlockTree;
import com.sun.source.tree.ClassTree;
import com.sun.source.tree.CompilationUnitTree;
import com.sun.source.tree.IdentifierTree;
import com.sun.source.tree.ImportTree;
import com.sun.source.tree.MethodTree;
import com.sun.source.tree.StatementTree;
import com.sun.source.tree.VariableTree;
import com.sun.source.util.TreeScanner;

/**
 * Old attempt to get the annotation working, have a better approach now.
 */
@Deprecated
class ASTTreeToSource extends TreeScanner<Void, StringBuilder> {
	public final static String PROPERTY_ANNOTATION_FULL = "com.hanhuy.panno.Property";
	public final static String PROPERTY_ANNOTATION = "Property";
	
	private final Stack<String> currentClassNameStack = new Stack<String>();
	private final Messager messager;
	private final Map<String, Property> fieldMap;
	private String currentClassName;
	private String currentField;
	private String currentType;
	
	ASTTreeToSource(Map<String, Property> fieldMap, Messager messager) {
		this.fieldMap = fieldMap;
		this.messager = messager;
	}
	
	/**
	 * Handle initializer blocks, both static and non.
	 */
	@Override
	public Void visitBlock(BlockTree node, StringBuilder p) {
		p.append("\n");
		if ( node.isStatic() ) p.append("static ");
		p.append("{\n");
		for ( StatementTree st : node.getStatements() ) {
			p.append(st);
			p.append(";");
		}
		p.append("\n}\n");
		return null;
	}
	
	/**
	 * Detect our annotation and add accessors if necessary.
	 */
	@Override
	public Void visitAnnotation(AnnotationTree node, StringBuilder p) {
		String anno = ((IdentifierTree)node.getAnnotationType()).getName().toString();
		if ( anno != null && PROPERTY_ANNOTATION.equals(anno.trim()) ) {
			Property pAnno = fieldMap.get(currentClassName + "." + currentField);
			
			String propName = currentField;
			if ( !"".equals(pAnno.name()) ) propName = pAnno.name();
			propName = PropertyProcessor.ucfirst(propName);
			
			if ( pAnno.writeOnly() && pAnno.readOnly() ) {
				messager.printMessage(Diagnostic.Kind.ERROR, "@Property's writeOnly and readOnly may not both be true");
			}
			
			if ( !"boolean".equals(currentType) && pAnno.useGet() ) {
				messager.printMessage(Diagnostic.Kind.WARNING, "@Property's useGet is only for booleans");
			}
			
			if ( !pAnno.writeOnly() ) {
				// generate a getter
				p.append("\n");
				p.append("public ");
				p.append(currentType);
				if ( !"boolean".equals(currentType) || pAnno.useGet() ) p.append(" get");
				else p.append(" is");
				p.append(propName);
				p.append("() { return ");
				p.append(currentField);
				p.append("; }");
			}
			if ( !pAnno.readOnly() ) {
				// generate a setter
				p.append("\n");
				p.append("public void set");
				p.append(propName);
				p.append("(");
				p.append(currentType);
				p.append(" ");
				p.append(currentField);
				p.append(") { this.");
				p.append(currentField);
				p.append(" = ");
				p.append(currentField);
				p.append("; }");
			}
		}
		return null;
	}
	
	/**
	 * Gather file information. Then continue to visit classes.
	 */
	@Override
	public Void visitCompilationUnit(CompilationUnitTree node, StringBuilder p) {
		new Exception("Visit CUT: " + node.getSourceFile()).printStackTrace();
		p.append("// Generated from: ");
		p.append(node.getSourceFile());
		p.append("\n\npackage ");
		p.append(node.getPackageName());
		p.append(";\n\n");
		
		for ( ImportTree it : node.getImports() ) {
			if ( PROPERTY_ANNOTATION_FULL.equals(it.getQualifiedIdentifier().toString().trim()) ) {
				continue;
			}
			p.append("import ");
			if ( it.isStatic() ) p.append("static ");
			p.append(it.getQualifiedIdentifier());
			p.append(";\n");
		}
		super.visitCompilationUnit(node, p);
		return null;
	}
	
	private void pushClass(String className) {
		currentClassName = className;
		currentClassNameStack.push(currentClassName);
	}
	
	private void popClass() {
		currentClassNameStack.pop();
		if ( currentClassNameStack.size() > 0 ) currentClassName = currentClassNameStack.peek();
	}
	
	/**
	 * Reproduce class declarations, and go on to handle methods, blocks and
	 * variables.
	 */
	@Override
	public Void visitClass(ClassTree node, StringBuilder p) {
		pushClass(node.getSimpleName().toString());
		
		// Check to see if this is an enum
		// I have no idea what will happen if we encounter an AST
		// implementation that is not Sun's (or if this has changed in java7)
		if ( node instanceof com.sun.tools.javac.tree.JCTree.JCClassDecl ) {
			com.sun.tools.javac.tree.JCTree.JCClassDecl jcd = (com.sun.tools.javac.tree.JCTree.JCClassDecl)node;
			if ( (jcd.mods.flags & 16384L) != 0L ) {
				p.append(node.getModifiers());
				p.append(" enum ");
				p.append(currentClassName);
				appendEnumBody(jcd.defs, p);
				
				popClass();
				
				return null;
			}
		}
		
		p.append("\n");
		p.append(node.getModifiers());
		p.append(" class ");
		p.append(currentClassName);
		if ( node.getTypeParameters().size() > 0 ) {
			p.append("<");
			p.append(node.getTypeParameters());
			p.append(">");
		}
		if ( node.getExtendsClause() != null ) {
			p.append("\nextends ");
			p.append(node.getExtendsClause());
		}
		if ( node.getImplementsClause().size() > 0 ) {
			p.append("\nimplements ");
			p.append(node.getImplementsClause());
		}
		
		p.append("\n{");
		super.visitClass(node, p);
		p.append("\n}\n");
		
		popClass();
		
		return null;
	}
	
	/**
	 * Enums appear to be a special case. We must reconstitute its source in a
	 * different manner from the rest. Need to remove any 'super()' statements
	 * and print the lines appropriately.
	 */
	public void appendEnumBody(com.sun.tools.javac.util.List<com.sun.tools.javac.tree.JCTree> list, StringBuilder p) {
		p.append("{\n");
		boolean flag = true;
		
		for ( com.sun.tools.javac.util.List<com.sun.tools.javac.tree.JCTree> list1 = list; list1.nonEmpty(); list1 = list1.tail ) {
			
			if ( !isEnumerator(list1.head) ) continue;
			
			if ( !flag ) p.append(",\n");
			
			p.append(list1.head);
			flag = false;
		}
		
		p.append(";\n");
		for ( com.sun.tools.javac.util.List<com.sun.tools.javac.tree.JCTree> list2 = list; list2.nonEmpty(); list2 = list2.tail ) {
			if ( !isEnumerator(list2.head) ) {
				if ( list2.head instanceof MethodTree ) visitMethod((MethodTree)list2.head, p);
				else p.append(list2.head);
				p.append("\n");
			}
		}
		
		p.append("}");
	}
	
	private boolean isEnumerator(com.sun.tools.javac.tree.JCTree jctree) {
		return jctree.tag == 5 && (((com.sun.tools.javac.tree.JCTree.JCVariableDecl)jctree).mods.flags & 16384L) != 0L;
	}
	
	/**
	 * Reconstitute methods. We could just do node.toString() here, but
	 * constructors need to be renamed from &lt;init&gt; to the class name, so
	 * handle all methods.
	 */
	@Override
	public Void visitMethod(MethodTree node, StringBuilder p) {
		p.append("\n");
		p.append(node.getModifiers());
		p.append(" ");
		if ( node.getReturnType() != null ) p.append(node.getReturnType());
		if ( node.getTypeParameters().size() > 0 ) {
			p.append(" <");
			p.append(node.getTypeParameters());
			p.append(">");
		}
		p.append(" ");
		if ( node.getName() != null && "<init>".equals(node.getName().toString().trim()) ) p.append(currentClassName);
		else p.append(node.getName());
		
		p.append("(");
		p.append(node.getParameters());
		p.append(")");
		if ( node.getThrows().size() > 0 ) {
			p.append("\nthrows ");
			p.append(node.getThrows());
		}
		
		p.append(" {\n");
		// this needs to be done for enums, otherwise we'll get a compile error
		// if we didn't need this case, we'd just do p.append(node.getBody())
		for ( StatementTree st : node.getBody().getStatements() ) {
			if ( "super()".equals(st.toString().trim()) ) continue;
			p.append(st);
			if ( p.charAt(p.length() - 1) != ';' ) p.append(";\n");
		}
		p.append("\n}\n");
		return null;
	}
	
	/**
	 * Reconstitute class fields. While doing so, search for our annotation to
	 * perform processing.
	 */
	@Override
	public Void visitVariable(VariableTree node, StringBuilder p) {
		currentField = node.getName().toString();
		currentType = node.getType().toString();
		p.append("\n");
		
		for ( AnnotationTree a : node.getModifiers().getAnnotations() ) {
			String anno = ((IdentifierTree)a.getAnnotationType()).getName().toString();
			if ( !PROPERTY_ANNOTATION.equals(anno) ) {
				p.append(a);
				p.append("\n");
			}
		}
		
		for ( Modifier m : node.getModifiers().getFlags() ) {
			p.append(m.toString());
			p.append(" ");
		}
		p.append(" ");
		p.append(currentType);
		p.append(" ");
		p.append(currentField);
		
		if ( node.getInitializer() != null ) {
			p.append(" = ");
			p.append(node.getInitializer());
		}
		p.append(";");
		super.visitVariable(node, p);
		return null;
	}
}