1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| package org.apache.hivemind.schema.rules; |
16 |
| |
17 |
| import java.lang.reflect.Method; |
18 |
| |
19 |
| import org.apache.hivemind.ApplicationRuntimeException; |
20 |
| import org.apache.hivemind.Element; |
21 |
| import org.apache.hivemind.schema.SchemaProcessor; |
22 |
| |
23 |
| |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| public class InvokeParentRule extends BaseRule |
33 |
| { |
34 |
| private String _methodName; |
35 |
| private int _depth = 1; |
36 |
| |
37 |
4170
| public InvokeParentRule()
|
38 |
| { |
39 |
| |
40 |
| } |
41 |
| |
42 |
519
| public InvokeParentRule(String methodName)
|
43 |
| { |
44 |
519
| _methodName = methodName;
|
45 |
| } |
46 |
| |
47 |
| |
48 |
| |
49 |
| |
50 |
6589
| public void begin(SchemaProcessor processor, Element element)
|
51 |
| { |
52 |
6589
| Object child = processor.peek();
|
53 |
6589
| Object parent = processor.peek(_depth);
|
54 |
| |
55 |
6589
| try
|
56 |
| { |
57 |
6589
| Method m = findMethod(parent, _methodName, child.getClass());
|
58 |
| |
59 |
6588
| m.invoke(parent, new Object[] { child });
|
60 |
| } |
61 |
| catch (Exception ex) |
62 |
| { |
63 |
1
| throw new ApplicationRuntimeException(
|
64 |
| RulesMessages.errorInvokingMethod(_methodName, parent, getLocation(), ex), |
65 |
| getLocation(), |
66 |
| ex); |
67 |
| } |
68 |
| } |
69 |
| |
70 |
92
| public String getMethodName()
|
71 |
| { |
72 |
92
| return _methodName;
|
73 |
| } |
74 |
| |
75 |
4170
| public void setMethodName(String string)
|
76 |
| { |
77 |
4170
| _methodName = string;
|
78 |
| } |
79 |
| |
80 |
| |
81 |
| |
82 |
| |
83 |
159
| public int getDepth()
|
84 |
| { |
85 |
159
| return _depth;
|
86 |
| } |
87 |
| |
88 |
| |
89 |
| |
90 |
| |
91 |
3339
| public void setDepth(int i)
|
92 |
| { |
93 |
3339
| _depth = i;
|
94 |
| } |
95 |
| |
96 |
| |
97 |
| |
98 |
| |
99 |
| |
100 |
| |
101 |
| |
102 |
6589
| private Method findMethod(Object target, String name, Class parameterType)
|
103 |
| throws NoSuchMethodException |
104 |
| { |
105 |
6589
| Method[] methods = target.getClass().getMethods();
|
106 |
| |
107 |
6589
| for (int i = 0; i < methods.length; i++)
|
108 |
| { |
109 |
23346
| Method m = methods[i];
|
110 |
| |
111 |
23346
| if (m.getParameterTypes().length != 1)
|
112 |
10112
| continue;
|
113 |
| |
114 |
13234
| if (!m.getName().equals(name))
|
115 |
6646
| continue;
|
116 |
| |
117 |
6588
| if (m.getParameterTypes()[0].isAssignableFrom(parameterType))
|
118 |
6588
| return m;
|
119 |
| |
120 |
| } |
121 |
| |
122 |
1
| throw new NoSuchMethodException(name);
|
123 |
| } |
124 |
| |
125 |
| } |