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 Brian Wing Shun Chan
025     */
026    public class MethodWrapper implements Serializable {
027    
028            public MethodWrapper(String className, String methodName) {
029                    this(className, methodName, new Object[0]);
030            }
031    
032            public MethodWrapper(String className, String methodName, Object argument) {
033                    this(className, methodName, new Object[] {argument});
034            }
035    
036            public MethodWrapper(
037                    String className, String methodName, Object[] arguments) {
038    
039                    _className = className;
040                    _methodName = methodName;
041                    _arguments = arguments;
042            }
043    
044            public MethodWrapper(Method method, Object[] arguments) {
045                    this(method.getDeclaringClass().getName(), method.getName(), arguments);
046    
047                    _argumentClassNames = new String[arguments.length];
048    
049                    Class<?>[] parameterTypes = method.getParameterTypes();
050    
051                    for (int i = 0; i < parameterTypes.length; i++) {
052                            _argumentClassNames[i] = parameterTypes[i].getName();
053                    }
054            }
055    
056            public String getClassName() {
057                    return _className;
058            }
059    
060            public String getMethodName() {
061                    return _methodName;
062            }
063    
064            /**
065             * @deprecated Use <code>getArguments</code>.
066             */
067            public Object[] getArgs() {
068                    return getArguments();
069            }
070    
071            public String[] getArgumentClassNames() {
072                    return _argumentClassNames;
073            }
074    
075            public Object[] getArguments() {
076                    Object[] arguments = new Object[_arguments.length];
077    
078                    System.arraycopy(_arguments, 0, arguments, 0, _arguments.length);
079    
080                    return arguments;
081            }
082    
083            public String toString() {
084                    StringBundler sb = new StringBundler(9);
085    
086                    sb.append("{className=");
087                    sb.append(_className);
088                    sb.append(", methodName=");
089                    sb.append(_methodName);
090    
091                    if (_argumentClassNames != null) {
092                            sb.append(", argumentClassNames=");
093                            sb.append(Arrays.toString(_argumentClassNames));
094                    }
095    
096                    sb.append(", arguments=");
097                    sb.append(Arrays.toString(_arguments));
098                    sb.append("}");
099    
100                    return sb.toString();
101            }
102    
103            private String _className;
104            private String _methodName;
105            private String[] _argumentClassNames;
106            private Object[] _arguments;
107    
108    }