Search in sources :

Example 6 with JavaType

use of com.pushtorefresh.storio.common.annotations.processor.introspection.JavaType in project storio by pushtorefresh.

the class GetResolverGenerator method createMapFromCursorMethodSpec.

@NotNull
private MethodSpec createMapFromCursorMethodSpec(@NotNull StorIOSQLiteTypeMeta storIOSQLiteTypeMeta, @NotNull ClassName storIOSQLiteTypeClassName) {
    final MethodSpec.Builder builder = MethodSpec.methodBuilder("mapFromCursor").addJavadoc("{@inheritDoc}\n").addAnnotation(Override.class).addAnnotation(ANDROID_NON_NULL_ANNOTATION_CLASS_NAME).addModifiers(PUBLIC).returns(storIOSQLiteTypeClassName).addParameter(ParameterSpec.builder(ClassName.get("android.database", "Cursor"), "cursor").addAnnotation(ANDROID_NON_NULL_ANNOTATION_CLASS_NAME).build()).addStatement("$T object = new $T()", storIOSQLiteTypeClassName, storIOSQLiteTypeClassName).addCode("\n");
    for (final StorIOSQLiteColumnMeta columnMeta : storIOSQLiteTypeMeta.columns.values()) {
        final String columnIndex = "cursor.getColumnIndex(\"" + columnMeta.storIOColumn.name() + "\")";
        final JavaType javaType = columnMeta.javaType;
        final String getFromCursor = getFromCursorString(columnMeta, javaType, columnIndex);
        final boolean isBoxed = javaType.isBoxedType();
        if (isBoxed) {
            // otherwise -> if primitive and value from cursor null -> fail early
            builder.beginControlFlow("if (!cursor.isNull($L))", columnIndex);
        }
        builder.addStatement("object.$L = cursor.$L", columnMeta.elementName, getFromCursor);
        if (isBoxed) {
            builder.endControlFlow();
        }
    }
    return builder.addCode("\n").addStatement("return object").build();
}
Also used : StorIOSQLiteColumnMeta(com.pushtorefresh.storio.sqlite.annotations.processor.introspection.StorIOSQLiteColumnMeta) JavaType(com.pushtorefresh.storio.common.annotations.processor.introspection.JavaType) MethodSpec(com.squareup.javapoet.MethodSpec) Common.getFromCursorString(com.pushtorefresh.storio.common.annotations.processor.generate.Common.getFromCursorString) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

JavaType (com.pushtorefresh.storio.common.annotations.processor.introspection.JavaType)6 NotNull (org.jetbrains.annotations.NotNull)6 Common.getFromCursorString (com.pushtorefresh.storio.common.annotations.processor.generate.Common.getFromCursorString)4 MethodSpec (com.squareup.javapoet.MethodSpec)4 StorIOContentResolverColumnMeta (com.pushtorefresh.storio.contentresolver.annotations.processor.introspection.StorIOContentResolverColumnMeta)3 StorIOSQLiteColumnMeta (com.pushtorefresh.storio.sqlite.annotations.processor.introspection.StorIOSQLiteColumnMeta)3 ProcessingException (com.pushtorefresh.storio.common.annotations.processor.ProcessingException)2 SkipNotAnnotatedClassWithAnnotatedParentException (com.pushtorefresh.storio.common.annotations.processor.SkipNotAnnotatedClassWithAnnotatedParentException)2 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)2 TypeName (com.squareup.javapoet.TypeName)2 StorIOContentResolverColumn (com.pushtorefresh.storio.contentresolver.annotations.StorIOContentResolverColumn)1 StorIOSQLiteColumn (com.pushtorefresh.storio.sqlite.annotations.StorIOSQLiteColumn)1