001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.kernel.util;
022:
023: import com.liferay.portal.kernel.log.Log;
024: import com.liferay.portal.kernel.log.LogFactoryUtil;
025:
026: import java.lang.reflect.InvocationTargetException;
027: import java.lang.reflect.Method;
028:
029: import java.util.ArrayList;
030: import java.util.List;
031:
032: /**
033: * <a href="MethodInvoker.java.html"><b><i>View Source</i></b></a>
034: *
035: * @author Brian Wing Shun Chan
036: *
037: */
038: public class MethodInvoker {
039:
040: public static Object invoke(MethodWrapper methodWrapper)
041: throws ClassNotFoundException, IllegalAccessException,
042: InstantiationException, InvocationTargetException,
043: NoSuchFieldException, NoSuchMethodException {
044:
045: return invoke(methodWrapper, true);
046: }
047:
048: public static Object invoke(MethodWrapper methodWrapper,
049: boolean newInstance) throws ClassNotFoundException,
050: IllegalAccessException, InstantiationException,
051: InvocationTargetException, NoSuchFieldException,
052: NoSuchMethodException {
053:
054: ClassLoader contextClassLoader = Thread.currentThread()
055: .getContextClassLoader();
056:
057: String className = methodWrapper.getClassName();
058: String methodName = methodWrapper.getMethodName();
059: Object[] args = methodWrapper.getArgs();
060:
061: List parameterTypes = new ArrayList();
062:
063: for (int i = 0; i < args.length; i++) {
064: if (args[i] == null) {
065: _log.error("Cannot invoke " + className + " "
066: + methodName + " on position " + i
067: + " because it is null");
068: }
069:
070: Class argClass = args[i].getClass();
071:
072: if (ClassUtil.isSubclass(argClass, PrimitiveWrapper.class)) {
073: parameterTypes.add(argClass.getField("TYPE").get(
074: args[i]));
075:
076: MethodKey methodKey = new MethodKey(argClass.getName(),
077: "getValue", null);
078:
079: Method method = MethodCache.get(methodKey);
080:
081: args[i] = method.invoke(args[i], null);
082: } else if (args[i] instanceof NullWrapper) {
083: NullWrapper nullWrapper = (NullWrapper) args[i];
084:
085: parameterTypes.add(contextClassLoader
086: .loadClass(nullWrapper.getClassName()));
087:
088: args[i] = null;
089: } else {
090: parameterTypes.add(argClass);
091: }
092: }
093:
094: Object classObj = contextClassLoader.loadClass(className);
095:
096: if (newInstance) {
097: classObj = ((Class) classObj).newInstance();
098: }
099:
100: Method method = null;
101:
102: try {
103: MethodKey methodKey = new MethodKey(methodWrapper
104: .getClassName(), methodWrapper.getMethodName(),
105: (Class[]) parameterTypes.toArray(new Class[0]));
106:
107: method = MethodCache.get(methodKey);
108: } catch (NoSuchMethodException nsme) {
109: Method[] methods = null;
110:
111: if (newInstance) {
112: methods = classObj.getClass().getMethods();
113: } else {
114: methods = ((Class) classObj).getMethods();
115: }
116:
117: for (int i = 0; i < methods.length; i++) {
118: Class[] methodParameterTypes = methods[i]
119: .getParameterTypes();
120:
121: if (methods[i].getName().equals(methodName)
122: && methodParameterTypes.length == parameterTypes
123: .size()) {
124:
125: boolean correctParams = true;
126:
127: for (int j = 0; j < parameterTypes.size(); j++) {
128: Class a = (Class) parameterTypes.get(j);
129: Class b = methodParameterTypes[j];
130:
131: if (!ClassUtil.isSubclass(a, b)) {
132: correctParams = false;
133:
134: break;
135: }
136: }
137:
138: if (correctParams) {
139: method = methods[i];
140:
141: break;
142: }
143: }
144: }
145:
146: if (method == null) {
147: throw nsme;
148: }
149: }
150:
151: Object returnObj = null;
152:
153: if (method != null) {
154: returnObj = method.invoke(classObj, args);
155: }
156:
157: return returnObj;
158: }
159:
160: private static Log _log = LogFactoryUtil
161: .getLog(MethodInvoker.class);
162:
163: }
|