Search in sources :

Example 6 with BindProperty

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());
}
Also used : MethodSpec(com.squareup.javapoet.MethodSpec) BindTransform(com.abubusoft.kripton.processor.bind.transform.BindTransform) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) BindProperty(com.abubusoft.kripton.processor.bind.model.BindProperty)

Example 7 with BindProperty

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
}
Also used : BindTransform(com.abubusoft.kripton.processor.bind.transform.BindTransform) BindProperty(com.abubusoft.kripton.processor.bind.model.BindProperty)

Example 8 with BindProperty

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");
    }
}
Also used : BindTransform(com.abubusoft.kripton.processor.bind.transform.BindTransform) BindProperty(com.abubusoft.kripton.processor.bind.model.BindProperty)

Example 9 with BindProperty

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());
}
Also used : MethodSpec(com.squareup.javapoet.MethodSpec) BindTransform(com.abubusoft.kripton.processor.bind.transform.BindTransform) BindProperty(com.abubusoft.kripton.processor.bind.model.BindProperty) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 10 with BindProperty

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
}
Also used : ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeName(com.squareup.javapoet.TypeName) BindProperty(com.abubusoft.kripton.processor.bind.model.BindProperty) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName)

Aggregations

BindProperty (com.abubusoft.kripton.processor.bind.model.BindProperty)22 BindTransform (com.abubusoft.kripton.processor.bind.transform.BindTransform)11 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)8 TypeName (com.squareup.javapoet.TypeName)8 MethodSpec (com.squareup.javapoet.MethodSpec)7 BindEntity (com.abubusoft.kripton.processor.bind.model.BindEntity)3 ModelAnnotation (com.abubusoft.kripton.processor.core.ModelAnnotation)3 InvalidDefinition (com.abubusoft.kripton.processor.exceptions.InvalidDefinition)3 Element (javax.lang.model.element.Element)3 TypeElement (javax.lang.model.element.TypeElement)3 BindDisabled (com.abubusoft.kripton.annotation.BindDisabled)2 CollectionUtils (com.abubusoft.kripton.common.CollectionUtils)2 KriptonRuntimeException (com.abubusoft.kripton.exception.KriptonRuntimeException)2 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)2 JsonParser (com.fasterxml.jackson.core.JsonParser)2 Elements (javax.lang.model.util.Elements)2 ColumnType (com.abubusoft.kripton.android.ColumnType)1 BindPreference (com.abubusoft.kripton.android.annotation.BindPreference)1 ForeignKeyAction (com.abubusoft.kripton.android.sqlite.ForeignKeyAction)1 Bind (com.abubusoft.kripton.annotation.Bind)1