JSONObject jso = (JSONObject) o;
BeanData bd;
try {
bd = getBeanData(clazz);
} catch (IntrospectionException e) {
throw new UnmarshallException(clazz.getName() + " is not a bean");
}
log.debug("instantiating {}", clazz.getName());
Object instance;
try {
String beanName = beanNameSimplifier.simplify(clazz.getName());
if (applicationContext.containsBean(beanName)) {
instance = applicationContext.getBean(beanName);
} else {
log.debug("instantiating " + clazz.getName());
instance = clazz.newInstance();
}
} catch (Exception e) {
log.error(e.getMessage(), e);
throw new UnmarshallException("can't instantiate bean " + clazz.getName() + ": " + e.getMessage());
}
Object[] invokeArgs = new Object[1];
Object fieldVal;
Iterator i = jso.keys();
while (i.hasNext()) {
String field = (String) i.next();
Method setMethod = bd.writableProps.get(field);
if (setMethod != null) {
try {
Class param = setMethod.getParameterTypes()[0];
if (instance instanceof PrimitiveAttribute && "value".equals(field)) {
log.debug("replace 'value' type for PrimitiveAttribute");
switch(((PrimitiveAttribute) instance).getType()) {
case BOOLEAN:
param = Boolean.class;
break;
case DATE:
param = Date.class;
break;
case DOUBLE:
param = Double.class;
break;
case FLOAT:
param = Float.class;
break;
case INTEGER:
param = Integer.class;
break;
case LONG:
param = Long.class;
break;
case SHORT:
param = Short.class;
break;
case CURRENCY:
case IMGURL:
case URL:
case STRING:
param = String.class;
break;
default:
throw new UnmarshallException("Unknown type of PrimitiveAttribute " +
((PrimitiveAttribute) instance).getType());
}
}
fieldVal = ser.unmarshall(state, param, jso.get(field));
} catch (UnmarshallException e) {
throw new UnmarshallException("bean " + clazz.getName() + " " + e.getMessage());
}
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());
}
}
}
return instance;