blob: 288a2dfe22e3935471d38ea3f684fe0a52df7fd2 (
plain)
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
|
/*******************************************************************************
* Copyright (c) 2009, 2010 compeople AG and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* compeople AG - initial API and implementation
*******************************************************************************/
package org.eclipse.swt.internal.qt;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
/**
* Generic .toString() that collects all Java bean properties.
*/
public final class ToString {
private static final String SEPARATOR = ", "; //$NON-NLS-1$
private ToString() {
}
/**
* Collect all Java bean properties of the given object and return them as a
* printable string.
*
* @param object
* @return toString()
*/
public static String of(Object object) {
if (object == null) {
return "NULL"; //$NON-NLS-1$
}
try {
return of(object, Introspector.getBeanInfo(object.getClass()));
} catch (IntrospectionException e) {
return e.toString();
}
}
/**
* Collect all Java bean properties of the given object but stop
* introspection at the given stop class and return them as a printable
* string.
*
* @param object
* @param stopClass
* if {@code null} stop class will be the super class of the
* given object
* @return toString()
*/
public static String of(Object object, Class<?> stopClass) {
if (object == null) {
return "NULL"; //$NON-NLS-1$
}
if (stopClass == null) {
stopClass = object.getClass().getSuperclass();
}
String result = object.toString() + " - "; //$NON-NLS-1$
try {
result = result + of(object, Introspector.getBeanInfo(object.getClass(), stopClass));
} catch (IntrospectionException e) {
result = result + e.toString();
}
return result;
}
@SuppressWarnings("nls")
private static String of(Object object, BeanInfo beanInfo) {
StringBuilder bob = new StringBuilder(object.getClass().getSimpleName()).append(" (");
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
Method readMethod = propertyDescriptor.getReadMethod();
try {
if (readMethod != null && !propertyDescriptor.getName().equals("class")) {
Object value = readMethod.invoke(object, (Object[]) null);
if (value != null && value.toString().startsWith(value.getClass().getName() + "@")) {
value = "..";
}
bob.append(propertyDescriptor.getName()).append('=').append(value).append(SEPARATOR);
}
} catch (Exception e) {
e.printStackTrace();
}
}
if (bob.toString().endsWith(SEPARATOR)) {
bob.setLength(bob.length() - SEPARATOR.length());
}
bob.append(" )");
return bob.toString();
}
}
|