1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.fulcrum.yaafi.interceptor.util;
21
22 import java.lang.reflect.Method;
23
24 import org.apache.fulcrum.yaafi.framework.util.StringUtils;
25
26 /**
27 * Creates a string representation of java.lang.reflect.Method
28 *
29 * @author <a href="mailto:siegfried.goeschl@it20one.at">Siegfried Goeschl</a>
30 */
31 public class MethodToStringBuilderImpl implements InterceptorToStringBuilder
32 {
33 /** include the method return type */
34 public static final int INCLUDE_RETURNTYPE = 0x1;
35
36 /** the default mode using class names and hashcode */
37 private static int defaultMode = 0x01;
38
39 /** our current formatting mode */
40 private int mode;
41
42 /** initial size for the StringBuffer */
43 private static final int BUF_SIZE = 512;
44
45 /** the method we are dumping */
46 private Method method;
47
48 /**
49 * Constructor
50 */
51 public MethodToStringBuilderImpl()
52 {
53 this.mode = MethodToStringBuilderImpl.defaultMode;
54 }
55
56 /**
57 * Constructor
58 *
59 * @param method the method to print
60 */
61 public MethodToStringBuilderImpl(Method method)
62 {
63 this.method = method;
64 this.mode = MethodToStringBuilderImpl.defaultMode;
65 }
66
67 /**
68 * Constructor
69 *
70 * @param method the method to print
71 * @param mode the formatting mode
72 */
73 public MethodToStringBuilderImpl(Method method, int mode)
74 {
75 this.method = method;
76 this.mode = mode;
77 }
78
79 /**
80 * @see org.apache.fulcrum.yaafi.interceptor.util.InterceptorToStringBuilder#setMaxArgLength(int)
81 */
82 public void setMaxArgLength(int maxArgLength)
83 {
84
85 }
86
87 /**
88 * @see org.apache.fulcrum.yaafi.interceptor.util.InterceptorToStringBuilder#setMode(int)
89 */
90 public void setMode(int mode)
91 {
92 this.mode = mode;
93 }
94
95 /**
96 * @see org.apache.fulcrum.yaafi.interceptor.util.InterceptorToStringBuilder#setTarget(java.lang.Object)
97 */
98 public void setTarget(Object target)
99 {
100 this.method = (Method) target;
101 }
102
103 /**
104 * @see java.lang.Object#toString()
105 */
106 public String toString()
107 {
108 try
109 {
110 StringBuffer buffer = new StringBuffer(BUF_SIZE);
111
112 Class returnType = method.getReturnType();
113 Class declaringClass = method.getDeclaringClass();
114 Class[] params = method.getParameterTypes();
115
116
117
118 if ((this.mode & INCLUDE_RETURNTYPE) == 1)
119 {
120 buffer.append( this.stripDefaultPackage(returnType.getName()));
121 buffer.append( ' ');
122 }
123
124
125
126 buffer.append( this.stripDefaultPackage(declaringClass.getName()));
127 buffer.append( '.');
128 buffer.append( method.getName() );
129 buffer.append( '(');
130
131
132
133 for (int i = 0; i < params.length; i++)
134 {
135 buffer.append( this.stripDefaultPackage(params[i].getName()) );
136 if (i < (params.length - 1))
137 {
138 buffer.append(",");
139 }
140 }
141
142 buffer.append(")");
143
144 return buffer.toString();
145 }
146 catch (Throwable t)
147 {
148 return "<" + t + ">";
149 }
150 }
151
152 /**
153 * Strips "java.lang" from the argument other than arrays
154 */
155 private String stripDefaultPackage( String arg )
156 {
157 if( arg.charAt(0) == '[' )
158 {
159
160 return StringUtils.replaceChars(arg,';','#');
161 }
162 else
163 {
164 return StringUtils.replace( arg, "java.lang.", "");
165 }
166 }
167 }