Skip to content

Commit

Permalink
Fix incremental processing
Browse files Browse the repository at this point in the history
  • Loading branch information
tompee26 committed Apr 2, 2020
1 parent a7e0f41 commit 0554b97
Show file tree
Hide file tree
Showing 13 changed files with 143 additions and 220 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ allprojects {
}
}

project.ext.set("publishVersion", "0.1.0")
project.ext.set("publishVersion", "0.2.0")

task clean(type: Delete) {
delete rootProject.buildDir
Expand Down
8 changes: 0 additions & 8 deletions compiler/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,6 @@ dependencies {
implementation "com.google.auto.service:auto-service:1.0-rc6"
kapt "com.google.auto.service:auto-service:1.0-rc6"

def dagger = "2.25.2"
implementation "com.google.dagger:dagger:$dagger"
kapt "com.google.dagger:dagger-compiler:$dagger"

def assistedInject = '0.5.2'
compileOnly "com.squareup.inject:assisted-inject-annotations-dagger2:$assistedInject"
kapt "com.squareup.inject:assisted-inject-processor-dagger2:$assistedInject"

def incap = "0.2"
compileOnly "net.ltgt.gradle.incap:incap:$incap"
kapt "net.ltgt.gradle.incap:incap-processor:$incap"
Expand Down
59 changes: 29 additions & 30 deletions compiler/src/main/java/com/tompee/bunch/compiler/BunchProcessor.kt
Original file line number Diff line number Diff line change
@@ -1,52 +1,51 @@
package com.tompee.bunch.compiler

import com.google.auto.common.BasicAnnotationProcessor
import com.google.auto.service.AutoService
import com.google.common.collect.ImmutableList
import com.squareup.kotlinpoet.metadata.KotlinPoetMetadataPreview
import com.tompee.bunch.annotation.Bunch
import com.tompee.bunch.compiler.di.AppComponent
import com.tompee.bunch.compiler.di.DaggerAppComponent
import com.tompee.bunch.compiler.generators.BunchGenerator
import net.ltgt.gradle.incap.IncrementalAnnotationProcessor
import net.ltgt.gradle.incap.IncrementalAnnotationProcessorType
import javax.annotation.processing.*
import javax.inject.Inject
import javax.annotation.processing.Processor
import javax.annotation.processing.SupportedOptions
import javax.annotation.processing.SupportedSourceVersion
import javax.lang.model.SourceVersion
import javax.lang.model.element.TypeElement
import javax.tools.Diagnostic

@AutoService(Processor::class)
@SupportedSourceVersion(SourceVersion.RELEASE_8)
@SupportedOptions(BunchProcessor.KAPT_KOTLIN_GENERATED_OPTION_NAME)
@IncrementalAnnotationProcessor(IncrementalAnnotationProcessorType.ISOLATING)
@KotlinPoetMetadataPreview
internal class BunchProcessor : AbstractProcessor() {

private lateinit var appComponent: AppComponent

@Inject
lateinit var generatorFactory: BunchGenerator.Factory
internal class BunchProcessor : BasicAnnotationProcessor() {

companion object {
const val KAPT_KOTLIN_GENERATED_OPTION_NAME = "kapt.kotlin.generated"
}

override fun getSupportedAnnotationTypes(): MutableSet<String> {
return mutableSetOf(Bunch::class.java.name)
}

override fun getSupportedSourceVersion(): SourceVersion = SourceVersion.latest()

override fun process(set: MutableSet<out TypeElement>?, env: RoundEnvironment?): Boolean {
appComponent = DaggerAppComponent.factory().create(processingEnv)
appComponent.inject(this)

env?.getElementsAnnotatedWith(Bunch::class.java)?.forEach {
try {
generatorFactory.create(it).generate()
} catch (e: ProcessorException) {
processingEnv.messager.printMessage(Diagnostic.Kind.ERROR, e.message, e.element)
}
}
return true
override fun initSteps(): MutableIterable<ProcessingStep> {
return ImmutableList.of(
GeneratorStep(
processingEnv.elementUtils,
processingEnv.typeUtils,
processingEnv.messager,
processingEnv.filer
)
)
}

// override fun process(set: MutableSet<out TypeElement>?, env: RoundEnvironment?): Boolean {
// appComponent = DaggerAppComponent.factory().create(processingEnv)
// appComponent.inject(this)
//
// env?.getElementsAnnotatedWith(Bunch::class.java)?.forEach {
// try {
// generatorFactory.create(it).generate()
// } catch (e: ProcessorException) {
// processingEnv.messager.printMessage(Diagnostic.Kind.ERROR, e.message, e.element)
// }
// }
// return true
// }
}
88 changes: 88 additions & 0 deletions compiler/src/main/java/com/tompee/bunch/compiler/GeneratorStep.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com.tompee.bunch.compiler

import com.google.auto.common.BasicAnnotationProcessor
import com.google.common.collect.SetMultimap
import com.squareup.kotlinpoet.*
import com.squareup.kotlinpoet.classinspector.elements.ElementsClassInspector
import com.squareup.kotlinpoet.metadata.KotlinPoetMetadataPreview
import com.tompee.bunch.annotation.Bunch
import com.tompee.bunch.compiler.generators.AssertGenerator
import com.tompee.bunch.compiler.generators.CompanionGenerator
import com.tompee.bunch.compiler.generators.MethodGenerator
import com.tompee.bunch.compiler.properties.JavaProperties
import com.tompee.bunch.compiler.properties.KotlinProperties
import javax.annotation.processing.Filer
import javax.annotation.processing.Messager
import javax.lang.model.element.Element
import javax.lang.model.element.TypeElement
import javax.lang.model.util.Elements
import javax.lang.model.util.Types
import javax.tools.Diagnostic

@KotlinPoetMetadataPreview
internal class GeneratorStep(
private val elements: Elements,
private val types: Types,
private val messager: Messager,
private val filer: Filer
) : BasicAnnotationProcessor.ProcessingStep {

private val classInspector = ElementsClassInspector.create(elements, types)

override fun process(elementsByAnnotation: SetMultimap<Class<out Annotation>, Element>): MutableSet<out Element> {
elementsByAnnotation.entries()
.map { it.value }
.forEach {
try {
generate(it as TypeElement)
} catch (e: ProcessorException) {
messager.printMessage(Diagnostic.Kind.ERROR, e.message, e.element)
}
}
return mutableSetOf()
}

override fun annotations(): MutableSet<out Class<out Annotation>> {
return mutableSetOf(Bunch::class.java)
}

private fun generate(element: TypeElement) {
val javaProperties = JavaProperties(element, elements)
val kotlinProperties = KotlinProperties(element, elements, classInspector)

val name = javaProperties.getBunchAnnotation().name

val fileSpec = FileSpec.builder(javaProperties.getPackageName(), name)
.addType(generateClassSpec(name, kotlinProperties, javaProperties))
.build()
fileSpec.writeTo(filer)
}

private fun generateClassSpec(
name: String,
kotlinProperties: KotlinProperties,
javaProperties: JavaProperties
): TypeSpec {
val constructor = FunSpec.constructorBuilder()
.addParameter("bundle", BUNDLE)
.build()

val methodGenerator = MethodGenerator()
return TypeSpec.classBuilder(name)
.apply { if (kotlinProperties.isInternal()) addModifiers(KModifier.INTERNAL) }
.primaryConstructor(constructor)
.addProperty(
PropertySpec.builder("bundle", BUNDLE)
.initializer("bundle")
.addModifiers(KModifier.PRIVATE)
.build()
)
.addType(AssertGenerator().generate(javaProperties, kotlinProperties))
.addType(CompanionGenerator().generate(javaProperties, kotlinProperties))
.addFunctions(methodGenerator.generateAsserts(javaProperties, kotlinProperties))
.addFunctions(methodGenerator.generateSetters(javaProperties, kotlinProperties))
.addFunctions(methodGenerator.generateGetters(javaProperties, kotlinProperties))
.addFunction(methodGenerator.generateCollector())
.build()
}
}

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@ import com.squareup.kotlinpoet.metadata.KotlinPoetMetadataPreview
import com.tompee.bunch.compiler.ProcessorException
import com.tompee.bunch.compiler.properties.JavaProperties
import com.tompee.bunch.compiler.properties.KotlinProperties
import javax.inject.Inject
import javax.lang.model.element.Element
import javax.lang.model.element.ElementKind

/**
* Assertion class generator
*/
@KotlinPoetMetadataPreview
internal class AssertGenerator @Inject constructor() {
internal class AssertGenerator {

/**
* Generates the assert type
Expand Down Expand Up @@ -98,7 +97,8 @@ internal class AssertGenerator @Inject constructor() {
jProp.getElement().enclosedElements.filter { it.kind == ElementKind.CLASS }
.flatMap { classes -> classes.enclosedElements.filter { it.kind == ElementKind.METHOD } }
val jFun =
jProp.getMethods().plus(enclosedElements).firstOrNull { it.simpleName.toString() == funSpec.name }
jProp.getMethods().plus(enclosedElements)
.firstOrNull { it.simpleName.toString() == funSpec.name }
?: throw ProcessorException(
jProp.getElement(),
"Some functions cannot be interpreted"
Expand Down

This file was deleted.

Loading

0 comments on commit 0554b97

Please sign in to comment.