Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
Johnson Lee committed May 30, 2022

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 67de01e commit c7b6e6a
Showing 10 changed files with 79 additions and 28 deletions.
22 changes: 20 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -9,10 +9,28 @@ buildscript {

dependencies {
classpath("com.android.tools.build:gradle:4.0.0")
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.31")
classpath("io.johnsonlee:sonatype-publish-plugin:1.5.6")
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:${embeddedKotlinVersion}")
classpath("io.johnsonlee:sonatype-publish-plugin:1.6.2")
}
}

group = "io.johnsonlee.codegen"
version = project.properties["version"]?.takeIf { it != DEFAULT_VERSION } ?: "1.0.0-SNAPSHOT"

allprojects {
group = rootProject.group
version = rootProject.version

repositories {
mavenCentral()
google()
}

tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict", "-Xskip-metadata-version-check")
jvmTarget = "1.8"
}
}

}
Original file line number Diff line number Diff line change
@@ -22,6 +22,7 @@ abstract class CodegenProcessor<T : Annotation> : AbstractProcessor() {

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

protected open fun onPostProcessing(processingEnv: ProcessingEnvironment) = Unit

protected fun <T : Model> generate(template: String, model: T, lang: Language = Language.JAVA) {
protected fun <T : Model> generate(template: String, model: T, output: Output) {
val originatingElements = model.references.map {
processingEnv.elementUtils.getTypeElement(it)
}.toTypedArray()
when (lang) {
Language.JAVA -> generateJavaSources(model, originatingElements)
else -> generateSources(lang, model, originatingElements)
}.openWriter().use { writer ->
engine.render("${template}.${lang.extension}.${engine.extension.lowercase()}", model, writer)
when (output) {
is Source -> when (output) {
Source.Java -> generateJavaSources(model, originatingElements)
else -> generateSources(output, model, originatingElements)
}
is Resource -> generateResource(output, model, originatingElements)
else -> null
}?.openWriter()?.use { writer ->
val ext = if (output.extension.isNotBlank()) ".${output.extension}" else ""
engine.render("${template}${ext}.${engine.extension.toLowerCase()}", model, writer)
writer.close()
}
}
@@ -86,7 +92,7 @@ abstract class CodegenProcessor<T : Annotation> : AbstractProcessor() {
}

private fun <T : Model> generateSources(
lang: Language,
source: Source,
model: T,
originatingElements: Array<TypeElement>
): FileObject {
@@ -95,10 +101,20 @@ abstract class CodegenProcessor<T : Annotation> : AbstractProcessor() {
return processingEnv.filer.createResource(
StandardLocation.SOURCE_OUTPUT,
pkg,
"${name}.${lang.extension}",
"${name}.${source.extension}",
*originatingElements
)
}

private fun <T : Model> generateResource(
resource: Resource,
model: T,
originatingElements: Array<TypeElement>
): FileObject = processingEnv.filer.createResource(
StandardLocation.CLASS_OUTPUT,
"",
"${resource.prefix}${File.separator}${model.qualifiedName}",
*originatingElements
)

}
Original file line number Diff line number Diff line change
@@ -7,6 +7,8 @@ import javax.lang.model.element.TypeElement

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

inline fun <reified T : Annotation> Element.getAnnotationMirror(): AnnotationMirror {
return MoreElements.getAnnotationMirror(this, T::class.java).get()
inline fun <reified T : Annotation> Element.getAnnotationMirror(): AnnotationMirror? {
return MoreElements.getAnnotationMirror(this, T::class.java).takeIf {
it.isPresent
}?.get()
}

This file was deleted.

16 changes: 16 additions & 0 deletions compiler/src/main/kotlin/io/johnsonlee/codegen/compiler/Output.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.johnsonlee.codegen.compiler

interface Output {
val extension: String
}

sealed class Source(override val extension: String) : Output {
object Java: Source("java")
object Kotlin: Source("kt")
object KotlinScript: Source("kts")
object Groovy: Source("groovy")
}

interface Resource : Output {
val prefix: String
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.johnsonlee.codegen.compiler

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

fun ProcessingEnvironment.fatal(e: Throwable) {
messager.printMessage(Diagnostic.Kind.ERROR, "FATAL ERROR: ${e.stackTraceToString()}")
val stack = StringWriter().run {
e.printStackTrace(PrintWriter(this))
toString()
}
messager.printMessage(Diagnostic.Kind.ERROR, "FATAL ERROR: $stack")
}
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ package io.johnsonlee.codegen.example

import com.google.auto.service.AutoService
import io.johnsonlee.codegen.compiler.CodegenProcessor
import io.johnsonlee.codegen.compiler.Language
import io.johnsonlee.codegen.compiler.Source
import io.johnsonlee.codegen.compiler.asElement
import io.johnsonlee.codegen.compiler.asTypeElement
import io.johnsonlee.codegen.compiler.error
@@ -14,13 +14,16 @@ import io.johnsonlee.codegen.template.mustache.MustacheEngine
import javax.annotation.processing.ProcessingEnvironment
import javax.annotation.processing.Processor
import javax.annotation.processing.RoundEnvironment
import javax.annotation.processing.SupportedSourceVersion
import javax.lang.model.SourceVersion
import javax.lang.model.element.AnnotationMirror
import javax.lang.model.element.ElementKind
import javax.lang.model.element.ExecutableElement
import javax.lang.model.element.TypeElement
import javax.lang.model.type.DeclaredType

@AutoService(Processor::class)
@SupportedSourceVersion(SourceVersion.RELEASE_8)
class AutoFactoryCodegen : CodegenProcessor<AutoFactory>() {

private val mustache: TemplateEngine by lazy(::MustacheEngine)
@@ -34,7 +37,7 @@ class AutoFactoryCodegen : CodegenProcessor<AutoFactory>() {
) {
roundEnv.onEachAnnotatedElement { element ->
val implementation = element.asTypeElement()
val mirror = element.getAnnotationMirror<AutoFactory>()
val mirror = element.getAnnotationMirror<AutoFactory>() ?: return@onEachAnnotatedElement
mirror.value.takeIf(Set<DeclaredType>::isNotEmpty)?.map(DeclaredType::asTypeElement)?.onEach { api ->
if (checkImplementation(implementation, api)) {
generateFactory(implementation, mirror)
@@ -82,7 +85,7 @@ class AutoFactoryCodegen : CodegenProcessor<AutoFactory>() {
generate(
"template/AutoFactory",
AutoFactoryModel(implementation.qualifiedName.toString(), args),
Language.KOTLIN
Source.Kotlin
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
io.johnsonlee.codegen.example.AutoFactoryCodegen,isolating

This file was deleted.

2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.2-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

0 comments on commit c7b6e6a

Please sign in to comment.