use of com.pushtorefresh.storio.contentresolver.annotations.processor.introspection.StorIOContentResolverColumnMeta in project storio by pushtorefresh.
the class GetResolverGenerator method createMapFromCursorWithCreatorMethodSpec.
@NotNull
private MethodSpec createMapFromCursorWithCreatorMethodSpec(@NotNull StorIOContentResolverTypeMeta storIOContentResolverTypeMeta, @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 StorIOContentResolverColumnMeta columnMeta : storIOContentResolverTypeMeta.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 (storIOContentResolverTypeMeta.creator.getKind() == ElementKind.CONSTRUCTOR) {
builder.addStatement("$T object = new $T" + paramsBuilder.toString(), storIOSQLiteTypeClassName, storIOSQLiteTypeClassName);
} else {
builder.addStatement("$T object = $T.$L", storIOSQLiteTypeClassName, storIOSQLiteTypeClassName, storIOContentResolverTypeMeta.creator.getSimpleName() + paramsBuilder.toString());
}
return builder.addCode("\n").addStatement("return object").build();
}
use of com.pushtorefresh.storio.contentresolver.annotations.processor.introspection.StorIOContentResolverColumnMeta in project storio by pushtorefresh.
the class StorIOContentResolverProcessor method processAnnotatedFieldsOrMethods.
/**
* Processes fields annotated with {@link StorIOContentResolverColumn}
*
* @param roundEnvironment current processing environment
* @param annotatedClasses map of classes annotated with {@link StorIOContentResolverType}
*/
@Override
protected void processAnnotatedFieldsOrMethods(@NotNull final RoundEnvironment roundEnvironment, @NotNull final Map<TypeElement, StorIOContentResolverTypeMeta> annotatedClasses) {
final Set<? extends Element> elementsAnnotatedWithStorIOContentResolverColumn = roundEnvironment.getElementsAnnotatedWith(StorIOContentResolverColumn.class);
for (final Element annotatedFieldElement : elementsAnnotatedWithStorIOContentResolverColumn) {
try {
validateAnnotatedFieldOrMethod(annotatedFieldElement);
final StorIOContentResolverColumnMeta storIOContentResolverColumnMeta = processAnnotatedFieldOrMethod(annotatedFieldElement);
final StorIOContentResolverTypeMeta storIOContentResolverTypeMeta = annotatedClasses.get(storIOContentResolverColumnMeta.enclosingElement);
// If class already contains column with same name -> throw an exception.
if (storIOContentResolverTypeMeta.columns.containsKey(storIOContentResolverColumnMeta.storIOColumn.name())) {
throw new ProcessingException(annotatedFieldElement, "Column name already used in this class: " + storIOContentResolverColumnMeta.storIOColumn.name());
}
// If field annotation applied to both fields and methods in a same class.
if ((storIOContentResolverTypeMeta.needCreator && !storIOContentResolverColumnMeta.isMethod()) || (!storIOContentResolverTypeMeta.needCreator && storIOContentResolverColumnMeta.isMethod() && !storIOContentResolverTypeMeta.columns.isEmpty())) {
throw new ProcessingException(annotatedFieldElement, "Can't apply " + StorIOContentResolverColumn.class.getSimpleName() + " annotation to both fields and methods in a same class: " + storIOContentResolverTypeMeta.simpleName);
}
// If column needs creator then enclosing class needs it as well.
if (!storIOContentResolverTypeMeta.needCreator && storIOContentResolverColumnMeta.isMethod()) {
storIOContentResolverTypeMeta.needCreator = true;
}
// Put meta column info.
storIOContentResolverTypeMeta.columns.put(storIOContentResolverColumnMeta.storIOColumn.name(), storIOContentResolverColumnMeta);
} catch (SkipNotAnnotatedClassWithAnnotatedParentException e) {
messager.printMessage(WARNING, e.getMessage());
}
}
}
use of com.pushtorefresh.storio.contentresolver.annotations.processor.introspection.StorIOContentResolverColumnMeta in project storio by pushtorefresh.
the class StorIOContentResolverProcessor method validateAnnotatedClassesAndColumns.
@Override
protected void validateAnnotatedClassesAndColumns(@NotNull final Map<TypeElement, StorIOContentResolverTypeMeta> annotatedClasses) {
// check that each annotated class has columns with at least one key column
for (Map.Entry<TypeElement, StorIOContentResolverTypeMeta> annotatedType : annotatedClasses.entrySet()) {
final StorIOContentResolverTypeMeta storIOContentResolverTypeMeta = annotatedType.getValue();
if (storIOContentResolverTypeMeta.columns.isEmpty()) {
throw new ProcessingException(annotatedType.getKey(), "Class marked with " + StorIOContentResolverType.class.getSimpleName() + " annotation should have at least one field or method marked with " + StorIOContentResolverColumn.class.getSimpleName() + " annotation: " + storIOContentResolverTypeMeta.simpleName);
}
boolean hasAtLeastOneKeyColumn = false;
for (final StorIOContentResolverColumnMeta columnMeta : annotatedType.getValue().columns.values()) {
if (columnMeta.storIOColumn.key()) {
hasAtLeastOneKeyColumn = true;
break;
}
}
if (!hasAtLeastOneKeyColumn) {
throw new ProcessingException(annotatedType.getKey(), "Class marked with " + StorIOContentResolverType.class.getSimpleName() + " annotation should have at least one KEY field or method marked with " + StorIOContentResolverColumn.class.getSimpleName() + " annotation: " + storIOContentResolverTypeMeta.simpleName);
}
if (storIOContentResolverTypeMeta.needCreator && storIOContentResolverTypeMeta.creator == null) {
throw new ProcessingException(annotatedType.getKey(), "Class marked with " + StorIOContentResolverType.class.getSimpleName() + " annotation needs factory method or constructor marked with " + StorIOContentResolverCreator.class.getSimpleName() + " annotation: " + storIOContentResolverTypeMeta.simpleName);
}
if (storIOContentResolverTypeMeta.needCreator && storIOContentResolverTypeMeta.creator.getParameters().size() != storIOContentResolverTypeMeta.columns.size()) {
throw new ProcessingException(annotatedType.getKey(), "Class marked with " + StorIOContentResolverType.class.getSimpleName() + " annotation needs factory method or constructor marked with " + StorIOContentResolverCreator.class.getSimpleName() + " annotation with the same amount of parameters as the number of columns: " + storIOContentResolverTypeMeta.simpleName);
}
}
}
use of com.pushtorefresh.storio.contentresolver.annotations.processor.introspection.StorIOContentResolverColumnMeta in project storio by pushtorefresh.
the class StorIOContentResolverProcessor method processAnnotatedFieldOrMethod.
/**
* Processes annotated field and returns result of processing or throws exception
*
* @param annotatedField field that was annotated with {@link StorIOContentResolverColumn}
* @return non-null {@link StorIOContentResolverColumnMeta} with meta information about field
*/
@NotNull
@Override
protected StorIOContentResolverColumnMeta 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 " + StorIOContentResolverColumn.class.getSimpleName() + " annotation, if you need to serialize/deserialize field of that type " + "-> please write your own resolver: " + e.getMessage());
}
final StorIOContentResolverColumn storIOContentResolverColumn = annotatedField.getAnnotation(StorIOContentResolverColumn.class);
if (storIOContentResolverColumn.ignoreNull() && annotatedField.asType().getKind().isPrimitive()) {
throw new ProcessingException(annotatedField, "ignoreNull should not be used for primitive type: " + annotatedField.getSimpleName());
}
final String columnName = storIOContentResolverColumn.name();
if (columnName.length() == 0) {
throw new ProcessingException(annotatedField, "Column name is empty: " + annotatedField.getSimpleName());
}
return new StorIOContentResolverColumnMeta(annotatedField.getEnclosingElement(), annotatedField, annotatedField.getSimpleName().toString(), javaType, storIOContentResolverColumn);
}
use of com.pushtorefresh.storio.contentresolver.annotations.processor.introspection.StorIOContentResolverColumnMeta in project storio by pushtorefresh.
the class GetResolverGenerator method createMapFromCursorMethodSpec.
@NotNull
private MethodSpec createMapFromCursorMethodSpec(@NotNull StorIOContentResolverTypeMeta storIOContentResolverTypeMeta, @NotNull ClassName storIOContentResolverTypeClassName) {
final MethodSpec.Builder builder = MethodSpec.methodBuilder("mapFromCursor").addJavadoc("{@inheritDoc}\n").addAnnotation(Override.class).addAnnotation(ANDROID_NON_NULL_ANNOTATION_CLASS_NAME).addModifiers(PUBLIC).returns(storIOContentResolverTypeClassName).addParameter(ParameterSpec.builder(ClassName.get("android.database", "Cursor"), "cursor").addAnnotation(ANDROID_NON_NULL_ANNOTATION_CLASS_NAME).build()).addStatement("$T object = new $T()", storIOContentResolverTypeClassName, storIOContentResolverTypeClassName).addCode("\n");
for (final StorIOContentResolverColumnMeta columnMeta : storIOContentResolverTypeMeta.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();
}
Aggregations