-
Notifications
You must be signed in to change notification settings - Fork 598
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1287 from Raizlabs/develop
4.0.2
- Loading branch information
Showing
50 changed files
with
1,381 additions
and
467 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
dbflow-processor/src/main/java/com/raizlabs/android/dbflow/processor/ProcessorUtils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} | ||
} |
102 changes: 102 additions & 0 deletions
102
...r/src/main/java/com/raizlabs/android/dbflow/processor/definition/InternalAdapterHelper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.