Skip to content

Commit 38c3c47

Browse files
authored
refactor: add extension method to retrieve stringified binary name for TypeElement (#7)
1 parent 0726965 commit 38c3c47

5 files changed

Lines changed: 96 additions & 3 deletions

File tree

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
kotlin.code.style=official
22
group=io.github.eventhorizonlab
3-
baseVersion=0.1.19
3+
baseVersion=0.1.20
44
org.gradle.jvmargs=-Xmx2g -Dfile.encoding=UTF-8

modules/processor/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ dependencies {
2929
testImplementation 'io.kotest:kotest-framework-engine:6.0.1'
3030
testImplementation 'io.kotest:kotest-assertions-core:6.0.1'
3131
testImplementation "dev.zacsweers.kctfork:core:0.8.0"
32+
testImplementation "io.mockk:mockk:1.14.5"
3233
}
3334

3435
var args = [

modules/processor/src/main/kotlin/com/github/eventhorizonlab/spi/ServiceSchemeProcessor.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.github.eventhorizonlab.spi
22

3+
import com.github.eventhorizonlab.spi.extensions.getStringifiedBinaryName
34
import com.google.auto.service.AutoService
45
import java.lang.annotation.Repeatable
56
import javax.annotation.processing.*
@@ -116,8 +117,8 @@ class ServiceSchemeProcessor : AbstractProcessor() {
116117

117118
private fun addProvider(providerElement: TypeElement, contractElement: TypeElement) {
118119
val contractCanonical = contractElement.qualifiedName.toString()
119-
val contractBinary = processingEnv.elementUtils.getBinaryName(contractElement).toString()
120-
val providerBinary = processingEnv.elementUtils.getBinaryName(providerElement).toString()
120+
val contractBinary = processingEnv.getStringifiedBinaryName(contractElement)
121+
val providerBinary = processingEnv.getStringifiedBinaryName(providerElement)
121122

122123
providers += ProviderInfo(contractCanonical, contractBinary, providerBinary)
123124
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.github.eventhorizonlab.spi.extensions
2+
3+
import javax.annotation.processing.ProcessingEnvironment
4+
import javax.lang.model.element.TypeElement
5+
6+
internal fun ProcessingEnvironment.getStringifiedBinaryName(type: TypeElement) =
7+
elementUtils.getBinaryName(type).toString()
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package com.github.eventhorizonlab.spi.extensions
2+
3+
import com.tschuchort.compiletesting.KotlinCompilation
4+
import com.tschuchort.compiletesting.SourceFile
5+
import io.kotest.core.spec.style.FunSpec
6+
import io.kotest.datatest.withData
7+
import io.kotest.matchers.shouldBe
8+
import org.jetbrains.kotlin.compiler.plugin.ExperimentalCompilerApi
9+
import javax.annotation.processing.AbstractProcessor
10+
import javax.annotation.processing.ProcessingEnvironment
11+
import javax.annotation.processing.RoundEnvironment
12+
import javax.lang.model.element.TypeElement
13+
14+
@OptIn(ExperimentalCompilerApi::class)
15+
class ProcessingEnvironmentExtensionsTests : FunSpec({
16+
17+
data class Case(
18+
val description: String,
19+
val source: SourceFile,
20+
val fqName: String,
21+
val expectedBinaryName: String
22+
)
23+
24+
val cases = listOf(
25+
Case(
26+
"Top-level Kotlin class",
27+
SourceFile.kotlin("TopLevel.kt", """
28+
package com.test
29+
class TopLevel
30+
""".trimIndent()),
31+
fqName = "com.test.TopLevel",
32+
expectedBinaryName = "com.test.TopLevel"
33+
),
34+
Case(
35+
"Nested Kotlin class",
36+
SourceFile.kotlin("Outer.kt", """
37+
package com.test
38+
class Outer {
39+
class Inner
40+
}
41+
""".trimIndent()),
42+
fqName = "com.test.Outer.Inner",
43+
expectedBinaryName = $$"com.test.Outer$Inner"
44+
),
45+
Case(
46+
"Inner (non-static) Java class",
47+
SourceFile.java("OuterJava.java", """
48+
package com.test;
49+
public class OuterJava {
50+
public class InnerJava {}
51+
}
52+
""".trimIndent()),
53+
fqName = "com.test.OuterJava.InnerJava",
54+
expectedBinaryName = $$"com.test.OuterJava$InnerJava"
55+
)
56+
)
57+
58+
withData(nameFn = { it.description }, cases) { case ->
59+
var actual: String? = null
60+
61+
val processor = object : AbstractProcessor() {
62+
override fun init(processingEnv: ProcessingEnvironment) {
63+
super.init(processingEnv)
64+
val type = processingEnv.elementUtils.getTypeElement(case.fqName)
65+
if (type != null) {
66+
actual = processingEnv.getStringifiedBinaryName(type)
67+
}
68+
}
69+
override fun process(
70+
annotations: MutableSet<out TypeElement>,
71+
roundEnv: RoundEnvironment
72+
) = false
73+
}
74+
75+
val result = KotlinCompilation().apply {
76+
sources = listOf(case.source)
77+
annotationProcessors = listOf(processor)
78+
inheritClassPath = true
79+
}.compile()
80+
81+
result.exitCode shouldBe KotlinCompilation.ExitCode.OK
82+
actual shouldBe case.expectedBinaryName
83+
}
84+
})

0 commit comments

Comments
 (0)