001: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
002: *
003: * Licensed under the Apache License, Version 2.0 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software
010: * distributed under the License is distributed on an "AS IS" BASIS,
011: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: * See the License for the specific language governing permissions and
013: * limitations under the License.
014: */
015:
016: package org.acegisecurity.util;
017:
018: import org.aopalliance.intercept.MethodInvocation;
019:
020: import org.springframework.util.Assert;
021:
022: import java.lang.reflect.Method;
023:
024: import java.util.ArrayList;
025: import java.util.List;
026:
027: /**
028: * Static utility methods for creating <code>MethodInvocation</code>s usable within Acegi Security.<p>All methods
029: * of this class return a {@link org.acegisecurity.util.SimpleMethodInvocation}.</p>
030: *
031: * @author Ben Alex
032: * @version $Id: MethodInvocationUtils.java 1784 2007-02-24 21:00:24Z luke_t $
033: */
034: public final class MethodInvocationUtils {
035: //~ Constructors ===================================================================================================
036:
037: private MethodInvocationUtils() {
038: }
039:
040: //~ Methods ========================================================================================================
041:
042: /**
043: * Generates a <code>MethodInvocation</code> for specified <code>methodName</code> on the passed object.
044: *
045: * @param object the object that will be used to find the relevant <code>Method</code>
046: * @param methodName the name of the method to find
047: *
048: * @return a <code>MethodInvocation</code>, or <code>null</code> if there was a problem
049: */
050: public static MethodInvocation create(Object object,
051: String methodName) {
052: return create(object, methodName, null);
053: }
054:
055: /**
056: * Generates a <code>MethodInvocation</code> for specified <code>methodName</code> on the passed object,
057: * using the <code>args</code> to locate the method.
058: *
059: * @param object the object that will be used to find the relevant <code>Method</code>
060: * @param methodName the name of the method to find
061: * @param args arguments that are required as part of the method signature
062: *
063: * @return a <code>MethodInvocation</code>, or <code>null</code> if there was a problem
064: */
065: public static MethodInvocation create(Object object,
066: String methodName, Object[] args) {
067: Assert.notNull(object, "Object required");
068:
069: Class[] classArgs = null;
070:
071: if (args != null) {
072: List list = new ArrayList();
073:
074: for (int i = 0; i < args.length; i++) {
075: list.add(args[i].getClass());
076: }
077:
078: classArgs = (Class[]) list.toArray(new Class[] {});
079: }
080:
081: return createFromClass(object.getClass(), methodName,
082: classArgs, args);
083: }
084:
085: /**
086: * Generates a <code>MethodInvocation</code> for specified <code>methodName</code> on the passed class.
087: *
088: * @param clazz the class of object that will be used to find the relevant <code>Method</code>
089: * @param methodName the name of the method to find
090: *
091: * @return a <code>MethodInvocation</code>, or <code>null</code> if there was a problem
092: */
093: public static MethodInvocation createFromClass(Class clazz,
094: String methodName) {
095: return createFromClass(clazz, methodName, null, null);
096: }
097:
098: /**
099: * Generates a <code>MethodInvocation</code> for specified <code>methodName</code> on the passed class,
100: * using the <code>args</code> to locate the method.
101: *
102: * @param clazz the class of object that will be used to find the relevant <code>Method</code>
103: * @param methodName the name of the method to find
104: * @param classArgs arguments that are required to locate the relevant method signature
105: * @param args the actual arguments that should be passed to SimpleMethodInvocation
106: * @return a <code>MethodInvocation</code>, or <code>null</code> if there was a problem
107: */
108: public static MethodInvocation createFromClass(Class clazz,
109: String methodName, Class[] classArgs, Object[] args) {
110: Assert.notNull(clazz, "Class required");
111: Assert.hasText(methodName, "MethodName required");
112:
113: Method method;
114:
115: try {
116: method = clazz.getMethod(methodName, classArgs);
117: } catch (Exception e) {
118: return null;
119: }
120:
121: return new SimpleMethodInvocation(method, args);
122: }
123: }
|