tracer.util.KspProvider.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ksp-util Show documentation
Show all versions of ksp-util Show documentation
"Remember to use with my sample which hasn't come out yet."
package tracer.util
import com.google.devtools.ksp.getClassDeclarationByName
import com.google.devtools.ksp.processing.*
import com.google.devtools.ksp.symbol.KSAnnotated
import java.io.File
import java.util.*
public lateinit var Environment: SymbolProcessorEnvironment
private set
public lateinit var resolver: Resolver
private val allCaches = mutableListOf>()
public val CodeGenerator.previousGeneratedFiles: MutableList by lazy { mutableListOf() }
/**
* Caches [this], and clear it every round, for saving KSNodes which may be invalid in the next round.
*/
@Synchronized
public fun > T.alsoRegister(): T = apply { allCaches += this }
// todo: introduce this into the authoritative ksp library.
/**
* A simplified and optimized [SymbolProcessorProvider].
*/
public abstract class KspProvider(
private val getTestProcessor: (()->KspProcessor)?,
private val getProcessor: ()->KspProcessor,
) : SymbolProcessorProvider {
// todo: switch the implementation detailTestTag to a gradle argument
// and here returns that SymbolProcessor directly.
override fun create(environment: SymbolProcessorEnvironment): SymbolProcessor = object : SymbolProcessor {
init {
Environment = environment
}
var times = 0
lateinit var processor: KspProcessor
override fun process(resolver: Resolver): List {
tracer.util.resolver = resolver
return try {
@Suppress("UNNECESSARY_NOT_NULL_ASSERTION")
if (++times == 1){
processor =
if (getTestProcessor != null
&& resolver.getClassDeclarationByName("test.KspUnitTestModuleTag") != null
)
//todo: report: here nullability inference is invalid.
getTestProcessor!!()
else
getProcessor()
}
processor.process(times)
} catch (e: Exception) {
environment.logger.error(
message = "$e\n ${e.stackTrace.joinToString("\n") { "at $it" }}",
symbol = null
)
emptyList()
} finally {
allCaches.forEach { it.clear() }
environment.codeGenerator.previousGeneratedFiles += environment.codeGenerator.generatedFile
}
}
override fun finish() {
processor.onFinish()
}
override fun onError() {
if (::processor.isInitialized)
processor.onErrorExceptSelfInitialization()
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy