1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.webmacro.engine; |
8 | |
|
9 | |
import org.webmacro.PropertyException; |
10 | |
|
11 | |
import java.lang.reflect.Method; |
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
public class MethodWrapper |
18 | |
{ |
19 | |
|
20 | |
private Object _instance; |
21 | |
private Class _class; |
22 | |
private Method[] _methods; |
23 | |
|
24 | |
|
25 | |
public MethodWrapper (Object o, String methodName) |
26 | 1365 | { |
27 | 1365 | if (o instanceof Class) |
28 | |
{ |
29 | |
|
30 | 1335 | _instance = null; |
31 | 1335 | _class = (Class) o; |
32 | |
} |
33 | |
else |
34 | |
{ |
35 | 30 | _instance = o; |
36 | 30 | _class = _instance.getClass(); |
37 | |
} |
38 | |
|
39 | 1365 | Method[] mArr = _class.getMethods(); |
40 | 1365 | java.util.ArrayList mList = new java.util.ArrayList(mArr.length); |
41 | 1365 | int methCnt = 0; |
42 | 39054 | for (int i = 0; i < mArr.length; i++) |
43 | |
{ |
44 | 37689 | if (mArr[i].getName().equals(methodName)) |
45 | |
{ |
46 | 2001 | methCnt++; |
47 | 2001 | if (_instance == null) |
48 | |
{ |
49 | |
|
50 | 1965 | int mod = mArr[i].getModifiers(); |
51 | 1965 | if (java.lang.reflect.Modifier.isStatic(mod)) mList.add(mArr[i]); |
52 | 1965 | } |
53 | |
else |
54 | 36 | mList.add(mArr[i]); |
55 | |
} |
56 | |
} |
57 | 1365 | if (mList.size() == 0) |
58 | 0 | if (methCnt == 0) |
59 | 0 | throw new IllegalArgumentException("No such method as " + methodName |
60 | |
+ " in class " + _class.getName() + "!"); |
61 | |
else |
62 | 0 | throw new IllegalStateException("Cannot invoke non-static method " |
63 | |
+ methodName + " without an instance of the class " + _class.getName() + "!"); |
64 | 1365 | _methods = new Method[mList.size()]; |
65 | 1365 | mList.toArray(_methods); |
66 | 1365 | } |
67 | |
|
68 | |
public Object invoke (Object[] args) |
69 | |
throws PropertyException |
70 | |
{ |
71 | 53 | Class[] types = IntrospectionUtils.createTypesFromArgs(args); |
72 | 55 | for (int i = 0; i < _methods.length; i++) |
73 | |
{ |
74 | 55 | Method m = _methods[i]; |
75 | 55 | Class[] sig = m.getParameterTypes(); |
76 | 55 | if (IntrospectionUtils.matches(sig, types)) |
77 | |
{ |
78 | |
try |
79 | |
{ |
80 | 53 | Object obj = m.invoke(_instance, args); |
81 | 53 | if (obj == null |
82 | |
&& m.getReturnType() == java.lang.Void.TYPE) |
83 | 2 | return org.webmacro.engine.VoidMacro.instance; |
84 | |
else |
85 | 51 | return obj; |
86 | |
} |
87 | 0 | catch (Exception e) |
88 | |
{ |
89 | 0 | String argList = java.util.Arrays.asList(args).toString(); |
90 | 0 | String errMsg = "Unable to execute " + getDescription() |
91 | |
+ " on supplied args: " + argList; |
92 | 0 | throw new PropertyException(errMsg, e); |
93 | |
} |
94 | |
} |
95 | |
} |
96 | |
|
97 | |
|
98 | 0 | String argList = java.util.Arrays.asList(args).toString(); |
99 | 0 | String errMsg = "Unable to execute " + getDescription() |
100 | |
+ " on supplied args: " + argList; |
101 | 0 | throw new PropertyException(errMsg); |
102 | |
} |
103 | |
|
104 | |
public String getDescription () |
105 | |
{ |
106 | 0 | String methName = _methods[0].getName(); |
107 | 0 | String className = _methods[0].getDeclaringClass().getName(); |
108 | 0 | return className + "." + methName + "()]"; |
109 | |
} |
110 | |
} |