KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jdon > aop > reflection > MethodConstructor


1 /**
2  * Copyright 2003-2006 the original author or authors. Licensed under the Apache
3  * License, Version 2.0 (the "License"); you may not use this file except in
4  * compliance with the License. You may obtain a copy of the License at
5  *
6  * http://www.apache.org/licenses/LICENSE-2.0
7  *
8  * Unless required by applicable law or agreed to in writing, software
9  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
11  * License for the specific language governing permissions and limitations under
12  * the License.
13  */

14
15 package com.jdon.aop.reflection;
16
17 import java.lang.reflect.Method JavaDoc;
18
19 import com.jdon.bussinessproxy.TargetMetaDef;
20 import com.jdon.bussinessproxy.meta.MethodMetaArgs;
21 import com.jdon.bussinessproxy.target.TargetServiceFactory;
22 import com.jdon.container.ContainerWrapper;
23 import com.jdon.container.access.TargetMetaRequest;
24 import com.jdon.container.visitor.ComponentVisitor;
25 import com.jdon.util.Debug;
26
27 public class MethodConstructor {
28
29     private final static String JavaDoc module = MethodConstructor.class.getName();
30
31     private MethodInvokerUtil methodInvokerUtil = new MethodInvokerUtil();
32     
33     
34     
35     /**
36      * @return Returns the methodInvokerUtil.
37      */

38     public MethodInvokerUtil getMethodInvokerUtil() {
39         return methodInvokerUtil;
40     }
41     
42     /**
43      * ejb's method creating must at first get service's EJB Object;
44      * pojo's method creating can only need service's class.
45      *
46      * @param targetServiceFactory
47      * @param targetMetaRequest
48      * @param methodMetaArgs
49      * @return
50      */

51     public Method JavaDoc createMethod(TargetServiceFactory targetServiceFactory,
52                                TargetMetaRequest targetMetaRequest) {
53         Method JavaDoc method = null;
54         Debug.logVerbose("[JdonFramework] enter create the Method " , module);
55         try {
56             if (targetMetaRequest.getTargetMetaDef().isEJB()) {
57                 Object JavaDoc obj= methodInvokerUtil.createTargetObject(targetServiceFactory, targetMetaRequest);
58                 method = createObjectMethod(obj, targetMetaRequest.getMethodMetaArgs());
59             }else{
60                 method = createPojoMethod(targetMetaRequest);
61             }
62         } catch (Exception JavaDoc ex) {
63             Debug.logError("[JdonFramework] createMethod error: " + ex, module);
64         }
65         
66         return method;
67
68     }
69     
70     
71     /**
72      * create a method object by its meta definition
73      * @param targetMetaDef
74      * @param cw
75      * @param methodMetaArgs
76      */

77     public Method JavaDoc createPojoMethod(TargetMetaRequest targetMetaRequest) {
78         Method JavaDoc method = null;
79         TargetMetaDef targetMetaDef = targetMetaRequest.getTargetMetaDef();
80         MethodMetaArgs methodMetaArgs = targetMetaRequest.getMethodMetaArgs();
81         Debug.logVerbose("[JdonFramework] createPOJO Method :" + methodMetaArgs.getMethodName() + " for target service: " + targetMetaDef.getName(), module);
82         try {
83             ComponentVisitor cm = targetMetaRequest.getComponentVisitor();
84             Class JavaDoc thisCLass = cm.getContainerWrapper().getComponentClass(targetMetaDef.getName());
85             if (thisCLass == null) return null;
86             method = thisCLass.getMethod(methodMetaArgs.getMethodName(),
87                     methodMetaArgs.getParamTypes());
88         } catch (NoSuchMethodException JavaDoc ne) {
89             Debug.logError("[JdonFramework] method name:"
90                     + methodMetaArgs.getMethodName() + " or method parameters type don't match with your service's method", module);
91             Object JavaDoc types[] = methodMetaArgs.getParamTypes();
92             for(int i = 0; i<types.length; i ++){
93                 Debug.logError("[JdonFramework]service's method parameter type must be:" + types[i] + "; ", module);
94             }
95         } catch (Exception JavaDoc ex) {
96             Debug.logError("[JdonFramework] createPojoMethod error: " + ex, module);
97         }
98         
99         return method;
100
101     }
102     
103     /**
104      * create a method object by target Object
105      * @param ownerClass
106      * @param methodMetaArgs
107      * @return
108      */

109     public Method JavaDoc createObjectMethod(Object JavaDoc ownerClass, MethodMetaArgs methodMetaArgs) {
110         Method JavaDoc m = null;
111         try {
112             m = ownerClass.getClass().getMethod(methodMetaArgs.getMethodName(),
113                                                 methodMetaArgs.getParamTypes());
114         } catch (NoSuchMethodException JavaDoc nsme) {
115             String JavaDoc errS = " NoSuchMethod:" + methodMetaArgs.getMethodName() + " in MethodMetaArgs of className:"
116                     + ownerClass.getClass().getName();
117             Debug.logError(errS, module);
118         } catch (Exception JavaDoc ex) {
119             Debug.logError("[JdonFramework] createMethod error:" + ex, module);
120         }
121         return m;
122     }
123     
124     /**
125      * create a method object
126      * @param ownerClass
127      * @param methodName
128      * @param paramTypes
129      * @return
130      */

131     public Method JavaDoc createObjectMethod(Object JavaDoc ownerClass, String JavaDoc methodName,
132             Class JavaDoc[] paramTypes) {
133         Method JavaDoc m = null;
134         try {
135             m = ownerClass.getClass().getMethod(methodName, paramTypes);
136         } catch (NoSuchMethodException JavaDoc nsme) {
137             String JavaDoc errS = " NoSuchMethod:" + methodName + " in className:"
138                     + ownerClass.getClass().getName() + " or method's args type error";
139             Debug.logError(errS, module);
140         } catch (Exception JavaDoc ex) {
141             Debug.logError("[JdonFramework] createMethod error:" + ex, module);
142         }
143         return m;
144     }
145
146 }
147
Popular Tags