Skip to content

Commit 12c86f6

Browse files
authored
More little cleanups and consolidating options (#11)
1 parent 60fbcec commit 12c86f6

File tree

3 files changed

+60
-43
lines changed

3 files changed

+60
-43
lines changed

src/main/kotlin/com/slack/auto/value/kotlin/AutoValueKotlinExtension.kt

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -50,23 +50,17 @@ import javax.lang.model.util.Elements
5050
import javax.lang.model.util.Types
5151
import javax.tools.Diagnostic
5252

53-
public class AutoValueKotlinExtension(private val realMessager: Messager) : AutoValueExtension() {
54-
55-
public companion object {
56-
// Options
57-
public const val OPT_SRC: String = "avkSrc"
58-
public const val OPT_TARGETS: String = "avkTargets"
59-
public const val OPT_IGNORE_NESTED: String = "avkIgnoreNested"
60-
}
53+
public class AutoValueKotlinExtension(
54+
private val realMessager: Messager,
55+
private val options: Options
56+
) : AutoValueExtension() {
6157

6258
internal val collectedKclassees = ConcurrentHashMap<ClassName, KotlinClass>()
6359
internal val collectedEnums = ConcurrentHashMap<ClassName, TypeSpec>()
6460
private lateinit var elements: Elements
6561
private lateinit var types: Types
6662

67-
override fun getSupportedOptions(): Set<String> {
68-
return setOf(OPT_SRC, OPT_TARGETS, OPT_IGNORE_NESTED)
69-
}
63+
override fun getSupportedOptions(): Set<String> = Options.ALL
7064

7165
override fun incrementalType(processingEnvironment: ProcessingEnvironment): IncrementalExtensionType {
7266
// This is intentionally not incremental, we are generating into source sets directly
@@ -91,11 +85,6 @@ public class AutoValueKotlinExtension(private val realMessager: Messager) : Auto
9185
classToExtend: String,
9286
isFinal: Boolean
9387
): String? {
94-
val options = Options(context.processingEnvironment().options)
95-
96-
val ignoreNested =
97-
context.processingEnvironment().options[OPT_IGNORE_NESTED]?.toBoolean() ?: false
98-
9988
if (options.targets.isNotEmpty() && context.autoValueClass().simpleName.toString() !in options.targets) {
10089
return null
10190
}
@@ -109,7 +98,7 @@ public class AutoValueKotlinExtension(private val realMessager: Messager) : Auto
10998
AutoValue::class.java
11099
)
111100
if (!isParentAv) {
112-
val diagnosticKind = if (ignoreNested) {
101+
val diagnosticKind = if (options.ignoreNested) {
113102
Diagnostic.Kind.WARNING
114103
} else {
115104
Diagnostic.Kind.ERROR

src/main/kotlin/com/slack/auto/value/kotlin/AutoValueKotlinProcessor.kt

Lines changed: 51 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import javax.lang.model.element.NestingKind
4848
import javax.lang.model.element.NestingKind.TOP_LEVEL
4949
import javax.lang.model.element.TypeElement
5050
import javax.tools.Diagnostic.Kind
51+
import javax.tools.Diagnostic.Kind.ERROR
5152
import javax.tools.FileObject
5253
import javax.tools.JavaFileManager.Location
5354
import javax.tools.JavaFileObject
@@ -60,6 +61,12 @@ public class AutoValueKotlinProcessor : AbstractProcessor() {
6061

6162
private val collectedClasses: MutableMap<ClassName, KotlinClass> = ConcurrentHashMap<ClassName, KotlinClass>()
6263
private val collectedEnums: MutableMap<ClassName, TypeSpec> = ConcurrentHashMap<ClassName, TypeSpec>()
64+
private lateinit var options: Options
65+
66+
override fun init(processingEnv: ProcessingEnvironment) {
67+
super.init(processingEnv)
68+
options = Options(processingEnv.options)
69+
}
6370

6471
override fun getSupportedAnnotationTypes(): Set<String> {
6572
return setOf(AutoValue::class.java.canonicalName)
@@ -69,38 +76,24 @@ public class AutoValueKotlinProcessor : AbstractProcessor() {
6976
return SourceVersion.latest()
7077
}
7178

79+
@Suppress("ReturnCount")
7280
override fun process(
7381
annotations: Set<TypeElement>,
7482
roundEnv: RoundEnvironment
7583
): Boolean {
76-
// Load extensions ourselves
77-
@Suppress("TooGenericExceptionCaught", "SwallowedException")
78-
val extensions = try {
79-
ServiceLoader.load(AutoValueExtension::class.java).toList()
80-
} catch (e: Exception) {
81-
emptyList()
84+
val avClasses = roundEnv.getElementsAnnotatedWith(AutoValue::class.java)
85+
if (avClasses.isNotEmpty()) {
86+
processAvElements(annotations, roundEnv, avClasses)
8287
}
8388

84-
// Make our extension
85-
val avkExtension = AutoValueKotlinExtension(processingEnv.messager)
86-
87-
// Create an in-memory av processor and run it
88-
val avProcessor = AutoValueProcessor(extensions + avkExtension)
89-
avProcessor.init(object : ProcessingEnvironment by processingEnv {
90-
override fun getMessager(): Messager = NoOpMessager
91-
92-
override fun getFiler(): Filer = NoOpFiler
93-
})
94-
avProcessor.process(annotations, roundEnv)
95-
96-
// Save off our extracted classes
97-
collectedClasses += avkExtension.collectedKclassees
98-
collectedEnums += avkExtension.collectedEnums
99-
10089
// We're done processing, write all our collected classes down
10190
if (roundEnv.processingOver()) {
91+
if (collectedClasses.isEmpty()) {
92+
processingEnv.messager.printMessage(ERROR, "No AutoValue classes found")
93+
return false
94+
}
10295
val srcDir =
103-
processingEnv.options[AutoValueKotlinExtension.OPT_SRC] ?: error("Missing src dir option")
96+
processingEnv.options[Options.OPT_SRC] ?: error("Missing src dir option")
10497
val roots = collectedClasses.filterValues { it.isTopLevel }
10598
.toMutableMap()
10699
for ((_, root) in roots) {
@@ -128,6 +121,41 @@ public class AutoValueKotlinProcessor : AbstractProcessor() {
128121
}
129122
.build()
130123
}
124+
125+
private fun processAvElements(
126+
annotations: Set<TypeElement>,
127+
roundEnv: RoundEnvironment,
128+
avClasses: Set<Element>
129+
) {
130+
if (options.targets.isNotEmpty() && avClasses.none { it.simpleName.toString() in options.targets }) {
131+
// Nothing to do this round
132+
return
133+
}
134+
135+
// Load extensions ourselves
136+
@Suppress("TooGenericExceptionCaught", "SwallowedException")
137+
val extensions = try {
138+
ServiceLoader.load(AutoValueExtension::class.java, AutoValueExtension::class.java.classLoader).toList()
139+
} catch (e: Exception) {
140+
emptyList()
141+
}
142+
143+
// Make our extension
144+
val avkExtension = AutoValueKotlinExtension(processingEnv.messager, options)
145+
146+
// Create an in-memory av processor and run it
147+
val avProcessor = AutoValueProcessor(extensions + avkExtension)
148+
avProcessor.init(object : ProcessingEnvironment by processingEnv {
149+
override fun getMessager(): Messager = NoOpMessager
150+
151+
override fun getFiler(): Filer = NoOpFiler
152+
})
153+
avProcessor.process(annotations, roundEnv)
154+
155+
// Save off our extracted classes
156+
collectedClasses += avkExtension.collectedKclassees
157+
collectedEnums += avkExtension.collectedEnums
158+
}
131159
}
132160

133161
private object NoOpMessager : Messager {

src/test/kotlin/com/slack/auto/value/kotlin/AutoValueKotlinExtensionTest.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,7 +1154,7 @@ class AutoValueKotlinExtensionTest {
11541154
""".trimIndent()
11551155
)
11561156
) {
1157-
withOptions(listOf(compilerSrcOption(), "-A${AutoValueKotlinExtension.OPT_IGNORE_NESTED}=true"))
1157+
withOptions(listOf(compilerSrcOption(), "-A${Options.OPT_IGNORE_NESTED}=true"))
11581158
}
11591159

11601160
result.succeeded()
@@ -1191,7 +1191,7 @@ class AutoValueKotlinExtensionTest {
11911191
""".trimIndent()
11921192
)
11931193
) {
1194-
withOptions(listOf(compilerSrcOption(), "-A${AutoValueKotlinExtension.OPT_TARGETS}=Example"))
1194+
withOptions(listOf(compilerSrcOption(), "-A${Options.OPT_TARGETS}=Example"))
11951195
}
11961196

11971197
result.succeeded()
@@ -1288,7 +1288,7 @@ class AutoValueKotlinExtensionTest {
12881288
}
12891289

12901290
private fun compilerSrcOption(): String {
1291-
return "-A${AutoValueKotlinExtension.OPT_SRC}=${srcDir.absolutePath}"
1291+
return "-A${Options.OPT_SRC}=${srcDir.absolutePath}"
12921292
}
12931293

12941294
private fun compile(vararg sourceFiles: JavaFileObject, compilerBody: Compiler.() -> Compiler = { this }): CompilationSubject {

0 commit comments

Comments
 (0)