1
2
3
4
5
6
7
8
9
10
11
12
13
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
30 * within Acegi Security.
31 *
32 * <p>
33 * All methods of this class return a {@link
34 * org.acegisecurity.util.SimpleMethodInvocation}.
35 * </p>
36 *
37 * @author Ben Alex
38 * @version $Id: MethodInvocationUtils.java,v 1.1 2005/11/25 04:17:24 benalex Exp $
39 */
40 public class MethodInvocationUtils {
41
42
43 /***
44 * Generates a <code>MethodInvocation</code> for specified
45 * <code>methodName</code> on the passed object.
46 *
47 * @param object the object that will be used to find the relevant
48 * <code>Method</code>
49 * @param methodName the name of the method to find
50 *
51 * @return a <code>MethodInvocation</code>, or <code>null</code> if there
52 * was a problem
53 */
54 public static MethodInvocation create(Object object, String methodName) {
55 return create(object, methodName, null);
56 }
57
58 /***
59 * Generates a <code>MethodInvocation</code> for specified
60 * <code>methodName</code> on the passed object, using the
61 * <code>args</code> to locate the method.
62 *
63 * @param object the object that will be used to find the relevant
64 * <code>Method</code>
65 * @param methodName the name of the method to find
66 * @param args arguments that are required as part of the method signature
67 *
68 * @return a <code>MethodInvocation</code>, or <code>null</code> if there
69 * was a problem
70 */
71 public static MethodInvocation create(Object object, String methodName,
72 Object[] args) {
73 Assert.notNull(object, "Object required");
74
75 Class[] classArgs = null;
76
77 if (args != null) {
78 List list = new ArrayList();
79
80 for (int i = 0; i < args.length; i++) {
81 list.add(args[i].getClass());
82 }
83
84 classArgs = (Class[]) list.toArray();
85 }
86
87 return createFromClass(object.getClass(), methodName, classArgs);
88 }
89
90 /***
91 * Generates a <code>MethodInvocation</code> for specified
92 * <code>methodName</code> on the passed class.
93 *
94 * @param clazz the class of object that will be used to find the relevant
95 * <code>Method</code>
96 * @param methodName the name of the method to find
97 *
98 * @return a <code>MethodInvocation</code>, or <code>null</code> if there
99 * was a problem
100 */
101 public static MethodInvocation createFromClass(Class clazz,
102 String methodName) {
103 return createFromClass(clazz, methodName);
104 }
105
106 /***
107 * Generates a <code>MethodInvocation</code> for specified
108 * <code>methodName</code> on the passed class, using the
109 * <code>args</code> to locate the method.
110 *
111 * @param clazz the class of object that will be used to find the relevant
112 * <code>Method</code>
113 * @param methodName the name of the method to find
114 * @param args arguments that are required as part of the method signature
115 *
116 * @return a <code>MethodInvocation</code>, or <code>null</code> if there
117 * was a problem
118 */
119 public static MethodInvocation createFromClass(Class clazz,
120 String methodName, Class[] args) {
121 Assert.notNull(clazz, "Class required");
122 Assert.hasText(methodName, "MethodName required");
123
124 Method method;
125
126 try {
127 method = clazz.getMethod(methodName, args);
128 } catch (Exception e) {
129 return null;
130 }
131
132 return new SimpleMethodInvocation(method, args);
133 }
134 }