001    /**
002     * Copyright (c) 2000-2013 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(getClassName());
073    
074                            targetObject = targetClass.newInstance();
075                    }
076    
077                    return method.invoke(targetObject, _arguments);
078            }
079    
080            public Object invoke(Object target) throws Exception {
081                    Method method = MethodCache.get(_methodKey);
082    
083                    return method.invoke(target, _arguments);
084            }
085    
086            @Override
087            public String toString() {
088                    StringBundler sb = new StringBundler(5);
089    
090                    sb.append("{arguments=");
091                    sb.append(Arrays.toString(_arguments));
092                    sb.append(", methodKey=");
093                    sb.append(_methodKey);
094                    sb.append("}");
095    
096                    return sb.toString();
097            }
098    
099            private Object[] _arguments;
100            private MethodKey _methodKey;
101    
102    }