}
return new ObjectMapper() {
@Override
public Statement getMarshaller() {
AnonymousClassStructureBuilder classStructureBuilder
= Stmt.create(context.getCodegenContext())
.newObject(parameterizedAs(Marshaller.class, typeParametersOf(toMap))).extend();
classStructureBuilder.publicOverridesMethod("getTypeHandled")
.append(Stmt.load(toMap).returnValue())
.finish();
classStructureBuilder.publicOverridesMethod("getEncodingType")
.append(Stmt.load("json").returnValue())
.finish();
/**
*
* DEMARSHALL METHOD
*
*/
BlockBuilder<?> builder =
classStructureBuilder.publicOverridesMethod("demarshall",
Parameter.of(EJValue.class, "a0"), Parameter.of(MarshallingSession.class, "a1"));
BlockBuilder<CatchBlockBuilder> tryBuilder = Stmt.try_();
tryBuilder.append(Stmt.if_(Bool.notEquals(Stmt.loadVariable("a0").invoke("isNull"), null))
.append(Stmt.load(null).returnValue()).finish());
tryBuilder.append(Stmt.declareVariable(EJObject.class).named("obj")
.initializeWith(loadVariable("a0").invoke("isObject")));
if (toMap.isEnum()) {
tryBuilder.append(Stmt.declareVariable(toMap).named("entity")
.initializeWith(demarshallEnum(loadVariable("obj"), toMap)));
}
else {
tryBuilder.append(Stmt.declareVariable(String.class).named("objId")
.initializeWith(loadVariable("obj")
.invoke("get", SerializationParts.OBJECT_ID)
.invoke("isString").invoke("stringValue")));
tryBuilder.append(
Stmt.if_(Bool.expr(loadVariable("a1").invoke("hasObjectHash", loadVariable("objId"))))
.append(loadVariable("a1").invoke("getObject", toMap, loadVariable("objId")).returnValue()).finish());
InstantiationMapping instantiationMapping = mapping.getInstantiationMapping();
/**
* Figure out how to construct this object.
*/
Mapping[] cMappings = instantiationMapping.getMappings();
if (cMappings.length > 0) {
// use constructor mapping.
final List<Statement> constructorParameters = new ArrayList<Statement>();
for (Mapping m : mapping.getInstantiationMapping().getMappings()) {
MetaClass type = m.getType().asBoxed();
if (context.canMarshal(type.getFullyQualifiedName())) {
if (type.isArray()) {
constructorParameters.add(context.getArrayMarshallerCallback()
.demarshall(type, extractJSONObjectProperty(m.getKey(), EJObject.class)));
}
else {
constructorParameters.add(fieldDemarshall(m, EJObject.class));
}
}
else {
throw new MarshallingException("no marshaller for type: " + type);
}
}
if (instantiationMapping instanceof ConstructorMapping) {
tryBuilder.append(Stmt.declareVariable(toMap).named("entity")
.initializeWith(Stmt.newObject(toMap)
.withParameters(constructorParameters.toArray(new Object[constructorParameters.size()]))));
}
else if (instantiationMapping instanceof FactoryMapping) {
tryBuilder.append(Stmt.declareVariable(toMap).named("entity")
.initializeWith(Stmt.invokeStatic(toMap, ((FactoryMapping) instantiationMapping).getMember().getName(),
constructorParameters.toArray(new Object[constructorParameters.size()]))));
}
}
else {
// use default constructor
tryBuilder.append(Stmt.declareVariable(toMap).named("entity").initializeWith(Stmt.nestedCall(Stmt.newObject(toMap))));
}
tryBuilder.append(loadVariable("a1").invoke("recordObjectHash",
loadVariable("objId"), loadVariable("entity")));
}
/**
* Start binding of fields here.
*/
for (MemberMapping memberMapping : mapping.getMemberMappings()) {
if (!memberMapping.canWrite()) continue;
Statement bindingStatement;
Statement val;
if (memberMapping.getType().isArray()) {
val = context.getArrayMarshallerCallback()
.demarshall(memberMapping.getType(), extractJSONObjectProperty(memberMapping.getKey(), EJObject.class));
}
else {
val = fieldDemarshall(memberMapping, MetaClassFactory.get(EJObject.class));
//val = fieldDemarshall(memberMapping.getKey(), MetaClassFactory.get(JSONValue.class), memberMapping.getType().asBoxed());
}
if (memberMapping.getBindingMember() instanceof MetaField) {
MetaField field = (MetaField) memberMapping.getBindingMember();
// handle long case -- GWT does not support long in JSNI
if (field.isPublic()) {
tryBuilder.append(loadVariable("entity").loadField(field.getName()).assignValue(val));
continue;
}
else {
MetaMethod setterMeth = GenUtil.findCaseInsensitiveMatch(null,
field.getDeclaringClass(), "set" + field.getName(),
field.getType());
if (setterMeth != null && !setterMeth.isPrivate()) {
// Bind via setter
bindingStatement = loadVariable("entity").invoke(setterMeth, val);
}
else if (field.getType().getCanonicalName().equals("long")) {
throw new RuntimeException("cannot support private field marshalling of long type" +
" (not supported by JSNI) for field: "
+ field.getDeclaringClass().getFullyQualifiedName() + "#" + field.getName());
}
else {
if (!context.isExposed(field)) {
GenUtil.addPrivateAccessStubs(gwtTarget, context.getClassStructureBuilder(), field);
context.markExposed(field);
}
// Bind via JSNI
bindingStatement = Stmt.invokeStatic(context.getGeneratedBootstrapClass(),
GenUtil.getPrivateFieldInjectorName(field),
loadVariable("entity"), val);
}
}
}
else if (memberMapping.getBindingMember() instanceof MetaMethod) {
bindingStatement = loadVariable("entity").invoke(((MetaMethod) memberMapping.getBindingMember()), val);
}
else {
throw new RuntimeException("unknown member mapping type: " + memberMapping.getType());
}
tryBuilder.append(
Stmt.if_(Bool.and(
Bool.expr(loadVariable("obj").invoke("containsKey", memberMapping.getKey())),
Bool.isNull(loadVariable("obj").invoke("get", memberMapping.getKey()).invoke("isNull"))
)).append(bindingStatement).finish());
}
tryBuilder.append(loadVariable("entity").returnValue());
tryBuilder.finish()
.catch_(Throwable.class, "t")
.append(loadVariable("t").invoke("printStackTrace"))
.append(Stmt.throw_(RuntimeException.class,
"error demarshalling entity: " + toMap.getFullyQualifiedName(), loadVariable("t")))
.finish();
builder.append(tryBuilder.finish()).finish();
/**
*
* MARSHAL METHOD
*
*/
BlockBuilder<?> marshallMethodBlock = classStructureBuilder.publicOverridesMethod("marshall",
Parameter.of(Object.class, "a0"), Parameter.of(MarshallingSession.class, "a1"));
marshallToJSON(marshallMethodBlock, toMap, mapping);
marshallMethodBlock.finish();
classStructureBuilder.publicOverridesMethod("handles", Parameter.of(EJValue.class, "a0"))
.append(Stmt.nestedCall(Bool.and(
Bool.notEquals(loadVariable("a0").invoke("isObject"), null),
loadVariable("a0").invoke("isObject").invoke("get", SerializationParts.ENCODED_TYPE).invoke("isString").invoke("stringValue")
.invoke("equals", loadVariable("this").invoke("getTypeHandled").invoke("getName"))
)).returnValue()).finish();
return classStructureBuilder.finish();
}
};
}