001    /**
002     * Copyright (c) 2000-2010 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    
021    import java.util.Arrays;
022    
023    /**
024     * @author Shuyang Zhou
025     */
026    public class MethodHandler implements Serializable {
027    
028            public MethodHandler(Method method, Object... arguments) {
029                    this(new MethodKey(method), arguments);
030            }
031    
032            public MethodHandler(MethodKey methodKey, Object... arguments) {
033                    _methodKey = methodKey;
034                    _arguments = arguments;
035            }
036    
037            public Object[] getArguments() {
038                    Object[] arguments = new Object[_arguments.length];
039    
040                    System.arraycopy(_arguments, 0, arguments, 0, _arguments.length);
041    
042                    return arguments;
043            }
044    
045            public Class<?>[] getArgumentsClasses() {
046                    return _methodKey.getParameterTypes();
047            }
048    
049            public String getClassName() {
050                    return _methodKey.getClassName();
051            }
052    
053            public MethodKey getMethodKey() {
054                    return _methodKey;
055            }
056    
057            public String getMethodName() {
058                    return _methodKey.getMethodName();
059            }
060    
061            public Object invoke(boolean newInstance) throws Exception {
062                    Thread currentThread = Thread.currentThread();
063    
064                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
065    
066                    Object targetObject = null;
067    
068                    if (newInstance) {
069                            Class<?> targetClass = contextClassLoader.loadClass(
070                                    getClassName());
071    
072                            targetObject = targetClass.newInstance();
073                    }
074    
075                    return invoke(targetObject);
076            }
077    
078            public Object invoke(Object target) throws Exception {
079                    Method method = MethodCache.get(_methodKey);
080    
081                    return method.invoke(target, _arguments);
082            }
083    
084            public String toString() {
085                    StringBundler sb = new StringBundler(5);
086    
087                    sb.append("{arguments=");
088                    sb.append(Arrays.toString(_arguments));
089                    sb.append(", methodKey=");
090                    sb.append(_methodKey);
091                    sb.append("}");
092    
093                    return sb.toString();
094            }
095    
096            private Object[] _arguments;
097            private MethodKey _methodKey;
098    
099    }