View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.proxy2.invoker;
19  
20  import java.lang.reflect.Method;
21  
22  import org.apache.commons.lang3.ArrayUtils;
23  import org.apache.commons.proxy2.ProxyUtils;
24  
25  /**
26   * Detached representation of a method invocation.
27   * 
28   */
29  public class RecordedInvocation
30  {
31      //******************************************************************************************************************
32      // Fields
33      //******************************************************************************************************************
34  
35      private final Method invokedMethod;
36      private final Object[] arguments;
37  
38      //******************************************************************************************************************
39      // Constructors
40      //******************************************************************************************************************
41  
42      /**
43       * Create a new RecordedInvocation instance.
44       * 
45       * @param invokedMethod
46       * @param arguments
47       */
48      public RecordedInvocation(Method invokedMethod, Object[] arguments)
49      {
50          this.invokedMethod = invokedMethod;
51          this.arguments = ArrayUtils.nullToEmpty(ArrayUtils.clone(arguments));
52      }
53  
54      //******************************************************************************************************************
55      // Getter/Setter Methods
56      //******************************************************************************************************************
57  
58      /**
59       * Get the invokedMethod.
60       * 
61       * @return Method
62       */
63      public Method getInvokedMethod()
64      {
65          return invokedMethod;
66      }
67  
68      //******************************************************************************************************************
69      // Canonical Methods
70      //******************************************************************************************************************
71  
72      /**
73       * {@inheritDoc}
74       */
75      @Override
76      public String toString()
77      {
78          StringBuilder buffer = new StringBuilder();
79          buffer.append(invokedMethod.getDeclaringClass().getName());
80          buffer.append(".");
81          buffer.append(invokedMethod.getName());
82          buffer.append("(");
83          int count = arguments.length;
84          for (int i = 0; i < count; i++)
85          {
86              Object arg = arguments[i];
87              if (i > 0)
88              {
89                  buffer.append(", ");
90              }
91              convert(buffer, arg);
92          }
93          buffer.append(")");
94          return buffer.toString();
95      }
96  
97      //******************************************************************************************************************
98      // Other Methods
99      //******************************************************************************************************************
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 }