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);
}
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));
}
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();
}
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();
}
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();
}
Aggregations