Coverage Report - org.acegisecurity.util.MethodInvocationUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
MethodInvocationUtils
68% 
100% 
1.8
 
 1  
 /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
 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  
 
 16  
 package org.acegisecurity.util;
 17  
 
 18  
 import org.aopalliance.intercept.MethodInvocation;
 19  
 
 20  
 import org.springframework.util.Assert;
 21  
 
 22  
 import java.lang.reflect.Method;
 23  
 
 24  
 import java.util.ArrayList;
 25  
 import java.util.List;
 26  
 
 27  
 
 28  
 /**
 29  
  * Static utility methods for creating <code>MethodInvocation</code>s usable within Acegi Security.<p>All methods
 30  
  * of this class return a {@link org.acegisecurity.util.SimpleMethodInvocation}.</p>
 31  
  *
 32  
  * @author Ben Alex
 33  
  * @version $Id: MethodInvocationUtils.java 1784 2007-02-24 21:00:24Z luke_t $
 34  
  */
 35  
 public final class MethodInvocationUtils {
 36  
     //~ Constructors ===================================================================================================
 37  
 
 38  0
     private MethodInvocationUtils() {
 39  0
     }
 40  
 
 41  
     //~ Methods ========================================================================================================
 42  
 
 43  
     /**
 44  
      * Generates a <code>MethodInvocation</code> for specified <code>methodName</code> on the passed object.
 45  
      *
 46  
      * @param object the object that will be used to find the relevant <code>Method</code>
 47  
      * @param methodName the name of the method to find
 48  
      *
 49  
      * @return a <code>MethodInvocation</code>, or <code>null</code> if there was a problem
 50  
      */
 51  
     public static MethodInvocation create(Object object, String methodName) {
 52  0
         return create(object, methodName, null);
 53  
     }
 54  
 
 55  
     /**
 56  
      * Generates a <code>MethodInvocation</code> for specified <code>methodName</code> on the passed object,
 57  
      * using the <code>args</code> to locate the method.
 58  
      *
 59  
      * @param object the object that will be used to find the relevant <code>Method</code>
 60  
      * @param methodName the name of the method to find
 61  
      * @param args arguments that are required as part of the method signature
 62  
      *
 63  
      * @return a <code>MethodInvocation</code>, or <code>null</code> if there was a problem
 64  
      */
 65  
     public static MethodInvocation create(Object object, String methodName, Object[] args) {
 66  2
         Assert.notNull(object, "Object required");
 67  
 
 68  2
         Class[] classArgs = null;
 69  
 
 70  2
         if (args != null) {
 71  2
             List list = new ArrayList();
 72  
 
 73  4
             for (int i = 0; i < args.length; i++) {
 74  2
                 list.add(args[i].getClass());
 75  
             }
 76  
 
 77  2
             classArgs = (Class[]) list.toArray(new Class[] {});
 78  
         }
 79  
 
 80  2
         return createFromClass(object.getClass(), methodName, classArgs, args);
 81  
     }
 82  
 
 83  
     /**
 84  
      * Generates a <code>MethodInvocation</code> for specified <code>methodName</code> on the passed class.
 85  
      *
 86  
      * @param clazz the class of object that will be used to find the relevant <code>Method</code>
 87  
      * @param methodName the name of the method to find
 88  
      *
 89  
      * @return a <code>MethodInvocation</code>, or <code>null</code> if there was a problem
 90  
      */
 91  
     public static MethodInvocation createFromClass(Class clazz, String methodName) {
 92  0
         return createFromClass(clazz, methodName, null, null);
 93  
     }
 94  
 
 95  
     /**
 96  
      * Generates a <code>MethodInvocation</code> for specified <code>methodName</code> on the passed class,
 97  
      * using the <code>args</code> to locate the method.
 98  
      *
 99  
      * @param clazz the class of object that will be used to find the relevant <code>Method</code>
 100  
      * @param methodName the name of the method to find
 101  
      * @param classArgs arguments that are required to locate the relevant method signature
 102  
      * @param args the actual arguments that should be passed to SimpleMethodInvocation
 103  
      * @return a <code>MethodInvocation</code>, or <code>null</code> if there was a problem
 104  
      */
 105  
     public static MethodInvocation createFromClass(Class clazz, String methodName, Class[] classArgs, Object[] args) {
 106  4
         Assert.notNull(clazz, "Class required");
 107  4
         Assert.hasText(methodName, "MethodName required");
 108  
 
 109  
         Method method;
 110  
 
 111  
         try {
 112  4
             method = clazz.getMethod(methodName, classArgs);
 113  0
         } catch (Exception e) {
 114  0
             return null;
 115  4
         }
 116  
 
 117  4
         return new SimpleMethodInvocation(method, args);
 118  
     }
 119  
 }