Search in sources :

Example 11 with BindProperty

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

the class AbstractCollectionBindTransform method generateSerializeOnXml.

@Override
public void generateSerializeOnXml(BindTypeContext context, MethodSpec.Builder methodBuilder, String serializerName, TypeName beanClass, String beanName, BindProperty property) {
    TypeName elementTypeName = extractTypeParameterName(property);
    // @formatter:off
    methodBuilder.beginControlFlow("if ($L!=null) ", getter(beanName, beanClass, property));
    switch(collectionType) {
        case LIST:
            methodBuilder.addStatement("int n=$L.size()", getter(beanName, beanClass, property));
            methodBuilder.addStatement("$T item", elementTypeName);
            break;
        case ARRAY:
            methodBuilder.addStatement("int n=$L.length", getter(beanName, beanClass, property));
            methodBuilder.addStatement("$T item", elementTypeName);
            break;
        case SET:
            methodBuilder.addStatement("int n=$L.size()", getter(beanName, beanClass, property));
            break;
    }
    if (property.xmlInfo.isWrappedCollection()) {
        methodBuilder.addCode("// write wrapper tag\n");
        methodBuilder.addStatement("$L.writeStartElement($S)", serializerName, property.label);
    }
    BindTransform transform = BindTransformer.lookup(elementTypeName);
    BindProperty elementProperty = BindProperty.builder(elementTypeName, property).inCollection(true).elementName(property.xmlInfo.labelItem).build();
    switch(collectionType) {
        case SET:
            methodBuilder.beginControlFlow("for ($T item: $L)", elementTypeName, getter(beanName, beanClass, property));
            break;
        case LIST:
            methodBuilder.beginControlFlow("for (int i=0; i<n; i++)");
            methodBuilder.addStatement("item=$L.get(i)", getter(beanName, beanClass, property));
            break;
        case ARRAY:
            methodBuilder.beginControlFlow("for (int i=0; i<n; i++)");
            methodBuilder.addStatement("item=$L[i]", getter(beanName, beanClass, property));
            break;
    }
    if (!TypeUtility.isTypePrimitive(elementTypeName)) {
        methodBuilder.beginControlFlow("if (item==null)");
        methodBuilder.addStatement("$L.writeEmptyElement($S)", serializerName, property.xmlInfo.labelItem);
        methodBuilder.nextControlFlow("else");
        transform.generateSerializeOnXml(context, methodBuilder, serializerName, null, "item", elementProperty);
        methodBuilder.endControlFlow();
    } else {
        transform.generateSerializeOnXml(context, methodBuilder, serializerName, null, "item", elementProperty);
    }
    methodBuilder.endControlFlow();
    if (property.xmlInfo.isWrappedCollection()) {
        methodBuilder.addStatement("$L.writeEndElement()", serializerName);
    } else {
        // if there's no wrap tag, we need to assure that empty collection will be writed.
        // to distinguish between first empty element and empty collection, we write an attribute emptyCollection to
        // say: this collection is empty
        methodBuilder.addCode("// to distinguish between first empty element and empty collection, we write an attribute emptyCollection\n");
        methodBuilder.beginControlFlow("if (n==0)");
        methodBuilder.addStatement("$L.writeStartElement($S)", serializerName, property.xmlInfo.labelItem);
        methodBuilder.addStatement("$L.writeAttribute($S, $S)", serializerName, EMPTY_COLLECTION_ATTRIBUTE_NAME, "true");
        methodBuilder.addStatement("$L.writeEndElement()", serializerName);
        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 12 with BindProperty

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

the class BindTypeBuilder method generateParseOnJackson.

private static void generateParseOnJackson(BindTypeContext context, BindEntity entity) {
    // @formatter:off
    MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder("parseOnJackson").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.currentToken() == null)");
    methodBuilder.addStatement("jacksonParser.nextToken()");
    methodBuilder.endControlFlow();
    methodBuilder.beginControlFlow("if (jacksonParser.currentToken() != $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.generateParseOnJackson(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 13 with BindProperty

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

the class BindTypeBuilder method generateSerializeOnJacksonAsString.

private static void generateSerializeOnJacksonAsString(BindTypeContext context, BindEntity entity) {
    // @formatter:off
    MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder("serializeOnJacksonAsString").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.generateSerializeOnJacksonAsString(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 14 with BindProperty

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

the class BindTypeBuilder method generate.

/**
 * @return typeName of generated class
 *
 * @throws IOException
 */
public static String generate(Filer filer, BindEntity item) throws IOException {
    Elements elementUtils = BaseProcessor.elementUtils;
    String beanClassName = item.getSimpleName().toString();
    boolean needSuffix = true;
    if (beanClassName.endsWith(SUFFIX)) {
        needSuffix = false;
    }
    String className = PREFIX + beanClassName + (needSuffix ? SUFFIX : "");
    PackageElement pkg = elementUtils.getPackageOf(item.getElement());
    String packageName = pkg.isUnnamed() ? "" : pkg.getQualifiedName().toString();
    AnnotationProcessorUtilis.infoOnGeneratedClasses(BindType.class, packageName, className);
    // @formatter:off
    TypeSpec.Builder builder = TypeSpec.classBuilder(className).addAnnotation(AnnotationSpec.builder(BindMap.class).addMember("value", "$T.class", typeName(item.getElement().asType())).build()).addModifiers(Modifier.PUBLIC).superclass(TypeUtility.parameterizedTypeName(className(AbstractMapper.class), typeName(item.getElement().asType())));
    BindTypeContext context = new BindTypeContext(builder, TypeUtility.typeName(packageName, className), Modifier.PRIVATE);
    // @formatter:on
    builder.addJavadoc("This class is binder map for $T\n\n", item.getElement());
    JavadocUtility.generateJavadocGeneratedBy(builder);
    builder.addJavadoc("@see $T\n", item.getElement());
    // order item by order, property typeName
    Collections.sort(item.getCollection(), new Comparator<BindProperty>() {

        @Override
        public int compare(BindProperty lhs, BindProperty rhs) {
            int c1 = lhs.order - rhs.order;
            if (c1 != 0)
                return c1;
            return lhs.getName().compareTo(rhs.getName());
        }
    });
    // generate serializeOnJackson
    generateSerializeOnJackson(context, item);
    // generate serializeOnJacksonAsString
    generateSerializeOnJacksonAsString(context, item);
    // order item by type (attribute, element, value), order, xmlName
    Collections.sort(item.getCollection(), new Comparator<BindProperty>() {

        @Override
        public int compare(BindProperty lhs, BindProperty rhs) {
            int c1 = lhs.xmlInfo.xmlType.ordinal() - rhs.xmlInfo.xmlType.ordinal();
            if (c1 != 0)
                return c1;
            c1 = lhs.order - rhs.order;
            if (c1 != 0)
                return c1;
            return lhs.label.compareTo(rhs.label);
        }
    });
    // generate serializeOnXml
    generateSerializeOnXml(context, item);
    // generate parseOnJackson
    generateParseOnJackson(context, item);
    // generate parseOnJacksonAsString
    generateParseOnJacksonAsString(context, item);
    // generate parseOnXml
    generateParseOnXml(context, item);
    TypeSpec typeSpec = builder.build();
    JavaWriterHelper.writeJava2File(filer, packageName, typeSpec);
    return className;
}
Also used : PackageElement(javax.lang.model.element.PackageElement) BindMap(com.abubusoft.kripton.annotation.BindMap) Elements(javax.lang.model.util.Elements) BindProperty(com.abubusoft.kripton.processor.bind.model.BindProperty) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 15 with BindProperty

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

the class BindTypeBuilder method generateParserOnXmlCharacters.

/**
 * Parse entity properties and write code to read only CData Text fields
 *
 * @param methodBuilder
 * @param entity
 */
private static void generateParserOnXmlCharacters(BindTypeContext context, MethodSpec.Builder methodBuilder, String instanceName, String parserName, BindEntity entity) {
    BindTransform bindTransform;
    int count = 0;
    for (BindProperty property : entity.getCollection()) {
        if (property.xmlInfo.xmlType != XmlType.VALUE && property.xmlInfo.xmlType != XmlType.VALUE_CDATA)
            continue;
        count++;
        methodBuilder.beginControlFlow("if (elementName!=null && $L.hasText())", parserName);
        methodBuilder.addCode("// property $L\n", property.getName());
        bindTransform = BindTransformer.lookup(property);
        bindTransform.generateParseOnXml(context, methodBuilder, parserName, property.getPropertyType().getTypeName(), "instance", property);
        methodBuilder.endControlFlow();
    }
    if (count == 0) {
        methodBuilder.addCode("// no property is binded to VALUE o CDATA ");
    }
}
Also used : BindTransform(com.abubusoft.kripton.processor.bind.transform.BindTransform) BindProperty(com.abubusoft.kripton.processor.bind.model.BindProperty)

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