1 19 20 package org.netbeans.modules.web.wizards; 21 22 import java.io.IOException ; 23 import java.util.Collections ; 24 25 import javax.lang.model.element.Modifier; 26 import javax.lang.model.type.TypeKind; 27 28 import com.sun.source.tree.ClassTree; 29 import com.sun.source.tree.CompilationUnitTree; 30 import com.sun.source.tree.ExpressionTree; 31 import com.sun.source.tree.MethodTree; 32 import com.sun.source.tree.Tree; 33 import com.sun.source.tree.TypeParameterTree; 34 import com.sun.source.tree.VariableTree; 35 36 import org.netbeans.api.java.source.JavaSource; 37 import org.netbeans.api.java.source.JavaSource.Phase; 38 import org.netbeans.api.java.source.ModificationResult; 39 import org.netbeans.api.java.source.TreeMaker; 40 import org.netbeans.api.java.source.WorkingCopy; 41 42 import org.netbeans.modules.j2ee.common.source.AbstractTask; 43 import org.netbeans.modules.j2ee.common.source.GenerationUtils; 44 45 51 public class TagHandlerGenerator { 52 private Object [][] attributes; 53 private JavaSource clazz; 54 private boolean isBodyTag; 55 private boolean evaluateBody; 56 57 58 public TagHandlerGenerator(JavaSource clazz, Object [][] attributes, boolean isBodyTag, boolean evaluateBody) { 59 this.clazz=clazz; 60 this.attributes=attributes; 61 this.isBodyTag=isBodyTag; 62 this.evaluateBody=evaluateBody; 63 } 64 65 public void generate() throws IOException { 66 AbstractTask task = new AbstractTask<WorkingCopy>() { 67 public void run(WorkingCopy workingCopy) throws Exception { 68 workingCopy.toPhase(Phase.RESOLVED); 69 TreeMaker make = workingCopy.getTreeMaker(); 70 GenerationUtils gu = GenerationUtils.newInstance(workingCopy); 71 ClassTree oldClassTree = gu.getClassTree(); 72 ClassTree classTree = addFields(gu, make, oldClassTree); 73 if (isBodyTag) { 74 classTree = make.addClassMember(classTree, addBodyEvaluatorCheck(evaluateBody, make)); 75 } 76 classTree = addSetters(gu, make, classTree); 77 workingCopy.rewrite(oldClassTree, classTree); 78 } 79 }; 80 ModificationResult result = clazz.runModificationTask(task); 81 result.commit(); 82 } 83 84 private ClassTree addFields(GenerationUtils gu, TreeMaker make, ClassTree classTree) throws IOException { 85 for (int i = 0; i < attributes.length; i++) { 86 VariableTree field = gu.createField(gu.createModifiers(Modifier.PRIVATE), (String ) attributes[i][0], (String ) attributes[i][1]); 87 classTree = make.insertClassMember(classTree, i, field); 88 89 } 91 92 return classTree; 93 } 94 95 private ClassTree addSetters(GenerationUtils gu, TreeMaker make, ClassTree newClassTree) throws IOException { 96 for (int i = 0; i < attributes.length; i++) { 97 MethodTree setter = gu.createPropertySetterMethod(gu.createModifiers(Modifier.PUBLIC), (String ) attributes[i][0], (String ) attributes[i][1]); 98 newClassTree = make.addClassMember(newClassTree, setter); 99 100 } 102 103 return newClassTree; 104 } 105 106 private MethodTree addBodyEvaluatorCheck(boolean evaluateBody, TreeMaker make) throws IOException { 107 StringBuffer methodBody = new StringBuffer (); 108 methodBody.append("{\n//"); methodBody.append("\n// TODO: code that determines whether the body should be"); methodBody.append("\n// evaluated should be placed here."); methodBody.append("\n// Called from the doStartTag() method."); methodBody.append("\n//"); methodBody.append("\nreturn " + (evaluateBody ? "true;" : "false;")); methodBody.append("\n}"); 116 MethodTree method = make.Method( 117 make.Modifiers(Collections.<Modifier>singleton(Modifier.PRIVATE)), 118 "theBodyShouldBeEvaluated", make.PrimitiveType(TypeKind.BOOLEAN), 120 Collections.<TypeParameterTree>emptyList(), 121 Collections.<VariableTree>emptyList(), 122 Collections.<ExpressionTree>emptyList(), 123 methodBody.toString(), 124 null); 125 126 128 return method; 129 } 130 131 } | Popular Tags |