KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ejbcore > action > AbstractMethodGenerator


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

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 JavaDoc;
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 /**
39  *
40  * @author Martin Adamek
41  */

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     /**
57      * Founds business interface if exists and adds method there, adds method into interface <code>className</code> otherwise
58      */

59     protected void addMethodToInterface(MethodModel methodModel, String JavaDoc className) throws IOException JavaDoc {
60         String JavaDoc commonInterface = findCommonInterface(ejb.getEjbClass(), className);
61         if (commonInterface == null) { // there is no 'business' interface
62
commonInterface = className;
63         }
64         FileObject fileObject = _RetoucheUtil.resolveFileObjectForClass(ejbClassFileObject, commonInterface);
65         addMethod(methodModel, fileObject, commonInterface);
66     }
67     
68     /**
69      * Adds method to class
70      */

71     protected static void addMethod(final MethodModel methodModel, FileObject fileObject, final String JavaDoc className) throws IOException JavaDoc {
72         JavaSource javaSource = JavaSource.forFileObject(fileObject);
73         javaSource.runModificationTask(new AbstractTask<WorkingCopy>() {
74             public void run(WorkingCopy workingCopy) throws IOException JavaDoc {
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 JavaDoc {
86         EjbJar ejbJar = DDProvider.getDefault().getDDRoot(ddFileObject);
87         if (ejbJar != null) {
88             ejbJar.write(ddFileObject);
89         }
90     }
91     
92     private static String JavaDoc findCommonInterface(final String JavaDoc className1, final String JavaDoc className2) throws IOException JavaDoc {
93         //TODO: RETOUCHE
94
return null;
95     }
96     
97 }
98
Popular Tags