|
|||||||||||||||||||
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.1% |
|
1 |
// Copyright 2004 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 | 2559 |
public InvokeParentRule()
|
38 |
{ |
|
39 |
|
|
40 |
} |
|
41 |
|
|
42 | 426 |
public InvokeParentRule(String methodName)
|
43 |
{ |
|
44 | 426 |
_methodName = methodName; |
45 |
} |
|
46 |
|
|
47 |
/**
|
|
48 |
* Invokes the named method on the parent object (using reflection).
|
|
49 |
*/
|
|
50 | 2996 |
public void begin(SchemaProcessor processor, Element element) |
51 |
{ |
|
52 | 2996 |
Object child = processor.peek(); |
53 | 2996 |
Object parent = processor.peek(_depth); |
54 |
|
|
55 | 2996 |
try
|
56 |
{ |
|
57 | 2996 |
Method m = findMethod(parent, _methodName, child.getClass()); |
58 |
|
|
59 | 2995 |
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 | 2 |
public String getMethodName()
|
71 |
{ |
|
72 | 2 |
return _methodName;
|
73 |
} |
|
74 |
|
|
75 | 2559 |
public void setMethodName(String string) |
76 |
{ |
|
77 | 2559 |
_methodName = string; |
78 |
} |
|
79 |
|
|
80 |
/**
|
|
81 |
* Sets the depth of the parent object. The default is 1.
|
|
82 |
*/
|
|
83 | 1992 |
public void setDepth(int i) |
84 |
{ |
|
85 | 1992 |
_depth = i; |
86 |
} |
|
87 |
|
|
88 |
/**
|
|
89 |
* Searches for the *first* public method the has the right name, and takes a
|
|
90 |
* single parameter that is compatible with the parameter type.
|
|
91 |
*
|
|
92 |
* @throws NoSuchMethodException if a method can't be found
|
|
93 |
*/
|
|
94 | 2996 |
private Method findMethod(Object target, String name, Class parameterType)
|
95 |
throws NoSuchMethodException
|
|
96 |
{ |
|
97 | 2996 |
Method[] methods = target.getClass().getMethods(); |
98 |
|
|
99 | 2996 |
for (int i = 0; i < methods.length; i++) |
100 |
{ |
|
101 | 9798 |
Method m = methods[i]; |
102 |
|
|
103 | 9798 |
if (m.getParameterTypes().length != 1)
|
104 | 4107 |
continue;
|
105 |
|
|
106 | 5691 |
if (!m.getName().equals(name))
|
107 | 2696 |
continue;
|
108 |
|
|
109 | 2995 |
if (m.getParameterTypes()[0].isAssignableFrom(parameterType))
|
110 | 2995 |
return m;
|
111 |
|
|
112 |
} |
|
113 |
|
|
114 | 1 |
throw new NoSuchMethodException(name); |
115 |
} |
|
116 |
|
|
117 |
} |
|
118 |
|
|