/
ToStringHelper.java
192 lines (176 loc) · 5.82 KB
/
ToStringHelper.java
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/**
* Copyright (c) 2004-2011 QOS.ch
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
package org.slf4j.instrumentation;
import java.util.Map;
import java.util.WeakHashMap;
public class ToStringHelper {
/**
* Prefix to use at the start of the representation. Always used.
*/
private static final char ARRAY_PREFIX = '[';
/**
* Suffix to use at the end of the representation. Always used.
*/
private static final char ARRAY_SUFFIX = ']';
/**
* String separating each element when rendering an array. To be compatible
* with lists comma-space is used.
*/
private static final char[] ELEMENT_SEPARATOR = ", ".toCharArray();
/**
* unrenderableClasses is essentially a Set of Class objects which has for
* some reason failed to render properly when invoked through a toString
* method call. To avoid memory leaks a data structure using weak references
* is needed, but unfortunately the runtime library does not contain a
* WeakHashSet class, so the behavior is emulated with a WeakHashmap with
* the class as the key, and a Long containing the value of
* System.currentTimeMilis when an instance of the class failed to render.
*/
final static Map<Class<?>, Object> unrenderableClasses = new WeakHashMap<Class<?>, Object>();
/**
* Returns o.toString() unless it throws an exception (which causes it to be
* stored in unrenderableClasses) or already was present in
* unrenderableClasses. If so, the same string is returned as would have
* been returned by Object.toString(). Arrays get special treatment as they
* don't have usable toString methods.
*
* @param o
* incoming object to render.
* @return
*/
public static String render(Object o) {
if (o == null) {
return String.valueOf(o);
}
Class<?> objectClass = o.getClass();
if (unrenderableClasses.containsKey(objectClass) == false) {
try {
if (objectClass.isArray()) {
return renderArray(o, objectClass).toString();
} else {
return o.toString();
}
} catch (Exception e) {
Long now = new Long(System.currentTimeMillis());
System.err.println("Disabling exception throwing class "
+ objectClass.getName() + ", " + e.getMessage());
unrenderableClasses.put(objectClass, now);
}
}
String name = o.getClass().getName();
return name + "@" + Integer.toHexString(o.hashCode());
}
/**
* renderArray returns an array similar to a List. If the array type is an
* object they are rendered with "render(object)" for each. If the array
* type is a primitive each element is added directly to the string buffer
* collecting the result.
*
* @param o
* @param objectClass
* @return
*/
private static StringBuilder renderArray(Object o, Class<?> objectClass) {
Class<?> componentType = objectClass.getComponentType();
StringBuilder sb = new StringBuilder(ARRAY_PREFIX);
if (componentType.isPrimitive() == false) {
Object[] oa = (Object[]) o;
for (int i = 0; i < oa.length; i++) {
if (i > 0) {
sb.append(ELEMENT_SEPARATOR);
}
sb.append(render(oa[i]));
}
} else {
if (Boolean.TYPE.equals(componentType)) {
boolean[] ba = (boolean[]) o;
for (int i = 0; i < ba.length; i++) {
if (i > 0) {
sb.append(ELEMENT_SEPARATOR);
}
sb.append(ba[i]);
}
} else if (Integer.TYPE.equals(componentType)) {
int[] ia = (int[]) o;
for (int i = 0; i < ia.length; i++) {
if (i > 0) {
sb.append(ELEMENT_SEPARATOR);
}
sb.append(ia[i]);
}
} else if (Long.TYPE.equals(componentType)) {
long[] ia = (long[]) o;
for (int i = 0; i < ia.length; i++) {
if (i > 0) {
sb.append(ELEMENT_SEPARATOR);
}
sb.append(ia[i]);
}
} else if (Double.TYPE.equals(componentType)) {
double[] ia = (double[]) o;
for (int i = 0; i < ia.length; i++) {
if (i > 0) {
sb.append(ELEMENT_SEPARATOR);
}
sb.append(ia[i]);
}
} else if (Float.TYPE.equals(componentType)) {
float[] ia = (float[]) o;
for (int i = 0; i < ia.length; i++) {
if (i > 0) {
sb.append(ELEMENT_SEPARATOR);
}
sb.append(ia[i]);
}
} else if (Character.TYPE.equals(componentType)) {
char[] ia = (char[]) o;
for (int i = 0; i < ia.length; i++) {
if (i > 0) {
sb.append(ELEMENT_SEPARATOR);
}
sb.append(ia[i]);
}
} else if (Short.TYPE.equals(componentType)) {
short[] ia = (short[]) o;
for (int i = 0; i < ia.length; i++) {
if (i > 0) {
sb.append(ELEMENT_SEPARATOR);
}
sb.append(ia[i]);
}
} else if (Byte.TYPE.equals(componentType)) {
byte[] ia = (byte[]) o;
for (int i = 0; i < ia.length; i++) {
if (i > 0) {
sb.append(ELEMENT_SEPARATOR);
}
sb.append(ia[i]);
}
}
}
sb.append(ARRAY_SUFFIX);
return sb;
}
}