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 |
| public class InvokeParentRule extends BaseRule |
32 |
| { |
33 |
| private String _methodName; |
34 |
| |
35 |
| private int _depth = 1; |
36 |
| |
37 |
4204
| public InvokeParentRule()
|
38 |
| { |
39 |
| |
40 |
| } |
41 |
| |
42 |
525
| public InvokeParentRule(String methodName)
|
43 |
| { |
44 |
525
| _methodName = methodName;
|
45 |
| } |
46 |
| |
47 |
| |
48 |
| |
49 |
| |
50 |
6606
| public void begin(SchemaProcessor processor, Element element)
|
51 |
| { |
52 |
6606
| Object child = processor.peek();
|
53 |
6606
| Class childClass = child == null ? null : child.getClass();
|
54 |
6606
| Object parent = processor.peek(_depth);
|
55 |
| |
56 |
6606
| try
|
57 |
| { |
58 |
6606
| Method m = findMethod(parent, _methodName, childClass);
|
59 |
| |
60 |
6604
| m.invoke(parent, new Object[]
|
61 |
| { child }); |
62 |
| } |
63 |
| catch (Exception ex) |
64 |
| { |
65 |
2
| throw new ApplicationRuntimeException(RulesMessages.errorInvokingMethod(
|
66 |
| _methodName, |
67 |
| parent, |
68 |
| getLocation(), |
69 |
| ex), getLocation(), ex); |
70 |
| } |
71 |
| } |
72 |
| |
73 |
92
| public String getMethodName()
|
74 |
| { |
75 |
92
| return _methodName;
|
76 |
| } |
77 |
| |
78 |
4204
| public void setMethodName(String string)
|
79 |
| { |
80 |
4204
| _methodName = string;
|
81 |
| } |
82 |
| |
83 |
| |
84 |
| |
85 |
| |
86 |
159
| public int getDepth()
|
87 |
| { |
88 |
159
| return _depth;
|
89 |
| } |
90 |
| |
91 |
| |
92 |
| |
93 |
| |
94 |
3366
| public void setDepth(int i)
|
95 |
| { |
96 |
3366
| _depth = i;
|
97 |
| } |
98 |
| |
99 |
| |
100 |
| |
101 |
| |
102 |
| |
103 |
| |
104 |
| |
105 |
| |
106 |
6606
| private Method findMethod(Object target, String name, Class parameterType)
|
107 |
| throws NoSuchMethodException |
108 |
| { |
109 |
6606
| Method[] methods = target.getClass().getMethods();
|
110 |
| |
111 |
6606
| for (int i = 0; i < methods.length; i++)
|
112 |
| { |
113 |
35993
| Method m = methods[i];
|
114 |
35993
| Class[] parameterTypes = m.getParameterTypes();
|
115 |
| |
116 |
35993
| if (parameterTypes.length != 1)
|
117 |
16254
| continue;
|
118 |
| |
119 |
19739
| if (!m.getName().equals(name))
|
120 |
13134
| continue;
|
121 |
| |
122 |
6605
| if ((parameterType != null && parameterTypes[0].isAssignableFrom(parameterType))
|
123 |
| || (parameterType == null && !parameterTypes[0].isPrimitive())) |
124 |
6604
| return m;
|
125 |
| } |
126 |
| |
127 |
2
| throw new NoSuchMethodException(name);
|
128 |
| } |
129 |
| } |