我正在使用自定义注释处理器,但遇到了一个新问题。 我正在使用 Gradle (3.1.1),当我将处理器生成的文件的路径添加到 sourceSet 时,我遇到了奇怪的问题。
我执行了一个gradle clean build
,然后是一个gradle build
,然后构建被销毁了。
令我惊讶的是,我得到的堆栈跟踪非常常见。
An exception has occurred in the compiler (1.8.0_91). Please file a bug against the Java compiler via the Java bug reporting page (http://bugreport.java.com) after checking the Bug Database (http://bugs.java.com) for duplicates. Include your program and the following diagnostic in your report. Thank you.
java.lang.IllegalStateException: endPosTable already set
at com.sun.tools.javac.util.DiagnosticSource.setEndPosTable(DiagnosticSource.java:136)
at com.sun.tools.javac.util.Log.setEndPosTable(Log.java:350)
at com.sun.tools.javac.main.JavaCompiler.parse(JavaCompiler.java:667)
at com.sun.tools.javac.main.JavaCompiler.parseFiles(JavaCompiler.java:950)
at com.sun.tools.javac.processing.JavacProcessingEnvironment$Round.<init>(JavacProcessingEnvironment.java:892)
at com.sun.tools.javac.processing.JavacProcessingEnvironment$Round.next(JavacProcessingEnvironment.java:921)
at com.sun.tools.javac.processing.JavacProcessingEnvironment.doProcessing(JavacProcessingEnvironment.java:1187)
at com.sun.tools.javac.main.JavaCompiler.processAnnotations(JavaCompiler.java:1170)
at com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:856)
at com.sun.tools.javac.main.Main.compile(Main.java:523)
at com.sun.tools.javac.main.Main.compile(Main.java:381)
at com.sun.tools.javac.main.Main.compile(Main.java:370)
at com.sun.tools.javac.main.Main.compile(Main.java:361)
at com.sun.tools.javac.Main.compile(Main.java:56)
at com.sun.tools.javac.Main.main(Main.java:42)
如果我在构建之前总是清理我没有问题,而且当我不添加源集时构建总是成功的。 我什至尝试在我的注释处理器中创建新生成的文件之前删除该文件,但这也没有成功。
在研究过程中,我还发现了这个有趣的链接: JDK Bug report
但是考虑到我要么总是先清理,要么将它从我的 sourceSet 中删除并不是很好。因为 Eclipse 不喜欢找不到按照约定应该存在的文件。
你对如何解决这个问题有什么建议吗?
最佳答案
由于这是 JDK 8 的官方 Bug,旨在在 JDK 9 中解决,因此我实现了一个解决方法,如果文件已存在,则使用 gradle 删除该文件。处理器不再有问题,我可以将它保存在我的 sourceSet 中。
问候:)
编辑:解决方法本身。
gradle.ext.generatedQueriesDir = 'build/generated-sources/local/query'
/*
* This is a workaround to delete the file that will be created by the annotation processor if it already exists.
* There is a known bug in the Java compiler and JDK 8 which should be fixed at JDK 9.
* http://bugs.java.com/bugdatabase/view_bug.do?bug_id=8067747 <-- Master report
* http://bugs.java.com/bugdatabase/view_bug.do?bug_id=8146348 <-- duplicates master report
*/
if ( file( gradle.ext.generatedQueriesDir ).exists() ) {
FileCollection collection = files { file( gradle.ext.generatedQueriesDir ).listFiles() }
collection.each { delete it }
}
我们使用 gradle,因此这是 gradle 脚本的解决方法。但基本上解决方法是删除在开始编译和预编译步骤之前将生成的文件。
关于java - 添加 sourceSet 时已经设置了 endPosTable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41219903/