|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
InvokeParentRule.java | 87.5% | 100% | 100% | 97.3% |
|
1 |
// Copyright 2004, 2005 The Apache Software Foundation
|
|
2 |
//
|
|
3 |
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4 |
// you may not use this file except in compliance with the License.
|
|
5 |
// You may obtain a copy of the License at
|
|
6 |
//
|
|
7 |
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8 |
//
|
|
9 |
// Unless required by applicable law or agreed to in writing, software
|
|
10 |
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12 |
// See the License for the specific language governing permissions and
|
|
13 |
// limitations under the License.
|
|
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 |
* Rule used to connect a child object to its parent by invoking a method
|
|
25 |
* on the parent, passing the child. The child object is the top object
|
|
26 |
* on the stack and the parent object is the next object down on the stack.
|
|
27 |
* Created from the <code><invoke-parent></code>
|
|
28 |
* element. Generally, this is the last rule in a sequence of rules.
|
|
29 |
*
|
|
30 |
* @author Howard Lewis Ship
|
|
31 |
*/
|
|
32 |
public class InvokeParentRule extends BaseRule |
|
33 |
{ |
|
34 |
private String _methodName;
|
|
35 |
private int _depth = 1; |
|
36 |
|
|
37 | 3741 |
public InvokeParentRule()
|
38 |
{ |
|
39 |
|
|
40 |
} |
|
41 |
|
|
42 | 465 |
public InvokeParentRule(String methodName)
|
43 |
{ |
|
44 | 465 |
_methodName = methodName; |
45 |
} |
|
46 |
|
|
47 |
/**
|
|
48 |
* Invokes the named method on the parent object (using reflection).
|
|
49 |
*/
|
|
50 | 5670 |
public void begin(SchemaProcessor processor, Element element) |
51 |
{ |
|
52 | 5670 |
Object child = processor.peek(); |
53 | 5670 |
Object parent = processor.peek(_depth); |
54 |
|
|
55 | 5670 |
try
|
56 |
{ |
|
57 | 5670 |
Method m = findMethod(parent, _methodName, child.getClass()); |
58 |
|
|
59 | 5669 |
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 | 3741 |
public void setMethodName(String string) |
76 |
{ |
|
77 | 3741 |
_methodName = string; |
78 |
} |
|
79 |
|
|
80 |
/**
|
|
81 |
* @since 1.1
|
|
82 |
*/
|
|
83 | 159 |
public int getDepth() |
84 |
{ |
|
85 | 159 |
return _depth;
|
86 |
} |
|
87 |
|
|
88 |
/**
|
|
89 |
* Sets the depth of the parent object. The default is 1.
|
|
90 |
*/
|
|
91 | 2988 |
public void setDepth(int i) |
92 |
{ |
|
93 | 2988 |
_depth = i; |
94 |
} |
|
95 |
|
|
96 |
/**
|
|
97 |
* Searches for the *first* public method the has the right name, and takes a
|
|
98 |
* single parameter that is compatible with the parameter type.
|
|
99 |
*
|
|
100 |
* @throws NoSuchMethodException if a method can't be found
|
|
101 |
*/
|
|
102 | 5670 |
private Method findMethod(Object target, String name, Class parameterType)
|
103 |
throws NoSuchMethodException
|
|
104 |
{ |
|
105 | 5670 |
Method[] methods = target.getClass().getMethods(); |
106 |
|
|
107 | 5670 |
for (int i = 0; i < methods.length; i++) |
108 |
{ |
|
109 | 20165 |
Method m = methods[i]; |
110 |
|
|
111 | 20165 |
if (m.getParameterTypes().length != 1)
|
112 | 8744 |
continue;
|
113 |
|
|
114 | 11421 |
if (!m.getName().equals(name))
|
115 | 5752 |
continue;
|
116 |
|
|
117 | 5669 |
if (m.getParameterTypes()[0].isAssignableFrom(parameterType))
|
118 | 5669 |
return m;
|
119 |
|
|
120 |
} |
|
121 |
|
|
122 | 1 |
throw new NoSuchMethodException(name); |
123 |
} |
|
124 |
|
|
125 |
} |
|
126 |
|
|