1 package dagger.functional.kotlin.processor
2 
3 import com.google.auto.service.AutoService
4 import com.squareup.javapoet.JavaFile
5 import com.squareup.javapoet.TypeSpec
6 import javax.annotation.processing.AbstractProcessor
7 import javax.annotation.processing.Processor
8 import javax.annotation.processing.RoundEnvironment
9 import javax.lang.model.SourceVersion
10 import javax.lang.model.element.Modifier
11 import javax.lang.model.element.TypeElement
12 
13 /**
14  * A processor to be used in functional tests that will generate a simple class with fqname
15  * 'dagger.functional.kotlin.GeneratedType'. This processor is useful for testing situations
16  * with KAPT where a type is not resolvable and for which KAPT will generate stubs containing
17  * the 'error.NonExistentClass' type.
18  */
19 @AutoService(Processor::class)
20 class TestGeneratedTypeProcessor : AbstractProcessor() {
21 
22   private var isSourceGenerated = false
23 
getSupportedAnnotationTypesnull24   override fun getSupportedAnnotationTypes() =
25     mutableSetOf("dagger.functional.kotlin.processor.TriggerGeneratedTypeProcessor")
26 
27   override fun getSupportedSourceVersion() = SourceVersion.latestSupported()
28 
29   override fun process(
30     annotations: MutableSet<out TypeElement>,
31     roundEnv: RoundEnvironment
32   ): Boolean {
33     if (isSourceGenerated) {
34       return false
35     }
36 
37     JavaFile.builder(
38       "dagger.functional.kotlin",
39       TypeSpec.classBuilder("GeneratedType")
40         .addModifiers(Modifier.PUBLIC, Modifier.FINAL)
41         .build()
42     ).build().writeTo(processingEnv.filer)
43 
44     isSourceGenerated = true
45 
46     return false
47   }
48 }
49