1
0
Fork 0
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:
Mike Kucera 2008-09-29 16:43:02 +00:00
parent 6aa77b925c
commit 0250835798

View file

@ -10,6 +10,13 @@
*******************************************************************************/
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
@ -69,4 +76,29 @@ public class DebugUtil {
String.valueOf(obj) + " " + obj.getClass().getSimpleName() :
"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) {}
}
}