Skip to content

Commit

Permalink
Add support annotations into generated code
Browse files Browse the repository at this point in the history
Resolves #1
  • Loading branch information
ZacSweers committed Sep 4, 2017
1 parent af4262e commit f0fae21
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion psync/src/main/kotlin/io/sweers/psync/PSyncTask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import android.content.Context
import android.content.SharedPreferences
import android.content.res.Resources
import android.preference.PreferenceManager
import android.support.annotation.CheckResult
import android.support.annotation.NonNull
import android.support.annotation.Nullable
import com.google.common.base.CaseFormat
import com.squareup.javapoet.AnnotationSpec
import com.squareup.javapoet.ClassName
import com.squareup.javapoet.CodeBlock
import com.squareup.javapoet.FieldSpec
Expand Down Expand Up @@ -268,7 +271,8 @@ open class PSyncTask : SourceTask() {
IllegalStateException::class.java,
"context cannot be null!")
.endControlFlow()
.addStatement("\$T applicationContext = context.getApplicationContext()", Context::class.java)
.addStatement("\$T applicationContext = context.getApplicationContext()",
Context::class.java)
.addStatement("RESOURCES = applicationContext.getResources()")
.beginControlFlow("if (autoCreateSharedPrefs)")
.addStatement("// Sensible default")
Expand Down Expand Up @@ -351,22 +355,36 @@ open class PSyncTask : SourceTask() {
if (entry.valueType != null || entry.defaultType != null) {
val prefType = (entry.valueType ?: entry.defaultType)!!
entryClass.addMethod(MethodSpec.methodBuilder("get")
.apply {
if (!prefType.isPrimitive) {
addAnnotation(AnnotationSpec.builder(Nullable::class.java).build())
}
}
.addModifiers(*MODIFIERS)
.returns(prefType)
.addStatement("return PREFERENCES.\$N", resolvePreferenceStmt(entry, true))
.build())

entryClass.addMethod(MethodSpec.methodBuilder("put")
.addAnnotation(AnnotationSpec.builder(CheckResult::class.java).build())
.addAnnotation(AnnotationSpec.builder(NonNull::class.java).build())
.addModifiers(*MODIFIERS)
.returns(SharedPreferences.Editor::class.java)
.addParameter(ParameterSpec.builder(prefType, "val", Modifier.FINAL)
.apply {
if (!prefType.isPrimitive) {
addAnnotation(AnnotationSpec.builder(Nullable::class.java).build())
}
}
.build())
.addStatement("return PREFERENCES.edit().\$N", resolvePreferenceStmt(entry, false))
.build())

val referenceType = resolveReferenceType(prefType)
if (generateRx && referenceType != null) {
entryClass.addMethod(MethodSpec.methodBuilder("rx")
.addAnnotation(AnnotationSpec.builder(CheckResult::class.java).build())
.addAnnotation(AnnotationSpec.builder(NonNull::class.java).build())
.addModifiers(*MODIFIERS)
.returns(ParameterizedTypeName.get(CN_RX_PREFERENCE, TypeName.get(referenceType)))
.addStatement("return RX_PREFERENCES.get\$N(KEY)", referenceType.simpleName)
Expand Down

0 comments on commit f0fae21

Please sign in to comment.