use of com.abubusoft.kripton.processor.bind.model.BindProperty in project kripton by xcesco.
the class BindTypeBuilder method generateSerializeOnJackson.
private static void generateSerializeOnJackson(BindTypeContext context, BindEntity entity) {
// @formatter:off
MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder("serializeOnJackson").addAnnotation(Override.class).addModifiers(Modifier.PUBLIC).addParameter(typeName(entity.getElement()), "object").addParameter(typeName(JsonGenerator.class), "jacksonSerializer").returns(Integer.TYPE).addException(Exception.class);
// @formatter:on
// methodBuilder.beginControlFlow("try");
// methodBuilder.addStatement("$T jacksonSerializer =
// wrapper.jacksonGenerator", className(JsonGenerator.class));
methodBuilder.addStatement("jacksonSerializer.writeStartObject()");
methodBuilder.addStatement("int fieldCount=0");
BindTransform bindTransform;
methodBuilder.addCode("\n");
// fields
methodBuilder.addCode("// Serialized Field:\n\n");
for (BindProperty item : entity.getCollection()) {
bindTransform = BindTransformer.lookup(item);
methodBuilder.addCode("// field $L (mapped with $S)\n", item.getName(), item.label);
bindTransform.generateSerializeOnJackson(context, methodBuilder, "jacksonSerializer", item.getPropertyType().getTypeName(), "object", item);
methodBuilder.addCode("\n");
}
methodBuilder.addStatement("jacksonSerializer.writeEndObject()");
methodBuilder.addStatement("return fieldCount");
// methodBuilder.nextControlFlow("catch($T e)",
// typeName(Exception.class));
// methodBuilder.addStatement("e.printStackTrace()");
// methodBuilder.addStatement("throw (new $T(e))",
// typeName(KriptonRuntimeException.class));
// methodBuilder.endControlFlow();
context.builder.addMethod(methodBuilder.build());
}
use of com.abubusoft.kripton.processor.bind.model.BindProperty in project kripton by xcesco.
the class BindTypeBuilder method generateParserOnXmlStartElement.
private static void generateParserOnXmlStartElement(BindTypeContext context, MethodSpec.Builder methodBuilder, String instanceName, String parserName, BindEntity entity) {
BindTransform bindTransform;
// start and inner bean
methodBuilder.addStatement("currentTag = xmlParser.getName().toString()");
int count = 0;
// count property to manage
{
// for each elements
for (BindProperty property : entity.getCollection()) {
if (property.xmlInfo.xmlType != XmlType.TAG)
continue;
bindTransform = BindTransformer.lookup(property);
// here we manage only property of bean type
if (bindTransform != null) {
count++;
}
}
}
if (count > 0) {
// switch for tag elements
// @formatter:off
methodBuilder.beginControlFlow("switch(currentTag)$>");
// for each elements
for (BindProperty property : entity.getCollection()) {
if (property.xmlInfo.xmlType != XmlType.TAG)
continue;
bindTransform = BindTransformer.lookup(property);
// here we manage only property of bean type
if (bindTransform != null) {
methodBuilder.addCode("case $S:\n$>", property.label);
methodBuilder.addCode("// property $L (mapped on $S)\n", property.getName(), property.label);
// methodBuilder.beginControlFlow("if
// (!xmlParser.isEmptyElement())");
bindTransform.generateParseOnXml(context, methodBuilder, "xmlParser", property.getPropertyType().getTypeName(), "instance", property);
// methodBuilder.endControlFlow();
methodBuilder.addStatement("$<break");
}
}
methodBuilder.addCode("default:\n$>");
// methodBuilder.addStatement("$L.skipElement()", parserName);
methodBuilder.addStatement("$<break");
methodBuilder.endControlFlow();
} else {
methodBuilder.addCode("// No property to manage here\n");
}
// @formatter:on
}
use of com.abubusoft.kripton.processor.bind.model.BindProperty in project kripton by xcesco.
the class BindTypeBuilder method generateParseOnXmlAttributes.
private static void generateParseOnXmlAttributes(BindTypeContext context, MethodSpec.Builder methodBuilder, BindEntity entity) {
BindTransform bindTransform;
int count = 0;
// count property to manage
{
// for each elements
for (BindProperty property : entity.getCollection()) {
if (property.xmlInfo.xmlType != XmlType.ATTRIBUTE)
continue;
count++;
}
}
if (count > 0) {
// @formatter:off
methodBuilder.addCode("\n// attributes \n");
methodBuilder.addStatement("String attributeName = null");
// methodBuilder.addStatement("String attributeValue = null");
methodBuilder.addStatement("int attributesCount = xmlParser.getAttributeCount();");
methodBuilder.beginControlFlow("for (int attributeIndex = 0; attributeIndex < attributesCount; attributeIndex++)");
methodBuilder.addStatement("attributeName = xmlParser.getAttributeName(attributeIndex)");
// methodBuilder.addStatement("attributeValue =
// $T.unescapeXml(xmlParser.getAttributeValue(attributeIndex))",
// StringEscapeUtils.class);
methodBuilder.beginControlFlow("switch(attributeName)$>");
for (BindProperty property : entity.getCollection()) {
if (property.xmlInfo.xmlType != XmlType.ATTRIBUTE)
continue;
methodBuilder.addCode("case $S:\n$>", property.label);
bindTransform = BindTransformer.lookup(property);
methodBuilder.addCode("// field $L (mapped by $S)\n", property.getName(), property.label);
bindTransform.generateParseOnXml(context, methodBuilder, "xmlParser", property.getPropertyType().getTypeName(), "instance", property);
methodBuilder.addStatement("$<break");
}
methodBuilder.addCode("default:\n$>");
methodBuilder.addStatement("$<break$<");
methodBuilder.endControlFlow();
methodBuilder.endControlFlow();
// @formatter:on
} else {
methodBuilder.addCode("// No attributes found\n");
}
}
use of com.abubusoft.kripton.processor.bind.model.BindProperty in project kripton by xcesco.
the class BindTypeBuilder method generateParseOnJacksonAsString.
private static void generateParseOnJacksonAsString(BindTypeContext context, BindEntity entity) {
// @formatter:off
MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder("parseOnJacksonAsString").addJavadoc("parse with jackson\n").addAnnotation(Override.class).addModifiers(Modifier.PUBLIC).addParameter(typeName(JsonParser.class), "jacksonParser").returns(typeName(entity.getElement())).addException(Exception.class);
// @formatter:on
// methodBuilder.beginControlFlow("try");
// methodBuilder.addStatement("$T jacksonParser =
// wrapper.jacksonParser", JsonParser.class);
methodBuilder.addStatement("$T instance = new $T()", entity.getElement(), entity.getElement());
methodBuilder.addStatement("String fieldName");
methodBuilder.beginControlFlow("if (jacksonParser.getCurrentToken() == null)");
methodBuilder.addStatement("jacksonParser.nextToken()");
methodBuilder.endControlFlow();
methodBuilder.beginControlFlow("if (jacksonParser.getCurrentToken() != $T.START_OBJECT)", JsonToken.class);
methodBuilder.addStatement("jacksonParser.skipChildren()");
methodBuilder.addStatement("return instance");
methodBuilder.endControlFlow();
methodBuilder.beginControlFlow("while (jacksonParser.nextToken() != $T.END_OBJECT)", JsonToken.class);
methodBuilder.addStatement("fieldName = jacksonParser.getCurrentName()");
methodBuilder.addStatement("jacksonParser.nextToken()");
if (entity.getCollection().size() > 0) {
methodBuilder.addCode("\n// Parse fields:\n");
methodBuilder.beginControlFlow("switch (fieldName)$>");
BindTransform bindTransform;
for (BindProperty item : entity.getCollection()) {
bindTransform = BindTransformer.lookup(item);
methodBuilder.addCode("case $S:\n$>", item.label);
methodBuilder.addCode("// field $L (mapped with $S)\n", item.getName(), item.label);
bindTransform.generateParseOnJacksonAsString(context, methodBuilder, "jacksonParser", item.getPropertyType().getTypeName(), "instance", item);
methodBuilder.addCode("$<break;\n");
}
methodBuilder.addCode("default:$>\n");
methodBuilder.addStatement("jacksonParser.skipChildren()");
methodBuilder.addCode("$<break;");
methodBuilder.addCode("$<");
methodBuilder.endControlFlow();
} else {
methodBuilder.addCode("\n// No field to parse\n");
}
methodBuilder.endControlFlow();
methodBuilder.addStatement("return instance");
// methodBuilder.nextControlFlow("catch ($T e)", Exception.class);
// methodBuilder.addStatement("e.printStackTrace()");
// methodBuilder.addStatement("throw new $T(e)",
// KriptonRuntimeException.class);
// methodBuilder.endControlFlow();
context.builder.addMethod(methodBuilder.build());
}
use of com.abubusoft.kripton.processor.bind.model.BindProperty in project kripton by xcesco.
the class MapBindTransformation method generateSerializeOnJacksonInternal.
void generateSerializeOnJacksonInternal(BindTypeContext context, MethodSpec.Builder methodBuilder, String serializerName, TypeName beanClass, String beanName, BindProperty property, boolean onString) {
// define key and value type
ParameterizedTypeName mapTypeName = (ParameterizedTypeName) property.getPropertyType().getTypeName();
TypeName keyTypeName = mapTypeName.typeArguments.get(0);
TypeName valueTypeName = mapTypeName.typeArguments.get(1);
// @formatter:off
methodBuilder.beginControlFlow("if ($L!=null) ", getter(beanName, beanClass, property));
if (property.isProperty()) {
methodBuilder.addStatement("fieldCount++");
}
// fields are in objects, no in collection
BindTransform transformKey = BindTransformer.lookup(keyTypeName);
BindProperty elementKeyProperty = BindProperty.builder(keyTypeName, property).nullable(false).xmlType(property.xmlInfo.mapEntryType.toXmlType()).inCollection(false).elementName(property.mapKeyName).build();
BindTransform transformValue = BindTransformer.lookup(valueTypeName);
BindProperty elementValueProperty = BindProperty.builder(valueTypeName, property).nullable(false).xmlType(property.xmlInfo.mapEntryType.toXmlType()).inCollection(false).elementName(property.mapValueName).build();
methodBuilder.addCode("// write wrapper tag\n");
// BEGIN - if map has elements
methodBuilder.beginControlFlow("if ($L.size()>0)", getter(beanName, beanClass, property));
methodBuilder.addStatement("$L.writeFieldName($S)", serializerName, property.label);
methodBuilder.addStatement("$L.writeStartArray()", serializerName);
methodBuilder.beginControlFlow("for ($T<$T, $T> item: $L.entrySet())", Entry.class, keyTypeName, valueTypeName, getter(beanName, beanClass, property));
methodBuilder.addStatement("$L.writeStartObject()", serializerName);
if (onString) {
transformKey.generateSerializeOnJacksonAsString(context, methodBuilder, serializerName, null, "item.getKey()", elementKeyProperty);
} else {
transformKey.generateSerializeOnJackson(context, methodBuilder, serializerName, null, "item.getKey()", elementKeyProperty);
}
// field is always nullable
methodBuilder.beginControlFlow("if (item.getValue()==null)");
// KRITPON-38: in a collection, for null object we write "null" string
if (onString) {
methodBuilder.addStatement("$L.writeStringField($S, \"null\")", serializerName, property.mapValueName);
} else {
methodBuilder.addStatement("$L.writeNullField($S)", serializerName, property.mapValueName);
}
methodBuilder.nextControlFlow("else");
if (onString) {
transformValue.generateSerializeOnJacksonAsString(context, methodBuilder, serializerName, null, "item.getValue()", elementValueProperty);
} else {
transformValue.generateSerializeOnJackson(context, methodBuilder, serializerName, null, "item.getValue()", elementValueProperty);
}
methodBuilder.endControlFlow();
methodBuilder.addStatement("$L.writeEndObject()", serializerName);
methodBuilder.endControlFlow();
methodBuilder.addStatement("$L.writeEndArray()", serializerName);
// ELSE - if map has elements
methodBuilder.nextControlFlow("else");
if (onString) {
methodBuilder.addStatement("$L.writeStringField($S, \"null\")", serializerName, property.label);
} else {
methodBuilder.addStatement("$L.writeNullField($S)", serializerName, property.label);
}
// END - if map has elements
methodBuilder.endControlFlow();
methodBuilder.endControlFlow();
// @formatter:on
}
Aggregations