KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > refactoring > Utility


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.refactoring;
21
22 import java.io.IOException JavaDoc;
23 import java.util.*;
24 import javax.jmi.reflect.RefObject;
25
26 import org.netbeans.api.java.classpath.GlobalPathRegistry;
27 import org.netbeans.api.project.FileOwnerQuery;
28 import org.netbeans.api.project.Project;
29 import org.netbeans.api.project.ui.OpenProjects;
30 import org.netbeans.jmi.javamodel.*;
31 import org.netbeans.jmi.javamodel.Method;
32 import org.netbeans.modules.j2ee.dd.api.ejb.*;
33 import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeAppProvider;
34 import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider;
35 import org.netbeans.modules.javacore.api.JavaModel;
36 import org.netbeans.modules.refactoring.api.Problem;
37 import org.netbeans.modules.web.api.webmodule.WebModule;
38 import org.netbeans.modules.web.spi.webmodule.WebModuleImplementation;
39 import org.netbeans.modules.websvc.api.webservices.WebServicesSupport;
40 import org.netbeans.spi.project.SubprojectProvider;
41 import org.openide.ErrorManager;
42 import org.openide.filesystems.FileObject;
43
44 /**
45  *
46  * @author mg116726
47  */

48 public class Utility {
49     
50     private static final ErrorManager err =
51             ErrorManager.getDefault().getInstance("org.netbeans.modules.j2ee.refactoring"); // NOI18N
52

53     public static final String JavaDoc ENTERPRISE_BEAN = "javax.ejb.EnterpriseBean"; //NOI18N
54
public static final String JavaDoc SESSION_BEAN = "javax.ejb.SessionBean"; //NOI18N
55
public static final String JavaDoc ENTITY_BEAN = "javax.ejb.EntityBean"; //NOI18N
56
public static final String JavaDoc MESSAGE_DRIVEN_BEAN = "javax.ejb.MessageDrivenBean"; //NOI18N
57
public static final String JavaDoc EJB_LOCAL_HOME = "javax.ejb.EJBLocalHome"; //NOI18N
58
public static final String JavaDoc EJB_HOME = "javax.ejb.EJBHome"; //NOI18N
59
public static final String JavaDoc EJB_LOCAL_OBJECT = "javax.ejb.EJBLocalObject"; //NOI18N
60
public static final String JavaDoc EJB_OBJECT = "javax.ejb.EJBObject"; //NOI18N
61

62     public static final String JavaDoc MESSAGE_LISTENER = "javax.jms.MessageListener"; //NOI18N
63

64     public static final String JavaDoc EJB_CREATE = "ejbCreate"; //NOI18N
65
public static final String JavaDoc EJB_ACTIVATE = "ejbActivate"; //NOI18N
66
public static final String JavaDoc EJB_PASSIVATE = "ejbPassivate"; //NOI18N
67
public static final String JavaDoc EJB_REMOVE = "ejbRemove"; //NOI18N
68
public static final String JavaDoc EJB_LOAD = "ejbLoad"; //NOI18N
69
public static final String JavaDoc EJB_STORE = "ejbStore"; //NOI18N
70
public static final String JavaDoc SET_SESSION_CONTEXT = "setSessionContext"; //NOI18N
71
public static final String JavaDoc SET_ENTITY_CONTEXT = "setEntityContext"; //NOI18N
72
public static final String JavaDoc SET_MSG_CONTEXT = "setMessageDrivenContext"; //NOI18N
73
public static final String JavaDoc UNSET_ENTITY_CONTEXT = "unsetEntityContext"; //NOI18N
74
public static final String JavaDoc ON_MESSAGE = "onMessage"; //NOI18N
75

76     public static final String JavaDoc[] NO_PARAMS = new String JavaDoc[] {};
77     public static final String JavaDoc[] SET_SESSION_CONTEXT_PARAMTYPENAMES = new String JavaDoc[] {"javax.ejb.SessionContext"};
78     public static final String JavaDoc[] SET_ENTITY_CONTEXT_PARAMTYPENAMES = new String JavaDoc[] {"javax.ejb.EntityContext"};
79     public static final String JavaDoc[] SET_MSG_CONTEXT_PARAMTYPENAMES = new String JavaDoc[] {"javax.ejb.MessageDrivenContext"};
80     public static final String JavaDoc[] ON_MESSAGE_PARAMTYPENAMES = new String JavaDoc[] {"javax.jms.Message"};
81     public static final String JavaDoc PREFIX_SET = "set"; //NOI18N
82
public static final String JavaDoc PREFIX_GET = "get"; //NOI18N
83
public static final String JavaDoc PREFIX_FIND = "find"; //NOI18N
84
public static final String JavaDoc PREFIX_EJBSELECT = "ejbSelect"; //NOI18N
85
public static final String JavaDoc PREFIX_EJBPOSTCREATE = "ejbPostCreate"; //NOI18N
86
public static final String JavaDoc PREFIX_EJBCREATE = "ejbCreate"; //NOI18N
87
public static final String JavaDoc PREFIX_CREATE = "create"; //NOI18N
88
public static final String JavaDoc PREFIX_EJBHOME = "ejbHome"; //NOI18N
89
public static final String JavaDoc PREFIX_EJBFIND = "ejbFind"; //NOI18N
90
private static final String JavaDoc PREFIX_POSTCREATE = "postCreate";
91     
92     /** Creates a new instance of Utility */
93     public Utility() {
94     }
95     
96     public static List getTypedParams(List params) {
97         if (params != null) {
98             List typedParams = new ArrayList(params.size());
99             for (int i=0; i < params.size(); i++) {
100                 Element param = (Element) params.get(i);
101                 Type type = param instanceof Parameter ? ((Parameter) param).getType() : (Type) param;
102                 typedParams.add(type);
103             }
104             return typedParams;
105         }
106         return Collections.EMPTY_LIST;
107     }
108     
109     public static EnterpriseBeans getEnterpriseBeansFromDD(FileObject fo) {
110         err.log(">Utility.getEnterpriseBeansFromDD (" + fo + ")" );
111         org.netbeans.modules.j2ee.api.ejbjar.EjbJar emod = org.netbeans.modules.j2ee.api.ejbjar.EjbJar.getEjbJar(fo);
112         if (emod != null) {
113             FileObject ejbJarFO = emod.getDeploymentDescriptor();
114             EjbJar ejbJarDD = null;
115             try {
116                 ejbJarDD = org.netbeans.modules.j2ee.dd.api.ejb.DDProvider.getDefault().getMergedDDRoot(emod.getMetadataUnit());
117             } catch (IOException JavaDoc ioe) {
118                 //ignore
119
}
120             if ((ejbJarDD != null) && (ejbJarDD.getStatus()!=EjbJar.STATE_INVALID_UNPARSABLE)) {
121                 return ejbJarDD.getEnterpriseBeans();
122             }
123         }
124         return null;
125     }
126     
127     public static JavaClass getImplClassForHome(EnterpriseBeans eBeans, Class JavaDoc beanType, String JavaDoc homeClassName,
128             boolean local) {
129         if (eBeans != null) {
130             Ejb[] ejbs = eBeans.getEjbs();
131             if (ejbs != null) {
132                 for (int i = 0; i < ejbs.length; i++) {
133                     Ejb ejb = ejbs[i];
134                     if (ejb != null && beanType.isAssignableFrom(ejb.getClass())) {
135                         EntityAndSession bean = (EntityAndSession) ejb;
136                         if (homeClassName.equals(getHomeInterface(bean, local))) {
137                             JavaClass javaClass = resolveRealClass(bean.getEjbClass());
138                             if (javaClass != null) {
139                                 return javaClass;
140                             }
141                         }
142                     }
143                 }
144             }
145         }
146         return null;
147     }
148     
149     public static Method getMethodInImplClassForHome(FileObject fo, Class JavaDoc beanType, String JavaDoc className,
150             String JavaDoc methodName, List parameters, boolean local) {
151         if (methodName == null || methodName.length() == 0) {
152             return null;
153         }
154         JavaClass ejbImplClass = getImplClassForHome(getEnterpriseBeansFromDD(fo), beanType, className, local);
155         if (ejbImplClass != null) {
156             return ejbImplClass.getMethod(methodName, getTypedParams(parameters), false);
157         } else {
158             return null;
159         }
160     }
161     
162     private static JavaClass getOtherHomeClassForHome(EnterpriseBeans eBeans, Class JavaDoc beanType, String JavaDoc homeClassName,
163             boolean local) {
164         if (eBeans != null) {
165             Ejb[] ejbs = eBeans.getEjbs();
166             if (ejbs != null) {
167                 for (int i = 0; i < ejbs.length; i++) {
168                     Ejb ejb = ejbs[i];
169                     if (ejb != null && beanType.isAssignableFrom(ejb.getClass())) {
170                         EntityAndSession bean = (EntityAndSession) ejbs[i];
171                         if (homeClassName.equals(getHomeInterface(bean, !local))) {
172                             JavaClass javaClass = resolveRealClass(getHomeInterface(bean, local));
173                             if (isSubTypeOf(javaClass, local ? EJB_LOCAL_HOME : EJB_HOME)) {
174                                 return javaClass;
175                             }
176                         }
177                     }
178                 }
179             }
180         }
181         return null;
182     }
183     
184     public static Method getMethodInOtherHomeClassForHome(FileObject fo, Class JavaDoc beanType, String JavaDoc className,
185             String JavaDoc methodName, List parameters, boolean local) {
186         JavaClass ejbHomeClass = getOtherHomeClassForHome(getEnterpriseBeansFromDD(fo), beanType, className, local);
187         if (ejbHomeClass != null) {
188             return ejbHomeClass.getMethod(methodName, getTypedParams(parameters), false);
189         }
190         return null;
191     }
192     
193     private static JavaClass getHomeClassForImpl(EnterpriseBeans eBeans, Class JavaDoc beanType, String JavaDoc implClassName,
194             boolean local) {
195         Ejb bean = EjbInterfaceType.BEAN_IMPL.getBean(eBeans, beanType, implClassName);
196         if (bean != null) {
197             EjbInterfaceType ejbInterfaceType = local ? EjbInterfaceType.LOCAL : EjbInterfaceType.LOCAL_HOME;
198             return ejbInterfaceType.resolveClass(bean);
199         } else {
200             return null;
201         }
202     }
203     
204     public static Method getMethodInHomeClassForImpl(FileObject fo, Class JavaDoc beanType, String JavaDoc className,
205             String JavaDoc methodName, List parameters, boolean local) {
206         if (methodName == null || methodName.length() == 0) {
207             return null;
208         }
209         JavaClass ejbHomeClass = getHomeClassForImpl(getEnterpriseBeansFromDD(fo), beanType, className, local);
210         if (ejbHomeClass != null) {
211             return ejbHomeClass.getMethod(methodName, getTypedParams(parameters), false);
212         }
213         return null;
214     }
215     
216     private static String JavaDoc getHomeInterface(EntityAndSession bean, boolean local) {
217         return local ? bean.getLocalHome() : bean.getHome();
218     }
219     
220     public static String JavaDoc addPrefix(String JavaDoc methodName, String JavaDoc prefix) {
221         return prefix + methodName.substring(0,1).toUpperCase() + methodName.substring(1);
222     }
223     
224     public static String JavaDoc stripNonEmptyPrefix(String JavaDoc methodName, String JavaDoc prefix) {
225         methodName = stripPrefix(methodName, prefix);
226         if (methodName != null && methodName.length() > 1 && isFirstUpperCase(methodName)) {
227             return methodName.substring(0, 1).toLowerCase() + methodName.substring(1);
228         }
229         return null;
230     }
231     
232     
233     public static String JavaDoc stripPrefix(String JavaDoc methodName, String JavaDoc prefix) {
234         if (methodName.startsWith(prefix)) {
235             return methodName.substring(prefix.length());
236         }
237         return null;
238     }
239     
240     public static String JavaDoc replacePrefix(String JavaDoc methodName, String JavaDoc oldPrefix, String JavaDoc newPrefix) {
241         if (newPrefix == null) {
242             return stripNonEmptyPrefix(methodName, oldPrefix);
243         } else if (methodName.startsWith(oldPrefix) && methodName.length() > oldPrefix.length()) {
244             return newPrefix + methodName.substring(oldPrefix.length());
245         } else {
246             return null;
247         }
248     }
249     
250     /** finds finder method in implementation class that coresponds to method in home interface (local or remote) based on valu of 'local' */
251     public static Method getImplFinderMethodForHomeFinderMethod(Method method, FileObject fo, String JavaDoc className, boolean local) {
252         err.log(">Utility.getImplFinderMethodForHomeFinderMethod (" + method + ", " + fo + ", " + className + ", " + local + ")" );
253         return getMethodInImplClassForHome(fo, EntityAndSession.class, className, local, method, PREFIX_FIND, PREFIX_EJBFIND);
254     }
255     
256     /** finds create method in implementation class that coresponds to method in home interface (local or remote) based on value of 'local' */
257     public static Method getImplCreateMethodForHomeCreateMethod(Method method, FileObject fo, String JavaDoc className, boolean local) {
258         err.log(">Utility.getImplCreateMethodForHomeCreateMethod (" + method + ", " + fo + ", " + className + ", " + local + ")");
259         String JavaDoc oldPrefix = PREFIX_CREATE;
260         String JavaDoc newPrefix = PREFIX_EJBCREATE;
261         Class JavaDoc beanType = EntityAndSession.class;
262         return getMethodInImplClassForHome(fo, beanType, className, local, method, oldPrefix, newPrefix);
263         
264     }
265     
266     /** finds create method in implementation class that coresponds to method in home interface (local or remote) based on value of 'local' */
267     public static Method getImplPostCreateMethodForHomeCreateMethod(Method method, FileObject fo, String JavaDoc className, boolean local) {
268         err.log(">Utility.getImplPostCreateMethodForHomeCreateMethod (" + method + ", " + fo + ", " + className + ", " + local + ")");
269         return getMethodInImplClassForHome(fo, EntityAndSession.class, className, local, method, PREFIX_EJBPOSTCREATE, PREFIX_CREATE);
270     }
271     
272     public static Method getMethod(Method origMethod, String JavaDoc origPrefix, String JavaDoc prefix) {
273         ClassDefinition jc = origMethod.getDeclaringClass(); // get affected class
274
err.log("classdefinition classDefinition: " + jc);
275         if ((jc instanceof JavaClass)) {
276             String JavaDoc methodName = replacePrefix(origMethod.getName(), origPrefix, prefix);
277             if (methodName == null || methodName.length() == 0) {
278                 return null;
279             }
280             return jc.getMethod(methodName, getTypedParams(origMethod.getParameters()), false);
281         } else {
282             return null;
283         }
284     }
285     
286     public static Method getMethodInHomeClassForImpl(FileObject fo, Class JavaDoc beanType, String JavaDoc className, boolean local,
287             Method origMethod, String JavaDoc origPrefix, String JavaDoc prefix) {
288         String JavaDoc methodName = replacePrefix(origMethod.getName(), origPrefix, prefix);
289         return getMethodInHomeClassForImpl(fo, beanType, className, methodName, origMethod.getParameters(),
290                 local);
291     }
292     
293     public static Method getMethodInImplClassForHome(FileObject fo, Class JavaDoc beanType, String JavaDoc className, boolean local,
294             Method origMethod, String JavaDoc origPrefix, String JavaDoc prefix) {
295         String JavaDoc methodName = replacePrefix(origMethod.getName(), origPrefix, prefix);
296         return getMethodInImplClassForHome(fo, beanType, className, methodName,
297                 origMethod.getParameters(), local);
298     }
299     
300     /** finds home method in implementation class that coresponds to method in home interface (local or remote) based on value of 'local' */
301     public static Method getImplHomeMethodForHomeHomeMethod(Method method, FileObject fo, String JavaDoc className, boolean local) {
302         err.log(">Utility.getImplHomeMethodForHomeHomeMethod (" + method + ", " + fo + ", " + className + ", " + local + ")");
303         String JavaDoc implMethodName = addPrefix(method.getName(), PREFIX_EJBHOME);
304         return getMethodInImplClassForHome(fo, EntityAndSession.class, className, implMethodName,
305                 method.getParameters(), local);
306     }
307     
308     /** finds home method in home/localhome interface that coresponds to method in localhome/home interface - 'local' if true, finds local, otherwise remote */
309     public static Method getHomeHomeMethodForHomeHomeMethod(Method method, FileObject fo, String JavaDoc className, boolean local) {
310         err.log(">Utility.getHomeHomeMethodForHomeHomeMethod (" + method + ", " + fo + ", " + className + ", " + local + ")");
311         String JavaDoc methodName = method.getName();
312         return getMethodInOtherHomeClassForHome(fo, EntityAndSession.class, className, methodName,
313                 method.getParameters(), local);
314     }
315     
316     /** finds finder method in home/local home iface that coresponds to method in localhome/home interface 'local' - if true, finds local, otherwise remote*/
317     public static Method getHomeFinderMethodForHomeFinderMethod(Method method, FileObject fo, String JavaDoc className, boolean local) {
318         err.log(">Utility.getHomeFinderMethodForHomeFinderMethod (" + method + ", " + fo + ", " + className + ", " + local + ")");
319         String JavaDoc methodName = method.getName();
320         if (!methodName.startsWith(PREFIX_FIND)) {
321             return null;
322         }
323         return getMethodInOtherHomeClassForHome(fo, EntityAndSession.class, className, methodName,
324                 method.getParameters(), local);
325     }
326     
327     /** finds create method in home/local home iface that coresponds to method in localhome/home interface 'local' - if true, finds local, otherwise remote*/
328     public static Method getHomeCreateMethodForHomeCreateMethod(Method method, FileObject fo, String JavaDoc className, boolean local) {
329         err.log(">Utility.getHomeCreateMethodForHomeCreateMethod (" + method + ", " + fo + ", " + className + ", " + local + ")");
330         String JavaDoc methodName = method.getName();
331         if (!methodName.startsWith(PREFIX_CREATE)) {
332             return null;
333         }
334         return getMethodInOtherHomeClassForHome(fo, EntityAndSession.class, className, methodName,
335                 method.getParameters(), local);
336     }
337     
338     /** finds finder method in home interface that coresponds to method in implementation class for EntityBeans*/
339     public static Method getHomeFinderMethodForImplFinderMethod(Method method, FileObject fo, String JavaDoc className, boolean local) {
340         err.log(">Utility.getHomeFinderMethodForImplFinderMethod (" + method + ", " + fo + ", " + className + ", " + local + ")");
341         return getMethodInHomeClassForImpl(fo, Entity.class, className, local, method, PREFIX_FIND, PREFIX_EJBFIND);
342     }
343     
344     /** finds home method in home interface that coresponds to method in implementation class for EntityBeans*/
345     public static Method getHomeHomeMethodForEntityImplHomeMethod(Method method, FileObject fo, String JavaDoc className, boolean local) {
346         err.log(">Utility.getHomeHomeMethodForEntityImplHomeMethod (" + method + ", " + fo + ", " + className + ", " + local + ")");
347         String JavaDoc methodName = stripNonEmptyPrefix(method.getName(), PREFIX_EJBHOME);
348         return getMethodInHomeClassForImpl(fo, Entity.class, className, methodName, method.getParameters(), local);
349     }
350     
351     /** finds home method in home interface that coresponds to method in implementation class for SessionBeans*/
352     public static Method getHomeHomeMethodForSessionImplHomeMethod(Method method, FileObject fo, String JavaDoc className, boolean local) {
353         err.log(">Utility.getHomeHomeMethodForSessionImplHomeMethod (" + method + ", " + fo + ", " + className + ", " + local + ")");
354         String JavaDoc methodName = stripNonEmptyPrefix(method.getName(), PREFIX_EJBHOME);
355         return getMethodInHomeClassForImpl(fo, Session.class, className, methodName, method.getParameters(), local);
356     }
357     
358     /** finds create method in home interface that coresponds to method in implementation class for SessionBeans*/
359     public static Method getHomeCreateMethodForSessionImplCreateMethod(Method method, FileObject fo, String JavaDoc className, boolean local) {
360         err.log(">Utility.getHomeCreateMethodForSessionImplCreateMethod (" + method + ", " + fo + ", " + className + ", " + local + ")");
361         return getMethodInHomeClassForImpl(fo, Session.class, className, local, method, PREFIX_EJBCREATE, PREFIX_CREATE);
362     }
363     
364     /**
365      * finds create method in home interface that coresponds to method in implementation class for EntityBeans
366      */

367     public static Method getHomeCreateMethodForEntityImplCreateMethod(Method method, FileObject fo, String JavaDoc className,
368             boolean local) {
369         err.log(">Utility.getHomeCreateMethodForEntityImplCreateMethod (" + method + ", " + fo + ", " + className +
370                 ", " + local + ")");
371         String JavaDoc oldPrefix = PREFIX_EJBCREATE;
372         String JavaDoc newPrefix = PREFIX_CREATE;
373         Class JavaDoc beanType = Entity.class;
374         return getMethodInHomeClassForImpl(fo, beanType, className, local, method, oldPrefix, newPrefix);
375     }
376     
377     /** finds create method in home interface that coresponds to postCreate method in implementation class for EntityBeans*/
378     public static Method getHomeCreateMethodForEntityImplPostCreateMethod(Method method, FileObject fo, String JavaDoc className, boolean local) {
379         err.log(">Utility.getHomeCreateMethodForEntityImplPostCreateMethod (" + method + ", " + fo + ", " + className + ", " + local + ")");
380         return getMethodInHomeClassForImpl(fo, Entity.class, className, local, method, PREFIX_EJBPOSTCREATE, PREFIX_CREATE);
381     }
382     
383     /** finds create method in home interface that coresponds to method in implementation class for EntityBeans*/
384     public static Method getHomePostCreateMethodForEntityImplCreateMethod(Method method, FileObject fo, String JavaDoc className, boolean local) {
385         err.log(">Utility.getHomePostCreateMethodForEntityImplCreateMethod (" + method + ", " + fo + ", " + className + ", " + local + ")");
386         return getMethodInHomeClassForImpl(fo, Entity.class, className, local, method, PREFIX_EJBCREATE, PREFIX_POSTCREATE);
387     }
388     
389     /** finds create method in home interface that coresponds to method in implementation class for EntityBeans*/
390     public static Method getImplPostCreateMethodForEntityImplCreateMethod(Method method) {
391         return getMethod(method, PREFIX_EJBCREATE, PREFIX_EJBPOSTCREATE);
392     }
393     
394     /** finds create method in home interface that coresponds to method in implementation class for EntityBeans*/
395     public static Method getImplCreateMethodForEntityImplPostCreateMethod(Method method) {
396         String JavaDoc oldPrefix = PREFIX_EJBPOSTCREATE;
397         String JavaDoc newPrefix = PREFIX_EJBCREATE;
398         return getMethod(method, oldPrefix, newPrefix);
399     }
400     
401     /** checks if a methods signature matches one of javax.ejb.SessionBean interface methods */
402     public static boolean isSignatureFromSessionBeanInterface(Method method) {
403         return (equalMethods(method, EJB_PASSIVATE, NO_PARAMS)) ||
404                 (equalMethods(method, EJB_ACTIVATE, NO_PARAMS)) ||
405                 (equalMethods(method, EJB_REMOVE, NO_PARAMS)) ||
406                 (equalMethods(method, SET_SESSION_CONTEXT, SET_SESSION_CONTEXT_PARAMTYPENAMES));
407     }
408     
409     /** checks if a methods signature matches one of javax.ejb.EntityBean interface methods */
410     public static boolean isSignatureFromEntityBeanInterface(Method method) {
411         return (equalMethods(method, EJB_PASSIVATE, NO_PARAMS)) ||
412                 (equalMethods(method, EJB_ACTIVATE, NO_PARAMS)) ||
413                 (equalMethods(method, EJB_REMOVE, NO_PARAMS)) ||
414                 (equalMethods(method, EJB_LOAD, NO_PARAMS)) ||
415                 (equalMethods(method, EJB_STORE, NO_PARAMS)) ||
416                 (equalMethods(method, UNSET_ENTITY_CONTEXT, NO_PARAMS)) ||
417                 (equalMethods(method, SET_ENTITY_CONTEXT, SET_ENTITY_CONTEXT_PARAMTYPENAMES));
418     }
419     
420     /** checks if a methods signature matches one of javax.ejb.MessageDrivenBean interface methods */
421     public static boolean isSignatureFromMessageDrivenBeanInterface(Method method) {
422         return (equalMethods(method, EJB_CREATE, NO_PARAMS)) ||
423                 (equalMethods(method, EJB_REMOVE, NO_PARAMS)) ||
424                 (equalMethods(method, SET_MSG_CONTEXT, SET_MSG_CONTEXT_PARAMTYPENAMES));
425     }
426     
427     /** checks if a methods signature matches one of javax.ejb.MessageDrivenBean interface methods */
428     public static boolean isSignatureFromMessageListenerInterface(Method method) {
429         return equalMethods(method, Utility.ON_MESSAGE, Utility.ON_MESSAGE_PARAMTYPENAMES);
430     }
431     
432     /** Returns collection of entity beans from dd or null if something happened */
433     public static EjbJar getEjbJar(org.netbeans.modules.j2ee.api.ejbjar.EjbJar emodule) {
434         if (emodule != null) {
435             FileObject ejbJarFO = emodule.getDeploymentDescriptor();
436             try {
437                 return org.netbeans.modules.j2ee.dd.api.ejb.DDProvider.getDefault().getMergedDDRoot(emodule.getMetadataUnit());
438             } catch (IOException JavaDoc ioe) {
439                 //ignore
440
}
441         }
442         return null;
443     }
444     
445     /** Returns collection of entity beans from dd or null if something happened */
446     public static Entity[] getEntityBeans(org.netbeans.modules.j2ee.api.ejbjar.EjbJar emodule) {
447         EjbJar ejbJarDD = getEjbJar(emodule);
448         if (ejbJarDD != null) {
449             EnterpriseBeans eBeans = ejbJarDD.getEnterpriseBeans();
450             if (eBeans != null) {
451                 return eBeans.getEntity();
452             }
453         }
454         return null;
455     }
456     
457     /** Returns relationships from dd or null if something happened */
458     public static EjbRelation[] getRelationships(org.netbeans.modules.j2ee.api.ejbjar.EjbJar emodule) {
459         EjbJar ejbJarDD = getEjbJar(emodule);
460         if (ejbJarDD != null) {
461             Relationships rels = ejbJarDD.getSingleRelationships();
462             if (rels != null) {
463                 return rels.getEjbRelation();
464             }
465         }
466         return null;
467     }
468     
469     /** Returns collection of entity beans from dd or null if something happened */
470     public static AssemblyDescriptor getAssemblyDescriptor(org.netbeans.modules.j2ee.api.ejbjar.EjbJar emodule) {
471         EjbJar ejbJarDD = getEjbJar(emodule);
472         if (ejbJarDD != null) {
473             return ejbJarDD.getSingleAssemblyDescriptor();
474         }
475         return null;
476     }
477     
478     public static String JavaDoc getEjbNameForClass(org.netbeans.modules.j2ee.dd.api.ejb.EjbJar ejbJarDD, ClassDefinition jc) {
479         EnterpriseBeans eBeans = ejbJarDD.getEnterpriseBeans();
480         if ((eBeans != null)) {
481             Ejb[] ejbs = eBeans.getEjbs();
482             for (int e=0; e<ejbs.length; e++) {
483                 Ejb ejb = ejbs[e];
484                 String JavaDoc ejbClass = ejb.getEjbClass();
485                 err.log("ejbclass: " + ejbClass);
486                 if (ejbClass.equals(jc.getName())) {
487                     return ejb.getEjbName();
488                 }
489             }
490         }
491         return null;
492     }
493     
494     /** Returns collection of entity beans from dd or null if something happened */
495     public static Session[] getSessionBeans(org.netbeans.modules.j2ee.api.ejbjar.EjbJar emodule) {
496         EjbJar ejbJarDD = getEjbJar(emodule);
497         if (ejbJarDD != null) {
498             EnterpriseBeans eBeans = ejbJarDD.getEnterpriseBeans();
499             if (eBeans != null) {
500                 return eBeans.getSession();
501             }
502         }
503         return null;
504     }
505     
506     /** Checks if signature of a 'method' matches 'name' and a set of paramNames */
507     public static boolean equalMethods(Method method, String JavaDoc name, String JavaDoc[] paramNames) {
508         if (!name.equals(method.getName())) {
509             return false;
510         }
511         if (method.getParameters().size() != paramNames.length) {
512             return false;
513         }
514         Iterator it = method.getParameters().iterator();
515         for (int i=0; i < paramNames.length; i++) {
516             Parameter param = (Parameter)it.next();
517             if (!param.getType().getName().equals(paramNames[i])) {
518                 return false;
519             }
520         }
521         return true;
522     }
523     
524     public static boolean equalMethods(Method method1, Method method2) {
525         if (method1 == null || method2 == null) {
526             return method1 == method2;
527         }
528         if (!method1.getName().equals(method2.getName())) {
529             return false;
530         }
531         List parameters1 = method1.getParameters();
532         List parameters2 = method2.getParameters();
533         int n = parameters1.size();
534         if (n != parameters2.size()) {
535             return false;
536         }
537         for (int i = 0; i < n; i++) {
538             Parameter param1 = (Parameter) parameters1.get(i);
539             Parameter param2 = (Parameter) parameters2.get(i);
540             if (!param1.getType().getName().equals(param2.getType().getName())) {
541                 return false;
542             }
543         }
544         return true;
545     }
546     
547     public static void addProblemsToEnd(Problem[] where, Problem what) {
548         where[0] = addProblemsToEnd(where[0], what);
549     }
550     
551     public static Problem addProblemsToEnd(Problem where, Problem what) {
552         if (where == null) {
553             return what;
554         } else{
555             if(what != null) {
556                 Problem tail = where;
557                 while (tail.getNext() != null) {
558                     tail = tail.getNext();
559                 }
560                 tail.setNext(what);
561             }
562             return where;
563         }
564     }
565     
566     /** Finds all ejb projects that depend on a project which is owner of the element */
567     public static Collection/*<EjbJar>*/ getRelevantEjbModules(Element element) {
568         FileObject fo = JavaModel.getFileObject(element.getResource());
569         return Utility.getRelevantEjbModules(fo);
570     }
571     
572     public static Collection/*<EjbJar>*/ getRelevantEjbModules(RefObject refObject) {
573         if (refObject instanceof Element){
574             return getRelevantEjbModules((Element)refObject);
575         }
576         return Collections.emptyList();
577     }
578     
579     /** Finds all ejb projects that depend on a project which is owner of FileObject 'fo' */
580     public static Collection/*<EjbJar>*/ getRelevantEjbModules(FileObject fo) {
581         
582         Project affectedProject = FileOwnerQuery.getOwner(fo);
583         Collection ejbmodules = new ArrayList();
584         Collection projects = new ArrayList();
585         
586         if (affectedProject != null) {
587             // first check if the project which directly contains fo is relevant
588
org.netbeans.modules.j2ee.api.ejbjar.EjbJar emod =
589                     org.netbeans.modules.j2ee.api.ejbjar.EjbJar.getEjbJar(affectedProject.getProjectDirectory());
590             if (emod != null) {
591                 projects.add(affectedProject);
592             } else {
593                 return Collections.EMPTY_SET;
594             }
595             for (Project project : OpenProjects.getDefault().getOpenProjects()){
596                 Object JavaDoc isJ2eeApp = project.getLookup().lookup(J2eeAppProvider.class);
597                 if (isJ2eeApp != null) {
598                     J2eeAppProvider j2eeApp = (J2eeAppProvider) isJ2eeApp;
599                     J2eeModuleProvider[] j2eeModules = j2eeApp.getChildModuleProviders();
600                     
601                     if (j2eeModules != null) {
602                         J2eeModuleProvider affectedPrjProvider =
603                                 (J2eeModuleProvider) affectedProject.getLookup().lookup(J2eeModuleProvider.class);
604
605                         if (affectedPrjProvider != null) {
606                             if (Arrays.asList(j2eeModules).contains(affectedPrjProvider)) {
607                                 for (int k = 0; k < j2eeModules.length; k++) {
608                                     FileObject[] sourceRoots = j2eeModules[k].getSourceRoots();
609                                     if (sourceRoots != null && sourceRoots.length > 0){
610                                         FileObject srcRoot = sourceRoots[0];
611                                         Project p = FileOwnerQuery.getOwner(srcRoot);
612                                         if ((p != null) && (!projects.contains(p))) {
613                                             projects.add(p);
614                                         }
615                                     }
616                                 }
617                             }
618                         }
619                     }
620                 }
621                 Object JavaDoc obj = project.getLookup().lookup(SubprojectProvider.class);
622                 if ((obj != null) && (obj instanceof SubprojectProvider)) {
623                     Set subprojects = ((SubprojectProvider) obj).getSubprojects();
624                     if (subprojects.contains(affectedProject)) {
625                         org.netbeans.modules.j2ee.api.ejbjar.EjbJar em = org.netbeans.modules.j2ee.api.ejbjar.EjbJar
626                                 .getEjbJar(project.getProjectDirectory());
627                         if (em != null) {
628                             if (!projects.contains(project)) { // include each project only once
629
projects.add(project);
630                             }
631                         }
632                     }
633                 }
634             }
635         }
636         
637         for (int j=0; j < projects.size(); j++) {
638             Project prj = (Project)((ArrayList)projects).get(j);
639             org.netbeans.modules.j2ee.api.ejbjar.EjbJar ejb =
640                     org.netbeans.modules.j2ee.api.ejbjar.EjbJar.getEjbJar(prj.getProjectDirectory());
641             if (ejb != null) {
642                 ejbmodules.add(ejb);
643             }
644         }
645         
646         err.log("Affected ejb modules: " + ejbmodules);
647         return ejbmodules;
648     }
649     
650     /** Finds all WS projects affected by the change of FileObject 'fo' */
651     public static Collection/*WebServicesSupport*/ getRelevantWSModules(FileObject fo) {
652         
653         Project affectedProject = FileOwnerQuery.getOwner(fo);
654         Collection wsmodules = new ArrayList();
655         Collection projects = new ArrayList();
656         
657         if (affectedProject != null) {
658             // first check if the project which directly contains fo is relevant
659
WebServicesSupport wsmod = WebServicesSupport.getWebServicesSupport(affectedProject.getProjectDirectory());
660             if (wsmod != null) {
661                 projects.add(affectedProject);
662             } else {
663                 return Collections.EMPTY_SET;
664             }
665             
666             for (Project project : OpenProjects.getDefault().getOpenProjects()){
667                 
668                 Object JavaDoc isJ2eeApp = project.getLookup().lookup(J2eeAppProvider.class);
669                 if (isJ2eeApp != null) {
670                     J2eeAppProvider j2eeApp = (J2eeAppProvider)isJ2eeApp;
671                     J2eeModuleProvider[] j2eeModules = j2eeApp.getChildModuleProviders();
672                 
673                     if (j2eeModules != null) {
674                         J2eeModuleProvider affectedPrjProvider =
675                                 (J2eeModuleProvider)affectedProject.getLookup().lookup(J2eeModuleProvider.class);
676                         if (affectedPrjProvider != null) {
677                             if (Arrays.asList(j2eeModules).contains(affectedPrjProvider)) {
678                                 for (int i=0; i<j2eeModules.length; i++) {
679                                     FileObject[] sourceRoots = j2eeModules[i].getSourceRoots();
680                                     if (sourceRoots != null && sourceRoots.length > 0){
681                                         FileObject srcRoot = sourceRoots[0];
682                                         Project p = FileOwnerQuery.getOwner(srcRoot);
683                                         if ((p != null) && (!projects.contains(p))) {
684                                             projects.add(p);
685                                         }
686                                     }
687                                 }
688                             }
689                         }
690                     }
691                     Object JavaDoc obj = project.getLookup().lookup(SubprojectProvider.class);
692                     if ((obj != null) && (obj instanceof SubprojectProvider)) {
693                         Set subprojects = ((SubprojectProvider)obj).getSubprojects();
694                         if (subprojects.contains(affectedProject)) {
695                             WebServicesSupport ws =
696                                     WebServicesSupport.getWebServicesSupport(project.getProjectDirectory());
697                             if (ws != null) {
698                                 if (!projects.contains(project)) { // include each project only once
699
projects.add(project);
700                                 }
701                             }
702                         }
703                     }
704                 }
705             }
706         }
707         
708         for (int j=0; j < projects.size(); j++) {
709             Project prj = (Project)((ArrayList)projects).get(j);
710             WebServicesSupport websvc = WebServicesSupport.getWebServicesSupport(prj.getProjectDirectory());
711             wsmodules.add(websvc);
712         }
713         
714         err.log("Affected ws modules: " + wsmodules);
715         return wsmodules;
716     }
717     
718     /** Finds all web projects that depend on a project which is owner of FileObject 'fo' */
719     public static Collection/*WebModule*/ getRelevantWebModules(Element e) {
720         Resource r = e.getResource();
721         if (r != null) {
722             FileObject f = JavaModel.getFileObject(r);
723             if (f != null) {
724                 Collection/*WebModule*/ c = getRelevantWebModules(f);
725                 return c;
726             }
727             
728         }
729         return Collections.EMPTY_LIST;
730     }
731     
732     /** Finds all web projects that depend on a project which is owner of FileObject 'fo' */
733     public static Collection/*WebModule*/ getRelevantWebModules(FileObject fo) {
734         
735         Collection webmodules = new ArrayList();
736         Collection projects = new ArrayList();
737         
738         if ( fo == null ) {
739             ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, "Passed null to getRelevantWebModules()");
740             return webmodules;
741         }
742         
743         Project affectedProject = FileOwnerQuery.getOwner(fo);
744         if (affectedProject != null) {
745             // first check if the project which directly contains fo is relevant
746
WebModule wmod = WebModule.getWebModule(affectedProject.getProjectDirectory());
747             if (wmod != null) {
748                 projects.add(affectedProject);
749             } else {
750                 return Collections.EMPTY_SET;
751             }
752             Set globalPath = GlobalPathRegistry.getDefault().getSourceRoots();
753             Iterator iter = globalPath.iterator();
754             while (iter.hasNext()) {
755                 FileObject sourceRoot = (FileObject)iter.next();
756                 Project project = FileOwnerQuery.getOwner(sourceRoot);
757                 if (project != null) {
758                     err.log("found this project: " + project);
759                     Object JavaDoc isJ2eeApp = project.getLookup().lookup(J2eeAppProvider.class);
760                     if (isJ2eeApp != null) {
761                         J2eeAppProvider j2eeApp = (J2eeAppProvider)isJ2eeApp;
762                         err.log("j2eeapp: " + j2eeApp);
763                         J2eeModuleProvider[] j2eeModules = j2eeApp.getChildModuleProviders();
764                         err.log("j2ee modules: " + j2eeModules);
765                         if (j2eeModules != null) {
766                             J2eeModuleProvider affectedPrjProvider =
767                                     (J2eeModuleProvider)affectedProject.getLookup().lookup(J2eeModuleProvider.class);
768                             if (affectedPrjProvider != null) {
769                                 if (Arrays.asList(j2eeModules).contains(affectedPrjProvider)) {
770                                     for (int i=0; i<j2eeModules.length; i++) {
771                                         if (j2eeModules[i] instanceof WebModuleImplementation) {
772                                             FileObject docBase =
773                                                     ((WebModuleImplementation)j2eeModules[i]).getDocumentBase();
774                                             Project p = FileOwnerQuery.getOwner(docBase);
775                                             if ((p != null) && (!projects.contains(p))) {
776                                                 projects.add(p);
777                                             }
778                                         }
779                                     }
780                                 }
781                             }
782                         }
783                     }
784                     Object JavaDoc obj = project.getLookup().lookup(SubprojectProvider.class);
785                     if ((obj != null) && (obj instanceof SubprojectProvider)) {
786                         Set subprojects = ((SubprojectProvider)obj).getSubprojects();
787                         if (subprojects.contains(affectedProject)) {
788                             WebModule wm = WebModule.getWebModule(project.getProjectDirectory());
789                             if (wm != null) {
790                                 if (!projects.contains(project)) { // include each project only once
791
projects.add(project);
792                                 }
793                             }
794                         }
795                     }
796                 }
797             }
798         }
799         
800         for (int j=0; j < projects.size(); j++) {
801             Project prj = (Project)((ArrayList)projects).get(j);
802             WebModule web = WebModule.getWebModule(prj.getProjectDirectory());
803             webmodules.add(web);
804         }
805         
806 // err.log("Affected web modules: " + webmodules);
807
return webmodules;
808     }
809     
810     public static boolean isCmpField(JavaClass jc, String JavaDoc fieldName) {
811         if ((jc != null) && (fieldName != null)) {
812             Collection emodules = getRelevantEjbModules(jc);
813             Iterator e = emodules.iterator();
814             while (e.hasNext()) {
815                 org.netbeans.modules.j2ee.api.ejbjar.EjbJar ejbJar =
816                         (org.netbeans.modules.j2ee.api.ejbjar.EjbJar)e.next();
817                 Entity[] eBeans = getEntityBeans(ejbJar);
818                 if (eBeans != null) {
819                     for (int i=0; i < eBeans.length; i++) {
820                         Entity ent = eBeans[i];
821                         String JavaDoc ejbClass = ent.getEjbClass();
822                         if (jc.getName().equals(ejbClass)) {
823                             CmpField[] cmpFields = ent.getCmpField();
824                             if (cmpFields != null) {
825                                 for (int c=0; c<cmpFields.length; c++) {
826                                     CmpField cmpField = cmpFields[c];
827                                     // TODO: guessing of field name passed to this method should be enhanced
828
// (e.g. when first letter of field is upper-case), just equals shoud be used
829
if (fieldName.equalsIgnoreCase(cmpField.getFieldName())) {
830                                         return true;
831                                     }
832                                 }
833                             }
834                             return false;
835                         }
836                     }
837                 }
838             }
839         }
840         return false;
841     }
842     
843     public static boolean isCmrField(JavaClass jc, String JavaDoc fieldName) {
844         if ((jc != null) && (fieldName != null)) {
845             Collection emodules = Utility.getRelevantEjbModules(jc);
846             Iterator e = emodules.iterator();
847             while (e.hasNext()) {
848                 org.netbeans.modules.j2ee.api.ejbjar.EjbJar ejbJar =
849                         (org.netbeans.modules.j2ee.api.ejbjar.EjbJar)e.next();
850                 EjbRelation[] ejbRelations = getRelationships(ejbJar);
851                 if (ejbRelations != null) {
852                     String JavaDoc ejbName = getEntityEjbNameForClass(jc);
853                     for (int i=0; i < ejbRelations.length; i++) {
854                         EjbRelation relation = ejbRelations[i];
855                         if (relation != null) {
856                             EjbRelationshipRole role = relation.getEjbRelationshipRole();
857                             if (role != null) {
858                                 RelationshipRoleSource relSource = role.getRelationshipRoleSource();
859                                 String JavaDoc roleEjbName = relSource.getEjbName();
860                                 if (roleEjbName.equals(ejbName)) {
861                                     CmrField cmrField = role.getCmrField();
862                                     if (cmrField != null) {
863                                         String JavaDoc cmrFieldName = cmrField.getCmrFieldName();
864                                         // TODO: guessing of field name passed to this method should be enhanced
865
// (e.g. when first letter of field is upper-case), just equals shoud be used
866
if (cmrFieldName.equalsIgnoreCase(fieldName)) {
867                                             return true;
868                                         }
869                                     }
870                                 }
871                             }
872                             EjbRelationshipRole role2 = relation.getEjbRelationshipRole2();
873                             if (role2 != null) {
874                                 RelationshipRoleSource relSource = role2.getRelationshipRoleSource();
875                                 String JavaDoc roleEjbName = relSource.getEjbName();
876                                 if (roleEjbName.equals(ejbName)) {
877                                     CmrField cmrField = role2.getCmrField();
878                                     if (cmrField != null) {
879                                         String JavaDoc cmrFieldName = cmrField.getCmrFieldName();
880                                         // TODO: guessing of field name passed to this method should be enhanced
881
// (e.g. when first letter of field is upper-case), just equals shoud be used
882
if (cmrFieldName.equalsIgnoreCase(fieldName)) {
883                                             return true;
884                                         }
885                                     }
886                                 }
887                             }
888                         }
889                     }
890                 }
891             }
892         }
893         return false;
894     }
895     
896     /* works for entity beans only to be quicker */
897     public static String JavaDoc getEntityEjbNameForClass(JavaClass jc) {
898         if (jc == null) {
899             return null;
900         }
901         Resource res = jc.getResource();
902         if (res != null) {
903             FileObject fo = JavaModel.getFileObject(res);
904             if (fo != null) {
905                 Project prj = FileOwnerQuery.getOwner(fo);
906                 if (prj != null) {
907                     org.netbeans.modules.j2ee.api.ejbjar.EjbJar emod =
908                             org.netbeans.modules.j2ee.api.ejbjar.EjbJar.getEjbJar(prj.getProjectDirectory());
909                     Entity[] entityBeans = getEntityBeans(emod);
910                     if (entityBeans != null) {
911                         for (int e=0; e<entityBeans.length; e++) {
912                             Entity entity = entityBeans[e];
913                             String JavaDoc ejbClass = entity.getEjbClass();
914                             err.log("ejbclass: " + ejbClass);
915                             if (ejbClass.equals(jc.getName())) {
916                                 return entity.getEjbName();
917                             }
918                         }
919                     }
920                 }
921             }
922         }
923         
924         return null;
925     }
926     
927     /**
928      * Creates full class name from package name and simple class name
929      * @param pkg package name
930      * @param simpleName simple class name
931      * @return full class name
932      */

933     public static String JavaDoc getClassName(String JavaDoc pkg, final String JavaDoc simpleName) {
934         return (pkg == null || pkg.length() == 0 ? "" : pkg + ".") + simpleName;
935     }
936     
937     public static Ejb getEjb(EjbJar ejbJar, ClassDefinition classDefinition) {
938         if (classDefinition == null) {
939             return null;
940         }
941         EnterpriseBeans enterpriseBeans = ejbJar.getEnterpriseBeans();
942         if (enterpriseBeans == null) {
943             return null;
944         }
945         String JavaDoc className = classDefinition.getName();
946         Ejb[] ejbs = enterpriseBeans.getEjbs();
947         for (int i = 0; i < ejbs.length; i++) {
948             Ejb ejb = ejbs[i];
949             if (className.equals(ejb.getEjbClass())) {
950                 return ejb;
951             }
952             if (ejb instanceof EntityAndSession) {
953                 EntityAndSession entityAndSession = ((EntityAndSession) ejb);
954                 if (isSubTypeOf(entityAndSession.getLocal(), classDefinition) ||
955                         isSubTypeOf(entityAndSession.getLocalHome(), classDefinition) ||
956                         isSubTypeOf(entityAndSession.getRemote(), classDefinition) ||
957                         isSubTypeOf(entityAndSession.getHome(), classDefinition)) {
958                     return ejb;
959                 }
960             }
961         }
962         return null;
963     }
964     
965     private static boolean isSubTypeOf(String JavaDoc thisClassName, ClassDefinition classDefinition) {
966         return thisClassName != null && resolveClass(thisClassName).isSubTypeOf(classDefinition);
967     }
968     
969     public static String JavaDoc getFieldName(Method method) {
970         final String JavaDoc methodName = method.getName();
971         if (methodName.length() > 3 && (methodName.startsWith(PREFIX_SET) || methodName.startsWith(PREFIX_GET))) {
972             final char c = methodName.charAt(3);
973             if (Character.isUpperCase(c)) {
974                 return Character.toLowerCase(c) + methodName.substring(4);
975             }
976         }
977         return null;
978     }
979     
980     private static String JavaDoc getGetterName(String JavaDoc fieldName) {
981         return PREFIX_GET + firstToUpperCase(fieldName);
982     }
983     
984     
985     private static String JavaDoc getSetterName(String JavaDoc fieldName) {
986         return PREFIX_SET + firstToUpperCase(fieldName);
987     }
988     
989     public static Method getGetterMethod(JavaClass javaClass, String JavaDoc fieldName) {
990         if (javaClass == null) {
991             return null;
992         }
993         return javaClass.getMethod(getGetterName(fieldName), Collections.EMPTY_LIST, true);
994     }
995     
996     private static Method getSetterMethod(JavaClass javaClass, String JavaDoc fieldName, Type type) {
997         if (javaClass == null) {
998             return null;
999         }
1000        return javaClass.getMethod(getSetterName(fieldName), Arrays.asList(new Type[]{type}), true);
1001    }
1002    
1003    public static Collection getAccessMethods(Entity entity, String JavaDoc fieldName) {
1004        Collection accessMethods = new LinkedList();
1005        JavaClass javaClass = resolveClass(entity.getEjbClass());
1006        Method method = getGetterMethod(javaClass, fieldName);
1007        if (method != null) {
1008            accessMethods.add(method);
1009            Type type = method.getType();
1010            addToCollection(accessMethods, getSetterMethod(javaClass, fieldName, type));
1011            
1012            addAccessMethods(accessMethods, resolveClass(entity.getLocal()), fieldName, type);
1013            addAccessMethods(accessMethods, resolveClass(entity.getRemote()), fieldName, type);
1014        }
1015        return accessMethods;
1016    }
1017    
1018    private static void addAccessMethods(Collection accessMethods, JavaClass javaClass, String JavaDoc fieldName, Type type) {
1019        if (javaClass != null) {
1020            addToCollection(accessMethods, getGetterMethod(javaClass, fieldName));
1021            addToCollection(accessMethods, getSetterMethod(javaClass, fieldName, type));
1022        }
1023    }
1024    
1025    public static JavaClass resolveClass(String JavaDoc className) {
1026        return (JavaClass) resolveType(className);
1027    }
1028    
1029    private static Type resolveType(String JavaDoc typeName) {
1030        Type type = JavaModel.getDefaultExtent().getType().resolve(typeName);
1031        if (type instanceof UnresolvedClass) {
1032            Type basicType = JavaModel.getDefaultExtent().getType().resolve("java.lang." + typeName); // NOI18N;
1033
if (!(basicType instanceof UnresolvedClass)) {
1034                return basicType;
1035            }
1036        }
1037        return type;
1038    }
1039    
1040    
1041    
1042    public static void addToCollection(Collection collection, Object JavaDoc object) {
1043        if (object != null) {
1044            collection.add(object);
1045        }
1046    }
1047    
1048    public static org.netbeans.modules.j2ee.api.ejbjar.EjbJar getApiEjbJar(Element element) {
1049        FileObject fileObject = JavaModel.getFileObject(element.getResource());
1050        if (fileObject != null) {
1051            return org.netbeans.modules.j2ee.api.ejbjar.EjbJar.getEjbJar(fileObject);
1052        }
1053        return null;
1054    }
1055    
1056    public static FileObject getEjbJarFileObject(Element element) {
1057        FileObject fileObject = JavaModel.getFileObject(element.getResource());
1058        if (fileObject != null) {
1059            Project project = FileOwnerQuery.getOwner(fileObject);
1060            if (project != null) {
1061                final org.netbeans.modules.j2ee.api.ejbjar.EjbJar ejbJar =
1062                        org.netbeans.modules.j2ee.api.ejbjar.EjbJar.getEjbJar(project.getProjectDirectory());
1063                if (ejbJar != null) {
1064                    return ejbJar.getDeploymentDescriptor();
1065                }
1066            }
1067        }
1068        return null;
1069    }
1070    
1071    public static EjbJar getEjbJar(FileObject fo) {
1072        if (fo != null) {
1073            try {
1074                return DDProvider.getDefault().getMergedDDRoot(org.netbeans.modules.j2ee.api.ejbjar.EjbJar.getEjbJar(fo).getMetadataUnit());
1075            } catch (IOException JavaDoc ioe) {
1076                //ignore
1077
}
1078        }
1079        return null;
1080    }
1081    
1082    public static Method findMethod(ClassDefinition classDefinition, Method method,
1083            boolean includeSuperTypes) {
1084        return getMethod(classDefinition, method.getName(), method.getParameters(), includeSuperTypes);
1085    }
1086    
1087    public static Method getMethod(ClassDefinition classDefinition, String JavaDoc methodName, List parameters,
1088            boolean includeSuperTypes) {
1089        if (classDefinition == null || methodName == null) {
1090            return null;
1091        }
1092        return classDefinition.getMethod(methodName, getTypedParams(parameters), includeSuperTypes);
1093    }
1094    
1095    private static List getQueryMethodParams(Query query) {
1096        String JavaDoc[] methodParam = query.getQueryMethod().getMethodParams().getMethodParam();
1097        List params = new LinkedList();
1098        for (int i = 0; i < methodParam.length; i++) {
1099            params.add(resolveType(methodParam[i]));
1100        }
1101        return params;
1102    }
1103    
1104    private static void addMethodToCollection(String JavaDoc className, Collection queryMethods, String JavaDoc methodName,
1105            List params) {
1106        JavaClass javaClass = resolveClass(className);
1107        addToCollection(queryMethods, getMethod(javaClass, methodName, params, false));
1108    }
1109    
1110    public static Collection getQueryMethods(Entity entity, Query query) {
1111        Collection queryMethods = new LinkedList();
1112        final List params = getQueryMethodParams(query);
1113        final String JavaDoc methodName = query.getQueryMethod().getMethodName();
1114        if (methodName.startsWith(PREFIX_EJBSELECT)) {
1115            addMethodToCollection(entity.getEjbClass(), queryMethods, methodName, params);
1116        } else if (methodName.startsWith(PREFIX_FIND)) {
1117            addMethodToCollection(entity.getLocalHome(), queryMethods, methodName, params);
1118            addMethodToCollection(entity.getHome(), queryMethods, methodName, params);
1119        }
1120        return queryMethods;
1121    }
1122    
1123    public static JavaClass resolveRealClass(String JavaDoc className) {
1124        JavaClass javaClass = resolveClass(className);
1125        return javaClass instanceof UnresolvedClass ? null : javaClass;
1126    }
1127    
1128    public static boolean isSubTypeOf(ClassDefinition classDefinition, String JavaDoc className) {
1129        if (classDefinition == null) {
1130            return false;
1131        }
1132        JavaClass javaClass = resolveClass(className);
1133        if (javaClass instanceof UnresolvedClass) {
1134            for (Iterator it = classDefinition.getInterfaces().iterator(); it.hasNext();) {
1135                if (className.equals(((JavaClass) it.next()).getName())) {
1136                    return true;
1137                }
1138            }
1139            return false;
1140        } else {
1141            return classDefinition.isSubTypeOf(javaClass);
1142        }
1143    }
1144    
1145    public static boolean isFirstUpperCase(String JavaDoc s) {
1146        return s.length() != 0 && Character.isUpperCase(s.charAt(0));
1147    }
1148    
1149    public static boolean isFirstLowerCase(String JavaDoc s) {
1150        return s.length() != 0 && Character.isLowerCase(s.charAt(0));
1151    }
1152    
1153    public static String JavaDoc firstToUpperCase(String JavaDoc s) {
1154        return s.length() == 0 ? s : Character.toUpperCase(s.charAt(0)) + s.substring(1);
1155    }
1156    
1157    public static String JavaDoc firstToLowerCase(String JavaDoc s) {
1158        return s.length() == 0 ? s : Character.toLowerCase(s.charAt(0)) + s.substring(1);
1159    }
1160    
1161    /**
1162     * Constructs new name for given class.
1163     * @param originalFullyQualifiedName old fully qualified name of the class.
1164     * @param newName new unqualified name of the class.
1165     * @return new fully qualified name of the class.
1166     */

1167    public static String JavaDoc renameClass(String JavaDoc originalFullyQualifiedName, String JavaDoc newName){
1168        if (isEmpty(originalFullyQualifiedName) || isEmpty(newName)){
1169            throw new IllegalArgumentException JavaDoc("Old and new name of the class must be given.");
1170        }
1171        int lastDot = originalFullyQualifiedName.lastIndexOf('.');
1172        return (lastDot <= 0) ? newName : originalFullyQualifiedName.substring(0, lastDot + 1) + newName;
1173    }
1174    
1175    /**
1176     * @param qualifiedName fully qualified name of a class
1177     * @return unqualified name
1178     */

1179    public static String JavaDoc unqualify(String JavaDoc qualifiedName){
1180        int lastDot = qualifiedName.lastIndexOf(".");
1181        return (lastDot < 0 ) ? qualifiedName : qualifiedName.substring(lastDot + 1);
1182        
1183    }
1184    
1185    /**
1186     * @return true if given str is null or empty.
1187     */

1188    public static boolean isEmpty(String JavaDoc str){
1189        return str == null || "".equals(str.trim());
1190    }
1191    
1192    /**
1193     * @return property name resolved from given <code>getter</code>, i.e.
1194     * if given arg was <code>getProperty</code>, this method will return
1195     * <code>property</code>. Given getter must follow JavaBeans naming
1196     * converntions for this method to work correctly.
1197     */

1198    public static String JavaDoc getPropertyName(String JavaDoc getter){
1199        if (!getter.startsWith("get")){
1200            return getter;
1201        }
1202        String JavaDoc sub = getter.substring(3);
1203        char c = sub.charAt(0);
1204        if (!Character.isUpperCase(c)){
1205            return getter;
1206        }
1207        return Character.toLowerCase(c) + sub.substring(1);
1208    }
1209    
1210    /**
1211     * @return true if given <code>project</code> is an EJB 3.0 project, false
1212     * otherwise.
1213     */

1214    public static boolean isEjb30(Project project){
1215        if (project == null){
1216            return false;
1217        }
1218        try {
1219            org.netbeans.modules.j2ee.api.ejbjar.EjbJar[] modules = org.netbeans.modules.j2ee.api.ejbjar.EjbJar.getEjbJars(project);
1220            if (modules != null && modules.length > 0){
1221                return org.netbeans.modules.j2ee.dd.api.ejb.EjbJar.VERSION_3_0.equals(DDProvider.getDefault().getMergedDDRoot(modules[0].getMetadataUnit()).getVersion().toString());
1222            }
1223        } catch (IOException JavaDoc e) {
1224            ErrorManager.getDefault().notify(e);
1225        }
1226        return false;
1227    }
1228    
1229    /**
1230     * @return true if given <code>refObject</code> is an instance of
1231     * Element and its project is a EJB 3.0 project, false otherwise.
1232     */

1233    public static boolean isEjb30(RefObject refObject){
1234        if (!(refObject instanceof Element)){
1235            return false;
1236        }
1237        Element element = (Element) refObject;
1238        FileObject fo = JavaModel.getFileObject(element.getResource());
1239        return isEjb30(FileOwnerQuery.getOwner(fo));
1240    }
1241    
1242    /**
1243     * @param ejbModules collection of <code>EjbJar</code>s.
1244     * @return true if any of the given modules has an ejb-jar.xml file.
1245     */

1246    public static boolean hasEjbJar(Collection ejbModules){
1247        for (Object JavaDoc elem : ejbModules) {
1248            org.netbeans.modules.j2ee.api.ejbjar.EjbJar em = (org.netbeans.modules.j2ee.api.ejbjar.EjbJar)elem;
1249            FileObject ejbJarFO = em.getDeploymentDescriptor();
1250            if (ejbJarFO != null){
1251                return true;
1252            }
1253        }
1254        return false;
1255    }
1256    
1257    /**
1258     * @param refObject
1259     * @return true if any of the relevant ajb modules of the given refObject
1260     * has an ejb-jar.xml file.
1261     */

1262    public static boolean hasEjbJar(RefObject refObject){
1263        return hasEjbJar(getRelevantEjbModules(refObject));
1264    }
1265}
1266
Popular Tags