2009/04/15 - Apache HiveMind has been retired.

For more information, please explore the Attic.

Clover coverage report - Code Coverage for hivemind release 1.2.1
Coverage timestamp: Fri Feb 10 2006 16:33:43 PST
file stats: LOC: 129   Methods: 8
NCLOC: 71   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
InvokeParentRule.java 100% 100% 100% 100%
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 on the parent, passing the
 25    * child. The child object is the top object on the stack and the parent object is the next object
 26    * down on the stack. Created from the <code>&lt;invoke-parent&gt;</code> element. Generally, this
 27    * is the last rule in a sequence of rules.
 28    *
 29    * @author Howard Lewis Ship
 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    * Invokes the named method on the parent object (using reflection).
 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    * @since 1.1
 85    */
 86  159 public int getDepth()
 87    {
 88  159 return _depth;
 89    }
 90   
 91    /**
 92    * Sets the depth of the parent object. The default is 1.
 93    */
 94  3366 public void setDepth(int i)
 95    {
 96  3366 _depth = i;
 97    }
 98   
 99    /**
 100    * Searches for the *first* public method the has the right name, and takes a single parameter
 101    * that is compatible with the parameter type.
 102    *
 103    * @throws NoSuchMethodException
 104    * if a method can't be found
 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    }