mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-29 19:45:01 +02:00
Added a method to DebugUtil that prints an object's properties
This commit is contained in:
parent
6aa77b925c
commit
0250835798
1 changed files with 32 additions and 0 deletions
|
@ -10,6 +10,13 @@
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
package org.eclipse.cdt.core.parser.util;
|
package org.eclipse.cdt.core.parser.util;
|
||||||
|
|
||||||
|
import java.beans.BeanInfo;
|
||||||
|
import java.beans.IntrospectionException;
|
||||||
|
import java.beans.Introspector;
|
||||||
|
import java.beans.PropertyDescriptor;
|
||||||
|
import java.lang.reflect.InvocationTargetException;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class contains several convenience methods
|
* This class contains several convenience methods
|
||||||
|
@ -69,4 +76,29 @@ public class DebugUtil {
|
||||||
String.valueOf(obj) + " " + obj.getClass().getSimpleName() :
|
String.valueOf(obj) + " " + obj.getClass().getSimpleName() :
|
||||||
"null";
|
"null";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prints the values of javabean properties to the console.
|
||||||
|
* This method is not recursive, it does not print nested properties.
|
||||||
|
*
|
||||||
|
* Example of usage:
|
||||||
|
*
|
||||||
|
* IResource resource = ...;
|
||||||
|
* DebugUtil.printObjectProperties(resource);
|
||||||
|
* DebugUtil.printObjectProperties(resource.getResourceAttributes());
|
||||||
|
*/
|
||||||
|
public static void printObjectProperties(Object obj) {
|
||||||
|
try {
|
||||||
|
System.out.println("Object: " + obj);
|
||||||
|
BeanInfo info = Introspector.getBeanInfo(obj.getClass());
|
||||||
|
|
||||||
|
for(PropertyDescriptor propertyDescriptor : info.getPropertyDescriptors()) {
|
||||||
|
Method getter = propertyDescriptor.getReadMethod();
|
||||||
|
try {
|
||||||
|
System.out.println(" " + getter.getName() + "=" + getter.invoke(obj, new Object[0]));
|
||||||
|
} catch (Exception e) {}
|
||||||
|
}
|
||||||
|
} catch (IntrospectionException e) {}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue