1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 package org.slf4j.instrumentation;
26
27 import javassist.CtBehavior;
28 import javassist.CtClass;
29 import javassist.CtMethod;
30 import javassist.Modifier;
31 import javassist.NotFoundException;
32 import javassist.bytecode.AttributeInfo;
33 import javassist.bytecode.CodeAttribute;
34 import javassist.bytecode.LocalVariableAttribute;
35
36
37
38
39
40 public class JavassistHelper {
41
42
43
44
45
46
47
48
49
50
51
52 public static String returnValue(CtBehavior method)
53 throws NotFoundException {
54
55 String returnValue = "";
56 if (methodReturnsValue(method)) {
57 returnValue = " returns: \" + $_ + \".";
58 }
59 return returnValue;
60 }
61
62
63
64
65
66
67
68
69
70 private static boolean methodReturnsValue(CtBehavior method)
71 throws NotFoundException {
72
73 if (method instanceof CtMethod == false) {
74 return false;
75 }
76
77 CtClass returnType = ((CtMethod) method).getReturnType();
78 String returnTypeName = returnType.getName();
79
80 boolean isVoidMethod = "void".equals(returnTypeName);
81
82 boolean methodReturnsValue = isVoidMethod == false;
83 return methodReturnsValue;
84 }
85
86
87
88
89
90
91
92
93
94
95 public static String getSignature(CtBehavior method)
96 throws NotFoundException {
97
98 CtClass parameterTypes[] = method.getParameterTypes();
99
100 CodeAttribute codeAttribute = method.getMethodInfo().getCodeAttribute();
101
102 LocalVariableAttribute locals = null;
103
104 if (codeAttribute != null) {
105 AttributeInfo attribute;
106 attribute = codeAttribute.getAttribute("LocalVariableTable");
107 locals = (LocalVariableAttribute) attribute;
108 }
109
110 String methodName = method.getName();
111
112 StringBuffer sb = new StringBuffer(methodName + "(\" ");
113 for (int i = 0; i < parameterTypes.length; i++) {
114 if (i > 0) {
115
116 sb.append(" + \", \" ");
117 }
118
119 CtClass parameterType = parameterTypes[i];
120 boolean isArray = parameterType.isArray();
121 CtClass arrayType = parameterType.getComponentType();
122 if (isArray) {
123 while (arrayType.isArray()) {
124 arrayType = arrayType.getComponentType();
125 }
126 }
127
128 sb.append(" + \"");
129 try {
130 sb.append(parameterNameFor(method, locals, i));
131 } catch (Exception e) {
132 sb.append("" + (i + 1));
133 }
134 sb.append("\" + \"=");
135
136 if (parameterType.isPrimitive()) {
137
138 sb.append("\"+ $" + (i + 1));
139 } else {
140 String s = "org.slf4j.instrumentation.ToStringHelper.render";
141 sb.append("\"+ " + s + "($" + (i + 1) + ")");
142 }
143 }
144 sb.append("+\")");
145
146 String signature = sb.toString();
147 return signature;
148 }
149
150
151
152
153
154
155
156
157
158
159
160 static String parameterNameFor(CtBehavior method,
161 LocalVariableAttribute locals, int i) {
162
163 if (locals == null) {
164 return Integer.toString(i + 1);
165 }
166
167 int modifiers = method.getModifiers();
168
169 int j = i;
170
171 if (Modifier.isSynchronized(modifiers)) {
172
173 j++;
174
175 }
176 if (Modifier.isStatic(modifiers) == false) {
177
178 j++;
179
180 }
181 String variableName = locals.variableName(j);
182
183
184
185
186
187
188 return variableName;
189 }
190 }