BeanData bd;
try {
bd = getBeanData(clazz);
}
catch (IntrospectionException e) {
throw new UnmarshallException(clazz.getName() + " is not a bean", e);
}
if (log.isDebugEnabled()) {
log.debug("instantiating " + clazz.getName());
}
Object instance;
try {
instance = clazz.newInstance();
}
catch (InstantiationException e) {
throw new UnmarshallException("could not instantiate bean of type " + clazz.getName() + ", make sure it has a no argument " + "constructor and that it is not an interface or " + "abstract class", e);
}
catch (IllegalAccessException e) {
throw new UnmarshallException("could not instantiate bean of type " + clazz.getName(), e);
}
catch (RuntimeException e) {
throw new UnmarshallException("could not instantiate bean of type " + clazz.getName(), e);
}
state.setSerialized(o, instance);
Object invokeArgs[] = new Object[1];
Object fieldVal;
Iterator<String> i = jso.keys();
while (i.hasNext()) {
String field = i.next();
Method setMethod = bd.writableProps.get(field);
if (setMethod != null) {
try {
Class<?> param[] = setMethod.getParameterTypes();
fieldVal = ser.unmarshall(state, param[0], jso.get(field));
}
catch (UnmarshallException e) {
throw new UnmarshallException("could not unmarshall field \"" + field + "\" of bean " + clazz.getName(), e);
}
catch (JSONException e) {
throw new UnmarshallException("could not unmarshall field \"" + field + "\" of bean " + clazz.getName(), e);
}
if (log.isDebugEnabled()) {
log.debug("invoking " + setMethod.getName() + "(" + fieldVal + ")");
}
invokeArgs[0] = fieldVal;
try {
setMethod.invoke(instance, invokeArgs);
}
catch (Throwable e) {
if (e instanceof InvocationTargetException) {
e = ((InvocationTargetException) e).getTargetException();
}
throw new UnmarshallException("bean " + clazz.getName() + "can't invoke " + setMethod.getName() + ": " + e.getMessage(), e);
}
}
}
return instance;
}