Upgrade to Pro — share decks privately, control downloads, hide ads and more …

KanmobaInKanto

 KanmobaInKanto

Takuji Nishibayashi

November 21, 2017
Tweet

More Decks by Takuji Nishibayashi

Other Decks in Technology

Transcript

  1. Loves • ! (AVG 175) • " # • http://photo.takuji31.jp

    • $ • http://nazuna.takuji31.jp • Android • Kotlin
  2. ྫ // ιʔείʔυͷόʔδϣϯ @SupportedSourceVersion(SourceVersion.RELEASE_8) // ॲཧ͢ΔΞϊςʔγϣϯͷҰཡ @SupportedAnnotationTypes("jp.takuji31.annotations.SomeAnnotation") // αϙʔτ͢ΔΦϓγϣϯ @SupportedOptions("kapt.kotlin.generated")

    class ExampleProcessor : AbstractProcessor() { override fun process(annotations: MutableSet<out TypeElement>, roundEnv: RoundEnvironment): Boolean { // do something return true } }
  3. ΫϥεΛ࡞Δ val packageName = "jp.takuji31.kanmoba" val userClassName = ClassName(packageName =

    packageName, simpleName = "User") val userClass = TypeSpec .classBuilder(userClassName) .addModifiers(KModifier.DATA) .build()
  4. ίϯετϥΫλʔΛఆٛ val userClass = TypeSpec .classBuilder(userClassName) .primaryConstructor( FunSpec .constructorBuilder() .addParameter(ParameterSpec

    .builder("id", Int::class) .build() ) .addParameter(ParameterSpec .builder("name", String::class) .build() ) .build() ) .build()
  5. ϓϩύςΟʔΛఆٛ val userClass = TypeSpec .classBuilder(userClassName) .addProperty( PropertySpec .builder("id", Int::class)

    .initializer("id") .build() ) .addProperty( PropertySpec .builder("name", String::class) .initializer("name") .build() ) .build()
  6. !

  7. ॻ͖ग़͢ val generatedDir = processingEnv.options["kapt.kotlin.generated"] ?: throw IllegalStateException("Needs kapt support.")

    KotlinFile.builder(packageName = packageName, fileName = "User") .addType(userClass.build()) .build() .writeTo(File(generatedDir))
  8. Πϝʔδ class MainActivity() : Activity() { val R by lazy

    { RKt(context = this) } fun onCreate(savedInstanceState: Bundle?) { // ic_launcher.png val icon: Drawable = R.drawable.ic_launcher.asDrawable() // Hello %1$s val message: String = R.string.welcome_message.format("Kanmoba") // Hello Kanmoba } }