public
Created

Code generation

  • Download Gist
Generator.java
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 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
/*
* Copyright (C) 2011 Victor Nazarov <asviraspossible@gmail.com>
*/
 
package pojoframework;
 
import com.sun.source.tree.AnnotationTree;
import com.sun.source.tree.ClassTree;
import com.sun.source.tree.CompilationUnitTree;
import com.sun.source.tree.ExpressionTree;
import com.sun.source.tree.IdentifierTree;
import com.sun.source.tree.ImportTree;
import com.sun.source.tree.MemberSelectTree;
import com.sun.source.tree.MethodTree;
import com.sun.source.tree.ModifiersTree;
import com.sun.source.tree.Tree;
import com.sun.source.tree.TypeParameterTree;
import com.sun.source.tree.VariableTree;
import com.sun.source.util.TreePath;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.TypeElement;
 
/**
*
* @author vir
*/
class Generator {
private final TypeElement annotationType;
private final TemplateRenderer annotation;
private final TreePath treePath;
private final Appendable output;
private final CompilationUnitTree compilationUnitTree;
 
public Generator(TypeElement annotationType, TemplateRenderer annotation, CompilationUnitTree compilationUnitTree, TreePath treePath, Appendable output) {
this.annotationType = annotationType;
this.annotation = annotation;
this.compilationUnitTree = compilationUnitTree;
this.treePath = treePath;
this.output = output;
}
 
public void write() throws IOException {
output.append("package ");
output.append(compilationUnitTree.getPackageName().toString());
output.append(";\n");
for (ImportTree it: compilationUnitTree.getImports()) {
output.append(it.toString());
output.append("\n");
}
for (Tree t: compilationUnitTree.getTypeDecls()) {
printTopLevel(t);
}
}
 
private void printTopLevel(Tree tree) throws IOException {
if (tree.getKind().equals(Tree.Kind.CLASS)) {
ClassTree ct = (ClassTree)tree;
if (ct.equals(treePath.getLeaf())) {
ModifiersTree mt = ct.getModifiers();
for (AnnotationTree at: mt.getAnnotations()) {
Tree t = at.getAnnotationType();
if (t.getKind().equals(Tree.Kind.IDENTIFIER)) {
IdentifierTree it = (IdentifierTree)t;
if (!it.getName().equals(annotationType.getSimpleName())) {
System.err.println(it.getName());
output.append(at.toString());
output.append("\n");
}
} else if (t.getKind().equals(Tree.Kind.MEMBER_SELECT)) {
MemberSelectTree mst = (MemberSelectTree)t;
if (!mst.getIdentifier().equals(annotationType.getSimpleName())) {
output.append(at.toString());
output.append("\n");
}
} else {
output.append(at.toString());
output.append("\n");
}
}
printModifiers(mt.getFlags());
output.append(" class ");
String className = annotation.className();
if (className.equals("")) {
className = ct.getSimpleName().toString();
String suffix = "Prototype";
if (className.endsWith(suffix))
className = className.substring(0, className.length() - suffix.length());
else
className = className + "TemplateRenderer";
}
output.append(className);
printTypeParameters(ct.getTypeParameters());
Tree extendsClause = ct.getExtendsClause();
if (extendsClause != null) {
output.append(" extends ");
output.append(extendsClause.toString());
}
List<? extends Tree> implementsClause = ct.getImplementsClause();
if (!implementsClause.isEmpty()) {
output.append(" implements ");
output.append(implementsClause.toString());
}
output.append(" {\n");
for (Tree membersTree: ct.getMembers()) {
printInner(membersTree, className);
}
output.append("}\n");
}
}
}
 
private void printInner(Tree tree, String className) throws IOException {
if (tree.getKind().equals(Tree.Kind.CLASS)) {
ClassTree ct = (ClassTree)tree;
ModifiersTree mt = ct.getModifiers();
for (AnnotationTree at: mt.getAnnotations()) {
output.append(at.toString());
output.append("\n");
}
printModifiers(mt.getFlags());
output.append(" class ");
output.append(ct.getSimpleName());
printTypeParameters(ct.getTypeParameters());
Tree extendsClause = ct.getExtendsClause();
if (extendsClause != null) {
output.append(" extends ");
output.append(extendsClause.toString());
}
List<? extends Tree> implementsClause = ct.getImplementsClause();
if (!implementsClause.isEmpty()) {
output.append(" implements ");
output.append(implementsClause.toString());
}
output.append(" {\n");
for (Tree membersTree: ct.getMembers()) {
printInner(membersTree, ct.getSimpleName().toString());
}
output.append("}\n");
} else if (tree.getKind().equals(Tree.Kind.METHOD)) {
MethodTree mt = (MethodTree)tree;
ModifiersTree modt = mt.getModifiers();
for (AnnotationTree at: modt.getAnnotations()) {
output.append(at.toString());
output.append("\n");
}
printModifiers(modt.getFlags());
output.append(" ");
printTypeParameters(mt.getTypeParameters());
output.append(" ");
Tree returnType = mt.getReturnType();
if (returnType != null) {
output.append(returnType.toString());
output.append(" ");
output.append(mt.getName().toString());
} else {
output.append(className);
}
output.append("(");
Iterator<? extends VariableTree> i = mt.getParameters().iterator();
if (i.hasNext()) {
VariableTree vt = i.next();
output.append(vt.toString());
while (i.hasNext()) {
output.append(", ");
output.append(i.next().toString());
}
}
output.append(")");
Tree t = mt.getDefaultValue();
if (t != null) {
output.append(" default ");
output.append(t.toString());
}
Iterator<? extends ExpressionTree> j = mt.getThrows().iterator();
if (j.hasNext()) {
output.append("throws ");
output.append(j.next().toString());
while (j.hasNext()) {
output.append(", ");
output.append(j.next().toString());
}
}
output.append(mt.getBody().toString());
output.append("\n");
} else if (tree.getKind().equals(Tree.Kind.BLOCK)) {
output.append(tree.toString());
output.append("\n");
} else {
output.append(tree.toString());
output.append(";\n");
}
}
 
private void printTypeParameters(List<? extends TypeParameterTree> typeParameters) throws IOException {
Iterator<? extends TypeParameterTree> iterator = typeParameters.iterator();
if (iterator.hasNext()) {
output.append("<");
output.append(iterator.next().toString());
while (iterator.hasNext()) {
output.append(", ");
output.append(iterator.next().toString());
}
output.append(">");
}
}
 
private void printModifiers(Set<Modifier> modifiers) throws IOException {
Iterator<Modifier> i = modifiers.iterator();
if (i.hasNext()) {
output.append(i.next().toString());
while (i.hasNext()) {
output.append(" ");
output.append(i.next().toString());
}
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.