1
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.Array;
29 import java.lang.reflect.InvocationTargetException;
30 import java.lang.reflect.Method;
31
32 import java.util.ArrayList;
33 import java.util.List;
34
35
42 public class MethodInvoker {
43
44 public static Object invoke(MethodWrapper methodWrapper)
45 throws ClassNotFoundException, IllegalAccessException,
46 InstantiationException, InvocationTargetException,
47 NoSuchFieldException, NoSuchMethodException {
48
49 return invoke(methodWrapper, true);
50 }
51
52 public static Object invoke(
53 MethodWrapper methodWrapper, boolean newInstance)
54 throws ClassNotFoundException, IllegalAccessException,
55 InstantiationException, InvocationTargetException,
56 NoSuchFieldException, NoSuchMethodException {
57
58 ClassLoader contextClassLoader =
59 Thread.currentThread().getContextClassLoader();
60
61 String className = methodWrapper.getClassName();
62 String methodName = methodWrapper.getMethodName();
63 Object[] args = methodWrapper.getArgs();
64
65 List<Object> parameterTypes = new ArrayList<Object>();
66
67 for (int i = 0; i < args.length; i++) {
68 if (args[i] == null) {
69 _log.error(
70 "Cannot invoke " + className + " " + methodName +
71 " on position " + i + " because it is null");
72 }
73
74 Class<?> argClass = args[i].getClass();
75
76 if (ClassUtil.isSubclass(argClass, PrimitiveWrapper.class)) {
77 parameterTypes.add(argClass.getField("TYPE").get(args[i]));
78
79 MethodKey methodKey = new MethodKey(
80 argClass.getName(), "getValue", null);
81
82 Method method = MethodCache.get(methodKey);
83
84 args[i] = method.invoke(args[i], (Object[])null);
85 }
86 else if (args[i] instanceof NullWrapper) {
87 NullWrapper nullWrapper = (NullWrapper)args[i];
88
89 String wrappedClassName = nullWrapper.getClassName();
90
91 if (wrappedClassName.startsWith(StringPool.OPEN_BRACKET) &&
92 wrappedClassName.endsWith(StringPool.SEMICOLON)) {
93
94 wrappedClassName = wrappedClassName.substring(
95 2, wrappedClassName.length() - 1);
96
97 Class<?> wrappedClass = contextClassLoader.loadClass(
98 wrappedClassName);
99
100 parameterTypes.add(
101 Array.newInstance(wrappedClass, 0).getClass());
102 }
103 else {
104 Class<?> wrappedClass = contextClassLoader.loadClass(
105 wrappedClassName);
106
107 parameterTypes.add(wrappedClass);
108 }
109
110 args[i] = null;
111 }
112 else {
113 parameterTypes.add(argClass);
114 }
115 }
116
117 Object classObj = contextClassLoader.loadClass(className);
118
119 if (newInstance) {
120 classObj = ((Class<?>)classObj).newInstance();
121 }
122
123 Method method = null;
124
125 try {
126 MethodKey methodKey = new MethodKey(
127 methodWrapper.getClassName(), methodWrapper.getMethodName(),
128 parameterTypes.toArray(new Class[0]));
129
130 method = MethodCache.get(methodKey);
131 }
132 catch (NoSuchMethodException nsme) {
133 Method[] methods = null;
134
135 if (newInstance) {
136 methods = classObj.getClass().getMethods();
137 }
138 else {
139 methods = ((Class<?>)classObj).getMethods();
140 }
141
142 for (int i = 0; i < methods.length; i++) {
143 Class<?>[] methodParameterTypes =
144 methods[i].getParameterTypes();
145
146 if (methods[i].getName().equals(methodName) &&
147 methodParameterTypes.length == parameterTypes.size()) {
148
149 boolean correctParams = true;
150
151 for (int j = 0; j < parameterTypes.size(); j++) {
152 Class<?> a = (Class<?>)parameterTypes.get(j);
153 Class<?> b = methodParameterTypes[j];
154
155 if (!ClassUtil.isSubclass(a, b)) {
156 correctParams = false;
157
158 break;
159 }
160 }
161
162 if (correctParams) {
163 method = methods[i];
164
165 break;
166 }
167 }
168 }
169
170 if (method == null) {
171 throw nsme;
172 }
173 }
174
175 Object returnObj = null;
176
177 if (method != null) {
178 returnObj = method.invoke(classObj, args);
179 }
180
181 return returnObj;
182 }
183
184 private static Log _log = LogFactoryUtil.getLog(MethodInvoker.class);
185
186 }