001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.util;
016    
017    import java.io.Serializable;
018    
019    import java.lang.reflect.Method;
020    import java.lang.reflect.Modifier;
021    
022    import java.util.Arrays;
023    
024    /**
025     * @author Shuyang Zhou
026     */
027    public class MethodHandler implements Serializable {
028    
029            public MethodHandler(Method method, Object... arguments) {
030                    this(new MethodKey(method), arguments);
031            }
032    
033            public MethodHandler(MethodKey methodKey, Object... arguments) {
034                    _methodKey = methodKey;
035                    _arguments = arguments;
036            }
037    
038            public Object[] getArguments() {
039                    Object[] arguments = new Object[_arguments.length];
040    
041                    System.arraycopy(_arguments, 0, arguments, 0, _arguments.length);
042    
043                    return arguments;
044            }
045    
046            public Class<?>[] getArgumentsClasses() {
047                    return _methodKey.getParameterTypes();
048            }
049    
050            public String getClassName() {
051                    return _methodKey.getClassName();
052            }
053    
054            public MethodKey getMethodKey() {
055                    return _methodKey;
056            }
057    
058            public String getMethodName() {
059                    return _methodKey.getMethodName();
060            }
061    
062            public Object invoke(boolean newInstance) throws Exception {
063                    Method method = MethodCache.get(_methodKey);
064    
065                    Thread currentThread = Thread.currentThread();
066    
067                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
068    
069                    Object targetObject = null;
070    
071                    if (newInstance && !Modifier.isStatic(method.getModifiers())) {
072                            Class<?> targetClass = contextClassLoader.loadClass(
073                                    getClassName());
074    
075                            targetObject = targetClass.newInstance();
076                    }
077    
078                    return method.invoke(targetObject, _arguments);
079            }
080    
081            public Object invoke(Object target) throws Exception {
082                    Method method = MethodCache.get(_methodKey);
083    
084                    return method.invoke(target, _arguments);
085            }
086    
087            @Override
088            public String toString() {
089                    StringBundler sb = new StringBundler(5);
090    
091                    sb.append("{arguments=");
092                    sb.append(Arrays.toString(_arguments));
093                    sb.append(", methodKey=");
094                    sb.append(_methodKey);
095                    sb.append("}");
096    
097                    return sb.toString();
098            }
099    
100            private Object[] _arguments;
101            private MethodKey _methodKey;
102    
103    }