}
@SuppressWarnings("unchecked")
private static INode introspect(String name, Object object)
{
INode node = new Node(name, object.getClass().getName());
Method[] methods = object.getClass().getMethods();
Arrays.sort(methods, new MethodNameComparator());
for(Method method : methods)
{
String methodName = method.getName();
if((methodName.startsWith(GETTER_PREFIX)) && (method.getParameterTypes().length == 0))
{
// Create the corresponding setter from the getter.
String setterMethodName = "s" + methodName.substring(1);
String propertyName = methodName.substring(3, 4).toLowerCase() + methodName.substring(4);
Method setter = null;
for(Method lookForSetter : methods)
{
if(lookForSetter.getName().equals(setterMethodName))
{
setter = lookForSetter;
break;
}
}
// If there is no setter, no use in saving the property, as the
// deserializer (factory) will barf on exact sane the missing setter.
if(setter == null)
{
continue;
}
// Get the return type and do the magic depending on the types.
Class<?> propertyType = method.getReturnType();
try
{
if(propertyType.isPrimitive())
{
Object propertyValue = method.invoke(object, VOID_PARAMETERS);
node.addChild(new Node(propertyName, propertyValue.toString()));
}
else if(String[].class.isAssignableFrom(propertyType))
{
INode newNode = new Node(propertyName);
node.addChild(newNode);
String[] values = (String[]) method.invoke(object, VOID_PARAMETERS);
if((values == null) || (values.length == 0)) continue;
for(String value : values)
{
newNode.addChild(new Node(value));
}
}
else if(Map.class.isAssignableFrom(propertyType))
{
INode newNode = new Node(propertyName);
node.addChild(newNode);
Map<String,String> map = (Map<String,String>) method.invoke(object, VOID_PARAMETERS);
if((map == null) || (map.size() == 0)) continue;
for(String key : map.keySet())
{
String value = (String) map.get(key);
newNode.addChild(new Node(key, value));
}
}
else if(String.class.isAssignableFrom(propertyType))
{
System.out.println("String : " + method.getName());