1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20 package org.apache.fulcrum.yaafi.interceptor.util;
21
22 import java.lang.reflect.Method;
23
24 /**
25 * Creates a string representation of java.lang.reflect.Method
26 *
27 * @author <a href="mailto:siegfried.goeschl@it20one.at">Siegfried Goeschl</a>
28 */
29 public class MethodToStringBuilderImpl implements InterceptorToStringBuilder
30 {
31 /** include the method return type */
32 public static final int INCLUDE_RETURNTYPE = 0x1;
33
34 /** the default mode using class names and hashcode */
35 private static int defaultMode = 0x01;
36
37 /** our current formatting mode */
38 private int mode;
39
40 /** initial size for the StringBuilder */
41 private static final int BUF_SIZE = 512;
42
43 /** the method we are dumping */
44 private Method method;
45
46 /**
47 * Constructor
48 */
49 public MethodToStringBuilderImpl()
50 {
51 this.mode = MethodToStringBuilderImpl.defaultMode;
52 }
53
54 /**
55 * Constructor
56 *
57 * @param method the method to print
58 */
59 public MethodToStringBuilderImpl(Method method)
60 {
61 this.method = method;
62 this.mode = MethodToStringBuilderImpl.defaultMode;
63 }
64
65 /**
66 * Constructor
67 *
68 * @param method the method to print
69 * @param mode the formatting mode
70 */
71 public MethodToStringBuilderImpl(Method method, int mode)
72 {
73 this.method = method;
74 this.mode = mode;
75 }
76
77 /**
78 * @see org.apache.fulcrum.yaafi.interceptor.util.InterceptorToStringBuilder#setMaxArgLength(int)
79 */
80 public void setMaxArgLength(int maxArgLength)
81 {
82 // not supported
83 }
84
85 /**
86 * @see org.apache.fulcrum.yaafi.interceptor.util.InterceptorToStringBuilder#setMode(int)
87 */
88 public void setMode(int mode)
89 {
90 this.mode = mode;
91 }
92
93 /**
94 * @see org.apache.fulcrum.yaafi.interceptor.util.InterceptorToStringBuilder#setTarget(java.lang.Object)
95 */
96 public void setTarget(Object target)
97 {
98 this.method = (Method) target;
99 }
100
101 /**
102 * @see java.lang.Object#toString()
103 */
104 public String toString()
105 {
106 try
107 {
108 StringBuilder buffer = new StringBuilder(BUF_SIZE);
109
110 Class<?> returnType = method.getReturnType();
111 Class<?> declaringClass = method.getDeclaringClass();
112 Class[] params = method.getParameterTypes();
113
114 // print return type
115
116 if ((this.mode & INCLUDE_RETURNTYPE) == 1)
117 {
118 buffer.append( returnType.getSimpleName() );
119 buffer.append( ' ');
120 }
121
122 // print class and method
123
124 buffer.append( declaringClass.getSimpleName() ) ;
125 buffer.append( '.');
126 buffer.append( method.getName() );
127 buffer.append( '(');
128
129 // print the argument list of the method
130
131 for (int i = 0; i < params.length; i++)
132 {
133 buffer.append( params[i].getSimpleName() );
134 if (i < (params.length - 1))
135 {
136 buffer.append(",");
137 }
138 }
139
140 buffer.append(")");
141
142 return buffer.toString();
143 }
144 catch (Throwable t)
145 {
146 return "<" + t.getClass().getSimpleName() + ">";
147 }
148 }
149 }