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) throws NotFoundException {
53
54 String returnValue = "";
55 if (methodReturnsValue(method)) {
56 returnValue = " returns: \" + $_ + \".";
57 }
58 return returnValue;
59 }
60
61
62
63
64
65
66
67
68
69 private static boolean methodReturnsValue(CtBehavior method) throws NotFoundException {
70
71 if (method instanceof CtMethod == false) {
72 return false;
73 }
74
75 CtClass returnType = ((CtMethod) method).getReturnType();
76 String returnTypeName = returnType.getName();
77
78 boolean isVoidMethod = "void".equals(returnTypeName);
79
80 boolean methodReturnsValue = isVoidMethod == false;
81 return methodReturnsValue;
82 }
83
84
85
86
87
88
89
90
91
92
93 public static String getSignature(CtBehavior method) throws NotFoundException {
94
95 CtClass[] parameterTypes = method.getParameterTypes();
96
97 CodeAttribute codeAttribute = method.getMethodInfo().getCodeAttribute();
98
99 LocalVariableAttribute locals = null;
100
101 if (codeAttribute != null) {
102 AttributeInfo attribute;
103 attribute = codeAttribute.getAttribute("LocalVariableTable");
104 locals = (LocalVariableAttribute) attribute;
105 }
106
107 String methodName = method.getName();
108
109 StringBuilder sb = new StringBuilder(methodName).append("(\" ");
110 for (int i = 0; i < parameterTypes.length; i++) {
111 if (i > 0) {
112
113 sb.append(" + \", \" ");
114 }
115
116 CtClass parameterType = parameterTypes[i];
117 boolean isArray = parameterType.isArray();
118 CtClass arrayType = parameterType.getComponentType();
119 if (isArray) {
120 while (arrayType.isArray()) {
121 arrayType = arrayType.getComponentType();
122 }
123 }
124
125 sb.append(" + \"");
126 try {
127 sb.append(parameterNameFor(method, locals, i));
128 } catch (Exception e) {
129 sb.append(i + 1);
130 }
131 sb.append("\" + \"=");
132
133 if (parameterType.isPrimitive()) {
134
135 sb.append("\"+ $").append(i + 1);
136 } else {
137 String s = "org.slf4j.instrumentation.ToStringHelper.render";
138 sb.append("\"+ ").append(s).append("($").append(i + 1).append(')');
139 }
140 }
141 sb.append("+\")");
142
143 String signature = sb.toString();
144 return signature;
145 }
146
147
148
149
150
151
152
153
154
155
156
157 static String parameterNameFor(CtBehavior method, LocalVariableAttribute locals, int i) {
158
159 if (locals == null) {
160 return Integer.toString(i + 1);
161 }
162
163 int modifiers = method.getModifiers();
164
165 int j = i;
166
167 if (Modifier.isSynchronized(modifiers)) {
168
169 j++;
170
171 }
172 if (Modifier.isStatic(modifiers) == false) {
173
174 j++;
175
176 }
177 String variableName = locals.variableName(j);
178
179
180
181
182
183
184 return variableName;
185 }
186 }