Coverage Report - org.webmacro.engine.MethodWrapper
 
Classes in this File Line Coverage Branch Coverage Complexity
MethodWrapper
41%
18/43
36%
8/22
7
 
 1  
 /*
 2  
  * MethodWrapper.java
 3  
  *
 4  
  * Created on May 24, 2002, 12:01 AM
 5  
  */
 6  
 
 7  
 package org.webmacro.engine;
 8  
 
 9  
 import org.webmacro.PropertyException;
 10  
 
 11  
 import java.lang.reflect.Method;
 12  
 
 13  
 /**
 14  
  *
 15  
  * @author  Keats
 16  
  */
 17  
 public class MethodWrapper
 18  
 {
 19  
 
 20  
     private Object _instance;
 21  
     private Class _class;
 22  
     private Method[] _methods;
 23  
 
 24  
     /** Creates a new instance of MethodWrapperMacro */
 25  
     public MethodWrapper (Object o, String methodName)
 26  126
     {
 27  126
         if (o instanceof Class)
 28  
         {
 29  
             // static methods
 30  126
             _instance = null;
 31  126
             _class = (Class) o;
 32  
         }
 33  
         else
 34  
         {
 35  0
             _instance = o;
 36  0
             _class = _instance.getClass();
 37  
         }
 38  
 
 39  126
         Method[] mArr = _class.getMethods();
 40  126
         java.util.ArrayList mList = new java.util.ArrayList(mArr.length);
 41  126
         int methCnt = 0;
 42  3432
         for (int i = 0; i < mArr.length; i++)
 43  
         {
 44  3306
             if (mArr[i].getName().equals(methodName))
 45  
             {
 46  186
                 methCnt++;
 47  186
                 if (_instance == null)
 48  
                 {
 49  
                     // no instance, exclude non-static methods
 50  186
                     int mod = mArr[i].getModifiers();
 51  186
                     if (java.lang.reflect.Modifier.isStatic(mod)) mList.add(mArr[i]);
 52  186
                 }
 53  
                 else
 54  0
                     mList.add(mArr[i]);
 55  
             }
 56  
         }
 57  126
         if (mList.size() == 0)
 58  0
             if (methCnt == 0)
 59  0
                 throw new IllegalArgumentException("No such method as " + methodName
 60  
                         + " in class " + _class.getName() + "!");
 61  
             else
 62  0
                 throw new IllegalStateException("Cannot invoke non-static method "
 63  
                         + methodName + " without an instance of the class " + _class.getName() + "!");
 64  126
         _methods = new Method[mList.size()];
 65  126
         mList.toArray(_methods);
 66  126
     }
 67  
 
 68  
     public Object invoke (Object[] args)
 69  
             throws PropertyException
 70  
     {
 71  0
         Class[] types = IntrospectionUtils.createTypesFromArgs(args);
 72  0
         for (int i = 0; i < _methods.length; i++)
 73  
         {
 74  0
             Method m = _methods[i];
 75  0
             Class[] sig = m.getParameterTypes();
 76  0
             if (IntrospectionUtils.matches(sig, types))
 77  
             {
 78  
                 try
 79  
                 {
 80  0
                     Object obj = m.invoke(_instance, args);
 81  0
                     if (obj == null
 82  
                             && m.getReturnType() == java.lang.Void.TYPE)
 83  0
                         return org.webmacro.engine.VoidMacro.instance;
 84  
                     else
 85  0
                         return obj;
 86  
                 }
 87  0
                 catch (Exception e)
 88  
                 {
 89  0
                     String argList = java.util.Arrays.asList(args).toString();
 90  0
                     String errMsg = "Unable to execute " + getDescription()
 91  
                             + " on supplied args: " + argList;
 92  0
                     throw new PropertyException(errMsg, e);
 93  
                 }
 94  
             }
 95  
         }
 96  
       
 97  
         // not found
 98  0
         String argList = java.util.Arrays.asList(args).toString();
 99  0
         String errMsg = "Unable to execute " + getDescription()
 100  
                 + " on supplied args: " + argList;
 101  0
         throw new PropertyException(errMsg);
 102  
     }
 103  
 
 104  
     public String getDescription ()
 105  
     {
 106  0
         String methName = _methods[0].getName();
 107  0
         String className = _methods[0].getDeclaringClass().getName();
 108  0
         return className + "." + methName + "()]";
 109  
     }
 110  
 }