Search in sources :

Example 11 with NotNull

use of org.jetbrains.annotations.NotNull in project storio by pushtorefresh.

the class StorIOSQLiteProcessor method processAnnotatedFieldOrMethod.

/**
     * Processes annotated field and returns result of processing or throws exception.
     *
     * @param annotatedField field that was annotated with {@link StorIOSQLiteColumn}
     * @return non-null {@link StorIOSQLiteColumnMeta} with meta information about field
     */
@NotNull
@Override
protected StorIOSQLiteColumnMeta processAnnotatedFieldOrMethod(@NotNull final Element annotatedField) {
    final JavaType javaType;
    try {
        javaType = JavaType.from(annotatedField.getKind() == ElementKind.FIELD ? annotatedField.asType() : ((ExecutableElement) annotatedField).getReturnType());
    } catch (Exception e) {
        throw new ProcessingException(annotatedField, "Unsupported type of field or method for " + StorIOSQLiteColumn.class.getSimpleName() + " annotation, if you need to serialize/deserialize field of that type " + "-> please write your own resolver: " + e.getMessage());
    }
    final StorIOSQLiteColumn storIOSQLiteColumn = annotatedField.getAnnotation(StorIOSQLiteColumn.class);
    if (storIOSQLiteColumn.ignoreNull() && annotatedField.asType().getKind().isPrimitive()) {
        throw new ProcessingException(annotatedField, "ignoreNull should not be used for primitive type: " + annotatedField.getSimpleName());
    }
    final String columnName = storIOSQLiteColumn.name();
    if (columnName.length() == 0) {
        throw new ProcessingException(annotatedField, "Column name is empty: " + annotatedField.getSimpleName());
    }
    return new StorIOSQLiteColumnMeta(annotatedField.getEnclosingElement(), annotatedField, annotatedField.getSimpleName().toString(), javaType, storIOSQLiteColumn);
}
Also used : StorIOSQLiteColumn(com.pushtorefresh.storio.sqlite.annotations.StorIOSQLiteColumn) StorIOSQLiteColumnMeta(com.pushtorefresh.storio.sqlite.annotations.processor.introspection.StorIOSQLiteColumnMeta) JavaType(com.pushtorefresh.storio.common.annotations.processor.introspection.JavaType) ProcessingException(com.pushtorefresh.storio.common.annotations.processor.ProcessingException) SkipNotAnnotatedClassWithAnnotatedParentException(com.pushtorefresh.storio.common.annotations.processor.SkipNotAnnotatedClassWithAnnotatedParentException) ProcessingException(com.pushtorefresh.storio.common.annotations.processor.ProcessingException) NotNull(org.jetbrains.annotations.NotNull)

Example 12 with NotNull

use of org.jetbrains.annotations.NotNull in project storio by pushtorefresh.

the class StorIOSQLiteProcessor method processAnnotatedClass.

/**
     * Processes annotated class.
     *
     * @param classElement type element annotated with {@link StorIOSQLiteType}
     * @param elementUtils utils for working with elementUtils
     * @return result of processing as {@link StorIOSQLiteTypeMeta}
     */
@NotNull
@Override
protected StorIOSQLiteTypeMeta processAnnotatedClass(@NotNull TypeElement classElement, @NotNull Elements elementUtils) {
    final StorIOSQLiteType storIOSQLiteType = classElement.getAnnotation(StorIOSQLiteType.class);
    final String tableName = storIOSQLiteType.table();
    if (tableName.length() == 0) {
        throw new ProcessingException(classElement, "Table name of " + classElement.getSimpleName() + " annotated with " + StorIOSQLiteType.class.getSimpleName() + " is empty");
    }
    final String simpleName = classElement.getSimpleName().toString();
    final String packageName = elementUtils.getPackageOf(classElement).getQualifiedName().toString();
    return new StorIOSQLiteTypeMeta(simpleName, packageName, storIOSQLiteType, classElement.getModifiers().contains(Modifier.ABSTRACT));
}
Also used : StorIOSQLiteTypeMeta(com.pushtorefresh.storio.sqlite.annotations.processor.introspection.StorIOSQLiteTypeMeta) StorIOSQLiteType(com.pushtorefresh.storio.sqlite.annotations.StorIOSQLiteType) ProcessingException(com.pushtorefresh.storio.common.annotations.processor.ProcessingException) NotNull(org.jetbrains.annotations.NotNull)

Example 13 with NotNull

use of org.jetbrains.annotations.NotNull in project storio by pushtorefresh.

the class DeleteResolverGenerator method generateJavaFile.

@NotNull
public JavaFile generateJavaFile(@NotNull StorIOSQLiteTypeMeta storIOSQLiteTypeMeta) {
    final ClassName storIOSQLiteTypeClassName = ClassName.get(storIOSQLiteTypeMeta.packageName, storIOSQLiteTypeMeta.simpleName);
    final TypeSpec deleteResolver = TypeSpec.classBuilder(generateName(storIOSQLiteTypeMeta)).addJavadoc("Generated resolver for Delete Operation.\n").addModifiers(PUBLIC).superclass(ParameterizedTypeName.get(ClassName.get("com.pushtorefresh.storio.sqlite.operations.delete", "DefaultDeleteResolver"), storIOSQLiteTypeClassName)).addMethod(createMapToDeleteQueryMethodSpec(storIOSQLiteTypeMeta, storIOSQLiteTypeClassName)).build();
    return JavaFile.builder(storIOSQLiteTypeMeta.packageName, deleteResolver).indent(INDENT).build();
}
Also used : ClassName(com.squareup.javapoet.ClassName) TypeSpec(com.squareup.javapoet.TypeSpec) NotNull(org.jetbrains.annotations.NotNull)

Example 14 with NotNull

use of org.jetbrains.annotations.NotNull in project storio by pushtorefresh.

the class GetResolverGenerator method createMapFromCursorWithCreatorMethodSpec.

@NotNull
private MethodSpec createMapFromCursorWithCreatorMethodSpec(@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()).addCode("\n");
    final StringBuilder paramsBuilder = new StringBuilder();
    paramsBuilder.append("(");
    boolean first = true;
    for (final StorIOSQLiteColumnMeta columnMeta : storIOSQLiteTypeMeta.getOrderedColumns()) {
        final String columnIndex = "cursor.getColumnIndex(\"" + columnMeta.storIOColumn.name() + "\")";
        final JavaType javaType = columnMeta.javaType;
        final String getFromCursor = getFromCursorString(columnMeta, javaType, columnIndex);
        final TypeName name = TypeName.get(((ExecutableElement) columnMeta.element).getReturnType());
        final boolean isBoxed = javaType.isBoxedType();
        if (isBoxed) {
            // otherwise -> if primitive and value from cursor null -> fail early
            builder.addStatement("$T $L = null", name, columnMeta.getRealElementName());
            builder.beginControlFlow("if (!cursor.isNull($L))", columnIndex);
            builder.addStatement("$L = cursor.$L", columnMeta.getRealElementName(), getFromCursor);
            builder.endControlFlow();
        } else {
            builder.addStatement("$T $L = cursor.$L", name, columnMeta.getRealElementName(), getFromCursor);
        }
        if (!first) {
            paramsBuilder.append(", ");
        }
        first = false;
        paramsBuilder.append(columnMeta.getRealElementName());
    }
    paramsBuilder.append(")");
    builder.addCode("\n");
    if (storIOSQLiteTypeMeta.creator.getKind() == ElementKind.CONSTRUCTOR) {
        builder.addStatement("$T object = new $T" + paramsBuilder.toString(), storIOSQLiteTypeClassName, storIOSQLiteTypeClassName);
    } else {
        builder.addStatement("$T object = $T.$L", storIOSQLiteTypeClassName, storIOSQLiteTypeClassName, storIOSQLiteTypeMeta.creator.getSimpleName() + paramsBuilder.toString());
    }
    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) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeName(com.squareup.javapoet.TypeName) MethodSpec(com.squareup.javapoet.MethodSpec) Common.getFromCursorString(com.pushtorefresh.storio.common.annotations.processor.generate.Common.getFromCursorString) NotNull(org.jetbrains.annotations.NotNull)

Example 15 with NotNull

use of org.jetbrains.annotations.NotNull in project storio by pushtorefresh.

the class MappingGenerator method createConstructor.

@NotNull
private MethodSpec createConstructor(StorIOSQLiteTypeMeta storIOSQLiteTypeMeta) {
    final ClassName putResolver = ClassName.get(storIOSQLiteTypeMeta.packageName, PutResolverGenerator.generateName(storIOSQLiteTypeMeta));
    final ClassName getResolver = ClassName.get(storIOSQLiteTypeMeta.packageName, GetResolverGenerator.generateName(storIOSQLiteTypeMeta));
    final ClassName deleteResolver = ClassName.get(storIOSQLiteTypeMeta.packageName, DeleteResolverGenerator.generateName(storIOSQLiteTypeMeta));
    return MethodSpec.constructorBuilder().addModifiers(PUBLIC).addStatement("super(new $T(),\nnew $T(),\nnew $T())", putResolver, getResolver, deleteResolver).build();
}
Also used : ClassName(com.squareup.javapoet.ClassName) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

NotNull (org.jetbrains.annotations.NotNull)6308 VirtualFile (com.intellij.openapi.vfs.VirtualFile)829 ArrayList (java.util.ArrayList)621 File (java.io.File)551 Project (com.intellij.openapi.project.Project)540 PsiElement (com.intellij.psi.PsiElement)461 Nullable (org.jetbrains.annotations.Nullable)394 Module (com.intellij.openapi.module.Module)315 PsiFile (com.intellij.psi.PsiFile)296 List (java.util.List)274 IOException (java.io.IOException)273 TextRange (com.intellij.openapi.util.TextRange)245 ContainerUtil (com.intellij.util.containers.ContainerUtil)170 Document (com.intellij.openapi.editor.Document)158 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)154 ASTNode (com.intellij.lang.ASTNode)145 Pair (com.intellij.openapi.util.Pair)141 StringUtil (com.intellij.openapi.util.text.StringUtil)133 Logger (com.intellij.openapi.diagnostic.Logger)127 Editor (com.intellij.openapi.editor.Editor)120