Skip to content

Commit

Permalink
Merge pull request #1287 from Raizlabs/develop
Browse files Browse the repository at this point in the history
4.0.2
  • Loading branch information
agrosner authored May 16, 2017
2 parents 8bec1a5 + 26d83d1 commit baa5270
Show file tree
Hide file tree
Showing 50 changed files with 1,381 additions and 467 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Add the library to the project-level build.gradle, using the apt plugin to enabl

```groovy
def dbflow_version = "4.0.1"
def dbflow_version = "4.0.2"
// or dbflow_version = "develop-SNAPSHOT" for grabbing latest dependency in your project on the develop branch
// or 10-digit short-hash of a specific commit. (Useful for bugs fixed in develop, but not in a release yet)
Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
buildscript {
ext.kotlin_version = '1.1.2'
ext.kotlin_version = '1.1.2-3'
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.1'
classpath 'com.android.tools.build:gradle:2.3.2'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
classpath 'com.getkeepsafe.dexcount:dexcount-gradle-plugin:0.6.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,8 @@
*/
String fieldName();

/**
* @return If specified other than {@link ConflictAction#NONE}, then we assume {@link NotNull}.
*/
ConflictAction nonNullConflict() default ConflictAction.NONE;
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ class ProcessorManager internal constructor(val processingEnvironment: Processin
holderDefinition?.tableDefinitionMap?.put(it, tableDefinition)
holderDefinition?.tableNameMap?.let {
if (holderDefinition.tableNameMap.containsKey(tableDefinition.tableName)) {
logError("Found duplicate table %1s for database %1s", tableDefinition.tableName,
holderDefinition.databaseDefinition?.databaseName)
logError("Found duplicate table ${tableDefinition.tableName} " +
"for database ${holderDefinition.databaseDefinition?.databaseName}")
} else tableDefinition.tableName?.let {
holderDefinition.tableNameMap.put(it, tableDefinition)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package com.raizlabs.android.dbflow.processor

import com.raizlabs.android.dbflow.processor.ProcessorManager.Companion.manager
import com.raizlabs.android.dbflow.processor.utils.ElementUtility
import com.raizlabs.android.dbflow.processor.utils.erasure
import com.raizlabs.android.dbflow.processor.utils.toTypeElement
import com.squareup.javapoet.ClassName
import javax.annotation.processing.ProcessingEnvironment
import javax.lang.model.element.Element
import javax.lang.model.element.Modifier
import javax.lang.model.element.TypeElement
import javax.lang.model.type.TypeMirror
import javax.tools.Diagnostic

/**
* Description: Provides handy methods for processing
*/
object ProcessorUtils {

/**
* Whether the specified element is assignable to the fqTn parameter
* @param processingEnvironment The environment this runs in
* *
* @param fqTn THe fully qualified type name of the element we want to check
* *
* @param element The element to check that implements
* *
* @return true if element implements the fqTn
*/
fun implementsClass(processingEnvironment: ProcessingEnvironment, fqTn: String, element: TypeElement?): Boolean {
val typeElement = processingEnvironment.elementUtils.getTypeElement(fqTn)
if (typeElement == null) {
processingEnvironment.messager.printMessage(Diagnostic.Kind.ERROR,
"Type Element was null for: $fqTn ensure that the visibility of the class is not private.")
return false
} else {
val classMirror: TypeMirror? = typeElement.asType().erasure()
if (classMirror == null || element?.asType() == null) {
return false
}
val elementType = element.asType()
return elementType != null && (processingEnvironment.typeUtils.isAssignable(elementType, classMirror) || elementType == classMirror)
}
}

/**
* Whether the specified element is assignable to the fqTn parameter
* @param processingEnvironment The environment this runs in
* *
* @param fqTn THe fully qualified type name of the element we want to check
* *
* @param element The element to check that implements
* *
* @return true if element implements the fqTn
*/
fun isSubclass(processingEnvironment: ProcessingEnvironment, fqTn: String, element: TypeElement?): Boolean {
val typeElement = processingEnvironment.elementUtils.getTypeElement(fqTn)
if (typeElement == null) {
processingEnvironment.messager.printMessage(Diagnostic.Kind.ERROR, "Type Element was null for: $fqTn ensure that the visibility of the class is not private.")
return false
} else {
val classMirror = typeElement.asType()
return classMirror != null && element != null && element.asType() != null && processingEnvironment.typeUtils.isSubtype(element.asType(), classMirror)
}
}

fun fromTypeMirror(typeMirror: TypeMirror, processorManager: ProcessorManager): ClassName? {
var className: ClassName? = null
val element = getTypeElement(typeMirror)
if (element != null) {
className = ClassName.get(element)
} else {
className = ElementUtility.getClassName(typeMirror.toString(), processorManager)
}
return className
}

fun getTypeElement(element: Element): TypeElement? {
val typeElement: TypeElement?
if (element is TypeElement) {
typeElement = element
} else {
typeElement = getTypeElement(element.asType())
}
return typeElement
}

fun getTypeElement(typeMirror: TypeMirror): TypeElement? {
val manager = ProcessorManager.manager
var typeElement: TypeElement? = typeMirror.toTypeElement(manager)
if (typeElement == null) {
val el = manager.typeUtils.asElement(typeMirror)
typeElement = if (el != null) (el as TypeElement) else null
}
return typeElement
}

fun ensureVisibleStatic(element: Element, typeElement: TypeElement,
name: String) {
if (element.modifiers.contains(Modifier.PRIVATE)
|| element.modifiers.contains(Modifier.PROTECTED)) {
manager.logError("$name must be visible from: " + typeElement)
}
if (!element.modifiers.contains(Modifier.STATIC)) {
manager.logError("$name must be static from: " + typeElement)
}

if (!element.modifiers.contains(Modifier.FINAL)) {
manager.logError("The $name must be final")
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package com.raizlabs.android.dbflow.processor.definition

import com.raizlabs.android.dbflow.processor.ClassNames
import com.raizlabs.android.dbflow.processor.definition.column.ColumnDefinition
import com.raizlabs.android.dbflow.processor.definition.column.DefinitionUtils
import com.raizlabs.android.dbflow.processor.utils.ModelUtils
import com.raizlabs.android.dbflow.sql.QueryBuilder
import com.squareup.javapoet.ArrayTypeName
import com.squareup.javapoet.ClassName
import com.squareup.javapoet.MethodSpec
import com.squareup.javapoet.ParameterizedTypeName
import com.squareup.javapoet.TypeName
import com.squareup.javapoet.TypeSpec
import javax.lang.model.element.Modifier

/**
* Description: Assists in writing methods for adapters
*/
object InternalAdapterHelper {

fun writeGetModelClass(typeBuilder: TypeSpec.Builder, modelClassName: ClassName?) {
typeBuilder.addMethod(MethodSpec.methodBuilder("getModelClass")
.addAnnotation(Override::class.java)
.addModifiers(Modifier.PUBLIC, Modifier.FINAL)
.addStatement("return \$T.class", modelClassName)
.returns(ParameterizedTypeName.get(ClassName.get(Class::class.java), modelClassName))
.build())
}

fun writeGetTableName(typeBuilder: TypeSpec.Builder, tableName: String?) {
typeBuilder.addMethod(MethodSpec.methodBuilder("getTableName")
.addModifiers(Modifier.PUBLIC, Modifier.FINAL)
.addStatement("return \$S", QueryBuilder.quote(tableName))
.returns(ClassName.get(String::class.java))
.build())
}

fun writeUpdateAutoIncrement(typeBuilder: TypeSpec.Builder, modelClassName: TypeName?,
autoIncrementDefinition: ColumnDefinition) {
typeBuilder.addMethod(MethodSpec.methodBuilder("updateAutoIncrement")
.addAnnotation(Override::class.java)
.addModifiers(Modifier.PUBLIC, Modifier.FINAL)
.addParameter(modelClassName, ModelUtils.variable)
.addParameter(ClassName.get(Number::class.java), "id")
.addCode(autoIncrementDefinition.updateAutoIncrementMethod)
.build())
}

fun writeGetCachingId(typeBuilder: TypeSpec.Builder, modelClassName: TypeName?,
primaryColumns: List<ColumnDefinition>) {
if (primaryColumns.size > 1) {
var methodBuilder: MethodSpec.Builder = MethodSpec.methodBuilder("getCachingColumnValuesFromModel")
.addAnnotation(Override::class.java).addModifiers(Modifier.PUBLIC, Modifier.FINAL)
.addParameter(ArrayTypeName.of(Any::class.java), "inValues")
.addParameter(modelClassName, ModelUtils.variable)
for (i in primaryColumns.indices) {
val column = primaryColumns[i]
methodBuilder.addCode(column.getColumnAccessString(i))
}
methodBuilder.addStatement("return \$L", "inValues").returns(ArrayTypeName.of(Any::class.java))
typeBuilder.addMethod(methodBuilder.build())

methodBuilder = MethodSpec.methodBuilder("getCachingColumnValuesFromCursor")
.addAnnotation(Override::class.java).addModifiers(Modifier.PUBLIC, Modifier.FINAL)
.addParameter(ArrayTypeName.of(Any::class.java), "inValues")
.addParameter(ClassNames.CURSOR, "cursor")
for (i in primaryColumns.indices) {
val column = primaryColumns[i]
val method = DefinitionUtils.getLoadFromCursorMethodString(column.elementTypeName, column.wrapperTypeName)
methodBuilder.addStatement("inValues[\$L] = \$L.\$L(\$L.getColumnIndex(\$S))", i, LoadFromCursorMethod.PARAM_CURSOR,
method, LoadFromCursorMethod.PARAM_CURSOR, column.columnName)
}
methodBuilder.addStatement("return \$L", "inValues").returns(ArrayTypeName.of(Any::class.java))
typeBuilder.addMethod(methodBuilder.build())
} else {
// single primary key
var methodBuilder: MethodSpec.Builder = MethodSpec.methodBuilder("getCachingColumnValueFromModel")
.addAnnotation(Override::class.java).addModifiers(Modifier.PUBLIC, Modifier.FINAL)
.addParameter(modelClassName, ModelUtils.variable)
methodBuilder.addCode(primaryColumns[0].getSimpleAccessString())
.returns(Any::class.java)
typeBuilder.addMethod(methodBuilder.build())

methodBuilder = MethodSpec.methodBuilder("getCachingColumnValueFromCursor")
.addAnnotation(Override::class.java).addModifiers(Modifier.PUBLIC, Modifier.FINAL)
.addParameter(ClassNames.CURSOR, "cursor")
val column = primaryColumns[0]
val method = DefinitionUtils.getLoadFromCursorMethodString(column.elementTypeName, column.wrapperTypeName)
methodBuilder.addStatement("return \$L.\$L(\$L.getColumnIndex(\$S))", LoadFromCursorMethod.PARAM_CURSOR,
method, LoadFromCursorMethod.PARAM_CURSOR, column.columnName).returns(Any::class.java)
typeBuilder.addMethod(methodBuilder.build())

methodBuilder = MethodSpec.methodBuilder("getCachingId")
.addModifiers(Modifier.PUBLIC, Modifier.FINAL)
.addParameter(modelClassName, ModelUtils.variable)
.addStatement("return getCachingColumnValueFromModel(\$L)",
ModelUtils.variable).returns(TypeName.OBJECT)
typeBuilder.addMethod(methodBuilder.build())
}
}

}
Loading

0 comments on commit baa5270

Please sign in to comment.