Clover coverage report - Code Coverage for hivemind release 1.0-beta-1
Coverage timestamp: Sat Jul 3 2004 09:41:37 EDT
file stats: LOC: 119   Methods: 7
NCLOC: 63   Classes: 1
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%
coverage coverage
 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.HiveMind;
 22   
 import org.apache.hivemind.schema.SchemaProcessor;
 23   
 
 24   
 /**
 25   
  * Rule used to connect a child object to its parent by invoking a method
 26   
  * on the parent, passing the child.  The child object is the top object
 27   
  * on the stack and the parent object is the next object down on the stack.
 28   
  * Created from the <code>&lt;invoke-parent&gt;</code>
 29   
  * element.  Generally, this is the last rule in a sequence of rules.
 30   
  *
 31   
  * @author Howard Lewis Ship
 32   
  */
 33   
 public class InvokeParentRule extends BaseRule
 34   
 {
 35   
     private String _methodName;
 36   
     private int _depth = 1;
 37   
 
 38  2253
     public InvokeParentRule()
 39   
     {
 40   
 
 41   
     }
 42   
 
 43  323
     public InvokeParentRule(String methodName)
 44   
     {
 45  323
         _methodName = methodName;
 46   
     }
 47   
 
 48   
     /**
 49   
      * Invokes the named method on the parent object (using reflection).
 50   
      */
 51  2878
     public void begin(SchemaProcessor processor, Element element)
 52   
     {
 53  2878
         Object child = processor.peek();
 54  2878
         Object parent = processor.peek(_depth);
 55   
 
 56  2878
         try
 57   
         {
 58  2878
             Method m = findMethod(parent, _methodName, child.getClass());
 59   
 
 60  2877
             m.invoke(parent, new Object[] { child });
 61   
         }
 62   
         catch (Exception ex)
 63   
         {
 64  1
             throw new ApplicationRuntimeException(
 65   
                 RulesMessages.errorInvokingMethod(_methodName, parent, getLocation(), ex),
 66   
                 getLocation(),
 67   
                 ex);
 68   
         }
 69   
     }
 70   
 
 71  2
     public String getMethodName()
 72   
     {
 73  2
         return _methodName;
 74   
     }
 75   
 
 76  2253
     public void setMethodName(String string)
 77   
     {
 78  2253
         _methodName = string;
 79   
     }
 80   
 
 81   
     /**
 82   
      * Sets the depth of the parent object. The default is 1.
 83   
      */
 84  1716
     public void setDepth(int i)
 85   
     {
 86  1716
         _depth = i;
 87   
     }
 88   
 
 89   
     /** 
 90   
      * Searches for the *first* public method the has the right name, and takes a
 91   
      * single parameter that is compatible with the parameter type.
 92   
      * 
 93   
      * @throws NoSuchMethodException if a method can't be found 
 94   
      */
 95  2878
     private Method findMethod(Object target, String name, Class parameterType)
 96   
         throws NoSuchMethodException
 97   
     {
 98  2878
         Method[] methods = target.getClass().getMethods();
 99   
 
 100  2878
         for (int i = 0; i < methods.length; i++)
 101   
         {
 102  11193
             Method m = methods[i];
 103   
 
 104  11193
             if (m.getParameterTypes().length != 1)
 105  5905
                 continue;
 106   
 
 107  5288
             if (!m.getName().equals(name))
 108  2411
                 continue;
 109   
 
 110  2877
             if (m.getParameterTypes()[0].isAssignableFrom(parameterType))
 111  2877
                 return m;
 112   
 
 113   
         }
 114   
 
 115  1
         throw new NoSuchMethodException(name);
 116   
     }
 117   
 
 118   
 }
 119