diff --git a/storio-common-annotations-processor/src/main/kotlin/com/pushtorefresh/storio/common/annotations/processor/Extensions.kt b/storio-common-annotations-processor/src/main/kotlin/com/pushtorefresh/storio/common/annotations/processor/Extensions.kt new file mode 100644 index 000000000..236ae8f0d --- /dev/null +++ b/storio-common-annotations-processor/src/main/kotlin/com/pushtorefresh/storio/common/annotations/processor/Extensions.kt @@ -0,0 +1,4 @@ +package com.pushtorefresh.storio.common.annotations.processor + +fun String.startsWithIs(): Boolean = this.startsWith("is") && this.length > 2 + && Character.isUpperCase(this[2]) diff --git a/storio-common-annotations-processor/src/main/kotlin/com/pushtorefresh/storio/common/annotations/processor/StorIOAnnotationsProcessor.kt b/storio-common-annotations-processor/src/main/kotlin/com/pushtorefresh/storio/common/annotations/processor/StorIOAnnotationsProcessor.kt index 341d7ec4a..d673f03bd 100644 --- a/storio-common-annotations-processor/src/main/kotlin/com/pushtorefresh/storio/common/annotations/processor/StorIOAnnotationsProcessor.kt +++ b/storio-common-annotations-processor/src/main/kotlin/com/pushtorefresh/storio/common/annotations/processor/StorIOAnnotationsProcessor.kt @@ -7,6 +7,7 @@ import com.pushtorefresh.storio.common.annotations.processor.introspection.StorI import javax.annotation.processing.* import javax.lang.model.SourceVersion import javax.lang.model.element.Element +import javax.lang.model.element.ElementKind import javax.lang.model.element.ElementKind.* import javax.lang.model.element.ExecutableElement import javax.lang.model.element.Modifier.* @@ -30,6 +31,10 @@ abstract class StorIOAnnotationsProcessor, out C private lateinit var typeUtils: Types protected lateinit var messager: Messager + // cashing getters and setters for private fields to avoid second pass since we already + // have result after the validation step + protected val accessorsMap = mutableMapOf>() + /** * Processes class annotations. * @@ -76,7 +81,7 @@ abstract class StorIOAnnotationsProcessor, out C /** * Checks that element annotated with [StorIOColumnMeta] satisfies all required conditions. * - * @param annotatedElement an annotated field + * @param annotatedElement an annotated field or method */ @Throws(SkipNotAnnotatedClassWithAnnotatedParentException::class) protected fun validateAnnotatedFieldOrMethod(annotatedElement: Element) { @@ -99,7 +104,13 @@ abstract class StorIOAnnotationsProcessor, out C } if (PRIVATE in annotatedElement.modifiers) { - throw ProcessingException(annotatedElement, "${columnAnnotationClass.simpleName} can not be applied to private field or method: ${annotatedElement.simpleName}") + if (annotatedElement.kind == FIELD) { + if (!findGetterAndSetterForPrivateField(annotatedElement)) { + throw ProcessingException(annotatedElement, "${columnAnnotationClass.simpleName} can not be applied to private field without corresponding getter and setter or private method: ${annotatedElement.simpleName}") + } + } else { + throw ProcessingException(annotatedElement, "${columnAnnotationClass.simpleName} can not be applied to private field without corresponding getter and setter or private method: ${annotatedElement.simpleName}") + } } if (annotatedElement.kind == FIELD && FINAL in annotatedElement.modifiers) { @@ -141,6 +152,53 @@ abstract class StorIOAnnotationsProcessor, out C } } + /** + * Checks that field is accessible via corresponding getter and setter. + * Cashes names of elements getter and setter into [accessorsMap]. + * + * @param annotatedElement an annotated field + */ + protected fun findGetterAndSetterForPrivateField(annotatedElement: Element): Boolean { + val name = annotatedElement.simpleName.toString() + var getter: String? = null + var setter: String? = null + annotatedElement.enclosingElement.enclosedElements.forEach { element -> + if (element.kind == ElementKind.METHOD) { + val method = element as ExecutableElement + val methodName = method.simpleName.toString() + // check if it is a valid getter + if ((methodName == String.format("get%s", name.capitalize()) + || methodName == String.format("is%s", name.capitalize()) + // Special case for properties which name starts with is. + // Kotlin will generate getter with the same name instead of isIsProperty. + || methodName == name && name.startsWithIs()) + && !method.modifiers.contains(PRIVATE) + && !method.modifiers.contains(STATIC) + && method.parameters.isEmpty() + && method.returnType == annotatedElement.asType()) { + getter = methodName + } + // check if it is a valid setter + if ((methodName == String.format("set%s", name.capitalize()) + // Special case for properties which name starts with is. + // Kotlin will generate setter with setProperty name instead of setIsProperty. + || name.startsWithIs() && methodName == String.format("set%s", name.substring(2, name.length))) + && !method.modifiers.contains(PRIVATE) + && !method.modifiers.contains(STATIC) + && method.parameters.size == 1 + && method.parameters[0].asType() == annotatedElement.asType()) { + setter = methodName + } + } + } + if (getter == null || setter == null) { + return false + } else { + accessorsMap += name to (getter!! to setter!!) + return true + } + } + @Synchronized override fun init(processingEnv: ProcessingEnvironment) { super.init(processingEnv) filer = processingEnv.filer @@ -245,4 +303,4 @@ abstract class StorIOAnnotationsProcessor, out C protected abstract fun createDeleteResolver(): Generator protected abstract fun createMapping(): Generator -} \ No newline at end of file +} diff --git a/storio-common-annotations-processor/src/main/kotlin/com/pushtorefresh/storio/common/annotations/processor/introspection/StorIOColumnMeta.kt b/storio-common-annotations-processor/src/main/kotlin/com/pushtorefresh/storio/common/annotations/processor/introspection/StorIOColumnMeta.kt index d478c35c1..b8a8e631e 100644 --- a/storio-common-annotations-processor/src/main/kotlin/com/pushtorefresh/storio/common/annotations/processor/introspection/StorIOColumnMeta.kt +++ b/storio-common-annotations-processor/src/main/kotlin/com/pushtorefresh/storio/common/annotations/processor/introspection/StorIOColumnMeta.kt @@ -9,7 +9,9 @@ open class StorIOColumnMeta( val element: Element, val elementName: String, val javaType: JavaType, - val storIOColumn: ColumnAnnotation) { + val storIOColumn: ColumnAnnotation, + val getter: String? = null, + val setter: String? = null) { val isMethod: Boolean get() = element.kind == ElementKind.METHOD @@ -21,6 +23,16 @@ open class StorIOColumnMeta( else -> elementName } + val needAccessors: Boolean + get() = getter != null && setter != null + + val contextAwareName: String + get() = when { + isMethod -> "$elementName()" + needAccessors -> "$getter()" + else -> elementName + } + override fun equals(other: Any?): Boolean { if (this === other) return true if (other?.javaClass != javaClass) return false @@ -32,6 +44,8 @@ open class StorIOColumnMeta( if (elementName != other.elementName) return false if (javaType != other.javaType) return false if (storIOColumn != other.storIOColumn) return false + if (getter != other.getter) return false + if (setter != other.setter) return false return true } @@ -42,13 +56,15 @@ open class StorIOColumnMeta( result = 31 * result + elementName.hashCode() result = 31 * result + javaType.hashCode() result = 31 * result + storIOColumn.hashCode() + result = 31 * result + (getter?.hashCode() ?: 0) + result = 31 * result + (setter?.hashCode() ?: 0) return result } - override fun toString() = "StorIOColumnMeta(enclosingElement=$enclosingElement, element=$element, elementName='$elementName', javaType=$javaType, storIOColumn=$storIOColumn)" + override fun toString(): String = "StorIOColumnMeta(enclosingElement=$enclosingElement, element=$element, elementName='$elementName', javaType=$javaType, storIOColumn=$storIOColumn, getter='$getter', setter='$setter')" private fun decapitalize(str: String) = when { str.length > 1 -> Character.toLowerCase(str[0]) + str.substring(1) else -> str.toLowerCase() } -} \ No newline at end of file +} diff --git a/storio-common-annotations-processor/src/test/kotlin/com/pushtorefresh/storio/common/annotations/processor/introspection/StorIOColumnMetaTest.kt b/storio-common-annotations-processor/src/test/kotlin/com/pushtorefresh/storio/common/annotations/processor/introspection/StorIOColumnMetaTest.kt index f3a4f34f7..fcdc5a014 100644 --- a/storio-common-annotations-processor/src/test/kotlin/com/pushtorefresh/storio/common/annotations/processor/introspection/StorIOColumnMetaTest.kt +++ b/storio-common-annotations-processor/src/test/kotlin/com/pushtorefresh/storio/common/annotations/processor/introspection/StorIOColumnMetaTest.kt @@ -45,10 +45,8 @@ class StorIOColumnMetaTest { @Test fun toStringValidation() { // given - val columnMeta = StorIOColumnMeta(elementMock, elementMock, "TEST", javaType, annotationMock) - val expectedString = "StorIOColumnMeta(enclosingElement=$elementMock," + - " element=$elementMock, elementName='TEST', javaType=" + javaType + - ", storIOColumn=" + annotationMock + ')' + val columnMeta = StorIOColumnMeta(elementMock, elementMock, "TEST", javaType, annotationMock, "getter", "setter") + val expectedString = "StorIOColumnMeta(enclosingElement=$elementMock, element=$elementMock, elementName='TEST', javaType=$javaType, storIOColumn=$annotationMock, getter='getter', setter='setter')" // when val toString = columnMeta.toString() @@ -112,4 +110,4 @@ class StorIOColumnMetaTest { assertThat(realName).isEqualTo("iso") } -} \ No newline at end of file +} diff --git a/storio-content-resolver-annotations-processor-test/src/test/java/com/pushtorefresh/storio/contentresolver/annotations/processor/test/StorIOContentResolverAnnotationsProcessorTest.java b/storio-content-resolver-annotations-processor-test/src/test/java/com/pushtorefresh/storio/contentresolver/annotations/processor/test/StorIOContentResolverAnnotationsProcessorTest.java index 8ed527e54..8933ce529 100644 --- a/storio-content-resolver-annotations-processor-test/src/test/java/com/pushtorefresh/storio/contentresolver/annotations/processor/test/StorIOContentResolverAnnotationsProcessorTest.java +++ b/storio-content-resolver-annotations-processor-test/src/test/java/com/pushtorefresh/storio/contentresolver/annotations/processor/test/StorIOContentResolverAnnotationsProcessorTest.java @@ -70,14 +70,25 @@ public void shouldNotCompileIfAnnotatedFieldInsideNotAnnotatedClass() { } @Test - public void shouldNotCompileIfAnnotatedFieldIsPrivate() { - JavaFileObject model = JavaFileObjects.forResource("PrivateField.java"); + public void shouldNotCompileIfAnnotatedFieldIsPrivateAndDoesNotHaveAccessors() { + JavaFileObject model = JavaFileObjects.forResource("PrivateFieldWithoutAccessors.java"); assert_().about(javaSource()) .that(model) .processedWith(new StorIOContentResolverProcessor()) .failsToCompile() - .withErrorContaining("StorIOContentResolverColumn can not be applied to private field or method: id"); + .withErrorContaining("StorIOContentResolverColumn can not be applied to private field without corresponding getter and setter or private method: id"); + } + + @Test + public void shouldNotCompileIfAnnotatedMethodIsPrivate() { + JavaFileObject model = JavaFileObjects.forResource("PrivateMethod.java"); + + assert_().about(javaSource()) + .that(model) + .processedWith(new StorIOContentResolverProcessor()) + .failsToCompile() + .withErrorContaining("StorIOContentResolverColumn can not be applied to private field without corresponding getter and setter or private method: id"); } @Test @@ -496,4 +507,96 @@ public void shouldCompileWithMethodsReturningBoxedTypesAndMarkedAsIgnoreNullAndF .generatesSources(generatedTypeMapping, generatedDeleteResolver, generatedGetResolver, generatedPutResolver); } + @Test + public void shouldNotCompileIfAnnotatedFieldIsPrivateAndDoesNotHaveSetter() { + JavaFileObject model = JavaFileObjects.forResource("PrivateFieldWithoutSetter.java"); + + assert_().about(javaSource()) + .that(model) + .processedWith(new StorIOContentResolverProcessor()) + .failsToCompile() + .withErrorContaining("StorIOContentResolverColumn can not be applied to private field without corresponding getter and setter or private method: id"); + } + + @Test + public void shouldNotCompileIfAnnotatedFieldIsPrivateAndDoesNotHaveGetter() { + JavaFileObject model = JavaFileObjects.forResource("PrivateFieldWithoutGetter.java"); + + assert_().about(javaSource()) + .that(model) + .processedWith(new StorIOContentResolverProcessor()) + .failsToCompile() + .withErrorContaining("StorIOContentResolverColumn can not be applied to private field without corresponding getter and setter or private method: id"); + } + + @Test + public void shouldCompileIfAnnotatedFieldIsPrivateAndHasIsGetter() { + JavaFileObject model = JavaFileObjects.forResource("PrivateFieldWithIsGetter.java"); + + assert_().about(javaSource()) + .that(model) + .processedWith(new StorIOContentResolverProcessor()) + .compilesWithoutError(); + } + + @Test + public void shouldCompileIfAnnotatedFieldIsPrivateAndHasNameStartingWithIs() { + JavaFileObject model = JavaFileObjects.forResource("PrivateFieldWithNameStartingWithIs.java"); + + assert_().about(javaSource()) + .that(model) + .processedWith(new StorIOContentResolverProcessor()) + .compilesWithoutError(); + } + + @Test + public void shouldCompileWithPrivatePrimitiveFieldsWithCorrepsondingAccessors() { + JavaFileObject model = JavaFileObjects.forResource("PrimitivePrivateFields.java"); + + JavaFileObject generatedTypeMapping = JavaFileObjects.forResource("PrimitivePrivateFieldsContentResolverTypeMapping.java"); + JavaFileObject generatedDeleteResolver = JavaFileObjects.forResource("PrimitivePrivateFieldsStorIOContentResolverDeleteResolver.java"); + JavaFileObject generatedGetResolver = JavaFileObjects.forResource("PrimitivePrivateFieldsStorIOContentResolverGetResolver.java"); + JavaFileObject generatedPutResolver = JavaFileObjects.forResource("PrimitivePrivateFieldsStorIOContentResolverPutResolver.java"); + + assert_().about(javaSource()) + .that(model) + .processedWith(new StorIOContentResolverProcessor()) + .compilesWithoutError() + .and() + .generatesSources(generatedTypeMapping, generatedDeleteResolver, generatedGetResolver, generatedPutResolver); + } + + @Test + public void shouldCompileWithPrivateBoxedTypesFieldsWithCorrespondingAccessors() { + JavaFileObject model = JavaFileObjects.forResource("BoxedTypesPrivateFields.java"); + + JavaFileObject generatedTypeMapping = JavaFileObjects.forResource("BoxedTypesPrivateFieldsContentResolverTypeMapping.java"); + JavaFileObject generatedDeleteResolver = JavaFileObjects.forResource("BoxedTypesPrivateFieldsStorIOContentResolverDeleteResolver.java"); + JavaFileObject generatedGetResolver = JavaFileObjects.forResource("BoxedTypesPrivateFieldsStorIOContentResolverGetResolver.java"); + JavaFileObject generatedPutResolver = JavaFileObjects.forResource("BoxedTypesPrivateFieldsStorIOContentResolverPutResolver.java"); + + assert_().about(javaSource()) + .that(model) + .processedWith(new StorIOContentResolverProcessor()) + .compilesWithoutError() + .and() + .generatesSources(generatedTypeMapping, generatedDeleteResolver, generatedGetResolver, generatedPutResolver); + } + + @Test + public void shouldCompileWithPrivateBoxedTypesFieldsWithCorresondingAccessorsAndMarkedAsIgnoreNull() { + JavaFileObject model = JavaFileObjects.forResource("BoxedTypesPrivateFieldsIgnoreNull.java"); + + JavaFileObject generatedTypeMapping = JavaFileObjects.forResource("BoxedTypesPrivateFieldsIgnoreNullContentResolverTypeMapping.java"); + JavaFileObject generatedDeleteResolver = JavaFileObjects.forResource("BoxedTypesPrivateFieldsIgnoreNullStorIOContentResolverDeleteResolver.java"); + JavaFileObject generatedGetResolver = JavaFileObjects.forResource("BoxedTypesPrivateFieldsIgnoreNullStorIOContentResolverGetResolver.java"); + JavaFileObject generatedPutResolver = JavaFileObjects.forResource("BoxedTypesPrivateFieldsIgnoreNullStorIOContentResolverPutResolver.java"); + + assert_().about(javaSource()) + .that(model) + .processedWith(new StorIOContentResolverProcessor()) + .compilesWithoutError() + .and() + .generatesSources(generatedTypeMapping, generatedDeleteResolver, generatedGetResolver, generatedPutResolver); + } } \ No newline at end of file diff --git a/storio-content-resolver-annotations-processor-test/src/test/resources/BoxedTypesPrivateFields.java b/storio-content-resolver-annotations-processor-test/src/test/resources/BoxedTypesPrivateFields.java new file mode 100644 index 000000000..323774cb7 --- /dev/null +++ b/storio-content-resolver-annotations-processor-test/src/test/resources/BoxedTypesPrivateFields.java @@ -0,0 +1,71 @@ +package com.pushtorefresh.storio.contentresolver.annotations; + +@StorIOContentResolverType(uri = "content://uri") +public class BoxedTypesPrivateFields { + + @StorIOContentResolverColumn(name = "field1") + private Boolean field1; + + @StorIOContentResolverColumn(name = "field2") + private Short field2; + + @StorIOContentResolverColumn(name = "field3") + private Integer field3; + + @StorIOContentResolverColumn(name = "field4", key = true) + private Long field4; + + @StorIOContentResolverColumn(name = "field5") + private Float field5; + + @StorIOContentResolverColumn(name = "field6") + private Double field6; + + public Boolean getField1() { + return field1; + } + + public void setField1(Boolean field1) { + this.field1 = field1; + } + + public Short getField2() { + return field2; + } + + public void setField2(Short field2) { + this.field2 = field2; + } + + public Integer getField3() { + return field3; + } + + public void setField3(Integer field3) { + this.field3 = field3; + } + + public Long getField4() { + return field4; + } + + public void setField4(Long field4) { + this.field4 = field4; + } + + public Float getField5() { + return field5; + } + + public void setField5(Float field5) { + this.field5 = field5; + } + + public Double getField6() { + return field6; + } + + public void setField6(Double field6) { + this.field6 = field6; + } +} \ No newline at end of file diff --git a/storio-content-resolver-annotations-processor-test/src/test/resources/BoxedTypesPrivateFieldsContentResolverTypeMapping.java b/storio-content-resolver-annotations-processor-test/src/test/resources/BoxedTypesPrivateFieldsContentResolverTypeMapping.java new file mode 100644 index 000000000..07b63ff4a --- /dev/null +++ b/storio-content-resolver-annotations-processor-test/src/test/resources/BoxedTypesPrivateFieldsContentResolverTypeMapping.java @@ -0,0 +1,14 @@ +package com.pushtorefresh.storio.contentresolver.annotations; + +import com.pushtorefresh.storio.contentresolver.ContentResolverTypeMapping; + +/** + * Generated mapping with collection of resolvers + */ +public class BoxedTypesPrivateFieldsContentResolverTypeMapping extends ContentResolverTypeMapping { + public BoxedTypesPrivateFieldsContentResolverTypeMapping() { + super(new BoxedTypesPrivateFieldsStorIOContentResolverPutResolver(), + new BoxedTypesPrivateFieldsStorIOContentResolverGetResolver(), + new BoxedTypesPrivateFieldsStorIOContentResolverDeleteResolver()); + } +} diff --git a/storio-content-resolver-annotations-processor-test/src/test/resources/BoxedTypesPrivateFieldsIgnoreNull.java b/storio-content-resolver-annotations-processor-test/src/test/resources/BoxedTypesPrivateFieldsIgnoreNull.java new file mode 100644 index 000000000..de23930d1 --- /dev/null +++ b/storio-content-resolver-annotations-processor-test/src/test/resources/BoxedTypesPrivateFieldsIgnoreNull.java @@ -0,0 +1,71 @@ +package com.pushtorefresh.storio.contentresolver.annotations; + +@StorIOContentResolverType(uri = "content://uri") +public class BoxedTypesPrivateFieldsIgnoreNull { + + @StorIOContentResolverColumn(name = "field1", ignoreNull = true) + private Boolean field1; + + @StorIOContentResolverColumn(name = "field2", ignoreNull = true) + private Short field2; + + @StorIOContentResolverColumn(name = "field3", ignoreNull = true) + private Integer field3; + + @StorIOContentResolverColumn(name = "field4", key = true, ignoreNull = true) + private Long field4; + + @StorIOContentResolverColumn(name = "field5", ignoreNull = true) + private Float field5; + + @StorIOContentResolverColumn(name = "field6", ignoreNull = true) + private Double field6; + + public Boolean getField1() { + return field1; + } + + public void setField1(Boolean field1) { + this.field1 = field1; + } + + public Short getField2() { + return field2; + } + + public void setField2(Short field2) { + this.field2 = field2; + } + + public Integer getField3() { + return field3; + } + + public void setField3(Integer field3) { + this.field3 = field3; + } + + public Long getField4() { + return field4; + } + + public void setField4(Long field4) { + this.field4 = field4; + } + + public Float getField5() { + return field5; + } + + public void setField5(Float field5) { + this.field5 = field5; + } + + public Double getField6() { + return field6; + } + + public void setField6(Double field6) { + this.field6 = field6; + } +} \ No newline at end of file diff --git a/storio-content-resolver-annotations-processor-test/src/test/resources/BoxedTypesPrivateFieldsIgnoreNullContentResolverTypeMapping.java b/storio-content-resolver-annotations-processor-test/src/test/resources/BoxedTypesPrivateFieldsIgnoreNullContentResolverTypeMapping.java new file mode 100644 index 000000000..2b5736b6b --- /dev/null +++ b/storio-content-resolver-annotations-processor-test/src/test/resources/BoxedTypesPrivateFieldsIgnoreNullContentResolverTypeMapping.java @@ -0,0 +1,14 @@ +package com.pushtorefresh.storio.contentresolver.annotations; + +import com.pushtorefresh.storio.contentresolver.ContentResolverTypeMapping; + +/** + * Generated mapping with collection of resolvers + */ +public class BoxedTypesPrivateFieldsIgnoreNullContentResolverTypeMapping extends ContentResolverTypeMapping { + public BoxedTypesPrivateFieldsIgnoreNullContentResolverTypeMapping() { + super(new BoxedTypesPrivateFieldsIgnoreNullStorIOContentResolverPutResolver(), + new BoxedTypesPrivateFieldsIgnoreNullStorIOContentResolverGetResolver(), + new BoxedTypesPrivateFieldsIgnoreNullStorIOContentResolverDeleteResolver()); + } +} \ No newline at end of file diff --git a/storio-content-resolver-annotations-processor-test/src/test/resources/BoxedTypesPrivateFieldsIgnoreNullStorIOContentResolverDeleteResolver.java b/storio-content-resolver-annotations-processor-test/src/test/resources/BoxedTypesPrivateFieldsIgnoreNullStorIOContentResolverDeleteResolver.java new file mode 100644 index 000000000..b00687e2d --- /dev/null +++ b/storio-content-resolver-annotations-processor-test/src/test/resources/BoxedTypesPrivateFieldsIgnoreNullStorIOContentResolverDeleteResolver.java @@ -0,0 +1,23 @@ +package com.pushtorefresh.storio.contentresolver.annotations; + +import android.support.annotation.NonNull; +import com.pushtorefresh.storio.contentresolver.operations.delete.DefaultDeleteResolver; +import com.pushtorefresh.storio.contentresolver.queries.DeleteQuery; +import java.lang.Override; + +/** + * Generated resolver for Delete Operation + */ +public class BoxedTypesPrivateFieldsIgnoreNullStorIOContentResolverDeleteResolver extends DefaultDeleteResolver { + /** + * {@inheritDoc} + */ + @Override + @NonNull + public DeleteQuery mapToDeleteQuery(@NonNull BoxedTypesPrivateFieldsIgnoreNull object) { + return DeleteQuery.builder() + .uri("content://uri") + .where("field4 = ?") + .whereArgs(object.getField4()) + .build();} +} \ No newline at end of file diff --git a/storio-content-resolver-annotations-processor-test/src/test/resources/BoxedTypesPrivateFieldsIgnoreNullStorIOContentResolverGetResolver.java b/storio-content-resolver-annotations-processor-test/src/test/resources/BoxedTypesPrivateFieldsIgnoreNullStorIOContentResolverGetResolver.java new file mode 100644 index 000000000..a83f7a4c4 --- /dev/null +++ b/storio-content-resolver-annotations-processor-test/src/test/resources/BoxedTypesPrivateFieldsIgnoreNullStorIOContentResolverGetResolver.java @@ -0,0 +1,41 @@ +package com.pushtorefresh.storio.contentresolver.annotations; + +import android.database.Cursor; +import android.support.annotation.NonNull; +import com.pushtorefresh.storio.contentresolver.operations.get.DefaultGetResolver; +import java.lang.Override; + +/** + * Generated resolver for Get Operation + */ +public class BoxedTypesPrivateFieldsIgnoreNullStorIOContentResolverGetResolver extends DefaultGetResolver { + /** + * {@inheritDoc} + */ + @Override + @NonNull + public BoxedTypesPrivateFieldsIgnoreNull mapFromCursor(@NonNull Cursor cursor) { + BoxedTypesPrivateFieldsIgnoreNull object = new BoxedTypesPrivateFieldsIgnoreNull(); + + if (!cursor.isNull(cursor.getColumnIndex("field1"))) { + object.setField1(cursor.getInt(cursor.getColumnIndex("field1")) == 1); + } + if (!cursor.isNull(cursor.getColumnIndex("field2"))) { + object.setField2(cursor.getShort(cursor.getColumnIndex("field2"))); + } + if (!cursor.isNull(cursor.getColumnIndex("field3"))) { + object.setField3(cursor.getInt(cursor.getColumnIndex("field3"))); + } + if (!cursor.isNull(cursor.getColumnIndex("field4"))) { + object.setField4(cursor.getLong(cursor.getColumnIndex("field4"))); + } + if (!cursor.isNull(cursor.getColumnIndex("field5"))) { + object.setField5(cursor.getFloat(cursor.getColumnIndex("field5"))); + } + if (!cursor.isNull(cursor.getColumnIndex("field6"))) { + object.setField6(cursor.getDouble(cursor.getColumnIndex("field6"))); + } + + return object; + } +} \ No newline at end of file diff --git a/storio-content-resolver-annotations-processor-test/src/test/resources/BoxedTypesPrivateFieldsIgnoreNullStorIOContentResolverPutResolver.java b/storio-content-resolver-annotations-processor-test/src/test/resources/BoxedTypesPrivateFieldsIgnoreNullStorIOContentResolverPutResolver.java new file mode 100644 index 000000000..7ab53b76f --- /dev/null +++ b/storio-content-resolver-annotations-processor-test/src/test/resources/BoxedTypesPrivateFieldsIgnoreNullStorIOContentResolverPutResolver.java @@ -0,0 +1,65 @@ +package com.pushtorefresh.storio.contentresolver.annotations; + +import android.content.ContentValues; +import android.support.annotation.NonNull; +import com.pushtorefresh.storio.contentresolver.operations.put.DefaultPutResolver; +import com.pushtorefresh.storio.contentresolver.queries.InsertQuery; +import com.pushtorefresh.storio.contentresolver.queries.UpdateQuery; +import java.lang.Override; + +/** + * Generated resolver for Put Operation + */ +public class BoxedTypesPrivateFieldsIgnoreNullStorIOContentResolverPutResolver extends DefaultPutResolver { + /** + * {@inheritDoc} + */ + @Override + @NonNull + public InsertQuery mapToInsertQuery(@NonNull BoxedTypesPrivateFieldsIgnoreNull object) { + return InsertQuery.builder() + .uri("content://uri") + .build();} + + /** + * {@inheritDoc} + */ + @Override + @NonNull + public UpdateQuery mapToUpdateQuery(@NonNull BoxedTypesPrivateFieldsIgnoreNull object) { + return UpdateQuery.builder() + .uri("content://uri") + .where("field4 = ?") + .whereArgs(object.getField4()) + .build();} + + /** + * {@inheritDoc} + */ + @Override + @NonNull + public ContentValues mapToContentValues(@NonNull BoxedTypesPrivateFieldsIgnoreNull object) { + ContentValues contentValues = new ContentValues(6); + + if (object.getField1() != null) { + contentValues.put("field1", object.getField1()); + } + if (object.getField2() != null) { + contentValues.put("field2", object.getField2()); + } + if (object.getField3() != null) { + contentValues.put("field3", object.getField3()); + } + if (object.getField4() != null) { + contentValues.put("field4", object.getField4()); + } + if (object.getField5() != null) { + contentValues.put("field5", object.getField5()); + } + if (object.getField6() != null) { + contentValues.put("field6", object.getField6()); + } + + return contentValues; + } +} \ No newline at end of file diff --git a/storio-content-resolver-annotations-processor-test/src/test/resources/BoxedTypesPrivateFieldsStorIOContentResolverDeleteResolver.java b/storio-content-resolver-annotations-processor-test/src/test/resources/BoxedTypesPrivateFieldsStorIOContentResolverDeleteResolver.java new file mode 100644 index 000000000..fd668c191 --- /dev/null +++ b/storio-content-resolver-annotations-processor-test/src/test/resources/BoxedTypesPrivateFieldsStorIOContentResolverDeleteResolver.java @@ -0,0 +1,24 @@ +package com.pushtorefresh.storio.contentresolver.annotations; + +import android.support.annotation.NonNull; +import com.pushtorefresh.storio.contentresolver.operations.delete.DefaultDeleteResolver; +import com.pushtorefresh.storio.contentresolver.queries.DeleteQuery; +import java.lang.Override; + +/** + * Generated resolver for Delete Operation + */ +public class BoxedTypesPrivateFieldsStorIOContentResolverDeleteResolver extends DefaultDeleteResolver { + /** + * {@inheritDoc} + */ + @Override + @NonNull + public DeleteQuery mapToDeleteQuery(@NonNull BoxedTypesPrivateFields object) { + return DeleteQuery.builder() + .uri("content://uri") + .where("field4 = ?") + .whereArgs(object.getField4()) + .build();} +} + diff --git a/storio-content-resolver-annotations-processor-test/src/test/resources/BoxedTypesPrivateFieldsStorIOContentResolverGetResolver.java b/storio-content-resolver-annotations-processor-test/src/test/resources/BoxedTypesPrivateFieldsStorIOContentResolverGetResolver.java new file mode 100644 index 000000000..ca6cd3a50 --- /dev/null +++ b/storio-content-resolver-annotations-processor-test/src/test/resources/BoxedTypesPrivateFieldsStorIOContentResolverGetResolver.java @@ -0,0 +1,41 @@ +package com.pushtorefresh.storio.contentresolver.annotations; + +import android.database.Cursor; +import android.support.annotation.NonNull; +import com.pushtorefresh.storio.contentresolver.operations.get.DefaultGetResolver; +import java.lang.Override; + +/** + * Generated resolver for Get Operation + */ +public class BoxedTypesPrivateFieldsStorIOContentResolverGetResolver extends DefaultGetResolver { + /** + * {@inheritDoc} + */ + @Override + @NonNull + public BoxedTypesPrivateFields mapFromCursor(@NonNull Cursor cursor) { + BoxedTypesPrivateFields object = new BoxedTypesPrivateFields(); + + if (!cursor.isNull(cursor.getColumnIndex("field1"))) { + object.setField1(cursor.getInt(cursor.getColumnIndex("field1")) == 1); + } + if (!cursor.isNull(cursor.getColumnIndex("field2"))) { + object.setField2(cursor.getShort(cursor.getColumnIndex("field2"))); + } + if (!cursor.isNull(cursor.getColumnIndex("field3"))) { + object.setField3(cursor.getInt(cursor.getColumnIndex("field3"))); + } + if (!cursor.isNull(cursor.getColumnIndex("field4"))) { + object.setField4(cursor.getLong(cursor.getColumnIndex("field4"))); + } + if (!cursor.isNull(cursor.getColumnIndex("field5"))) { + object.setField5(cursor.getFloat(cursor.getColumnIndex("field5"))); + } + if (!cursor.isNull(cursor.getColumnIndex("field6"))) { + object.setField6(cursor.getDouble(cursor.getColumnIndex("field6"))); + } + + return object; + } +} \ No newline at end of file diff --git a/storio-content-resolver-annotations-processor-test/src/test/resources/BoxedTypesPrivateFieldsStorIOContentResolverPutResolver.java b/storio-content-resolver-annotations-processor-test/src/test/resources/BoxedTypesPrivateFieldsStorIOContentResolverPutResolver.java new file mode 100644 index 000000000..8d0f71379 --- /dev/null +++ b/storio-content-resolver-annotations-processor-test/src/test/resources/BoxedTypesPrivateFieldsStorIOContentResolverPutResolver.java @@ -0,0 +1,53 @@ +package com.pushtorefresh.storio.contentresolver.annotations; + +import android.content.ContentValues; +import android.support.annotation.NonNull; +import com.pushtorefresh.storio.contentresolver.operations.put.DefaultPutResolver; +import com.pushtorefresh.storio.contentresolver.queries.InsertQuery; +import com.pushtorefresh.storio.contentresolver.queries.UpdateQuery; +import java.lang.Override; + +/** + * Generated resolver for Put Operation + */ +public class BoxedTypesPrivateFieldsStorIOContentResolverPutResolver extends DefaultPutResolver { + /** + * {@inheritDoc} + */ + @Override + @NonNull + public InsertQuery mapToInsertQuery(@NonNull BoxedTypesPrivateFields object) { + return InsertQuery.builder() + .uri("content://uri") + .build();} + + /** + * {@inheritDoc} + */ + @Override + @NonNull + public UpdateQuery mapToUpdateQuery(@NonNull BoxedTypesPrivateFields object) { + return UpdateQuery.builder() + .uri("content://uri") + .where("field4 = ?") + .whereArgs(object.getField4()) + .build();} + + /** + * {@inheritDoc} + */ + @Override + @NonNull + public ContentValues mapToContentValues(@NonNull BoxedTypesPrivateFields object) { + ContentValues contentValues = new ContentValues(6); + + contentValues.put("field1", object.getField1()); + contentValues.put("field2", object.getField2()); + contentValues.put("field3", object.getField3()); + contentValues.put("field4", object.getField4()); + contentValues.put("field5", object.getField5()); + contentValues.put("field6", object.getField6()); + + return contentValues; + } +} \ No newline at end of file diff --git a/storio-content-resolver-annotations-processor-test/src/test/resources/PrimitivePrivateFields.java b/storio-content-resolver-annotations-processor-test/src/test/resources/PrimitivePrivateFields.java new file mode 100644 index 000000000..e26cc37ba --- /dev/null +++ b/storio-content-resolver-annotations-processor-test/src/test/resources/PrimitivePrivateFields.java @@ -0,0 +1,93 @@ +package com.pushtorefresh.storio.contentresolver.annotations; + +@StorIOContentResolverType(uri = "content://uri") +public class PrimitivePrivateFields { + + @StorIOContentResolverColumn(name = "field1") + private boolean field1; + + @StorIOContentResolverColumn(name = "field2") + private short field2; + + @StorIOContentResolverColumn(name = "field3") + private int field3; + + @StorIOContentResolverColumn(name = "field4", key = true) + private long field4; + + @StorIOContentResolverColumn(name = "field5") + private float field5; + + @StorIOContentResolverColumn(name = "field6") + private double field6; + + @StorIOContentResolverColumn(name = "field7") + private String field7; + + @StorIOContentResolverColumn(name = "field8") + private byte[] field8; + + public boolean isField1() { + return field1; + } + + public void setField1(boolean field1) { + this.field1 = field1; + } + + public short getField2() { + return field2; + } + + public void setField2(short field2) { + this.field2 = field2; + } + + public int getField3() { + return field3; + } + + public void setField3(int field3) { + this.field3 = field3; + } + + public long getField4() { + return field4; + } + + public void setField4(long field4) { + this.field4 = field4; + } + + public float getField5() { + return field5; + } + + public void setField5(float field5) { + this.field5 = field5; + } + + public double getField6() { + return field6; + } + + public void setField6(double field6) { + this.field6 = field6; + } + + public String getField7() { + return field7; + } + + public void setField7(String field7) { + this.field7 = field7; + } + + public byte[] getField8() { + return field8; + } + + public void setField8(byte[] field8) { + this.field8 = field8; + } +} \ No newline at end of file diff --git a/storio-content-resolver-annotations-processor-test/src/test/resources/PrimitivePrivateFieldsContentResolverTypeMapping.java b/storio-content-resolver-annotations-processor-test/src/test/resources/PrimitivePrivateFieldsContentResolverTypeMapping.java new file mode 100644 index 000000000..7ab79e246 --- /dev/null +++ b/storio-content-resolver-annotations-processor-test/src/test/resources/PrimitivePrivateFieldsContentResolverTypeMapping.java @@ -0,0 +1,14 @@ +package com.pushtorefresh.storio.contentresolver.annotations; + +import com.pushtorefresh.storio.contentresolver.ContentResolverTypeMapping; + +/** + * Generated mapping with collection of resolvers + */ +public class PrimitivePrivateFieldsContentResolverTypeMapping extends ContentResolverTypeMapping { + public PrimitivePrivateFieldsContentResolverTypeMapping() { + super(new PrimitivePrivateFieldsStorIOContentResolverPutResolver(), + new PrimitivePrivateFieldsStorIOContentResolverGetResolver(), + new PrimitivePrivateFieldsStorIOContentResolverDeleteResolver()); + } +} diff --git a/storio-content-resolver-annotations-processor-test/src/test/resources/PrimitivePrivateFieldsStorIOContentResolverDeleteResolver.java b/storio-content-resolver-annotations-processor-test/src/test/resources/PrimitivePrivateFieldsStorIOContentResolverDeleteResolver.java new file mode 100644 index 000000000..7e0eda92f --- /dev/null +++ b/storio-content-resolver-annotations-processor-test/src/test/resources/PrimitivePrivateFieldsStorIOContentResolverDeleteResolver.java @@ -0,0 +1,23 @@ +package com.pushtorefresh.storio.contentresolver.annotations; + +import android.support.annotation.NonNull; +import com.pushtorefresh.storio.contentresolver.operations.delete.DefaultDeleteResolver; +import com.pushtorefresh.storio.contentresolver.queries.DeleteQuery; +import java.lang.Override; + +/** + * Generated resolver for Delete Operation + */ +public class PrimitivePrivateFieldsStorIOContentResolverDeleteResolver extends DefaultDeleteResolver { + /** + * {@inheritDoc} + */ + @Override + @NonNull + public DeleteQuery mapToDeleteQuery(@NonNull PrimitivePrivateFields object) { + return DeleteQuery.builder() + .uri("content://uri") + .where("field4 = ?") + .whereArgs(object.getField4()) + .build();} +} \ No newline at end of file diff --git a/storio-content-resolver-annotations-processor-test/src/test/resources/PrimitivePrivateFieldsStorIOContentResolverGetResolver.java b/storio-content-resolver-annotations-processor-test/src/test/resources/PrimitivePrivateFieldsStorIOContentResolverGetResolver.java new file mode 100644 index 000000000..002c4d6e6 --- /dev/null +++ b/storio-content-resolver-annotations-processor-test/src/test/resources/PrimitivePrivateFieldsStorIOContentResolverGetResolver.java @@ -0,0 +1,31 @@ +package com.pushtorefresh.storio.contentresolver.annotations; + +import android.database.Cursor; +import android.support.annotation.NonNull; +import com.pushtorefresh.storio.contentresolver.operations.get.DefaultGetResolver; +import java.lang.Override; + +/** + * Generated resolver for Get Operation + */ +public class PrimitivePrivateFieldsStorIOContentResolverGetResolver extends DefaultGetResolver { + /** + * {@inheritDoc} + */ + @Override + @NonNull + public PrimitivePrivateFields mapFromCursor(@NonNull Cursor cursor) { + PrimitivePrivateFields object = new PrimitivePrivateFields(); + + object.setField1(cursor.getInt(cursor.getColumnIndex("field1")) == 1); + object.setField2(cursor.getShort(cursor.getColumnIndex("field2"))); + object.setField3(cursor.getInt(cursor.getColumnIndex("field3"))); + object.setField4(cursor.getLong(cursor.getColumnIndex("field4"))); + object.setField5(cursor.getFloat(cursor.getColumnIndex("field5"))); + object.setField6(cursor.getDouble(cursor.getColumnIndex("field6"))); + object.setField7(cursor.getString(cursor.getColumnIndex("field7"))); + object.setField8(cursor.getBlob(cursor.getColumnIndex("field8"))); + + return object; + } +} \ No newline at end of file diff --git a/storio-content-resolver-annotations-processor-test/src/test/resources/PrimitivePrivateFieldsStorIOContentResolverPutResolver.java b/storio-content-resolver-annotations-processor-test/src/test/resources/PrimitivePrivateFieldsStorIOContentResolverPutResolver.java new file mode 100644 index 000000000..75d354988 --- /dev/null +++ b/storio-content-resolver-annotations-processor-test/src/test/resources/PrimitivePrivateFieldsStorIOContentResolverPutResolver.java @@ -0,0 +1,55 @@ +package com.pushtorefresh.storio.contentresolver.annotations; + +import android.content.ContentValues; +import android.support.annotation.NonNull; +import com.pushtorefresh.storio.contentresolver.operations.put.DefaultPutResolver; +import com.pushtorefresh.storio.contentresolver.queries.InsertQuery; +import com.pushtorefresh.storio.contentresolver.queries.UpdateQuery; +import java.lang.Override; + +/** + * Generated resolver for Put Operation + */ +public class PrimitivePrivateFieldsStorIOContentResolverPutResolver extends DefaultPutResolver { + /** + * {@inheritDoc} + */ + @Override + @NonNull + public InsertQuery mapToInsertQuery(@NonNull PrimitivePrivateFields object) { + return InsertQuery.builder() + .uri("content://uri") + .build();} + + /** + * {@inheritDoc} + */ + @Override + @NonNull + public UpdateQuery mapToUpdateQuery(@NonNull PrimitivePrivateFields object) { + return UpdateQuery.builder() + .uri("content://uri") + .where("field4 = ?") + .whereArgs(object.getField4()) + .build();} + + /** + * {@inheritDoc} + */ + @Override + @NonNull + public ContentValues mapToContentValues(@NonNull PrimitivePrivateFields object) { + ContentValues contentValues = new ContentValues(8); + + contentValues.put("field1", object.isField1()); + contentValues.put("field2", object.getField2()); + contentValues.put("field3", object.getField3()); + contentValues.put("field4", object.getField4()); + contentValues.put("field5", object.getField5()); + contentValues.put("field6", object.getField6()); + contentValues.put("field7", object.getField7()); + contentValues.put("field8", object.getField8()); + + return contentValues; + } +} \ No newline at end of file diff --git a/storio-content-resolver-annotations-processor-test/src/test/resources/PrivateFieldWithIsGetter.java b/storio-content-resolver-annotations-processor-test/src/test/resources/PrivateFieldWithIsGetter.java new file mode 100644 index 000000000..c7dd5c7fb --- /dev/null +++ b/storio-content-resolver-annotations-processor-test/src/test/resources/PrivateFieldWithIsGetter.java @@ -0,0 +1,27 @@ +package com.pushtorefresh.storio.contentresolver.annotations; + +@StorIOContentResolverType(uri = "content://uri") +public class PrivateFieldWithIsGetter { + + @StorIOContentResolverColumn(name = "id", key = true) + private long id; + + @StorIOContentResolverColumn(name = "flag") + private boolean flag; + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public boolean isFlag() { + return flag; + } + + public void setFlag(boolean flag) { + this.flag = flag; + } +} \ No newline at end of file diff --git a/storio-content-resolver-annotations-processor-test/src/test/resources/PrivateFieldWithNameStartingWithIs.java b/storio-content-resolver-annotations-processor-test/src/test/resources/PrivateFieldWithNameStartingWithIs.java new file mode 100644 index 000000000..36e5fe34c --- /dev/null +++ b/storio-content-resolver-annotations-processor-test/src/test/resources/PrivateFieldWithNameStartingWithIs.java @@ -0,0 +1,27 @@ +package com.pushtorefresh.storio.contentresolver.annotations; + +@StorIOContentResolverType(uri = "content://uri") +public class PrivateFieldWithNameStartingWithIs { + + @StorIOContentResolverColumn(name = "id", key = true) + private long id; + + @StorIOContentResolverColumn(name = "is_flag") + private boolean isFlag; + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public boolean isFlag() { + return isFlag; + } + + public void setFlag(boolean flag) { + isFlag = flag; + } +} \ No newline at end of file diff --git a/storio-content-resolver-annotations-processor-test/src/test/resources/PrivateField.java b/storio-content-resolver-annotations-processor-test/src/test/resources/PrivateFieldWithoutAccessors.java similarity index 81% rename from storio-content-resolver-annotations-processor-test/src/test/resources/PrivateField.java rename to storio-content-resolver-annotations-processor-test/src/test/resources/PrivateFieldWithoutAccessors.java index 9d9ead6bb..8a2f5a37d 100644 --- a/storio-content-resolver-annotations-processor-test/src/test/resources/PrivateField.java +++ b/storio-content-resolver-annotations-processor-test/src/test/resources/PrivateFieldWithoutAccessors.java @@ -1,7 +1,7 @@ package com.pushtorefresh.storio.contentresolver.annotations; @StorIOContentResolverType(uri = "content://uri") -public class PrivateField { +public class PrivateFieldWithoutAccessors { @StorIOContentResolverColumn(name = "id", key = true) private long id; diff --git a/storio-content-resolver-annotations-processor-test/src/test/resources/PrivateFieldWithoutGetter.java b/storio-content-resolver-annotations-processor-test/src/test/resources/PrivateFieldWithoutGetter.java new file mode 100644 index 000000000..051a08f00 --- /dev/null +++ b/storio-content-resolver-annotations-processor-test/src/test/resources/PrivateFieldWithoutGetter.java @@ -0,0 +1,12 @@ +package com.pushtorefresh.storio.contentresolver.annotations; + +@StorIOContentResolverType(uri = "content://uri") +public class PrivateFieldWithoutGetter { + + @StorIOContentResolverColumn(name = "id", key = true) + private long id; + + public void setId(long id) { + this.id = id; + } +} \ No newline at end of file diff --git a/storio-content-resolver-annotations-processor-test/src/test/resources/PrivateFieldWithoutSetter.java b/storio-content-resolver-annotations-processor-test/src/test/resources/PrivateFieldWithoutSetter.java new file mode 100644 index 000000000..ce27776dc --- /dev/null +++ b/storio-content-resolver-annotations-processor-test/src/test/resources/PrivateFieldWithoutSetter.java @@ -0,0 +1,12 @@ +package com.pushtorefresh.storio.contentresolver.annotations; + +@StorIOContentResolverType(uri = "content://uri") +public class PrivateFieldWithoutSetter { + + @StorIOContentResolverColumn(name = "id", key = true) + private long id; + + public long getId() { + return id; + } +} \ No newline at end of file diff --git a/storio-content-resolver-annotations-processor-test/src/test/resources/PrivateMethod.java b/storio-content-resolver-annotations-processor-test/src/test/resources/PrivateMethod.java new file mode 100644 index 000000000..532222004 --- /dev/null +++ b/storio-content-resolver-annotations-processor-test/src/test/resources/PrivateMethod.java @@ -0,0 +1,10 @@ +package com.pushtorefresh.storio.contentresolver.annotations; + +@StorIOContentResolverType(uri = "content://uri") +public class PrivateMethod { + + @StorIOContentResolverColumn(name = "id", key = true) + private long id() { + return 0; + } +} \ No newline at end of file diff --git a/storio-content-resolver-annotations-processor/src/main/kotlin/com/pushtorefresh/storio/contentresolver/annotations/processor/StorIOContentResolverProcessor.kt b/storio-content-resolver-annotations-processor/src/main/kotlin/com/pushtorefresh/storio/contentresolver/annotations/processor/StorIOContentResolverProcessor.kt index 8bf64b71f..f5ac9dc14 100644 --- a/storio-content-resolver-annotations-processor/src/main/kotlin/com/pushtorefresh/storio/contentresolver/annotations/processor/StorIOContentResolverProcessor.kt +++ b/storio-content-resolver-annotations-processor/src/main/kotlin/com/pushtorefresh/storio/contentresolver/annotations/processor/StorIOContentResolverProcessor.kt @@ -16,10 +16,7 @@ import com.pushtorefresh.storio.contentresolver.annotations.processor.introspect import com.pushtorefresh.storio.contentresolver.annotations.processor.introspection.StorIOContentResolverCreatorMeta import com.pushtorefresh.storio.contentresolver.annotations.processor.introspection.StorIOContentResolverTypeMeta import javax.annotation.processing.RoundEnvironment -import javax.lang.model.element.Element -import javax.lang.model.element.ElementKind -import javax.lang.model.element.ExecutableElement -import javax.lang.model.element.TypeElement +import javax.lang.model.element.* import javax.lang.model.util.Elements import javax.tools.Diagnostic.Kind.WARNING @@ -172,6 +169,13 @@ open class StorIOContentResolverProcessor : StorIOAnnotationsProcessor { // otherwise -> if primitive and value from cursor null -> fail early if (isBoxed) builder.beginControlFlow("if (!cursor.isNull(\$L))", columnIndex) - builder.addStatement("object.\$L = cursor.\$L", columnMeta.elementName, getFromCursor) + if (columnMeta.needAccessors) { + builder.addStatement("object.\$L(cursor.\$L)", columnMeta.setter, getFromCursor) + } else { + builder.addStatement("object.\$L = cursor.\$L", columnMeta.elementName, getFromCursor) + } if (isBoxed) builder.endControlFlow() } diff --git a/storio-content-resolver-annotations-processor/src/main/kotlin/com/pushtorefresh/storio/contentresolver/annotations/processor/generate/PutResolverGenerator.kt b/storio-content-resolver-annotations-processor/src/main/kotlin/com/pushtorefresh/storio/contentresolver/annotations/processor/generate/PutResolverGenerator.kt index 813ab4a23..165123364 100644 --- a/storio-content-resolver-annotations-processor/src/main/kotlin/com/pushtorefresh/storio/contentresolver/annotations/processor/generate/PutResolverGenerator.kt +++ b/storio-content-resolver-annotations-processor/src/main/kotlin/com/pushtorefresh/storio/contentresolver/annotations/processor/generate/PutResolverGenerator.kt @@ -67,11 +67,11 @@ object PutResolverGenerator : Generator { .addAnnotation(ANDROID_NON_NULL_ANNOTATION_CLASS_NAME) .build()) .addCode("""return UpdateQuery.builder() -$INDENT.uri(${"$"}S) -$INDENT.where(${"$"}S) -$INDENT.whereArgs(${"$"}L) -$INDENT.build(); -""", + $INDENT.uri(${"$"}S) + $INDENT.where(${"$"}S) + $INDENT.whereArgs(${"$"}L) + $INDENT.build(); + """.trimIndent(), updateUri, where[QueryGenerator.WHERE_CLAUSE], where[QueryGenerator.WHERE_ARGS]) @@ -88,19 +88,15 @@ $INDENT.build(); .addParameter(ParameterSpec.builder(className, "object") .addAnnotation(ANDROID_NON_NULL_ANNOTATION_CLASS_NAME) .build()) - .addStatement("ContentValues contentValues = new ContentValues(\$L)", - typeMeta - .columns.size) + .addStatement("ContentValues contentValues = new ContentValues(\$L)", typeMeta.columns.size) .addCode("\n") - for (columnMeta in typeMeta - .columns.values) { + typeMeta.columns.values.forEach { columnMeta -> val ignoreNull = columnMeta.storIOColumn.ignoreNull if (ignoreNull) { - builder.beginControlFlow("if (object.\$L != null)", "${columnMeta.elementName}${if (columnMeta.isMethod) "()" else ""}") + builder.beginControlFlow("if (object.\$L != null)", columnMeta.contextAwareName) } - builder.addStatement("contentValues.put(\$S, object.\$L)", columnMeta.storIOColumn.name, "${columnMeta.elementName}${if (columnMeta.isMethod) "()" else ""}" - ) + builder.addStatement("contentValues.put(\$S, object.\$L)", columnMeta.storIOColumn.name, columnMeta.contextAwareName) if (ignoreNull) builder.endControlFlow() } diff --git a/storio-content-resolver-annotations-processor/src/main/kotlin/com/pushtorefresh/storio/contentresolver/annotations/processor/generate/QueryGenerator.kt b/storio-content-resolver-annotations-processor/src/main/kotlin/com/pushtorefresh/storio/contentresolver/annotations/processor/generate/QueryGenerator.kt index 5a8641277..822851382 100644 --- a/storio-content-resolver-annotations-processor/src/main/kotlin/com/pushtorefresh/storio/contentresolver/annotations/processor/generate/QueryGenerator.kt +++ b/storio-content-resolver-annotations-processor/src/main/kotlin/com/pushtorefresh/storio/contentresolver/annotations/processor/generate/QueryGenerator.kt @@ -23,7 +23,7 @@ object QueryGenerator { whereArgs .append(varName) .append(".") - .append(columnMeta.elementName) + .append(columnMeta.contextAwareName) } else { whereClause .append(" AND ") @@ -34,11 +34,8 @@ object QueryGenerator { .append(", ") .append(varName) .append(".") - .append(columnMeta.elementName) + .append(columnMeta.contextAwareName) } - - if (columnMeta.isMethod) whereArgs.append("()") - i++ } } diff --git a/storio-content-resolver-annotations-processor/src/main/kotlin/com/pushtorefresh/storio/contentresolver/annotations/processor/introspection/StorIOContentResolverColumnMeta.kt b/storio-content-resolver-annotations-processor/src/main/kotlin/com/pushtorefresh/storio/contentresolver/annotations/processor/introspection/StorIOContentResolverColumnMeta.kt index cea81757a..cc9518248 100644 --- a/storio-content-resolver-annotations-processor/src/main/kotlin/com/pushtorefresh/storio/contentresolver/annotations/processor/introspection/StorIOContentResolverColumnMeta.kt +++ b/storio-content-resolver-annotations-processor/src/main/kotlin/com/pushtorefresh/storio/contentresolver/annotations/processor/introspection/StorIOContentResolverColumnMeta.kt @@ -11,10 +11,14 @@ class StorIOContentResolverColumnMeta( element: Element, fieldName: String, javaType: JavaType, - storIOColumn: StorIOContentResolverColumn) + storIOColumn: StorIOContentResolverColumn, + getter: String? = null, + setter: String? = null) : StorIOColumnMeta( enclosingElement, element, fieldName, javaType, - storIOColumn) \ No newline at end of file + storIOColumn, + getter, + setter) \ No newline at end of file diff --git a/storio-sqlite-annotations-processor-test/src/test/java/com/pushtorefresh/storio/sqlite/annotations/processor/test/StorIOSQLiteAnnotationsProcessorTest.java b/storio-sqlite-annotations-processor-test/src/test/java/com/pushtorefresh/storio/sqlite/annotations/processor/test/StorIOSQLiteAnnotationsProcessorTest.java index ad1cc0775..277bf873a 100644 --- a/storio-sqlite-annotations-processor-test/src/test/java/com/pushtorefresh/storio/sqlite/annotations/processor/test/StorIOSQLiteAnnotationsProcessorTest.java +++ b/storio-sqlite-annotations-processor-test/src/test/java/com/pushtorefresh/storio/sqlite/annotations/processor/test/StorIOSQLiteAnnotationsProcessorTest.java @@ -71,14 +71,25 @@ public void shouldNotCompileIfAnnotatedFieldInsideNotAnnotatedClass() { } @Test - public void shouldNotCompileIfAnnotatedFieldIsPrivate() { - JavaFileObject model = JavaFileObjects.forResource("PrivateField.java"); + public void shouldNotCompileIfAnnotatedFieldIsPrivateAndDoesNotHaveAccessors() { + JavaFileObject model = JavaFileObjects.forResource("PrivateFieldWithoutAccessors.java"); assert_().about(javaSource()) .that(model) .processedWith(new StorIOSQLiteProcessor()) .failsToCompile() - .withErrorContaining("StorIOSQLiteColumn can not be applied to private field or method: id"); + .withErrorContaining("StorIOSQLiteColumn can not be applied to private field without corresponding getter and setter or private method: id"); + } + + @Test + public void shouldNotCompileIfAnnotatedMethodIsPrivate() { + JavaFileObject model = JavaFileObjects.forResource("PrivateMethod.java"); + + assert_().about(javaSource()) + .that(model) + .processedWith(new StorIOSQLiteProcessor()) + .failsToCompile() + .withErrorContaining("StorIOSQLiteColumn can not be applied to private field without corresponding getter and setter or private method: id"); } @Test @@ -397,8 +408,7 @@ public void shouldCompileWithMethodsReturningBoxedTypesAndConstructorAsCreator() } @Test - public void - shouldCompileWithMethodsReturningBoxedTypesAndMarkedAsIgnoreNullAndConstructorAsCreator() { + public void shouldCompileWithMethodsReturningBoxedTypesAndMarkedAsIgnoreNullAndConstructorAsCreator() { JavaFileObject model = JavaFileObjects.forResource("BoxedTypesMethodsConstructorIgnoreNull.java"); JavaFileObject generatedTypeMapping = JavaFileObjects.forResource("BoxedTypesMethodsConstructorIgnoreNullSQLiteTypeMapping.java"); @@ -449,8 +459,7 @@ public void shouldCompileWithMethodsReturningBoxedTypesAndFactoryMethodAsCreator } @Test - public void - shouldCompileWithMethodsReturningBoxedTypesAndMarkedAsIgnoreNullAndFactoryMethodAsCreator() { + public void shouldCompileWithMethodsReturningBoxedTypesAndMarkedAsIgnoreNullAndFactoryMethodAsCreator() { JavaFileObject model = JavaFileObjects.forResource("BoxedTypesMethodsFactoryMethodIgnoreNull.java"); JavaFileObject generatedTypeMapping = JavaFileObjects.forResource("BoxedTypesMethodsFactoryMethodIgnoreNullSQLiteTypeMapping.java"); @@ -466,4 +475,96 @@ public void shouldCompileWithMethodsReturningBoxedTypesAndFactoryMethodAsCreator .generatesSources(generatedTypeMapping, generatedDeleteResolver, generatedGetResolver, generatedPutResolver); } -} \ No newline at end of file + @Test + public void shouldNotCompileIfAnnotatedFieldIsPrivateAndDoesNotHaveSetter() { + JavaFileObject model = JavaFileObjects.forResource("PrivateFieldWithoutSetter.java"); + + assert_().about(javaSource()) + .that(model) + .processedWith(new StorIOSQLiteProcessor()) + .failsToCompile() + .withErrorContaining("StorIOSQLiteColumn can not be applied to private field without corresponding getter and setter or private method: id"); + } + + @Test + public void shouldNotCompileIfAnnotatedFieldIsPrivateAndDoesNotHaveGetter() { + JavaFileObject model = JavaFileObjects.forResource("PrivateFieldWithoutGetter.java"); + + assert_().about(javaSource()) + .that(model) + .processedWith(new StorIOSQLiteProcessor()) + .failsToCompile() + .withErrorContaining("StorIOSQLiteColumn can not be applied to private field without corresponding getter and setter or private method: id"); + } + + @Test + public void shouldCompileIfAnnotatedFieldIsPrivateAndHasIsGetter() { + JavaFileObject model = JavaFileObjects.forResource("PrivateFieldWithIsGetter.java"); + + assert_().about(javaSource()) + .that(model) + .processedWith(new StorIOSQLiteProcessor()) + .compilesWithoutError(); + } + + @Test + public void shouldCompileIfAnnotatedFieldIsPrivateAndHasNameStartingWithIs() { + JavaFileObject model = JavaFileObjects.forResource("PrivateFieldWithNameStartingWithIs.java"); + + assert_().about(javaSource()) + .that(model) + .processedWith(new StorIOSQLiteProcessor()) + .compilesWithoutError(); + } + + @Test + public void shouldCompileWithPrivatePrimitiveFieldsWithCorrespondingAccessors() { + JavaFileObject model = JavaFileObjects.forResource("PrimitivePrivateFields.java"); + + JavaFileObject generatedTypeMapping = JavaFileObjects.forResource("PrimitivePrivateFieldsSQLiteTypeMapping.java"); + JavaFileObject generatedDeleteResolver = JavaFileObjects.forResource("PrimitivePrivateFieldsStorIOSQLiteDeleteResolver.java"); + JavaFileObject generatedGetResolver = JavaFileObjects.forResource("PrimitivePrivateFieldsStorIOSQLiteGetResolver.java"); + JavaFileObject generatedPutResolver = JavaFileObjects.forResource("PrimitivePrivateFieldsStorIOSQLitePutResolver.java"); + + assert_().about(javaSource()) + .that(model) + .processedWith(new StorIOSQLiteProcessor()) + .compilesWithoutError() + .and() + .generatesSources(generatedTypeMapping, generatedDeleteResolver, generatedGetResolver, generatedPutResolver); + } + + @Test + public void shouldCompileWithPrivateBoxedTypesFieldsWithCorrespondingAccessors() { + JavaFileObject model = JavaFileObjects.forResource("BoxedTypesPrivateFields.java"); + + JavaFileObject generatedTypeMapping = JavaFileObjects.forResource("BoxedTypesPrivateFieldsSQLiteTypeMapping.java"); + JavaFileObject generatedDeleteResolver = JavaFileObjects.forResource("BoxedTypesPrivateFieldsStorIOSQLiteDeleteResolver.java"); + JavaFileObject generatedGetResolver = JavaFileObjects.forResource("BoxedTypesPrivateFieldsStorIOSQLiteGetResolver.java"); + JavaFileObject generatedPutResolver = JavaFileObjects.forResource("BoxedTypesPrivateFieldsStorIOSQLitePutResolver.java"); + + assert_().about(javaSource()) + .that(model) + .processedWith(new StorIOSQLiteProcessor()) + .compilesWithoutError() + .and() + .generatesSources(generatedTypeMapping, generatedDeleteResolver, generatedGetResolver, generatedPutResolver); + } + + @Test + public void shouldCompileWithPrivateBoxedTypesFieldsWithCorrespondingAccessorsAndMarkedAsIgnoreNull() { + JavaFileObject model = JavaFileObjects.forResource("BoxedTypesPrivateFieldsIgnoreNull.java"); + + JavaFileObject generatedTypeMapping = JavaFileObjects.forResource("BoxedTypesPrivateFieldsIgnoreNullSQLiteTypeMapping.java"); + JavaFileObject generatedDeleteResolver = JavaFileObjects.forResource("BoxedTypesPrivateFieldsIgnoreNullStorIOSQLiteDeleteResolver.java"); + JavaFileObject generatedGetResolver = JavaFileObjects.forResource("BoxedTypesPrivateFieldsIgnoreNullStorIOSQLiteGetResolver.java"); + JavaFileObject generatedPutResolver = JavaFileObjects.forResource("BoxedTypesPrivateFieldsIgnoreNullStorIOSQLitePutResolver.java"); + + assert_().about(javaSource()) + .that(model) + .processedWith(new StorIOSQLiteProcessor()) + .compilesWithoutError() + .and() + .generatesSources(generatedTypeMapping, generatedDeleteResolver, generatedGetResolver, generatedPutResolver); + } +} diff --git a/storio-sqlite-annotations-processor-test/src/test/resources/BoxedTypesPrivateFields.java b/storio-sqlite-annotations-processor-test/src/test/resources/BoxedTypesPrivateFields.java new file mode 100644 index 000000000..4c518fb1f --- /dev/null +++ b/storio-sqlite-annotations-processor-test/src/test/resources/BoxedTypesPrivateFields.java @@ -0,0 +1,71 @@ +package com.pushtorefresh.storio.sqlite.annotations; + +@StorIOSQLiteType(table = "table") +public class BoxedTypesPrivateFields { + + @StorIOSQLiteColumn(name = "field1") + private Boolean field1; + + @StorIOSQLiteColumn(name = "field2") + private Short field2; + + @StorIOSQLiteColumn(name = "field3") + private Integer field3; + + @StorIOSQLiteColumn(name = "field4", key = true) + private Long field4; + + @StorIOSQLiteColumn(name = "field5") + private Float field5; + + @StorIOSQLiteColumn(name = "field6") + private Double field6; + + public Boolean getField1() { + return field1; + } + + public void setField1(Boolean field1) { + this.field1 = field1; + } + + public Short getField2() { + return field2; + } + + public void setField2(Short field2) { + this.field2 = field2; + } + + public Integer getField3() { + return field3; + } + + public void setField3(Integer field3) { + this.field3 = field3; + } + + public Long getField4() { + return field4; + } + + public void setField4(Long field4) { + this.field4 = field4; + } + + public Float getField5() { + return field5; + } + + public void setField5(Float field5) { + this.field5 = field5; + } + + public Double getField6() { + return field6; + } + + public void setField6(Double field6) { + this.field6 = field6; + } +} \ No newline at end of file diff --git a/storio-sqlite-annotations-processor-test/src/test/resources/BoxedTypesPrivateFieldsIgnoreNull.java b/storio-sqlite-annotations-processor-test/src/test/resources/BoxedTypesPrivateFieldsIgnoreNull.java new file mode 100644 index 000000000..acd167384 --- /dev/null +++ b/storio-sqlite-annotations-processor-test/src/test/resources/BoxedTypesPrivateFieldsIgnoreNull.java @@ -0,0 +1,71 @@ +package com.pushtorefresh.storio.sqlite.annotations; + +@StorIOSQLiteType(table = "table") +public class BoxedTypesPrivateFieldsIgnoreNull { + + @StorIOSQLiteColumn(name = "field1", ignoreNull = true) + private Boolean field1; + + @StorIOSQLiteColumn(name = "field2", ignoreNull = true) + private Short field2; + + @StorIOSQLiteColumn(name = "field3", ignoreNull = true) + private Integer field3; + + @StorIOSQLiteColumn(name = "field4", key = true, ignoreNull = true) + private Long field4; + + @StorIOSQLiteColumn(name = "field5", ignoreNull = true) + private Float field5; + + @StorIOSQLiteColumn(name = "field6", ignoreNull = true) + private Double field6; + + public Boolean getField1() { + return field1; + } + + public void setField1(Boolean field1) { + this.field1 = field1; + } + + public Short getField2() { + return field2; + } + + public void setField2(Short field2) { + this.field2 = field2; + } + + public Integer getField3() { + return field3; + } + + public void setField3(Integer field3) { + this.field3 = field3; + } + + public Long getField4() { + return field4; + } + + public void setField4(Long field4) { + this.field4 = field4; + } + + public Float getField5() { + return field5; + } + + public void setField5(Float field5) { + this.field5 = field5; + } + + public Double getField6() { + return field6; + } + + public void setField6(Double field6) { + this.field6 = field6; + } +} \ No newline at end of file diff --git a/storio-sqlite-annotations-processor-test/src/test/resources/BoxedTypesPrivateFieldsIgnoreNullSQLiteTypeMapping.java b/storio-sqlite-annotations-processor-test/src/test/resources/BoxedTypesPrivateFieldsIgnoreNullSQLiteTypeMapping.java new file mode 100644 index 000000000..f9f8a0bea --- /dev/null +++ b/storio-sqlite-annotations-processor-test/src/test/resources/BoxedTypesPrivateFieldsIgnoreNullSQLiteTypeMapping.java @@ -0,0 +1,14 @@ +package com.pushtorefresh.storio.sqlite.annotations; + +import com.pushtorefresh.storio.sqlite.SQLiteTypeMapping; + +/** + * Generated mapping with collection of resolvers. + */ +public class BoxedTypesPrivateFieldsIgnoreNullSQLiteTypeMapping extends SQLiteTypeMapping { + public BoxedTypesPrivateFieldsIgnoreNullSQLiteTypeMapping() { + super(new BoxedTypesPrivateFieldsIgnoreNullStorIOSQLitePutResolver(), + new BoxedTypesPrivateFieldsIgnoreNullStorIOSQLiteGetResolver(), + new BoxedTypesPrivateFieldsIgnoreNullStorIOSQLiteDeleteResolver()); + } +} \ No newline at end of file diff --git a/storio-sqlite-annotations-processor-test/src/test/resources/BoxedTypesPrivateFieldsIgnoreNullStorIOSQLiteDeleteResolver.java b/storio-sqlite-annotations-processor-test/src/test/resources/BoxedTypesPrivateFieldsIgnoreNullStorIOSQLiteDeleteResolver.java new file mode 100644 index 000000000..8e23e4c8e --- /dev/null +++ b/storio-sqlite-annotations-processor-test/src/test/resources/BoxedTypesPrivateFieldsIgnoreNullStorIOSQLiteDeleteResolver.java @@ -0,0 +1,23 @@ +package com.pushtorefresh.storio.sqlite.annotations; + +import android.support.annotation.NonNull; +import com.pushtorefresh.storio.sqlite.operations.delete.DefaultDeleteResolver; +import com.pushtorefresh.storio.sqlite.queries.DeleteQuery; +import java.lang.Override; + +/** + * Generated resolver for Delete Operation. + */ +public class BoxedTypesPrivateFieldsIgnoreNullStorIOSQLiteDeleteResolver extends DefaultDeleteResolver { + /** + * {@inheritDoc} + */ + @Override + @NonNull + public DeleteQuery mapToDeleteQuery(@NonNull BoxedTypesPrivateFieldsIgnoreNull object) { + return DeleteQuery.builder() + .table("table") + .where("field4 = ?") + .whereArgs(object.getField4()) + .build();} +} \ No newline at end of file diff --git a/storio-sqlite-annotations-processor-test/src/test/resources/BoxedTypesPrivateFieldsIgnoreNullStorIOSQLiteGetResolver.java b/storio-sqlite-annotations-processor-test/src/test/resources/BoxedTypesPrivateFieldsIgnoreNullStorIOSQLiteGetResolver.java new file mode 100644 index 000000000..d957b3005 --- /dev/null +++ b/storio-sqlite-annotations-processor-test/src/test/resources/BoxedTypesPrivateFieldsIgnoreNullStorIOSQLiteGetResolver.java @@ -0,0 +1,41 @@ +package com.pushtorefresh.storio.sqlite.annotations; + +import android.database.Cursor; +import android.support.annotation.NonNull; +import com.pushtorefresh.storio.sqlite.operations.get.DefaultGetResolver; +import java.lang.Override; + +/** + * Generated resolver for Get Operation. + */ +public class BoxedTypesPrivateFieldsIgnoreNullStorIOSQLiteGetResolver extends DefaultGetResolver { + /** + * {@inheritDoc} + */ + @Override + @NonNull + public BoxedTypesPrivateFieldsIgnoreNull mapFromCursor(@NonNull Cursor cursor) { + BoxedTypesPrivateFieldsIgnoreNull object = new BoxedTypesPrivateFieldsIgnoreNull(); + + if (!cursor.isNull(cursor.getColumnIndex("field1"))) { + object.setField1(cursor.getInt(cursor.getColumnIndex("field1")) == 1); + } + if (!cursor.isNull(cursor.getColumnIndex("field2"))) { + object.setField2(cursor.getShort(cursor.getColumnIndex("field2"))); + } + if (!cursor.isNull(cursor.getColumnIndex("field3"))) { + object.setField3(cursor.getInt(cursor.getColumnIndex("field3"))); + } + if (!cursor.isNull(cursor.getColumnIndex("field4"))) { + object.setField4(cursor.getLong(cursor.getColumnIndex("field4"))); + } + if (!cursor.isNull(cursor.getColumnIndex("field5"))) { + object.setField5(cursor.getFloat(cursor.getColumnIndex("field5"))); + } + if (!cursor.isNull(cursor.getColumnIndex("field6"))) { + object.setField6(cursor.getDouble(cursor.getColumnIndex("field6"))); + } + + return object; + } +} \ No newline at end of file diff --git a/storio-sqlite-annotations-processor-test/src/test/resources/BoxedTypesPrivateFieldsIgnoreNullStorIOSQLitePutResolver.java b/storio-sqlite-annotations-processor-test/src/test/resources/BoxedTypesPrivateFieldsIgnoreNullStorIOSQLitePutResolver.java new file mode 100644 index 000000000..8de927a24 --- /dev/null +++ b/storio-sqlite-annotations-processor-test/src/test/resources/BoxedTypesPrivateFieldsIgnoreNullStorIOSQLitePutResolver.java @@ -0,0 +1,66 @@ +package com.pushtorefresh.storio.sqlite.annotations; + +import android.content.ContentValues; +import android.support.annotation.NonNull; +import com.pushtorefresh.storio.sqlite.operations.put.DefaultPutResolver; +import com.pushtorefresh.storio.sqlite.queries.InsertQuery; +import com.pushtorefresh.storio.sqlite.queries.UpdateQuery; +import java.lang.Override; + +/** + * Generated resolver for Put Operation. + */ +public class BoxedTypesPrivateFieldsIgnoreNullStorIOSQLitePutResolver extends DefaultPutResolver { + /** + * {@inheritDoc} + */ + @Override + @NonNull + public InsertQuery mapToInsertQuery(@NonNull BoxedTypesPrivateFieldsIgnoreNull object) { + return InsertQuery.builder() + .table("table") + .build();} + + /** + * {@inheritDoc} + */ + @Override + @NonNull + public UpdateQuery mapToUpdateQuery(@NonNull BoxedTypesPrivateFieldsIgnoreNull object) { + return UpdateQuery.builder() + .table("table") + .where("field4 = ?") + .whereArgs(object.getField4()) + .build(); + } + + /** + * {@inheritDoc} + */ + @Override + @NonNull + public ContentValues mapToContentValues(@NonNull BoxedTypesPrivateFieldsIgnoreNull object) { + ContentValues contentValues = new ContentValues(6); + + if (object.getField1() != null) { + contentValues.put("field1", object.getField1()); + } + if (object.getField2() != null) { + contentValues.put("field2", object.getField2()); + } + if (object.getField3() != null) { + contentValues.put("field3", object.getField3()); + } + if (object.getField4() != null) { + contentValues.put("field4", object.getField4()); + } + if (object.getField5() != null) { + contentValues.put("field5", object.getField5()); + } + if (object.getField6() != null) { + contentValues.put("field6", object.getField6()); + } + + return contentValues; + } +} \ No newline at end of file diff --git a/storio-sqlite-annotations-processor-test/src/test/resources/BoxedTypesPrivateFieldsSQLiteTypeMapping.java b/storio-sqlite-annotations-processor-test/src/test/resources/BoxedTypesPrivateFieldsSQLiteTypeMapping.java new file mode 100644 index 000000000..84e9dd3c6 --- /dev/null +++ b/storio-sqlite-annotations-processor-test/src/test/resources/BoxedTypesPrivateFieldsSQLiteTypeMapping.java @@ -0,0 +1,14 @@ +package com.pushtorefresh.storio.sqlite.annotations; + +import com.pushtorefresh.storio.sqlite.SQLiteTypeMapping; + +/** + * Generated mapping with collection of resolvers. + */ +public class BoxedTypesPrivateFieldsSQLiteTypeMapping extends SQLiteTypeMapping { + public BoxedTypesPrivateFieldsSQLiteTypeMapping() { + super(new BoxedTypesPrivateFieldsStorIOSQLitePutResolver(), + new BoxedTypesPrivateFieldsStorIOSQLiteGetResolver(), + new BoxedTypesPrivateFieldsStorIOSQLiteDeleteResolver()); + } +} \ No newline at end of file diff --git a/storio-sqlite-annotations-processor-test/src/test/resources/BoxedTypesPrivateFieldsStorIOSQLiteDeleteResolver.java b/storio-sqlite-annotations-processor-test/src/test/resources/BoxedTypesPrivateFieldsStorIOSQLiteDeleteResolver.java new file mode 100644 index 000000000..13dc6e8bb --- /dev/null +++ b/storio-sqlite-annotations-processor-test/src/test/resources/BoxedTypesPrivateFieldsStorIOSQLiteDeleteResolver.java @@ -0,0 +1,24 @@ +package com.pushtorefresh.storio.sqlite.annotations; + +import android.support.annotation.NonNull; +import com.pushtorefresh.storio.sqlite.operations.delete.DefaultDeleteResolver; +import com.pushtorefresh.storio.sqlite.queries.DeleteQuery; +import java.lang.Override; + +/** + * Generated resolver for Delete Operation. + */ +public class BoxedTypesPrivateFieldsStorIOSQLiteDeleteResolver extends DefaultDeleteResolver { + /** + * {@inheritDoc} + */ + @Override + @NonNull + public DeleteQuery mapToDeleteQuery(@NonNull BoxedTypesPrivateFields object) { + return DeleteQuery.builder() + .table("table") + .where("field4 = ?") + .whereArgs(object.getField4()) + .build();} +} + diff --git a/storio-sqlite-annotations-processor-test/src/test/resources/BoxedTypesPrivateFieldsStorIOSQLiteGetResolver.java b/storio-sqlite-annotations-processor-test/src/test/resources/BoxedTypesPrivateFieldsStorIOSQLiteGetResolver.java new file mode 100644 index 000000000..770866f57 --- /dev/null +++ b/storio-sqlite-annotations-processor-test/src/test/resources/BoxedTypesPrivateFieldsStorIOSQLiteGetResolver.java @@ -0,0 +1,41 @@ +package com.pushtorefresh.storio.sqlite.annotations; + +import android.database.Cursor; +import android.support.annotation.NonNull; +import com.pushtorefresh.storio.sqlite.operations.get.DefaultGetResolver; +import java.lang.Override; + +/** + * Generated resolver for Get Operation. + */ +public class BoxedTypesPrivateFieldsStorIOSQLiteGetResolver extends DefaultGetResolver { + /** + * {@inheritDoc} + */ + @Override + @NonNull + public BoxedTypesPrivateFields mapFromCursor(@NonNull Cursor cursor) { + BoxedTypesPrivateFields object = new BoxedTypesPrivateFields(); + + if (!cursor.isNull(cursor.getColumnIndex("field1"))) { + object.setField1(cursor.getInt(cursor.getColumnIndex("field1")) == 1); + } + if (!cursor.isNull(cursor.getColumnIndex("field2"))) { + object.setField2(cursor.getShort(cursor.getColumnIndex("field2"))); + } + if (!cursor.isNull(cursor.getColumnIndex("field3"))) { + object.setField3(cursor.getInt(cursor.getColumnIndex("field3"))); + } + if (!cursor.isNull(cursor.getColumnIndex("field4"))) { + object.setField4(cursor.getLong(cursor.getColumnIndex("field4"))); + } + if (!cursor.isNull(cursor.getColumnIndex("field5"))) { + object.setField5(cursor.getFloat(cursor.getColumnIndex("field5"))); + } + if (!cursor.isNull(cursor.getColumnIndex("field6"))) { + object.setField6(cursor.getDouble(cursor.getColumnIndex("field6"))); + } + + return object; + } +} \ No newline at end of file diff --git a/storio-sqlite-annotations-processor-test/src/test/resources/BoxedTypesPrivateFieldsStorIOSQLitePutResolver.java b/storio-sqlite-annotations-processor-test/src/test/resources/BoxedTypesPrivateFieldsStorIOSQLitePutResolver.java new file mode 100644 index 000000000..7827939cc --- /dev/null +++ b/storio-sqlite-annotations-processor-test/src/test/resources/BoxedTypesPrivateFieldsStorIOSQLitePutResolver.java @@ -0,0 +1,54 @@ +package com.pushtorefresh.storio.sqlite.annotations; + +import android.content.ContentValues; +import android.support.annotation.NonNull; +import com.pushtorefresh.storio.sqlite.operations.put.DefaultPutResolver; +import com.pushtorefresh.storio.sqlite.queries.InsertQuery; +import com.pushtorefresh.storio.sqlite.queries.UpdateQuery; +import java.lang.Override; + +/** + * Generated resolver for Put Operation. + */ +public class BoxedTypesPrivateFieldsStorIOSQLitePutResolver extends DefaultPutResolver { + /** + * {@inheritDoc} + */ + @Override + @NonNull + public InsertQuery mapToInsertQuery(@NonNull BoxedTypesPrivateFields object) { + return InsertQuery.builder() + .table("table") + .build();} + + /** + * {@inheritDoc} + */ + @Override + @NonNull + public UpdateQuery mapToUpdateQuery(@NonNull BoxedTypesPrivateFields object) { + return UpdateQuery.builder() + .table("table") + .where("field4 = ?") + .whereArgs(object.getField4()) + .build(); + } + + /** + * {@inheritDoc} + */ + @Override + @NonNull + public ContentValues mapToContentValues(@NonNull BoxedTypesPrivateFields object) { + ContentValues contentValues = new ContentValues(6); + + contentValues.put("field1", object.getField1()); + contentValues.put("field2", object.getField2()); + contentValues.put("field3", object.getField3()); + contentValues.put("field4", object.getField4()); + contentValues.put("field5", object.getField5()); + contentValues.put("field6", object.getField6()); + + return contentValues; + } +} \ No newline at end of file diff --git a/storio-sqlite-annotations-processor-test/src/test/resources/PrimitivePrivateFields.java b/storio-sqlite-annotations-processor-test/src/test/resources/PrimitivePrivateFields.java new file mode 100644 index 000000000..b90a0b801 --- /dev/null +++ b/storio-sqlite-annotations-processor-test/src/test/resources/PrimitivePrivateFields.java @@ -0,0 +1,93 @@ +package com.pushtorefresh.storio.sqlite.annotations; + +@StorIOSQLiteType(table = "table") +public class PrimitivePrivateFields { + + @StorIOSQLiteColumn(name = "field1") + private boolean field1; + + @StorIOSQLiteColumn(name = "field2") + private short field2; + + @StorIOSQLiteColumn(name = "field3") + private int field3; + + @StorIOSQLiteColumn(name = "field4", key = true) + private long field4; + + @StorIOSQLiteColumn(name = "field5") + private float field5; + + @StorIOSQLiteColumn(name = "field6") + private double field6; + + @StorIOSQLiteColumn(name = "field7") + private String field7; + + @StorIOSQLiteColumn(name = "field8") + private byte[] field8; + + public boolean isField1() { + return field1; + } + + public void setField1(boolean field1) { + this.field1 = field1; + } + + public short getField2() { + return field2; + } + + public void setField2(short field2) { + this.field2 = field2; + } + + public int getField3() { + return field3; + } + + public void setField3(int field3) { + this.field3 = field3; + } + + public long getField4() { + return field4; + } + + public void setField4(long field4) { + this.field4 = field4; + } + + public float getField5() { + return field5; + } + + public void setField5(float field5) { + this.field5 = field5; + } + + public double getField6() { + return field6; + } + + public void setField6(double field6) { + this.field6 = field6; + } + + public String getField7() { + return field7; + } + + public void setField7(String field7) { + this.field7 = field7; + } + + public byte[] getField8() { + return field8; + } + + public void setField8(byte[] field8) { + this.field8 = field8; + } +} \ No newline at end of file diff --git a/storio-sqlite-annotations-processor-test/src/test/resources/PrimitivePrivateFieldsSQLiteTypeMapping.java b/storio-sqlite-annotations-processor-test/src/test/resources/PrimitivePrivateFieldsSQLiteTypeMapping.java new file mode 100644 index 000000000..56dc3e926 --- /dev/null +++ b/storio-sqlite-annotations-processor-test/src/test/resources/PrimitivePrivateFieldsSQLiteTypeMapping.java @@ -0,0 +1,14 @@ +package com.pushtorefresh.storio.sqlite.annotations; + +import com.pushtorefresh.storio.sqlite.SQLiteTypeMapping; + +/** + * Generated mapping with collection of resolvers. + */ +public class PrimitivePrivateFieldsSQLiteTypeMapping extends SQLiteTypeMapping { + public PrimitivePrivateFieldsSQLiteTypeMapping() { + super(new PrimitivePrivateFieldsStorIOSQLitePutResolver(), + new PrimitivePrivateFieldsStorIOSQLiteGetResolver(), + new PrimitivePrivateFieldsStorIOSQLiteDeleteResolver()); + } +} \ No newline at end of file diff --git a/storio-sqlite-annotations-processor-test/src/test/resources/PrimitivePrivateFieldsStorIOSQLiteDeleteResolver.java b/storio-sqlite-annotations-processor-test/src/test/resources/PrimitivePrivateFieldsStorIOSQLiteDeleteResolver.java new file mode 100644 index 000000000..e8f4d7480 --- /dev/null +++ b/storio-sqlite-annotations-processor-test/src/test/resources/PrimitivePrivateFieldsStorIOSQLiteDeleteResolver.java @@ -0,0 +1,23 @@ +package com.pushtorefresh.storio.sqlite.annotations; + +import android.support.annotation.NonNull; +import com.pushtorefresh.storio.sqlite.operations.delete.DefaultDeleteResolver; +import com.pushtorefresh.storio.sqlite.queries.DeleteQuery; +import java.lang.Override; + +/** + * Generated resolver for Delete Operation. + */ +public class PrimitivePrivateFieldsStorIOSQLiteDeleteResolver extends DefaultDeleteResolver { + /** + * {@inheritDoc} + */ + @Override + @NonNull + public DeleteQuery mapToDeleteQuery(@NonNull PrimitivePrivateFields object) { + return DeleteQuery.builder() + .table("table") + .where("field4 = ?") + .whereArgs(object.getField4()) + .build();} +} \ No newline at end of file diff --git a/storio-sqlite-annotations-processor-test/src/test/resources/PrimitivePrivateFieldsStorIOSQLiteGetResolver.java b/storio-sqlite-annotations-processor-test/src/test/resources/PrimitivePrivateFieldsStorIOSQLiteGetResolver.java new file mode 100644 index 000000000..2afa483c7 --- /dev/null +++ b/storio-sqlite-annotations-processor-test/src/test/resources/PrimitivePrivateFieldsStorIOSQLiteGetResolver.java @@ -0,0 +1,31 @@ +package com.pushtorefresh.storio.sqlite.annotations; + +import android.database.Cursor; +import android.support.annotation.NonNull; +import com.pushtorefresh.storio.sqlite.operations.get.DefaultGetResolver; +import java.lang.Override; + +/** + * Generated resolver for Get Operation. + */ +public class PrimitivePrivateFieldsStorIOSQLiteGetResolver extends DefaultGetResolver { + /** + * {@inheritDoc} + */ + @Override + @NonNull + public PrimitivePrivateFields mapFromCursor(@NonNull Cursor cursor) { + PrimitivePrivateFields object = new PrimitivePrivateFields(); + + object.setField1(cursor.getInt(cursor.getColumnIndex("field1")) == 1); + object.setField2(cursor.getShort(cursor.getColumnIndex("field2"))); + object.setField3(cursor.getInt(cursor.getColumnIndex("field3"))); + object.setField4(cursor.getLong(cursor.getColumnIndex("field4"))); + object.setField5(cursor.getFloat(cursor.getColumnIndex("field5"))); + object.setField6(cursor.getDouble(cursor.getColumnIndex("field6"))); + object.setField7(cursor.getString(cursor.getColumnIndex("field7"))); + object.setField8(cursor.getBlob(cursor.getColumnIndex("field8"))); + + return object; + } +} \ No newline at end of file diff --git a/storio-sqlite-annotations-processor-test/src/test/resources/PrimitivePrivateFieldsStorIOSQLitePutResolver.java b/storio-sqlite-annotations-processor-test/src/test/resources/PrimitivePrivateFieldsStorIOSQLitePutResolver.java new file mode 100644 index 000000000..f9002246c --- /dev/null +++ b/storio-sqlite-annotations-processor-test/src/test/resources/PrimitivePrivateFieldsStorIOSQLitePutResolver.java @@ -0,0 +1,56 @@ +package com.pushtorefresh.storio.sqlite.annotations; + +import android.content.ContentValues; +import android.support.annotation.NonNull; +import com.pushtorefresh.storio.sqlite.operations.put.DefaultPutResolver; +import com.pushtorefresh.storio.sqlite.queries.InsertQuery; +import com.pushtorefresh.storio.sqlite.queries.UpdateQuery; +import java.lang.Override; + +/** + * Generated resolver for Put Operation. + */ +public class PrimitivePrivateFieldsStorIOSQLitePutResolver extends DefaultPutResolver { + /** + * {@inheritDoc} + */ + @Override + @NonNull + public InsertQuery mapToInsertQuery(@NonNull PrimitivePrivateFields object) { + return InsertQuery.builder() + .table("table") + .build();} + + /** + * {@inheritDoc} + */ + @Override + @NonNull + public UpdateQuery mapToUpdateQuery(@NonNull PrimitivePrivateFields object) { + return UpdateQuery.builder() + .table("table") + .where("field4 = ?") + .whereArgs(object.getField4()) + .build(); + } + + /** + * {@inheritDoc} + */ + @Override + @NonNull + public ContentValues mapToContentValues(@NonNull PrimitivePrivateFields object) { + ContentValues contentValues = new ContentValues(8); + + contentValues.put("field1", object.isField1()); + contentValues.put("field2", object.getField2()); + contentValues.put("field3", object.getField3()); + contentValues.put("field4", object.getField4()); + contentValues.put("field5", object.getField5()); + contentValues.put("field6", object.getField6()); + contentValues.put("field7", object.getField7()); + contentValues.put("field8", object.getField8()); + + return contentValues; + } +} \ No newline at end of file diff --git a/storio-sqlite-annotations-processor-test/src/test/resources/PrivateFieldWithCorrespondingAccessors.java b/storio-sqlite-annotations-processor-test/src/test/resources/PrivateFieldWithCorrespondingAccessors.java new file mode 100644 index 000000000..89516b6c9 --- /dev/null +++ b/storio-sqlite-annotations-processor-test/src/test/resources/PrivateFieldWithCorrespondingAccessors.java @@ -0,0 +1,16 @@ +package com.pushtorefresh.storio.sqlite.annotations; + +@StorIOSQLiteType(table = "table") +public class PrivateFieldWithCorrespondingAccessors { + + @StorIOSQLiteColumn(name = "id", key = true) + private long id; + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } +} diff --git a/storio-sqlite-annotations-processor-test/src/test/resources/PrivateFieldWithIsGetter.java b/storio-sqlite-annotations-processor-test/src/test/resources/PrivateFieldWithIsGetter.java new file mode 100644 index 000000000..d773ff875 --- /dev/null +++ b/storio-sqlite-annotations-processor-test/src/test/resources/PrivateFieldWithIsGetter.java @@ -0,0 +1,27 @@ +package com.pushtorefresh.storio.sqlite.annotations; + +@StorIOSQLiteType(table = "table") +public class PrivateFieldWithIsGetter { + + @StorIOSQLiteColumn(name = "id", key = true) + private long id; + + @StorIOSQLiteColumn(name = "flag") + private boolean flag; + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public boolean isFlag() { + return flag; + } + + public void setFlag(boolean flag) { + this.flag = flag; + } +} \ No newline at end of file diff --git a/storio-sqlite-annotations-processor-test/src/test/resources/PrivateFieldWithNameStartingWithIs.java b/storio-sqlite-annotations-processor-test/src/test/resources/PrivateFieldWithNameStartingWithIs.java new file mode 100644 index 000000000..80fac2e08 --- /dev/null +++ b/storio-sqlite-annotations-processor-test/src/test/resources/PrivateFieldWithNameStartingWithIs.java @@ -0,0 +1,27 @@ +package com.pushtorefresh.storio.sqlite.annotations; + +@StorIOSQLiteType(table = "table") +public class PrivateFieldWithNameStartingWithIs { + + @StorIOSQLiteColumn(name = "id", key = true) + private long id; + + @StorIOSQLiteColumn(name = "is_flag") + private boolean isFlag; + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public boolean isFlag() { + return isFlag; + } + + public void setFlag(boolean flag) { + isFlag = flag; + } +} \ No newline at end of file diff --git a/storio-sqlite-annotations-processor-test/src/test/resources/PrivateField.java b/storio-sqlite-annotations-processor-test/src/test/resources/PrivateFieldWithoutAccessors.java similarity index 78% rename from storio-sqlite-annotations-processor-test/src/test/resources/PrivateField.java rename to storio-sqlite-annotations-processor-test/src/test/resources/PrivateFieldWithoutAccessors.java index 9919b301d..d7eb208f3 100644 --- a/storio-sqlite-annotations-processor-test/src/test/resources/PrivateField.java +++ b/storio-sqlite-annotations-processor-test/src/test/resources/PrivateFieldWithoutAccessors.java @@ -1,7 +1,7 @@ package com.pushtorefresh.storio.sqlite.annotations; @StorIOSQLiteType(table = "table") -public class PrivateField { +public class PrivateFieldWithoutAccessors { @StorIOSQLiteColumn(name = "id", key = true) private long id; diff --git a/storio-sqlite-annotations-processor-test/src/test/resources/PrivateFieldWithoutGetter.java b/storio-sqlite-annotations-processor-test/src/test/resources/PrivateFieldWithoutGetter.java new file mode 100644 index 000000000..12b6f298c --- /dev/null +++ b/storio-sqlite-annotations-processor-test/src/test/resources/PrivateFieldWithoutGetter.java @@ -0,0 +1,12 @@ +package com.pushtorefresh.storio.sqlite.annotations; + +@StorIOSQLiteType(table = "table") +public class PrivateFieldWithoutGetter { + + @StorIOSQLiteColumn(name = "id", key = true) + private long id; + + public void setId(long id) { + this.id = id; + } +} \ No newline at end of file diff --git a/storio-sqlite-annotations-processor-test/src/test/resources/PrivateFieldWithoutSetter.java b/storio-sqlite-annotations-processor-test/src/test/resources/PrivateFieldWithoutSetter.java new file mode 100644 index 000000000..7bb83e4f5 --- /dev/null +++ b/storio-sqlite-annotations-processor-test/src/test/resources/PrivateFieldWithoutSetter.java @@ -0,0 +1,12 @@ +package com.pushtorefresh.storio.sqlite.annotations; + +@StorIOSQLiteType(table = "table") +public class PrivateFieldWithoutSetter { + + @StorIOSQLiteColumn(name = "id", key = true) + private long id; + + public long getId() { + return id; + } +} \ No newline at end of file diff --git a/storio-sqlite-annotations-processor-test/src/test/resources/PrivateMethod.java b/storio-sqlite-annotations-processor-test/src/test/resources/PrivateMethod.java new file mode 100644 index 000000000..a1414f6e0 --- /dev/null +++ b/storio-sqlite-annotations-processor-test/src/test/resources/PrivateMethod.java @@ -0,0 +1,10 @@ +package com.pushtorefresh.storio.sqlite.annotations; + +@StorIOSQLiteType(table = "table") +public class PrivateMethod { + + @StorIOSQLiteColumn(name = "id", key = true) + private long id() { + return 0; + } +} diff --git a/storio-sqlite-annotations-processor/src/main/kotlin/com/pushtorefresh/storio/sqlite/annotations/processor/StorIOSQLiteProcessor.kt b/storio-sqlite-annotations-processor/src/main/kotlin/com/pushtorefresh/storio/sqlite/annotations/processor/StorIOSQLiteProcessor.kt index 2b9c3f188..9030f862d 100644 --- a/storio-sqlite-annotations-processor/src/main/kotlin/com/pushtorefresh/storio/sqlite/annotations/processor/StorIOSQLiteProcessor.kt +++ b/storio-sqlite-annotations-processor/src/main/kotlin/com/pushtorefresh/storio/sqlite/annotations/processor/StorIOSQLiteProcessor.kt @@ -16,7 +16,12 @@ import com.pushtorefresh.storio.sqlite.annotations.processor.introspection.StorI import com.pushtorefresh.storio.sqlite.annotations.processor.introspection.StorIOSQLiteCreatorMeta import com.pushtorefresh.storio.sqlite.annotations.processor.introspection.StorIOSQLiteTypeMeta import javax.annotation.processing.RoundEnvironment -import javax.lang.model.element.* +import javax.lang.model.element.Element +import javax.lang.model.element.ElementKind +import javax.lang.model.element.ExecutableElement +import javax.lang.model.element.Modifier.ABSTRACT +import javax.lang.model.element.Modifier.PRIVATE +import javax.lang.model.element.TypeElement import javax.lang.model.util.Elements import javax.tools.Diagnostic.Kind.WARNING @@ -57,7 +62,7 @@ open class StorIOSQLiteProcessor : StorIOAnnotationsProcessor = DeleteResolverGenerator override fun createMapping(): Generator = MappingGenerator -} \ No newline at end of file +} diff --git a/storio-sqlite-annotations-processor/src/main/kotlin/com/pushtorefresh/storio/sqlite/annotations/processor/generate/GetResolverGenerator.kt b/storio-sqlite-annotations-processor/src/main/kotlin/com/pushtorefresh/storio/sqlite/annotations/processor/generate/GetResolverGenerator.kt index 766413af5..f7b64ae2d 100644 --- a/storio-sqlite-annotations-processor/src/main/kotlin/com/pushtorefresh/storio/sqlite/annotations/processor/generate/GetResolverGenerator.kt +++ b/storio-sqlite-annotations-processor/src/main/kotlin/com/pushtorefresh/storio/sqlite/annotations/processor/generate/GetResolverGenerator.kt @@ -58,7 +58,11 @@ object GetResolverGenerator : Generator { // otherwise -> if primitive and value from cursor null -> fail early if (isBoxed) builder.beginControlFlow("if (!cursor.isNull(\$L))", columnIndex) - builder.addStatement("object.\$L = cursor.\$L", columnMeta.elementName, getFromCursor) + if (columnMeta.needAccessors) { + builder.addStatement("object.\$L(cursor.\$L)", columnMeta.setter, getFromCursor) + } else { + builder.addStatement("object.\$L = cursor.\$L", columnMeta.elementName, getFromCursor) + } if (isBoxed) builder.endControlFlow() } @@ -99,8 +103,7 @@ object GetResolverGenerator : Generator { builder.addStatement("\$L = cursor.\$L", columnMeta.realElementName, getFromCursor) builder.endControlFlow() } else { - builder.addStatement("\$T \$L = cursor.\$L", name, columnMeta.realElementName, - getFromCursor) + builder.addStatement("\$T \$L = cursor.\$L", name, columnMeta.realElementName, getFromCursor) } if (!first) paramsBuilder.append(", ") @@ -124,4 +127,4 @@ object GetResolverGenerator : Generator { } fun generateName(typeMeta: StorIOSQLiteTypeMeta) = "${typeMeta.simpleName}$SUFFIX" -} \ No newline at end of file +} diff --git a/storio-sqlite-annotations-processor/src/main/kotlin/com/pushtorefresh/storio/sqlite/annotations/processor/generate/PutResolverGenerator.kt b/storio-sqlite-annotations-processor/src/main/kotlin/com/pushtorefresh/storio/sqlite/annotations/processor/generate/PutResolverGenerator.kt index 1ee953c1e..dbfa326df 100644 --- a/storio-sqlite-annotations-processor/src/main/kotlin/com/pushtorefresh/storio/sqlite/annotations/processor/generate/PutResolverGenerator.kt +++ b/storio-sqlite-annotations-processor/src/main/kotlin/com/pushtorefresh/storio/sqlite/annotations/processor/generate/PutResolverGenerator.kt @@ -60,11 +60,11 @@ object PutResolverGenerator : Generator { .addAnnotation(ANDROID_NON_NULL_ANNOTATION_CLASS_NAME) .build()) .addCode("""return UpdateQuery.builder() -$INDENT.table(${"$"}S) -$INDENT.where(${"$"}S) -$INDENT.whereArgs(${"$"}L) -$INDENT.build(); -""", + $INDENT.table(${"$"}S) + $INDENT.where(${"$"}S) + $INDENT.whereArgs(${"$"}L) + $INDENT.build(); + """.trimIndent(), typeMeta.storIOType.table, where[QueryGenerator.WHERE_CLAUSE], where[QueryGenerator.WHERE_ARGS]) @@ -87,9 +87,9 @@ $INDENT.build(); typeMeta.columns.values.forEach { columnMeta -> val ignoreNull = columnMeta.storIOColumn.ignoreNull if (ignoreNull) { - builder.beginControlFlow("if (object.\$L != null)", "${columnMeta.elementName}${if (columnMeta.isMethod) "()" else ""}") + builder.beginControlFlow("if (object.\$L != null)", columnMeta.contextAwareName) } - builder.addStatement("contentValues.put(\$S, object.\$L)", columnMeta.storIOColumn.name, "${columnMeta.elementName}${if (columnMeta.isMethod) "()" else ""}") + builder.addStatement("contentValues.put(\$S, object.\$L)", columnMeta.storIOColumn.name, columnMeta.contextAwareName) if (ignoreNull) builder.endControlFlow() } @@ -100,4 +100,4 @@ $INDENT.build(); } fun generateName(typeMeta: StorIOSQLiteTypeMeta) = "${typeMeta.simpleName}$SUFFIX" -} \ No newline at end of file +} diff --git a/storio-sqlite-annotations-processor/src/main/kotlin/com/pushtorefresh/storio/sqlite/annotations/processor/generate/QueryGenerator.kt b/storio-sqlite-annotations-processor/src/main/kotlin/com/pushtorefresh/storio/sqlite/annotations/processor/generate/QueryGenerator.kt index 16382493a..9d17ce341 100644 --- a/storio-sqlite-annotations-processor/src/main/kotlin/com/pushtorefresh/storio/sqlite/annotations/processor/generate/QueryGenerator.kt +++ b/storio-sqlite-annotations-processor/src/main/kotlin/com/pushtorefresh/storio/sqlite/annotations/processor/generate/QueryGenerator.kt @@ -23,7 +23,7 @@ object QueryGenerator { whereArgs .append(varName) .append(".") - .append(columnMeta.elementName) + .append(columnMeta.contextAwareName) } else { whereClause .append(" AND ") @@ -34,11 +34,8 @@ object QueryGenerator { .append(", ") .append(varName) .append(".") - .append(columnMeta.elementName) + .append(columnMeta.contextAwareName) } - - if (columnMeta.isMethod) whereArgs.append("()") - i++ } } @@ -50,4 +47,4 @@ object QueryGenerator { WHERE_ARGS to whereArgs.toString()) } } -} \ No newline at end of file +} diff --git a/storio-sqlite-annotations-processor/src/main/kotlin/com/pushtorefresh/storio/sqlite/annotations/processor/introspection/StorIOSQLiteColumnMeta.kt b/storio-sqlite-annotations-processor/src/main/kotlin/com/pushtorefresh/storio/sqlite/annotations/processor/introspection/StorIOSQLiteColumnMeta.kt index fc1a575de..8e6beed42 100644 --- a/storio-sqlite-annotations-processor/src/main/kotlin/com/pushtorefresh/storio/sqlite/annotations/processor/introspection/StorIOSQLiteColumnMeta.kt +++ b/storio-sqlite-annotations-processor/src/main/kotlin/com/pushtorefresh/storio/sqlite/annotations/processor/introspection/StorIOSQLiteColumnMeta.kt @@ -10,10 +10,14 @@ class StorIOSQLiteColumnMeta( element: Element, fieldName: String, javaType: JavaType, - storIOColumn: StorIOSQLiteColumn) + storIOColumn: StorIOSQLiteColumn, + getter: String? = null, + setter: String? = null) : StorIOColumnMeta( enclosingElement, element, fieldName, javaType, - storIOColumn) \ No newline at end of file + storIOColumn, + getter, + setter)