KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > standard > lang > jpath > expression > MethodCall


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

16
17 package org.apache.taglibs.standard.lang.jpath.expression;
18
19 import java.beans.BeanInfo JavaDoc;
20 import java.beans.IntrospectionException JavaDoc;
21 import java.beans.Introspector JavaDoc;
22 import java.beans.MethodDescriptor JavaDoc;
23 import java.lang.reflect.InvocationTargetException JavaDoc;
24 import java.lang.reflect.Method JavaDoc;
25 import java.lang.reflect.Modifier JavaDoc;
26
27 import javax.servlet.jsp.PageContext JavaDoc;
28
29 import org.apache.taglibs.standard.lang.jpath.adapter.IterationContext;
30
31 /**
32  * The MethodCall class
33  *
34  *
35  * @author <a HREF='mailto:scott.hasse@isthmusgroup.com'>Scott Hasse</a>
36  * @version
37  */

38 public class MethodCall extends SimpleNode implements Introspectable {
39
40     /**
41      * Used to create an instance of the MethodCall class
42      *
43      *
44      * @param id
45      *
46      */

47     public MethodCall(int id) {
48         super(id);
49     }
50
51     /**
52      * Used to create an instance of the MethodCall class
53      *
54      *
55      * @param p
56      * @param id
57      *
58      */

59     public MethodCall(Parser p, int id) {
60         super(p, id);
61     }
62
63     /**
64      * The evaluate method
65      *
66      *
67      * @param pageContext
68      * @param icontext
69      * @param scope
70      *
71      * @return
72      *
73      * @throws EvaluationException
74      *
75      */

76     public Object JavaDoc evaluate(
77             PageContext JavaDoc pageContext, IterationContext icontext, int scope)
78                 throws EvaluationException {
79         throw new EvaluationException(this,
80                 "A MethodCall must be called on " + "another object");
81     }
82
83     /**
84      * The evaluate method
85      *
86      *
87      * @param pageContext
88      * @param icontext
89      * @param parent
90      *
91      * @return
92      *
93      * @throws EvaluationException
94      *
95      */

96     public Object JavaDoc evaluate(
97             PageContext JavaDoc pageContext, IterationContext icontext, Object JavaDoc parent)
98                 throws EvaluationException {
99
100         Object JavaDoc result = null;
101
102         //try {
103
String JavaDoc methodName = ((Identifier) jjtGetChild(0)).val;
104
105         if (parent != null) {
106             try {
107                 MethodDescriptor JavaDoc md = getFeatureDescriptor(parent.getClass(),
108                                           methodName);
109                 Object JavaDoc[] args = new Object JavaDoc[jjtGetNumChildren() - 1];
110
111                 for (int i = 1; i < jjtGetNumChildren(); i++) {
112                     args[i - 1] = jjtGetChild(i).evaluate(pageContext,
113                             icontext);
114                 }
115
116                 if (md != null) {
117
118                     //result = getAttribute(md, parent, args);
119
result = tempGetAttribute(parent, methodName, args);
120                 }
121             } catch (IntrospectionException JavaDoc ie) {
122                 throw new EvaluationException(this,
123                         "Introspection Exception:" + ie.getMessage());
124             } catch (NoSuchMethodException JavaDoc nsme) {
125                 throw new EvaluationException(this,
126                         "NoSuchMethodException:" + nsme.getMessage());
127             } catch (IllegalAccessException JavaDoc iae) {
128                 throw new EvaluationException(this,
129                         "IllegalAccessException:" + iae.getMessage());
130             } catch (InvocationTargetException JavaDoc ite) {
131                 throw new EvaluationException(this,
132                         "InvocationTargetException:" + ite.getMessage());
133             }
134         }
135
136         //result = Convert.toJSPType(result);
137
//} catch (ConversionException ce) {
138
//throw new EvaluationException(this, ce.getMessage());
139
//}
140
return result;
141     }
142
143     /**
144      * The getFeatureDescriptor method
145      *
146      *
147      * @param c
148      * @param key
149      *
150      * @return
151      *
152      * @throws IntrospectionException
153      *
154      */

155     private MethodDescriptor JavaDoc getFeatureDescriptor(Class JavaDoc c, String JavaDoc key)
156             throws IntrospectionException JavaDoc {
157
158         BeanInfo JavaDoc beanInfo = Introspector.getBeanInfo(c);
159         MethodDescriptor JavaDoc[] mda = beanInfo.getMethodDescriptors();
160
161         for (int i = mda.length - 1; i >= 0; --i) {
162             MethodDescriptor JavaDoc md = mda[i];
163
164             if (md.getName().equals(key)) {
165                 return md;
166             }
167         }
168
169         return null;
170     }
171
172     /**
173      * The getAttribute method
174      *
175      *
176      * @param md
177      * @param o
178      * @param args
179      *
180      * @return
181      *
182      * @throws IllegalAccessException
183      * @throws InvocationTargetException
184      * @throws NoSuchMethodException
185      *
186      */

187     private Object JavaDoc getAttribute(MethodDescriptor JavaDoc md, Object JavaDoc o, Object JavaDoc[] args)
188             throws NoSuchMethodException JavaDoc, IllegalAccessException JavaDoc,
189                 InvocationTargetException JavaDoc {
190
191         Object JavaDoc result = null;
192         Method JavaDoc m = md.getMethod();
193
194         result = m.invoke(o, args);
195
196         return result;
197     }
198
199     /**
200      * The tempGetAttribute method
201      *
202      *
203      * @param parent
204      * @param key
205      * @param args
206      *
207      * @return
208      *
209      * @throws IllegalAccessException
210      * @throws IntrospectionException
211      * @throws InvocationTargetException
212      * @throws NoSuchMethodException
213      *
214      */

215     private Object JavaDoc tempGetAttribute(Object JavaDoc parent, String JavaDoc key, Object JavaDoc[] args)
216             throws IntrospectionException JavaDoc, NoSuchMethodException JavaDoc,
217                 IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
218
219         Object JavaDoc result;
220         Class JavaDoc c = parent.getClass();
221         Method JavaDoc[] methods = c.getMethods();
222         Method JavaDoc m = null;
223
224         for (int i = 0; i < methods.length; i++) {
225             if (methods[i].getName().equals(key)) {
226                 m = methods[i];
227             }
228         }
229
230         m = getPublicMethod(c, m.getName(), m.getParameterTypes());
231         result = m.invoke(parent, args);
232
233         return result;
234     }
235
236     /**
237      * The getPublicMethod method
238      *
239      *
240      * @param c
241      * @param name
242      * @param paramTypes
243      *
244      * @return
245      *
246      * @throws NoSuchMethodException
247      *
248      */

249     private Method JavaDoc getPublicMethod(Class JavaDoc c, String JavaDoc name, Class JavaDoc[] paramTypes)
250             throws NoSuchMethodException JavaDoc {
251
252         Method JavaDoc result = null;
253
254         if ((c.getModifiers() & Modifier.PUBLIC) == 0) {
255             Class JavaDoc sc = c.getSuperclass();
256
257             if (sc != null) {
258                 try {
259                     result = getPublicMethod(sc, name, paramTypes);
260                 } catch (NoSuchMethodException JavaDoc nsme) {
261
262                     //Intentionally ignored and thrown later
263
}
264             }
265
266             if (result == null) {
267                 Class JavaDoc[] interfaces = c.getInterfaces();
268
269                 for (int i = 0; i < interfaces.length; i++) {
270                     try {
271                         result = getPublicMethod(interfaces[i], name,
272                                 paramTypes);
273                     } catch (NoSuchMethodException JavaDoc nsme) {
274
275                         //Intentionally ignored and thrown later
276
}
277                 }
278             }
279         } else {
280
281             //It was public
282
result = c.getMethod(name, paramTypes);
283         }
284
285         return result;
286     }
287 }
288
Popular Tags