1 19 20 package org.netbeans.modules.j2ee.ejbcore.action; 21 22 import com.sun.source.tree.ClassTree; 23 import com.sun.source.tree.MethodTree; 24 import java.io.IOException ; 25 import java.util.Arrays ; 26 import java.util.Collections ; 27 import java.util.HashSet ; 28 import java.util.Set ; 29 import javax.lang.model.element.Modifier; 30 import javax.lang.model.element.TypeElement; 31 import org.netbeans.api.java.source.JavaSource; 32 import org.netbeans.api.java.source.WorkingCopy; 33 import org.netbeans.modules.j2ee.common.method.MethodModel; 34 import org.netbeans.modules.j2ee.common.method.MethodModelSupport; 35 import org.netbeans.modules.j2ee.common.source.AbstractTask; 36 import org.netbeans.modules.j2ee.dd.api.ejb.CmpField; 37 import org.netbeans.modules.j2ee.dd.api.ejb.Entity; 38 import org.netbeans.modules.j2ee.ejbcore._RetoucheUtil; 39 import org.openide.filesystems.FileObject; 40 41 45 public class CmFieldGenerator extends AbstractMethodGenerator { 46 47 private static final Set <Modifier> PUBLIC_ABSTRACT = new HashSet <Modifier>(Arrays.asList(new Modifier[] { 48 Modifier.PUBLIC, 49 Modifier.ABSTRACT 50 })); 51 52 private CmFieldGenerator(Entity entity, FileObject ejbClassFileObject, FileObject ddFileObject) { 53 super(entity, ejbClassFileObject, ddFileObject); 54 } 55 56 public static CmFieldGenerator create(Entity entity, FileObject ejbClassFileObject, FileObject ddFileObject) { 57 return new CmFieldGenerator(entity, ejbClassFileObject, ddFileObject); 58 } 59 60 public void addCmpField(MethodModel.Variable field, boolean localGetter, boolean localSetter, 61 boolean remoteGetter, boolean remoteSetter, String description) throws IOException { 62 addFieldToClass(field, localGetter, localSetter, remoteGetter, remoteSetter); 63 Entity entity = (Entity) ejb; 64 CmpField cmpField = entity.newCmpField(); 65 cmpField.setFieldName(field.getName()); 66 cmpField.setDescription(description); 67 entity.addCmpField(cmpField); 68 saveXml(); 69 } 70 71 public void addFieldToClass(final MethodModel.Variable variable, final boolean localGetter, final boolean localSetter, 72 final boolean remoteGetter, final boolean remoteSetter) throws IOException { 73 74 JavaSource javaSource = JavaSource.forFileObject(ejbClassFileObject); 76 javaSource.runModificationTask(new AbstractTask<WorkingCopy>() { 77 public void run(WorkingCopy workingCopy) throws Exception { 78 workingCopy.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED); 79 TypeElement typeElement = workingCopy.getElements().getTypeElement(ejb.getEjbClass()); 80 MethodTree getterTree = createGetter(workingCopy, variable); 81 MethodTree setterTree = createSetter(workingCopy, variable); 82 ClassTree classTree = workingCopy.getTrees().getTree(typeElement); 83 ClassTree newClassTree = workingCopy.getTreeMaker().addClassMember(classTree, getterTree); 84 newClassTree = workingCopy.getTreeMaker().addClassMember(newClassTree, setterTree); 85 workingCopy.rewrite(classTree, newClassTree); 86 } 87 }).commit(); 88 89 if (localGetter || localSetter) { 91 FileObject localFileObject = _RetoucheUtil.resolveFileObjectForClass(ejbClassFileObject, ejb.getLocal()); 92 javaSource = JavaSource.forFileObject(localFileObject); 93 javaSource.runModificationTask(new AbstractTask<WorkingCopy>() { 94 public void run(WorkingCopy workingCopy) throws Exception { 95 workingCopy.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED); 96 TypeElement typeElement = workingCopy.getElements().getTypeElement(ejb.getLocal()); 97 ClassTree classTree = workingCopy.getTrees().getTree(typeElement); 98 ClassTree newClassTree = classTree; 99 if (localGetter) { 100 MethodTree getterTree = createGetter(workingCopy, variable); 101 newClassTree = workingCopy.getTreeMaker().addClassMember(newClassTree, getterTree); 102 } 103 if (localSetter) { 104 MethodTree setterTree = createSetter(workingCopy, variable); 105 newClassTree = workingCopy.getTreeMaker().addClassMember(newClassTree, setterTree); 106 } 107 workingCopy.rewrite(classTree, newClassTree); 108 } 109 }).commit(); 110 } 111 112 if (remoteGetter || remoteSetter) { 114 FileObject remoteFileObject = _RetoucheUtil.resolveFileObjectForClass(ejbClassFileObject, ejb.getRemote()); 115 javaSource = JavaSource.forFileObject(remoteFileObject); 116 javaSource.runModificationTask(new AbstractTask<WorkingCopy>() { 117 public void run(WorkingCopy workingCopy) throws Exception { 118 workingCopy.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED); 119 TypeElement typeElement = workingCopy.getElements().getTypeElement(ejb.getRemote()); 120 ClassTree classTree = workingCopy.getTrees().getTree(typeElement); 121 ClassTree newClassTree = classTree; 122 if (remoteGetter) { 123 MethodTree getterTree = createGetter(workingCopy, variable); 124 newClassTree = workingCopy.getTreeMaker().addClassMember(newClassTree, getterTree); 125 } 126 if (remoteSetter) { 127 MethodTree setterTree = createSetter(workingCopy, variable); 128 newClassTree = workingCopy.getTreeMaker().addClassMember(newClassTree, setterTree); 129 } 130 workingCopy.rewrite(classTree, newClassTree); 131 } 132 }).commit(); 133 } 134 135 } 136 137 private static MethodTree createGetter(WorkingCopy workingCopy, MethodModel.Variable field) { 138 MethodModel methodModel = MethodModel.create( 139 "get" + Character.toUpperCase(field.getName().charAt(0)) + field.getName().substring(1), 140 field.getType(), 141 null, 142 Collections.<MethodModel.Variable>emptyList(), 143 Collections.<String >emptyList(), 144 PUBLIC_ABSTRACT 145 ); 146 return MethodModelSupport.createMethodTree(workingCopy, methodModel); 147 } 148 149 private static MethodTree createSetter(WorkingCopy workingCopy, MethodModel.Variable field) { 150 MethodModel methodModel = MethodModel.create( 151 "set" + Character.toUpperCase(field.getName().charAt(0)) + field.getName().substring(1), 152 "void", 153 null, 154 Collections.singletonList(MethodModel.Variable.create(field.getType(), field.getName())), 155 Collections.<String >emptyList(), 156 PUBLIC_ABSTRACT 157 ); 158 return MethodModelSupport.createMethodTree(workingCopy, methodModel); 159 } 160 161 } 162 | Popular Tags |