JSONObject jso = (JSONObject) o;
BeanData bd;
try {
bd = getBeanData(clazz);
} catch (IntrospectionException e) {
throw new UnmarshallException(clazz.getName() + " is not a bean", e);
}
if (logger.isLoggable(Level.FINE)) {
logger.log(Level.FINE, "instantiating {0}", 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 i = jso.keys();
while (i.hasNext()) {
String field = (String) i.next();
Method setMethod = (Method) 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 (logger.isLoggable(Level.FINE)) {
logger.log(Level.FINE, "invoking {0}({1})",
new Object[]{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;