Search in sources :

Example 21 with BindProperty

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

the class MapBindTransformation method generateParseOnJacksonInternal.

public void generateParseOnJacksonInternal(BindTypeContext context, Builder methodBuilder, String parserName, 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.currentToken()==$T.START_ARRAY)", parserName, JsonToken.class);
    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(false).xmlType(property.xmlInfo.mapEntryType.toXmlType()).elementName(property.mapKeyName).nullable(false).build();
    BindTransform transformValue = BindTransformer.lookup(valueTypeName);
    BindProperty elementValueProperty = BindProperty.builder(valueTypeName, property).inCollection(false).xmlType(property.xmlInfo.mapEntryType.toXmlType()).elementName(property.mapValueName).nullable(true).build();
    methodBuilder.addStatement("$T key=$L", elementKeyProperty.getPropertyType().getTypeName(), DEFAULT_VALUE);
    methodBuilder.addStatement("$T value=$L", elementValueProperty.getPropertyType().getTypeName(), DEFAULT_VALUE);
    if (onString) {
        methodBuilder.addStatement("$T current", JsonToken.class);
        methodBuilder.addStatement("String tempValue=null");
    }
    methodBuilder.beginControlFlow("while ($L.nextToken() != $T.END_ARRAY)", parserName, JsonToken.class);
    if (onString) {
        // on string
        methodBuilder.addStatement("current=$L.currentToken()", parserName);
        methodBuilder.beginControlFlow("for (int i=0; i<2 ;i++)");
        methodBuilder.beginControlFlow("while (current != $T.FIELD_NAME)", JsonToken.class);
        methodBuilder.addStatement("current=$L.nextToken()", parserName);
        methodBuilder.endControlFlow();
        methodBuilder.addStatement("$L.nextValue()", parserName);
        // 
        methodBuilder.addCode("switch(jacksonParser.getCurrentName()) {\n");
        methodBuilder.addCode("case $S:$>\n", property.mapKeyName);
        transformKey.generateParseOnJacksonAsString(context, methodBuilder, parserName, null, "key", elementKeyProperty);
        methodBuilder.addStatement("$<break");
        methodBuilder.addCode("case $S:$>\n", property.mapValueName);
        methodBuilder.addStatement("tempValue=$L.getValueAsString()", parserName);
        methodBuilder.beginControlFlow("if ($L.currentToken()==$T.VALUE_STRING && \"null\".equals(tempValue))", parserName, JsonToken.class);
        methodBuilder.addStatement("value=$L", DEFAULT_VALUE);
        methodBuilder.nextControlFlow("else");
        transformValue.generateParseOnJacksonAsString(context, methodBuilder, parserName, null, "value", elementValueProperty);
        methodBuilder.endControlFlow();
        methodBuilder.addStatement("$<break");
        methodBuilder.addCode("}\n");
        // 
        methodBuilder.endControlFlow();
    } else {
        // key
        methodBuilder.addStatement("$L.nextValue()", parserName);
        transformKey.generateParseOnJackson(context, methodBuilder, parserName, null, "key", elementKeyProperty);
        // value
        methodBuilder.addStatement("$L.nextValue()", parserName);
        transformValue.generateParseOnJackson(context, methodBuilder, parserName, null, "value", elementValueProperty);
    }
    methodBuilder.addStatement("collection.put(key, value)");
    methodBuilder.addStatement("key=$L", DEFAULT_VALUE);
    methodBuilder.addStatement("value=$L", DEFAULT_VALUE);
    methodBuilder.addStatement("$L.nextToken()", parserName);
    methodBuilder.endControlFlow();
    methodBuilder.addStatement(setter(beanClass, beanName, property, "collection"));
    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)

Example 22 with BindProperty

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

the class MapBindTransformation method generateSerializeOnXml.

@Override
public void generateSerializeOnXml(BindTypeContext context, MethodSpec.Builder methodBuilder, String serializerName, 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("if ($L!=null) ", getter(beanName, beanClass, property));
    if (property.xmlInfo.isWrappedCollection()) {
        methodBuilder.addCode("// write wrapper tag\n");
        methodBuilder.addStatement("$L.writeStartElement($S)", serializerName, property.label);
    }
    BindTransform transformKey = BindTransformer.lookup(keyTypeName);
    // key can not be null
    // not in collection, it's included in an element
    BindProperty elementKeyProperty = BindProperty.builder(keyTypeName, property).inCollection(false).nullable(false).xmlType(property.xmlInfo.mapEntryType.toXmlType()).elementName(property.mapKeyName).build();
    BindTransform transformValue = BindTransformer.lookup(valueTypeName);
    // not in collection, it's included in an element
    BindProperty elementValueProperty = BindProperty.builder(valueTypeName, property).inCollection(false).xmlType(property.xmlInfo.mapEntryType.toXmlType()).elementName(property.mapValueName).build();
    methodBuilder.beginControlFlow("for ($T<$T, $T> item: $L.entrySet())", Entry.class, keyTypeName, valueTypeName, getter(beanName, beanClass, property));
    methodBuilder.addStatement("$L.writeStartElement($S)$>", serializerName, property.xmlInfo.labelItem);
    transformKey.generateSerializeOnXml(context, methodBuilder, serializerName, null, "item.getKey()", elementKeyProperty);
    if (elementValueProperty.isNullable()) {
        methodBuilder.beginControlFlow("if (item.getValue()==null)");
        methodBuilder.addStatement("$L.writeEmptyElement($S)", serializerName, property.mapValueName);
        methodBuilder.nextControlFlow("else");
        transformValue.generateSerializeOnXml(context, methodBuilder, serializerName, null, "item.getValue()", elementValueProperty);
        methodBuilder.endControlFlow();
    } else {
        transformValue.generateSerializeOnXml(context, methodBuilder, serializerName, null, "item.getValue()", elementValueProperty);
    }
    methodBuilder.addStatement("$<$L.writeEndElement()", serializerName);
    methodBuilder.endControlFlow();
    if (property.xmlInfo.isWrappedCollection()) {
        methodBuilder.addStatement("$L.writeEndElement()", serializerName);
    }
    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