Search in sources :

Example 16 with BindProperty

use of com.abubusoft.kripton.processor.bind.model.BindProperty in project kripton by xcesco.

the class BindTypeBuilder method generateSerializeOnXml.

private static void generateSerializeOnXml(BindTypeContext context, BindEntity entity) {
    // @formatter:off
    MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder("serializeOnXml").addJavadoc("method for xml serialization\n").addAnnotation(Override.class).addModifiers(Modifier.PUBLIC).addParameter(typeName(entity.getElement()), "object").addParameter(typeName(XMLSerializer.class), "xmlSerializer").addParameter(typeName(Integer.TYPE), "currentEventType").returns(Void.TYPE).addException(Exception.class);
    // @formatter:on
    methodBuilder.beginControlFlow("if (currentEventType == 0)");
    methodBuilder.addStatement("xmlSerializer.writeStartElement(\"$L\")", entity.xmlInfo.label);
    methodBuilder.endControlFlow();
    BindTransform bindTransform;
    methodBuilder.addCode("\n");
    // attributes
    methodBuilder.addCode("// Persisted fields:\n\n");
    for (BindProperty item : entity.getCollection()) {
        bindTransform = BindTransformer.lookup(item);
        methodBuilder.addCode("// field $L (mapped with $S)\n", item.getName(), item.label);
        if (item.hasTypeAdapter()) {
            methodBuilder.addCode("// field trasformation $L $L \n", item.typeAdapter.dataType, item.typeAdapter.adapterClazz);
        }
        bindTransform.generateSerializeOnXml(context, methodBuilder, "xmlSerializer", item.getPropertyType().getTypeName(), "object", item);
        methodBuilder.addCode("\n");
    }
    methodBuilder.beginControlFlow("if (currentEventType == 0)");
    methodBuilder.addStatement("xmlSerializer.writeEndElement()");
    methodBuilder.endControlFlow();
    // 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) BindProperty(com.abubusoft.kripton.processor.bind.model.BindProperty)

Example 17 with BindProperty

use of com.abubusoft.kripton.processor.bind.model.BindProperty in project kripton by xcesco.

the class AbstractCollectionBindTransform method generateParseOnJacksonInternal.

public void generateParseOnJacksonInternal(BindTypeContext context, Builder methodBuilder, String parserName, TypeName beanClass, String beanName, BindProperty property, boolean onString) {
    TypeName elementTypeName = extractTypeParameterName(property);
    // @formatter:off
    methodBuilder.beginControlFlow("if ($L.currentToken()==$T.START_ARRAY)", parserName, JsonToken.class);
    if (collectionType == CollectionType.ARRAY) {
        methodBuilder.addStatement("$T<$T> collection=new $T<>()", ArrayList.class, elementTypeName.box(), ArrayList.class);
    } else {
        // it's for sure a parametrized type
        ParameterizedTypeName collectionTypeName = (ParameterizedTypeName) property.getPropertyType().getTypeName();
        methodBuilder.addStatement("$T<$T> collection=new $T<>()", defineCollectionClass(collectionTypeName), elementTypeName.box(), defineCollectionClass(collectionTypeName));
    }
    methodBuilder.addStatement("$T item=$L", elementTypeName.box(), DEFAULT_VALUE);
    if (onString) {
        methodBuilder.addStatement("String tempValue=null");
    }
    BindTransform transform = BindTransformer.lookup(elementTypeName);
    BindProperty elementProperty = BindProperty.builder(elementTypeName, property).inCollection(true).nullable(false).build();
    methodBuilder.beginControlFlow("while ($L.nextToken() != $T.END_ARRAY)", parserName, JsonToken.class);
    if (onString) {
        methodBuilder.addStatement("tempValue=$L.getValueAsString()", parserName);
        methodBuilder.beginControlFlow("if ($L.currentToken()==$T.VALUE_STRING && \"null\".equals(tempValue))", parserName, JsonToken.class);
    } else {
        methodBuilder.beginControlFlow("if ($L.currentToken()==$T.VALUE_NULL)", parserName, JsonToken.class);
    }
    methodBuilder.addStatement("item=$L", DEFAULT_VALUE);
    methodBuilder.nextControlFlow("else");
    if (onString) {
        transform.generateParseOnJacksonAsString(context, methodBuilder, parserName, null, "item", elementProperty);
    } else {
        transform.generateParseOnJackson(context, methodBuilder, parserName, null, "item", elementProperty);
    }
    methodBuilder.endControlFlow();
    methodBuilder.addStatement("collection.add(item)");
    methodBuilder.endControlFlow();
    if (collectionType == CollectionType.ARRAY) {
        if (TypeUtility.isTypePrimitive(elementTypeName)) {
            methodBuilder.addStatement(setter(beanClass, beanName, property, "$T.as$TTypeArray(collection)"), CollectionUtils.class, elementTypeName.box());
        } else if (TypeUtility.isTypeWrappedPrimitive(elementTypeName)) {
            methodBuilder.addStatement(setter(beanClass, beanName, property, "$T.as$TArray(collection)"), CollectionUtils.class, elementTypeName);
        } else {
            methodBuilder.addStatement(setter(beanClass, beanName, property, "$T.asArray(collection, new $T[collection.size()])"), CollectionUtils.class, elementTypeName);
        }
    } else {
        methodBuilder.addStatement(setter(beanClass, beanName, property, "collection"));
    }
    if (onString) {
        // ELSE: check if empty string (== empty collection but not null)
        methodBuilder.nextControlFlow("else if ($L.currentToken()==$T.VALUE_STRING && !$T.hasText($L.getValueAsString()))", parserName, JsonToken.class, StringUtils.class, parserName);
        // create collection
        if (collectionType == CollectionType.ARRAY) {
            methodBuilder.addStatement("$T<$T> collection=new $T<>()", ArrayList.class, elementTypeName.box(), ArrayList.class);
        } else {
            // it's for sure a parametrized type
            ParameterizedTypeName collectionTypeName = (ParameterizedTypeName) property.getPropertyType().getTypeName();
            methodBuilder.addStatement("$T<$T> collection=new $T<>()", defineCollectionClass(collectionTypeName), elementTypeName.box(), defineCollectionClass(collectionTypeName));
        }
        // set collection
        if (collectionType == CollectionType.ARRAY) {
            if (TypeUtility.isTypePrimitive(elementTypeName)) {
                methodBuilder.addStatement(setter(beanClass, beanName, property, "$T.as$TTypeArray(collection)"), CollectionUtils.class, elementTypeName.box());
            } else if (TypeUtility.isTypeWrappedPrimitive(elementTypeName)) {
                methodBuilder.addStatement(setter(beanClass, beanName, property, "$T.as$TArray(collection)"), CollectionUtils.class, elementTypeName.box());
            } else {
                methodBuilder.addStatement(setter(beanClass, beanName, property, "$T.asArray(collection, new $T[collection.size()])"), CollectionUtils.class, elementTypeName);
            }
        } else {
            methodBuilder.addStatement(setter(beanClass, beanName, property, "collection"));
        }
    // END: we use the next endControlFlow
    }
    methodBuilder.endControlFlow();
// @formatter:on
}
Also used : ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeName(com.squareup.javapoet.TypeName) CollectionUtils(com.abubusoft.kripton.common.CollectionUtils) BindProperty(com.abubusoft.kripton.processor.bind.model.BindProperty) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName)

Example 18 with BindProperty

use of com.abubusoft.kripton.processor.bind.model.BindProperty in project kripton by xcesco.

the class AbstractCollectionBindTransform method generateSerializeOnJacksonInternal.

void generateSerializeOnJacksonInternal(BindTypeContext context, MethodSpec.Builder methodBuilder, String serializerName, TypeName beanClass, String beanName, BindProperty property, boolean onString) {
    TypeName elementTypeName = extractTypeParameterName(property);
    // @formatter:off
    methodBuilder.beginControlFlow("if ($L!=null) ", getter(beanName, beanClass, property));
    if (property.isProperty()) {
        methodBuilder.addStatement("fieldCount++");
    }
    if (collectionType == CollectionType.LIST) {
        methodBuilder.addStatement("int n=$L.size()", getter(beanName, beanClass, property));
        methodBuilder.addStatement("$T item", elementTypeName);
    } else if (collectionType == CollectionType.ARRAY) {
        methodBuilder.addStatement("int n=$L.length", getter(beanName, beanClass, property));
        methodBuilder.addStatement("$T item", elementTypeName);
    } else if (onString) {
        methodBuilder.addStatement("int n=$L.size()", getter(beanName, beanClass, property));
    }
    BindTransform transform = BindTransformer.lookup(elementTypeName);
    BindProperty elementProperty = BindProperty.builder(elementTypeName, property).inCollection(true).nullable(false).build();
    methodBuilder.addCode("// write wrapper tag\n");
    methodBuilder.addStatement("$L.writeFieldName($S)", serializerName, property.label);
    if (onString) {
        // BEGIN - IF there are some elements
        methodBuilder.beginControlFlow("if (n>0)");
    }
    methodBuilder.addStatement("$L.writeStartArray()", serializerName);
    if (collectionType == CollectionType.SET) {
        methodBuilder.beginControlFlow("for ($T item: $L)", elementTypeName, getter(beanName, beanClass, property));
    } else if (collectionType == CollectionType.LIST) {
        methodBuilder.beginControlFlow("for (int i=0; i<n; i++)");
        methodBuilder.addStatement("item=$L.get(i)", getter(beanName, beanClass, property));
    } else if (collectionType == CollectionType.ARRAY) {
        methodBuilder.beginControlFlow("for (int i=0; i<n; i++)");
        methodBuilder.addStatement("item=$L[i]", getter(beanName, beanClass, property));
    }
    // We are in a collection, so we need to write null value too. In all other case, null value will be skipped.
    if (elementProperty.getPropertyType().isArray() || !TypeUtility.isTypePrimitive(elementTypeName)) {
        // CASE 1 ASSERT: we are with item of kink array OR we are not manage a simple primitive
        methodBuilder.beginControlFlow("if (item==null)");
        if (onString) {
            // KRIPTON-38: supporto for null value as string
            methodBuilder.addStatement("$L.writeString(\"null\")", serializerName);
        } else {
            methodBuilder.addStatement("$L.writeNull()", serializerName);
        }
        methodBuilder.nextControlFlow("else");
    }
    if (onString) {
        transform.generateSerializeOnJacksonAsString(context, methodBuilder, serializerName, null, "item", elementProperty);
    } else {
        transform.generateSerializeOnJackson(context, methodBuilder, serializerName, null, "item", elementProperty);
    }
    if (elementProperty.getPropertyType().isArray() || !TypeUtility.isTypePrimitive(elementTypeName)) {
        // CASE 1 ASSERT: we are with item of kink array OR we are not manage a simple primitive
        methodBuilder.endControlFlow();
    }
    methodBuilder.endControlFlow();
    methodBuilder.addStatement("$L.writeEndArray()", serializerName);
    if (onString) {
        // ELSE - IF there are some elements
        methodBuilder.nextControlFlow("else");
        // if collection has 0 elements, write an empty string
        methodBuilder.addStatement("$L.writeString(\"\")", serializerName);
        // END - IF there are some 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)

Example 19 with BindProperty

use of com.abubusoft.kripton.processor.bind.model.BindProperty in project kripton by xcesco.

the class AbstractCollectionBindTransform method generateParseOnXml.

@Override
public void generateParseOnXml(BindTypeContext context, MethodSpec.Builder methodBuilder, String parserName, TypeName beanClass, String beanName, BindProperty property) {
    TypeName elementTypeName = extractTypeParameterName(property);
    // @formatter:off
    methodBuilder.beginControlFlow("");
    switch(collectionType) {
        case ARRAY:
            methodBuilder.addStatement("$T<$T> collection=new $T<>()", ArrayList.class, elementTypeName.box(), ArrayList.class);
            break;
        case LIST:
        case SET:
            // it's for sure a parametrized type
            ParameterizedTypeName collectionTypeName = (ParameterizedTypeName) property.getPropertyType().getTypeName();
            methodBuilder.addStatement("$T<$T> collection=new $T<>()", defineCollectionClass(collectionTypeName), elementTypeName.box(), defineCollectionClass(collectionTypeName));
            break;
    }
    methodBuilder.addStatement("$T item", elementTypeName.box());
    BindTransform transform = BindTransformer.lookup(elementTypeName);
    BindProperty elementProperty = BindProperty.builder(elementTypeName, property).inCollection(true).build();
    if (property.xmlInfo.isWrappedCollection()) {
        // with wrap element
        methodBuilder.beginControlFlow("while ($L.nextTag() != $T.END_TAG && $L.getName().toString().equals($S))", parserName, XmlPullParser.class, parserName, property.xmlInfo.labelItem);
    } else {
        // no wrap element
        methodBuilder.addCode("// add first element\n");
        methodBuilder.addStatement("item=$L", DEFAULT_VALUE);
        methodBuilder.beginControlFlow("if ($L.isEmptyElement())", parserName);
        methodBuilder.addCode("// if there's a an empty collection it marked with attribute emptyCollection\n");
        methodBuilder.beginControlFlow("if ($T.getAttributeAsBoolean($L, $S, false)==false)", XmlAttributeUtils.class, parserName, EMPTY_COLLECTION_ATTRIBUTE_NAME);
        methodBuilder.addStatement("collection.add(item)");
        methodBuilder.endControlFlow();
        methodBuilder.addStatement("$L.nextTag()", parserName);
        methodBuilder.nextControlFlow("else");
        transform.generateParseOnXml(context, methodBuilder, parserName, null, "item", elementProperty);
        methodBuilder.addStatement("collection.add(item)");
        methodBuilder.endControlFlow();
        methodBuilder.beginControlFlow("while ($L.nextTag() != $T.END_TAG && $L.getName().toString().equals($S))", parserName, XmlPullParser.class, parserName, property.label);
    }
    // for all
    methodBuilder.beginControlFlow("if ($L.isEmptyElement())", parserName);
    methodBuilder.addStatement("item=$L", DEFAULT_VALUE);
    methodBuilder.addStatement("$L.nextTag()", parserName);
    methodBuilder.nextControlFlow("else");
    transform.generateParseOnXml(context, methodBuilder, parserName, null, "item", elementProperty);
    methodBuilder.endControlFlow();
    methodBuilder.addStatement("collection.add(item)");
    methodBuilder.endControlFlow();
    if (collectionType == CollectionType.ARRAY) {
        if (TypeUtility.isTypePrimitive(elementTypeName)) {
            methodBuilder.addStatement(setter(beanClass, beanName, property, "$T.as$TTypeArray(collection)"), CollectionUtils.class, elementTypeName.box());
        } else if (TypeUtility.isTypeWrappedPrimitive(elementTypeName)) {
            methodBuilder.addStatement(setter(beanClass, beanName, property, "$T.as$TArray(collection)"), CollectionUtils.class, elementTypeName);
        } else {
            methodBuilder.addStatement(setter(beanClass, beanName, property, "$T.asArray(collection, new $T[collection.size()])"), CollectionUtils.class, elementTypeName);
        }
    } else {
        methodBuilder.addStatement(setter(beanClass, beanName, property, "collection"));
    }
    if (!property.xmlInfo.isWrappedCollection()) {
        methodBuilder.addStatement("read=false");
    }
    methodBuilder.endControlFlow();
// @formatter:on
}
Also used : ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeName(com.squareup.javapoet.TypeName) CollectionUtils(com.abubusoft.kripton.common.CollectionUtils) BindProperty(com.abubusoft.kripton.processor.bind.model.BindProperty) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName)

Example 20 with BindProperty

use of com.abubusoft.kripton.processor.bind.model.BindProperty in project kripton by xcesco.

the class MapBindTransformation method generateParseOnXml.

@Override
public void generateParseOnXml(BindTypeContext context, MethodSpec.Builder methodBuilder, String parserName, TypeName beanClass, String beanName, BindProperty property) {
    // 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("");
    methodBuilder.addStatement("$T<$T, $T> collection=new $T<>()", defineMapClass(mapTypeName), keyTypeName, valueTypeName, defineMapClass(mapTypeName));
    BindTransform transformKey = BindTransformer.lookup(keyTypeName);
    BindProperty elementKeyProperty = BindProperty.builder(keyTypeName, property).inCollection(true).xmlType(property.xmlInfo.mapEntryType.toXmlType()).elementName(property.mapKeyName).build();
    BindTransform transformValue = BindTransformer.lookup(valueTypeName);
    BindProperty elementValueProperty = BindProperty.builder(valueTypeName, property).xmlType(property.xmlInfo.mapEntryType.toXmlType()).elementName(property.mapValueName).build();
    methodBuilder.addStatement("$T key", elementKeyProperty.getPropertyType().getTypeName());
    methodBuilder.addStatement("$T value", elementValueProperty.getPropertyType().getTypeName());
    if (property.xmlInfo.mapEntryType == MapEntryType.ATTRIBUTE) {
        methodBuilder.addStatement("int attributeIndex");
    }
    if (property.xmlInfo.isWrappedCollection()) {
        methodBuilder.beginControlFlow("while ($L.nextTag() != $T.END_TAG && $L.getName().toString().equals($S))", parserName, XmlPullParser.class, parserName, property.xmlInfo.labelItem);
    } else {
        methodBuilder.addCode("// add first element\n");
        switch(property.xmlInfo.mapEntryType) {
            case TAG:
                methodBuilder.addStatement("$L.nextTag()", parserName);
                transformKey.generateParseOnXml(context, methodBuilder, parserName, null, "key", elementKeyProperty);
                methodBuilder.addStatement("$L.nextTag()", parserName);
                methodBuilder.beginControlFlow("if ($L.isEmptyElement())", parserName);
                methodBuilder.addStatement("value=null");
                methodBuilder.addStatement("$L.nextTag()", parserName);
                methodBuilder.nextControlFlow("else");
                transformValue.generateParseOnXml(context, methodBuilder, parserName, null, "value", elementValueProperty);
                methodBuilder.endControlFlow();
                methodBuilder.addStatement("$L.nextTag()", parserName);
                break;
            case ATTRIBUTE:
                methodBuilder.addStatement("attributeIndex=$L.getAttributeIndex(null, $S)", parserName, property.mapKeyName);
                transformKey.generateParseOnXml(context, methodBuilder, parserName, null, "key", elementKeyProperty);
                methodBuilder.addStatement("attributeIndex=$L.getAttributeIndex(null, $S)", parserName, property.mapValueName);
                transformValue.generateParseOnXml(context, methodBuilder, parserName, null, "value", elementValueProperty);
                break;
        }
        methodBuilder.addStatement("collection.put(key, value)");
        methodBuilder.beginControlFlow("while ($L.nextTag() != $T.END_TAG && $L.getName().toString().equals($S))", parserName, XmlPullParser.class, parserName, property.xmlInfo.labelItem);
    }
    switch(property.xmlInfo.mapEntryType) {
        case TAG:
            methodBuilder.addStatement("$L.nextTag()", parserName);
            transformKey.generateParseOnXml(context, methodBuilder, parserName, null, "key", elementKeyProperty);
            methodBuilder.addStatement("$L.nextTag()", parserName);
            methodBuilder.beginControlFlow("if ($L.isEmptyElement())", parserName);
            methodBuilder.addStatement("value=null");
            methodBuilder.addStatement("$L.nextTag()", parserName);
            methodBuilder.nextControlFlow("else");
            transformValue.generateParseOnXml(context, methodBuilder, parserName, null, "value", elementValueProperty);
            methodBuilder.endControlFlow();
            methodBuilder.addStatement("$L.nextTag()", parserName);
            break;
        case ATTRIBUTE:
            methodBuilder.addStatement("attributeIndex=$L.getAttributeIndex(null, $S)", parserName, property.mapKeyName);
            transformKey.generateParseOnXml(context, methodBuilder, parserName, null, "key", elementKeyProperty);
            methodBuilder.addStatement("attributeIndex=$L.getAttributeIndex(null, $S)", parserName, property.mapValueName);
            transformValue.generateParseOnXml(context, methodBuilder, parserName, null, "value", elementValueProperty);
            break;
    }
    methodBuilder.addStatement("collection.put(key, value)");
    methodBuilder.endControlFlow();
    methodBuilder.addStatement(setter(beanClass, beanName, property, "collection"));
    if (!property.xmlInfo.isWrappedCollection()) {
        methodBuilder.addStatement("read=false");
    }
    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