001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.commons.proxy2.invoker;
019
020import java.lang.reflect.Method;
021
022import org.apache.commons.lang3.ArrayUtils;
023import org.apache.commons.proxy2.ProxyUtils;
024
025/**
026 * Detached representation of a method invocation.
027 * 
028 */
029public class RecordedInvocation
030{
031    //******************************************************************************************************************
032    // Fields
033    //******************************************************************************************************************
034
035    private final Method invokedMethod;
036    private final Object[] arguments;
037
038    //******************************************************************************************************************
039    // Constructors
040    //******************************************************************************************************************
041
042    /**
043     * Create a new RecordedInvocation instance.
044     * 
045     * @param invokedMethod
046     * @param arguments
047     */
048    public RecordedInvocation(Method invokedMethod, Object[] arguments)
049    {
050        this.invokedMethod = invokedMethod;
051        this.arguments = ArrayUtils.nullToEmpty(ArrayUtils.clone(arguments));
052    }
053
054    //******************************************************************************************************************
055    // Getter/Setter Methods
056    //******************************************************************************************************************
057
058    /**
059     * Get the invokedMethod.
060     * 
061     * @return Method
062     */
063    public Method getInvokedMethod()
064    {
065        return invokedMethod;
066    }
067
068    //******************************************************************************************************************
069    // Canonical Methods
070    //******************************************************************************************************************
071
072    /**
073     * {@inheritDoc}
074     */
075    @Override
076    public String toString()
077    {
078        StringBuilder buffer = new StringBuilder();
079        buffer.append(invokedMethod.getDeclaringClass().getName());
080        buffer.append(".");
081        buffer.append(invokedMethod.getName());
082        buffer.append("(");
083        int count = arguments.length;
084        for (int i = 0; i < count; i++)
085        {
086            Object arg = arguments[i];
087            if (i > 0)
088            {
089                buffer.append(", ");
090            }
091            convert(buffer, arg);
092        }
093        buffer.append(")");
094        return buffer.toString();
095    }
096
097    //******************************************************************************************************************
098    // Other Methods
099    //******************************************************************************************************************
100
101    /**
102     * Add a string representation of <code>input</code> to <code>buffer</code>.
103     * 
104     * @param buffer
105     * @param input
106     */
107    protected void convert(StringBuilder buffer, Object input)
108    {
109        if (input == null)
110        {
111            buffer.append("<null>");
112            return;
113        }
114
115        // Primitive types, and non-object arrays
116        // use toString().
117        if (!(input instanceof Object[]))
118        {
119            buffer.append(input.toString());
120            return;
121        }
122        buffer.append("(");
123        buffer.append(ProxyUtils.getJavaClassName(input.getClass()));
124        buffer.append("){");
125        Object[] array = (Object[]) input;
126        int count = array.length;
127        for (int i = 0; i < count; i++)
128        {
129            if (i > 0)
130            {
131                buffer.append(", ");
132            }
133            // We use convert() again, because it could be a multi-dimensional array
134            // where each element must be converted.
135            convert(buffer, array[i]);
136        }
137        buffer.append("}");
138    }
139
140    /**
141     * Get the arguments.
142     * 
143     * @return Object[]
144     */
145    public Object[] getArguments()
146    {
147        return ArrayUtils.clone(arguments);
148    }
149}