【问题标题】:Kotlin KSP annotation Processor generated class not found during jupiter test在木星测试期间未找到 Kotlin KSP 注释处理器生成的类
【发布时间】:2022-06-21 23:42:23
【问题描述】:

我正在为 Kotlin 开发一个 KSP 注释处理器。代码在编译期间正确生成,我可以在输出目录中看到生成的类。现在我想通过 JUnit 和“com.github.tschuchortdev.KotlinCompilation”测试我的注释处理器。如果我调用 compile 方法,将生成代码,我可以在 Temp-Directory 中看到生成的类,但如果我尝试加载该类,则会收到“java.lang.ClassNotFoundException:test.pack.TestClassDslBuilder”异常。我希望代码是不言自明的。 我的问题是:为什么类没有编译并且不可加载?可能缺少 kompiler 的配置。

    @BeforeEach
    fun setup() {
        val kotlinSource = SourceFile.kotlin(
            "TestClass.kt", """
                package test.pack

                import yy.xxx.dsl.builder.annotation.DslBuilder
                @DslBuilder        
                class TestClass {

                }
            """
        )

        val compilation = KotlinCompilation().apply {
            sources = listOf(kotlinSource)
            symbolProcessorProviders = listOf(DslBuilderProcessorProvider())
            //workingDir =
            inheritClassPath = true
            verbose = false
            //messageOutputStream = System.out
            kspIncremental = true
        }
        compilationResult = compilation.compile()
        assertEquals(KotlinCompilation.ExitCode.OK, compilationResult.exitCode)

        // The next line leads to java.lang.ClassNotFoundException 
        compilationResult.classLoader.loadClass("test.pack.TestClassDslBuilder")
    }

【问题讨论】:

标签: kotlin junit ksp


【解决方案1】:

我也有同样的问题。 没有修复它,但在我看来,这个问题出在生成文件的路径中(你写的像字符串一样),就像在谷歌编译测试中一样,当你尝试找到 generateFile() 时,使用 StandardLocation.SOURCE_OUTPUT 很重要. 但就我而言,它不会生成任何东西——我的意思是 KotlinCompilation.Result 中的 generatedFiles.size() == 0

【讨论】:

    猜你喜欢
    • 2017-05-22
    • 1970-01-01
    • 1970-01-01
    • 2011-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多