抽象处理器能否检测带有编译错误的已注释方法?

huangapple go评论73阅读模式
英文:

Can AbstractProcessor detect if annotated method has compilation errors?

问题

我的AbstractProcessor实现会被调用,即使被注解的方法中包含会导致编译错误的代码。(即,处理器会被注解的方法触发,其目标为ElementType.METHOD)。

经过一些实验,我有一种印象,方法体中的语法错误会导致AbstractProcessor不被触发,而“引用错误”,比如尝试调用无法访问的私有方法,会导致AbstractProcessor被调用。

我很高兴AbstractProcessor被调用了,但我需要知道被注解的方法是否包含任何错误。

我有两个问题:

  1. 我的AbstractProcessor代码如何知道我所在的方法上的ExecutableElement是否包含错误?我知道Google Auto的SuperficialValidation,但我无法让它检测出这些错误 - 也许它只适用于TypeElements
  2. 我能否确定所有编译器版本在处理哪种类型的错误时都有相同的行为,以防止AbstractProcessor被调用,以及哪些错误会使其执行其逻辑?
英文:

My AbstractProcessorimplementation 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:

  1. 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?
  2. 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.

huangapple
  • 本文由 发表于 2020年5月29日 20:50:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/62086430.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定