@@ -48,6 +48,7 @@ import javax.lang.model.element.NestingKind
4848import javax.lang.model.element.NestingKind.TOP_LEVEL
4949import javax.lang.model.element.TypeElement
5050import javax.tools.Diagnostic.Kind
51+ import javax.tools.Diagnostic.Kind.ERROR
5152import javax.tools.FileObject
5253import javax.tools.JavaFileManager.Location
5354import 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
133161private object NoOpMessager : Messager {
0 commit comments