英文:
Can AbstractProcessor detect if annotated method has compilation errors?
问题
我的AbstractProcessor
实现会被调用,即使被注解的方法中包含会导致编译错误的代码。(即,处理器会被注解的方法触发,其目标为ElementType.METHOD
)。
经过一些实验,我有一种印象,方法体中的语法错误会导致AbstractProcessor
不被触发,而“引用错误”,比如尝试调用无法访问的私有方法,会导致AbstractProcessor
被调用。
我很高兴AbstractProcessor
被调用了,但我需要知道被注解的方法是否包含任何错误。
我有两个问题:
- 我的
AbstractProcessor
代码如何知道我所在的方法上的ExecutableElement
是否包含错误?我知道Google Auto的SuperficialValidation,但我无法让它检测出这些错误 - 也许它只适用于TypeElements
? - 我能否确定所有编译器版本在处理哪种类型的错误时都有相同的行为,以防止
AbstractProcessor
被调用,以及哪些错误会使其执行其逻辑?
英文:
My AbstractProcessor
implementation gets called even though the annotated method contains code that results in compiler errors. (I.e. the processor is triggered by the presence of an annotation whose target is ElementType.METHOD
).
Having experimented a bit I get the impression that syntax errors in the method body results in the AbstractProcessor not being triggered, whereas "reference errors", fx trying to call a private method that can't be reached, does result in the AbstractProcessor being called.
I am glad that the AbstractProcessor is being called, but I need to know if the annotated method contains any errors.
I have two questions:
- How can my AbstractProcessor code know, if the ExecutableElement my method is found on, contains errors? I am aware of Google Auto's SuperficialValidation, but I can't get it to detect these errors - perhaps it only works on TypeElements?
- Can I be sure that all compiler versions have the same behavior with regards to with kinds of errors prevents the AbstractProcessor from being called, and which ones will let it perform its logic?
答案1
得分: 1
The JavaCompiler has multiple phases that run.
You can see if the processing is complete from the RoundEnvironment.processingOver()
and walk the tree using a TreePathScanner
.
However a lot of the actual errors are found after annotation processing is complete and can be found in the diagnostics. You might be able to find some information by supplying a DiagnosticListener
.
There are some ways to detect that an error has occurred from inspecting the Symbol/Type
of the symbol.
Using a TreePathScanner
inside the visitMethodInvocation
you would expect the symbol to be a method, but if that method doesn't exist it may be null or a ClassSymbol
.
英文:
The JavaCompiler has multiple phases that run.
You can see if the processing is complete from the RoundEnvironment.processingOver()
and walk the tree using a TreePathScanner
.
However a lot of the actual errors are found after annotation processing is complete and can be found in the diagnostics. You might be able to find some information by supplying a DiagnosticListener
.
There are some ways to detect that an error has occurred from inspecting the Symbol/Type
of the symbol.
Using a TreePathScanner
inside the visitMethodInvocatio
you would expect the symbol to be a method, but if that method doesn't exist it may be null or a ClassSymbol
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论