Skip to content

Commit c7b6e6a

Browse files
author
Johnson Lee
committed
Refactoring
1 parent 67de01e commit c7b6e6a

10 files changed

Lines changed: 79 additions & 28 deletions

File tree

build.gradle.kts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,28 @@ buildscript {
99

1010
dependencies {
1111
classpath("com.android.tools.build:gradle:4.0.0")
12-
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.31")
13-
classpath("io.johnsonlee:sonatype-publish-plugin:1.5.6")
12+
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:${embeddedKotlinVersion}")
13+
classpath("io.johnsonlee:sonatype-publish-plugin:1.6.2")
1414
}
1515
}
1616

1717
group = "io.johnsonlee.codegen"
1818
version = project.properties["version"]?.takeIf { it != DEFAULT_VERSION } ?: "1.0.0-SNAPSHOT"
19+
20+
allprojects {
21+
group = rootProject.group
22+
version = rootProject.version
23+
24+
repositories {
25+
mavenCentral()
26+
google()
27+
}
28+
29+
tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
30+
kotlinOptions {
31+
freeCompilerArgs = listOf("-Xjsr305=strict", "-Xskip-metadata-version-check")
32+
jvmTarget = "1.8"
33+
}
34+
}
35+
36+
}

compiler/src/main/kotlin/io/johnsonlee/codegen/compiler/CodegenProcessor.kt

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ abstract class CodegenProcessor<T : Annotation> : AbstractProcessor() {
2222

2323
protected val supportedAnnotation: Class<T> by lazy {
2424
(javaClass.genericSuperclass as ParameterizedType).actualTypeArguments.map {
25+
@Suppress("UNCHECKED_CAST")
2526
it as Class<T>
2627
}.first()
2728
}
@@ -60,15 +61,20 @@ abstract class CodegenProcessor<T : Annotation> : AbstractProcessor() {
6061

6162
protected open fun onPostProcessing(processingEnv: ProcessingEnvironment) = Unit
6263

63-
protected fun <T : Model> generate(template: String, model: T, lang: Language = Language.JAVA) {
64+
protected fun <T : Model> generate(template: String, model: T, output: Output) {
6465
val originatingElements = model.references.map {
6566
processingEnv.elementUtils.getTypeElement(it)
6667
}.toTypedArray()
67-
when (lang) {
68-
Language.JAVA -> generateJavaSources(model, originatingElements)
69-
else -> generateSources(lang, model, originatingElements)
70-
}.openWriter().use { writer ->
71-
engine.render("${template}.${lang.extension}.${engine.extension.lowercase()}", model, writer)
68+
when (output) {
69+
is Source -> when (output) {
70+
Source.Java -> generateJavaSources(model, originatingElements)
71+
else -> generateSources(output, model, originatingElements)
72+
}
73+
is Resource -> generateResource(output, model, originatingElements)
74+
else -> null
75+
}?.openWriter()?.use { writer ->
76+
val ext = if (output.extension.isNotBlank()) ".${output.extension}" else ""
77+
engine.render("${template}${ext}.${engine.extension.toLowerCase()}", model, writer)
7278
writer.close()
7379
}
7480
}
@@ -86,7 +92,7 @@ abstract class CodegenProcessor<T : Annotation> : AbstractProcessor() {
8692
}
8793

8894
private fun <T : Model> generateSources(
89-
lang: Language,
95+
source: Source,
9096
model: T,
9197
originatingElements: Array<TypeElement>
9298
): FileObject {
@@ -95,10 +101,20 @@ abstract class CodegenProcessor<T : Annotation> : AbstractProcessor() {
95101
return processingEnv.filer.createResource(
96102
StandardLocation.SOURCE_OUTPUT,
97103
pkg,
98-
"${name}.${lang.extension}",
104+
"${name}.${source.extension}",
99105
*originatingElements
100106
)
101107
}
102108

109+
private fun <T : Model> generateResource(
110+
resource: Resource,
111+
model: T,
112+
originatingElements: Array<TypeElement>
113+
): FileObject = processingEnv.filer.createResource(
114+
StandardLocation.CLASS_OUTPUT,
115+
"",
116+
"${resource.prefix}${File.separator}${model.qualifiedName}",
117+
*originatingElements
118+
)
103119

104120
}

compiler/src/main/kotlin/io/johnsonlee/codegen/compiler/ElementExtension.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import javax.lang.model.element.TypeElement
77

88
fun Element.asTypeElement(): TypeElement = MoreElements.asType(this)
99

10-
inline fun <reified T : Annotation> Element.getAnnotationMirror(): AnnotationMirror {
11-
return MoreElements.getAnnotationMirror(this, T::class.java).get()
10+
inline fun <reified T : Annotation> Element.getAnnotationMirror(): AnnotationMirror? {
11+
return MoreElements.getAnnotationMirror(this, T::class.java).takeIf {
12+
it.isPresent
13+
}?.get()
1214
}

compiler/src/main/kotlin/io/johnsonlee/codegen/compiler/Language.kt

Lines changed: 0 additions & 10 deletions
This file was deleted.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package io.johnsonlee.codegen.compiler
2+
3+
interface Output {
4+
val extension: String
5+
}
6+
7+
sealed class Source(override val extension: String) : Output {
8+
object Java: Source("java")
9+
object Kotlin: Source("kt")
10+
object KotlinScript: Source("kts")
11+
object Groovy: Source("groovy")
12+
}
13+
14+
interface Resource : Output {
15+
val prefix: String
16+
}

compiler/src/main/kotlin/io/johnsonlee/codegen/compiler/ProcessingEnvironmentExtension.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.johnsonlee.codegen.compiler
22

3+
import java.io.PrintWriter
4+
import java.io.StringWriter
35
import javax.annotation.processing.ProcessingEnvironment
46
import javax.lang.model.element.AnnotationMirror
57
import javax.lang.model.element.Element
@@ -26,5 +28,9 @@ fun ProcessingEnvironment.error(msg: String, element: Element, annotation: Annot
2628
}
2729

2830
fun ProcessingEnvironment.fatal(e: Throwable) {
29-
messager.printMessage(Diagnostic.Kind.ERROR, "FATAL ERROR: ${e.stackTraceToString()}")
31+
val stack = StringWriter().run {
32+
e.printStackTrace(PrintWriter(this))
33+
toString()
34+
}
35+
messager.printMessage(Diagnostic.Kind.ERROR, "FATAL ERROR: $stack")
3036
}

example/src/main/kotlin/io/johnsonlee/codegen/example/AutoFactoryCodegen.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package io.johnsonlee.codegen.example
22

33
import com.google.auto.service.AutoService
44
import io.johnsonlee.codegen.compiler.CodegenProcessor
5-
import io.johnsonlee.codegen.compiler.Language
5+
import io.johnsonlee.codegen.compiler.Source
66
import io.johnsonlee.codegen.compiler.asElement
77
import io.johnsonlee.codegen.compiler.asTypeElement
88
import io.johnsonlee.codegen.compiler.error
@@ -14,13 +14,16 @@ import io.johnsonlee.codegen.template.mustache.MustacheEngine
1414
import javax.annotation.processing.ProcessingEnvironment
1515
import javax.annotation.processing.Processor
1616
import javax.annotation.processing.RoundEnvironment
17+
import javax.annotation.processing.SupportedSourceVersion
18+
import javax.lang.model.SourceVersion
1719
import javax.lang.model.element.AnnotationMirror
1820
import javax.lang.model.element.ElementKind
1921
import javax.lang.model.element.ExecutableElement
2022
import javax.lang.model.element.TypeElement
2123
import javax.lang.model.type.DeclaredType
2224

2325
@AutoService(Processor::class)
26+
@SupportedSourceVersion(SourceVersion.RELEASE_8)
2427
class AutoFactoryCodegen : CodegenProcessor<AutoFactory>() {
2528

2629
private val mustache: TemplateEngine by lazy(::MustacheEngine)
@@ -34,7 +37,7 @@ class AutoFactoryCodegen : CodegenProcessor<AutoFactory>() {
3437
) {
3538
roundEnv.onEachAnnotatedElement { element ->
3639
val implementation = element.asTypeElement()
37-
val mirror = element.getAnnotationMirror<AutoFactory>()
40+
val mirror = element.getAnnotationMirror<AutoFactory>() ?: return@onEachAnnotatedElement
3841
mirror.value.takeIf(Set<DeclaredType>::isNotEmpty)?.map(DeclaredType::asTypeElement)?.onEach { api ->
3942
if (checkImplementation(implementation, api)) {
4043
generateFactory(implementation, mirror)
@@ -82,7 +85,7 @@ class AutoFactoryCodegen : CodegenProcessor<AutoFactory>() {
8285
generate(
8386
"template/AutoFactory",
8487
AutoFactoryModel(implementation.qualifiedName.toString(), args),
85-
Language.KOTLIN
88+
Source.Kotlin
8689
)
8790
}
8891
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
io.johnsonlee.codegen.example.AutoFactoryCodegen,isolating

example/src/main/resources/META-INF/incremental.annotation.processors

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-6.2-bin.zip
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists

0 commit comments

Comments
 (0)