1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.kernel.util;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  
28  import java.lang.reflect.InvocationTargetException;
29  import java.lang.reflect.Method;
30  
31  import java.util.ArrayList;
32  import java.util.List;
33  
34  /**
35   * <a href="MethodInvoker.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Brian Wing Shun Chan
38   *
39   */
40  public class MethodInvoker {
41  
42      public static Object invoke(MethodWrapper methodWrapper)
43          throws ClassNotFoundException, IllegalAccessException,
44                 InstantiationException, InvocationTargetException,
45                 NoSuchFieldException, NoSuchMethodException {
46  
47          return invoke(methodWrapper, true);
48      }
49  
50      public static Object invoke(
51              MethodWrapper methodWrapper, boolean newInstance)
52          throws ClassNotFoundException, IllegalAccessException,
53                 InstantiationException, InvocationTargetException,
54                 NoSuchFieldException, NoSuchMethodException {
55  
56          ClassLoader contextClassLoader =
57              Thread.currentThread().getContextClassLoader();
58  
59          String className = methodWrapper.getClassName();
60          String methodName = methodWrapper.getMethodName();
61          Object[] args = methodWrapper.getArgs();
62  
63          List parameterTypes = new ArrayList();
64  
65          for (int i = 0; i < args.length; i++) {
66              if (args[i] == null) {
67                  _log.error(
68                      "Cannot invoke " + className + " " + methodName +
69                          " on position " + i + " because it is null");
70              }
71  
72              Class argClass = args[i].getClass();
73  
74              if (ClassUtil.isSubclass(argClass, PrimitiveWrapper.class)) {
75                  parameterTypes.add(argClass.getField("TYPE").get(args[i]));
76  
77                  MethodKey methodKey = new MethodKey(
78                      argClass.getName(), "getValue", null);
79  
80                  Method method = MethodCache.get(methodKey);
81  
82                  args[i] = method.invoke(args[i], null);
83              }
84              else if (args[i] instanceof NullWrapper) {
85                  NullWrapper nullWrapper = (NullWrapper)args[i];
86  
87                  parameterTypes.add(
88                      contextClassLoader.loadClass(nullWrapper.getClassName()));
89  
90                  args[i] = null;
91              }
92              else {
93                  parameterTypes.add(argClass);
94              }
95          }
96  
97          Object classObj = contextClassLoader.loadClass(className);
98  
99          if (newInstance) {
100             classObj = ((Class)classObj).newInstance();
101         }
102 
103         Method method = null;
104 
105         try {
106             MethodKey methodKey = new MethodKey(
107                 methodWrapper.getClassName(), methodWrapper.getMethodName(),
108                 (Class[])parameterTypes.toArray(new Class[0]));
109 
110             method = MethodCache.get(methodKey);
111         }
112         catch (NoSuchMethodException nsme) {
113             Method[] methods = null;
114 
115             if (newInstance) {
116                 methods = classObj.getClass().getMethods();
117             }
118             else {
119                 methods = ((Class)classObj).getMethods();
120             }
121 
122             for (int i = 0; i < methods.length; i++) {
123                 Class[] methodParameterTypes = methods[i].getParameterTypes();
124 
125                 if (methods[i].getName().equals(methodName) &&
126                     methodParameterTypes.length == parameterTypes.size()) {
127 
128                     boolean correctParams = true;
129 
130                     for (int j = 0; j < parameterTypes.size(); j++) {
131                         Class a = (Class)parameterTypes.get(j);
132                         Class b = methodParameterTypes[j];
133 
134                         if (!ClassUtil.isSubclass(a, b)) {
135                             correctParams = false;
136 
137                             break;
138                         }
139                     }
140 
141                     if (correctParams) {
142                         method = methods[i];
143 
144                         break;
145                     }
146                 }
147             }
148 
149             if (method == null) {
150                 throw nsme;
151             }
152         }
153 
154         Object returnObj = null;
155 
156         if (method != null) {
157             returnObj = method.invoke(classObj, args);
158         }
159 
160         return returnObj;
161     }
162 
163     private static Log _log = LogFactoryUtil.getLog(MethodInvoker.class);
164 
165 }