001    /**
002     * Copyright (c) 2000-present 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 MethodKey getMethodKey() {
047                    return _methodKey;
048            }
049    
050            public Object invoke() throws Exception {
051                    Method method = _methodKey.getMethod();
052    
053                    Object targetObject = null;
054    
055                    if (!Modifier.isStatic(method.getModifiers())) {
056                            Class<?> targetClass = _methodKey.getDeclaringClass();
057    
058                            targetObject = targetClass.newInstance();
059                    }
060    
061                    return method.invoke(targetObject, _arguments);
062            }
063    
064            /**
065             * @deprecated As of 7.0.0, replaced by {@link #invoke}
066             */
067            @Deprecated
068            public Object invoke(boolean newInstance) throws Exception {
069                    return invoke();
070            }
071    
072            public Object invoke(Object target) throws Exception {
073                    Method method = _methodKey.getMethod();
074    
075                    return method.invoke(target, _arguments);
076            }
077    
078            @Override
079            public String toString() {
080                    StringBundler sb = new StringBundler(5);
081    
082                    sb.append("{arguments=");
083                    sb.append(Arrays.toString(_arguments));
084                    sb.append(", methodKey=");
085                    sb.append(_methodKey);
086                    sb.append("}");
087    
088                    return sb.toString();
089            }
090    
091            private final Object[] _arguments;
092            private final MethodKey _methodKey;
093    
094    }