Kotlin注释处理器不工作?我错过了什么?
最近,我一直试图通过Kotlin制作一个注释处理器,但我似乎无法让它工作。所有的东西都会编译,我没有得到任何错误,但是当我检查jar文件的内容时,我看不到我试图创建的资源 我已经试了好几个小时了,我真的被卡住了,所以我只是想寻求帮助:/我确实有一个随机的类被注释,看看它是否有效,但没有运气 注释类Kotlin注释处理器不工作?我错过了什么?,kotlin,annotations,kapt,Kotlin,Annotations,Kapt,最近,我一直试图通过Kotlin制作一个注释处理器,但我似乎无法让它工作。所有的东西都会编译,我没有得到任何错误,但是当我检查jar文件的内容时,我看不到我试图创建的资源 我已经试了好几个小时了,我真的被卡住了,所以我只是想寻求帮助:/我确实有一个随机的类被注释,看看它是否有效,但没有运气 注释类 @Retention(AnnotationRetention.RUNTIME) @Target(AnnotationTarget.CLASS) annotation class TestAnnotat
@Retention(AnnotationRetention.RUNTIME)
@Target(AnnotationTarget.CLASS)
annotation class TestAnnotation
处理器类
@AutoService(TestAnnotation::class)
class TestAnnotationProcessor : AbstractProcessor() {
override fun process(annotations: MutableSet<out TypeElement>, environment: RoundEnvironment): Boolean {
this.processingEnv.filer.createResource(StandardLocation.CLASS_OUTPUT, "", "test.txt")
return true
}
override fun getSupportedSourceVersion() = SourceVersion.RELEASE_8
override fun getSupportedAnnotationTypes() = setOf(TestAnnotation::class.java.canonicalName)
}
@AutoService(TestAnnotation::class)
类TestAnnotationProcessor:AbstractProcessor(){
重写有趣的过程(注释:MutableSet,环境:RoundEnvironment):布尔{
this.processingEnv.filer.createResource(StandardLocation.CLASS_输出,“,”test.txt“)
返回真值
}
覆盖getSupportedSourceVersion()=SourceVersion.RELEASE_8
重写fun getSupportedAnnotationTypes()=setOf(TestAnnotation::class.java.canonicalName)
}
我的身材.gradle.kts
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
plugins {
java
kotlin("jvm") version "1.3.50"
kotlin("kapt") version "1.3.50"
id("com.github.johnrengelman.shadow") version "5.1.0"
}
group = "com.example.test"
version = "1.0"
repositories {
mavenCentral()
}
dependencies {
implementation(kotlin("stdlib-jdk8"))
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.1")
implementation("com.google.auto.service:auto-service:1.0-rc6")
kapt("com.google.auto.service:auto-service:1.0-rc6")
}
configure<JavaPluginConvention> {
sourceCompatibility = JavaVersion.VERSION_1_8
}
tasks.withType<KotlinCompile> {
kotlinOptions.jvmTarget = "1.8"
}
tasks.withType<ShadowJar> {
dependencies {
exclude(dependency("com.google.auto.service:auto-service:1.0-rc6"))
}
}
import org.jetbrains.kotlin.gradle.tasks.kotlincomfile
导入com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
插件{
JAVA
kotlin(“jvm”)版本“1.3.50”
kotlin(“kapt”)版本“1.3.50”
id(“com.github.johnrengelman.shadow”)版本“5.1.0”
}
group=“com.example.test”
version=“1.0”
存储库{
mavenCentral()
}
依赖关系{
实施(kotlin(“stdlib-jdk8”))
实现(“org.jetbrains.kotlinx:kotlinx协同程序核心:1.3.1”)
实现(“com.google.auto.service:auto-service:1.0-rc6”)
kapt(“com.google.auto.service:auto-service:1.0-rc6”)
}
配置{
sourceCompatibility=JavaVersion.VERSION\u 1\u 8
}
tasks.withType{
kotlinOptions.jvmTarget=“1.8”
}
tasks.withType{
依赖关系{
排除(依赖项(“com.google.auto.service:auto-service:1.0-rc6”))
}
}
为了让自定义注释处理器工作,您必须在构建脚本依赖项中使用它
dependencies {
kapt project(':processor-module') // or what ever your processor's module is named.
//OR
kapt 'gourp:artifact:version' // if your processor is published into a maven repository.
}