Clover coverage report - Code Coverage for hivemind release 1.1-beta-2
Coverage timestamp: Tue Jun 28 2005 10:28:23 EDT
file stats: LOC: 125   Methods: 8
NCLOC: 66   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
InvokeParentRule.java 87.5% 100% 100% 97.3%
coverage coverage
 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>&lt;invoke-parent&gt;</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  4170 public InvokeParentRule()
 38    {
 39   
 40    }
 41   
 42  519 public InvokeParentRule(String methodName)
 43    {
 44  519 _methodName = methodName;
 45    }
 46   
 47    /**
 48    * Invokes the named method on the parent object (using reflection).
 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    * @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  3339 public void setDepth(int i)
 92    {
 93  3339 _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  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    }