KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ejbcore > api > methodcontroller > EntityGenerateFromIntfVisitor


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 package org.netbeans.modules.j2ee.ejbcore.api.methodcontroller;
20
21 import java.io.IOException JavaDoc;
22 import java.util.Collections JavaDoc;
23 import javax.lang.model.element.Modifier;
24 import javax.lang.model.element.TypeElement;
25 import org.netbeans.api.java.source.CompilationController;
26 import org.netbeans.api.java.source.JavaSource;
27 import org.netbeans.api.java.source.JavaSource.Phase;
28 import org.netbeans.modules.j2ee.common.source.AbstractTask;
29 import org.netbeans.modules.j2ee.common.method.MethodModel;
30 import org.netbeans.modules.j2ee.dd.api.ejb.Entity;
31 import org.netbeans.modules.j2ee.ejbcore.api.methodcontroller.MethodType.BusinessMethodType;
32 import org.netbeans.modules.j2ee.ejbcore.api.methodcontroller.MethodType.CreateMethodType;
33 import org.netbeans.modules.j2ee.ejbcore.api.methodcontroller.MethodType.FinderMethodType;
34 import org.netbeans.modules.j2ee.ejbcore.api.methodcontroller.MethodType.HomeMethodType;
35 import org.openide.ErrorManager;
36 import org.openide.filesystems.FileObject;
37
38 /**
39  *
40  * @author Chris Webster
41  * @author Martin Adamek
42  */

43 class EntityGenerateFromIntfVisitor implements MethodType.MethodTypeVisitor, AbstractMethodController.GenerateFromIntf {
44
45     private static final String JavaDoc TODO = "//TODO implement "; //NOI18N
46

47     private final FileObject ejbClassFO;
48     private final Entity entity;
49     private MethodModel implMethod;
50     private MethodModel secondaryMethod;
51     
52     public EntityGenerateFromIntfVisitor(FileObject ejbClassFO, Entity entity) {
53         this.ejbClassFO = ejbClassFO;
54         this.entity = entity;
55     }
56     
57     public void getInterfaceMethodFromImpl(MethodType methodType) {
58         methodType.accept(this);
59     }
60     
61     public MethodModel getImplMethod() {
62         return implMethod;
63     }
64     
65     public MethodModel getSecondaryMethod() {
66         return secondaryMethod;
67     }
68     
69     public void visit(BusinessMethodType bmt) {
70         implMethod = bmt.getMethodElement();
71         String JavaDoc body = TODO + implMethod.getName() + implMethod.getReturnType();
72         implMethod = MethodModel.create(
73                 implMethod.getName(),
74                 implMethod.getReturnType(),
75                 body,
76                 implMethod.getParameters(),
77                 implMethod.getExceptions(),
78                 Collections.singleton(Modifier.PUBLIC)
79                 );
80     }
81     
82     public void visit(CreateMethodType cmt) {
83         implMethod = cmt.getMethodElement();
84         String JavaDoc origName = implMethod.getName();
85         String JavaDoc newName = prependAndUpper(origName,"ejb"); //NOI18N
86
String JavaDoc type = entity.getPrimKeyClass();
87         String JavaDoc body = TODO + newName + type;
88         implMethod = MethodModel.create(
89                 newName,
90                 type,
91                 body,
92                 implMethod.getParameters(),
93                 implMethod.getExceptions(),
94                 Collections.singleton(Modifier.PUBLIC)
95                 );
96         secondaryMethod = cmt.getMethodElement();
97         origName = secondaryMethod.getName();
98         newName = prependAndUpper(origName,"ejbPost"); //NOI18N
99
body = TODO + newName;
100         secondaryMethod = MethodModel.create(
101                 newName,
102                 "void",
103                 body,
104                 secondaryMethod.getParameters(),
105                 secondaryMethod.getExceptions(),
106                 Collections.singleton(Modifier.PUBLIC)
107                 );
108     }
109     
110     public void visit(HomeMethodType hmt) {
111         implMethod = hmt.getMethodElement();
112         String JavaDoc origName = implMethod.getName();
113         String JavaDoc newName = prependAndUpper(origName,"ejbHome"); //NOI18N
114
String JavaDoc body = TODO + implMethod.getName() + implMethod.getReturnType();
115         implMethod = MethodModel.create(
116                 newName,
117                 implMethod.getReturnType(),
118                 body,
119                 implMethod.getParameters(),
120                 implMethod.getExceptions(),
121                 Collections.singleton(Modifier.PUBLIC)
122                 );
123     }
124     
125     public void visit(FinderMethodType fmt) {
126         implMethod = fmt.getMethodElement();
127         String JavaDoc origName = implMethod.getName();
128         String JavaDoc newName = prependAndUpper(origName,"ejb"); //NOI18N
129
String JavaDoc body = TODO + implMethod.getName() + implMethod.getReturnType();
130         String JavaDoc collectionType = java.util.Collection JavaDoc.class.getName();
131         String JavaDoc implMethodElement = implMethod.getReturnType();
132         boolean isAssignable = false;
133         try {
134             isAssignable = isSubtype(implMethodElement, collectionType);
135         } catch (IOException JavaDoc e) {
136             ErrorManager.getDefault().notify(e);
137         }
138         implMethod = MethodModel.create(
139                 newName,
140                 isAssignable ? "void" : entity.getPrimKeyClass(),
141                 body,
142                 implMethod.getParameters(),
143                 implMethod.getExceptions(),
144                 Collections.singleton(Modifier.PUBLIC)
145                 );
146     }
147     
148     private boolean isSubtype(final String JavaDoc className1, final String JavaDoc className2) throws IOException JavaDoc {
149         JavaSource javaSource = JavaSource.forFileObject(ejbClassFO);
150         final boolean[] result = new boolean[] {false};
151         javaSource.runUserActionTask(new AbstractTask<CompilationController>() {
152             public void run(CompilationController controller) throws IOException JavaDoc {
153                 controller.toPhase(Phase.ELEMENTS_RESOLVED);
154                 TypeElement typeElement1 = controller.getElements().getTypeElement(className1);
155                 TypeElement typeElement2 = controller.getElements().getTypeElement(className2);
156                 result[0] = controller.getTypes().isSubtype(typeElement1.asType(), typeElement2.asType());
157             }
158         }, true);
159         return result[0];
160     }
161     
162     private String JavaDoc prependAndUpper(String JavaDoc fullName, String JavaDoc prefix) {
163         StringBuffer JavaDoc stringBuffer = new StringBuffer JavaDoc(fullName);
164         stringBuffer.setCharAt(0, Character.toUpperCase(stringBuffer.charAt(0)));
165         return prefix + stringBuffer.toString();
166     }
167     
168     public static String JavaDoc getReturnStatement(String JavaDoc type) {
169         String JavaDoc result = "";
170         if ("boolean".equals(type)) {
171             result = "\nreturn false;";
172         } else if ("byte".equals(type)) {
173             result = "\nreturn 0;";
174         } else if ("char".equals(type)) {
175             result ="\nreturn '0';";
176         } else if ("double".equals(type)) {
177             result ="\nreturn 0.0;";
178         } else if ("float".equals(type)) {
179             result ="\nreturn 0;";
180         } else if ("int".equals(type)) {
181             result ="\nreturn 0;";
182         } else if ("long".equals(type)) {
183             result ="\nreturn 0;";
184         } else if ("short".equals(type)) {
185             result ="\nreturn 0;";
186         } else{
187             result ="\nreturn null;";
188         }
189         return result;
190     }
191
192 }
193
Popular Tags