1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.kernel.util;
16  
17  import java.io.Serializable;
18  
19  import java.lang.reflect.Method;
20  
21  import java.util.Arrays;
22  
23  /**
24   * <a href="MethodHandler.java.html"><b><i>View Source</i></b></a>
25   *
26   * @author Shuyang Zhou
27   */
28  public class MethodHandler implements Serializable {
29  
30      public MethodHandler(Method method, Object... arguments) {
31          this(new MethodKey(method), arguments);
32      }
33  
34      public MethodHandler(MethodKey methodKey, Object... arguments) {
35          _methodKey = methodKey;
36          _arguments = arguments;
37      }
38  
39      public Object[] getArguments() {
40          Object[] arguments = new Object[_arguments.length];
41  
42          System.arraycopy(_arguments, 0, arguments, 0, _arguments.length);
43  
44          return arguments;
45      }
46  
47      public Class<?>[] getArgumentsClasses() {
48          return _methodKey.getParameterTypes();
49      }
50  
51      public String getClassName() {
52          return _methodKey.getClassName();
53      }
54  
55      public MethodKey getMethodKey() {
56          return _methodKey;
57      }
58  
59      public String getMethodName() {
60          return _methodKey.getMethodName();
61      }
62  
63      public Object invoke(boolean newInstance) throws Exception {
64          Thread currentThread = Thread.currentThread();
65  
66          ClassLoader contextClassLoader = currentThread.getContextClassLoader();
67  
68          Object targetObject = null;
69  
70          if (newInstance) {
71              Class<?> targetClass = contextClassLoader.loadClass(
72                  getClassName());
73  
74              targetObject = targetClass.newInstance();
75          }
76  
77          return invoke(targetObject);
78      }
79  
80      public Object invoke(Object target) throws Exception {
81          Method method = MethodCache.get(_methodKey);
82  
83          return method.invoke(target, _arguments);
84      }
85  
86      public String toString() {
87          StringBundler sb = new StringBundler(5);
88  
89          sb.append("{arguments=");
90          sb.append(Arrays.toString(_arguments));
91          sb.append(", methodKey=");
92          sb.append(_methodKey);
93          sb.append("}");
94  
95          return sb.toString();
96      }
97  
98      private Object[] _arguments;
99      private MethodKey _methodKey;
100 
101 }