-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
143 additions
and
220 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
59 changes: 29 additions & 30 deletions
59
compiler/src/main/java/com/tompee/bunch/compiler/BunchProcessor.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 |
---|---|---|
@@ -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
88
compiler/src/main/java/com/tompee/bunch/compiler/GeneratorStep.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,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() | ||
} | ||
} |
25 changes: 0 additions & 25 deletions
25
compiler/src/main/java/com/tompee/bunch/compiler/di/AppComponent.kt
This file was deleted.
Oops, something went wrong.
8 changes: 0 additions & 8 deletions
8
compiler/src/main/java/com/tompee/bunch/compiler/di/AssistedInjectModule.kt
This file was deleted.
Oops, something went wrong.
45 changes: 0 additions & 45 deletions
45
compiler/src/main/java/com/tompee/bunch/compiler/di/ProcessingModule.kt
This file was deleted.
Oops, something went wrong.
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
64 changes: 0 additions & 64 deletions
64
compiler/src/main/java/com/tompee/bunch/compiler/generators/BunchGenerator.kt
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.