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