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 javax.lang.model.element.TypeElement; 26 import org.netbeans.api.java.source.JavaSource; 27 import org.netbeans.api.java.source.WorkingCopy; 28 import org.netbeans.modules.j2ee.common.method.MethodModel; 29 import org.netbeans.modules.j2ee.common.method.MethodModelSupport; 30 import org.netbeans.modules.j2ee.common.source.AbstractTask; 31 import org.netbeans.modules.j2ee.dd.api.ejb.DDProvider; 32 import org.netbeans.modules.j2ee.dd.api.ejb.EjbJar; 33 import org.netbeans.modules.j2ee.dd.api.ejb.EntityAndSession; 34 import org.netbeans.modules.j2ee.ejbcore._RetoucheUtil; 35 import org.openide.filesystems.FileObject; 36 import org.openide.util.Parameters; 37 38 42 public abstract class AbstractMethodGenerator { 43 44 protected final EntityAndSession ejb; 45 protected final FileObject ejbClassFileObject; 46 protected final FileObject ddFileObject; 47 48 protected AbstractMethodGenerator(EntityAndSession ejb, FileObject ejbClassFileObject, FileObject ddFileObject) { 49 Parameters.notNull("ejb", ejb); 50 Parameters.notNull("ejbClassFileObject", ejbClassFileObject); 51 this.ejb = ejb; 52 this.ejbClassFileObject = ejbClassFileObject; 53 this.ddFileObject = ddFileObject; 54 } 55 56 59 protected void addMethodToInterface(MethodModel methodModel, String className) throws IOException { 60 String commonInterface = findCommonInterface(ejb.getEjbClass(), className); 61 if (commonInterface == null) { commonInterface = className; 63 } 64 FileObject fileObject = _RetoucheUtil.resolveFileObjectForClass(ejbClassFileObject, commonInterface); 65 addMethod(methodModel, fileObject, commonInterface); 66 } 67 68 71 protected static void addMethod(final MethodModel methodModel, FileObject fileObject, final String className) throws IOException { 72 JavaSource javaSource = JavaSource.forFileObject(fileObject); 73 javaSource.runModificationTask(new AbstractTask<WorkingCopy>() { 74 public void run(WorkingCopy workingCopy) throws IOException { 75 workingCopy.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED); 76 MethodTree methodTree = MethodModelSupport.createMethodTree(workingCopy, methodModel); 77 TypeElement typeElement = workingCopy.getElements().getTypeElement(className); 78 ClassTree classTree = workingCopy.getTrees().getTree(typeElement); 79 ClassTree newClassTree = workingCopy.getTreeMaker().addClassMember(classTree, methodTree); 80 workingCopy.rewrite(classTree, newClassTree); 81 } 82 }).commit(); 83 } 84 85 protected void saveXml() throws IOException { 86 EjbJar ejbJar = DDProvider.getDefault().getDDRoot(ddFileObject); 87 if (ejbJar != null) { 88 ejbJar.write(ddFileObject); 89 } 90 } 91 92 private static String findCommonInterface(final String className1, final String className2) throws IOException { 93 return null; 95 } 96 97 } 98 | Popular Tags |