View Javadoc

1   package org.apache.commons.digester3;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.lang.reflect.Method;
23  
24  /**
25   * Detached representation of a method invocation.
26   * From Commons [proxy] v2 branch.
27   * @author James Carman
28   * @since 3.2
29   */
30  final class RecordedInvocation
31  {
32  
33      //******************************************************************************************************************
34      // Fields
35      //******************************************************************************************************************
36  
37      private final Method invokedMethod;
38  
39      private final Object[] arguments;
40  
41      //******************************************************************************************************************
42      // Constructors
43      //******************************************************************************************************************
44  
45      /**
46       * Create a new RecordedInvocation instance.
47       *
48       * @param invokedMethod
49       * @param arguments
50       */
51      public RecordedInvocation( Method invokedMethod, Object[] arguments )
52      {
53          this.invokedMethod = invokedMethod;
54          this.arguments = arguments;
55      }
56  
57      //******************************************************************************************************************
58      // Canonical Methods
59      //******************************************************************************************************************
60  
61      /**
62       * Get the invokedMethod.
63       *
64       * @return Method
65       */
66      public Method getInvokedMethod()
67      {
68          return invokedMethod;
69      }
70  
71      /**
72       * Get the arguments.
73       *
74       * @return Object[]
75       */
76      public Object[] getArguments()
77      {
78          return arguments;
79      }
80  
81      /**
82       * {@inheritDoc}
83       */
84      @Override
85      public String toString()
86      {
87          StringBuilder buffer = new StringBuilder();
88          buffer.append( invokedMethod.getDeclaringClass().getName() );
89          buffer.append( "." );
90          buffer.append( invokedMethod.getName() );
91          buffer.append( "(" );
92          int count = arguments.length;
93          for ( int i = 0; i < count; i++ )
94          {
95              Object arg = arguments[i];
96              if ( i > 0 )
97              {
98                  buffer.append( ", " );
99              }
100             convert( buffer, arg );
101         }
102         buffer.append( ")" );
103         return buffer.toString();
104     }
105 
106     /**
107      * Add a string representation of <code>input</code> to <code>buffer</code>.
108      *
109      * @param buffer the buffer to append the string representation of the input object.
110      * @param input the input object has to be serialized to string.
111      */
112     protected void convert( StringBuilder buffer, Object input )
113     {
114         if ( input == null )
115         {
116             buffer.append( "<null>" );
117             return;
118         }
119 
120         // Primitive types, and non-object arrays
121         // use toString().
122         if ( !( input instanceof Object[] ) )
123         {
124             buffer.append( input.toString() );
125             return;
126         }
127         else
128         {
129             buffer.append( "(" );
130             buffer.append( input.getClass().getSimpleName() );
131             buffer.append( "){" );
132             Object[] array = (Object[]) input;
133             int count = array.length;
134             for ( int i = 0; i < count; i++ )
135             {
136                 if ( i > 0 )
137                 {
138                     buffer.append( ", " );
139                 }
140                 // We use convert() again, because it could be a multi-dimensional array
141                 // where each element must be converted.
142                 convert( buffer, array[i] );
143             }
144             buffer.append( "}" );
145         }
146     }
147 
148 }