如何在Java注释处理器中解析SwitchTree表达式类型?
让我们考虑以下代码:
switch ( <em>switchTreeExpression</em> ) {
<em>cases</em>
}
我想找出 SwitchTree Expression
是什么类型。
我有以下代码草稿:
...
MethodTree methodTree = trees.getTree(method);
BlockTree blockTree = methodTree.getBody();
for (StatementTree statementTree : blockTree.getStatements()) {
if (statementTree.getKind() == Tree.Kind.SWITCH) {
SwitchTree switchTree = (SwitchTree) statementTree;
ExpressionTree switchTreeExpression = switchTree.getExpression();
// I need to get the type of *switchTreeExpression* here
}
}
有趣的是,我可以从 .class
文件中获取 switchtree Expression
的类型。但是,似乎没有办法在这一注释处理阶段获取当前类的字节代码(如果我错了,我很高兴只能获取字节代码并使用 objectweb asm
库进行分析。 )。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可能的解决方案
注释处理器
让我们考虑类型注释的注释处理器(
@target(elementType.type)
)。限制:
processor.process()
方法:无方法:
克服限制:使用
com.sun.source.util.taskListener
该想法是处理类型元素分析完成事件。
processor.init()
方法:注册任务侦听器,并使用捕获的注释类型元素处理类型元素分析完成事件。processor.process()
方法:捕获带注释的类型元素。一些相关的引用:
概述。 处理代码。
灵感。检查器框架。
相关问题。 java-如何通过EnnotationProcessor-stack Overflow 。 /p>
相关问题。 java-jaxb-jaxb dymind bind sublasses dymindy-stack Overflow 。
。
相关问题。 让Java Compiler Warn何时警告使用带注释的方法(例如@deprecated) - 堆栈溢出。
关于实施方法的注意,
一些第三方依赖(库和框架)可用于实现注释处理器。
例如,已经提到的检查器框架。
一些相关引用:
请注意,检查器框架处理器使用
@supportedAnnotationTypes(“*”)
。实施草案
让我们考虑实施草案,该草案不使用“实施方法注释”部分中提到的第三方依赖项。
注释处理器项目
Maven Project
AbstractTyPeprocessor
类:基类:我们介绍具有以下摘要方法的基类:
checkmethodbodies
class>类:注释> code> chece> checkmethodbodiesprocessor class class class
测试项目
maven项目,
以便能够使用注释类:
switcher
类:使用注释测试
执行注释处理器项目的命令:
执行测试项目的命令:
观察的输出注释处理器:
独立程序
可以在独立程序中使用JAVAC功能。
看来有必要获取树路径然后获取类型镜:
摘录文档: typemirror(java Platform se 8):
草稿实现
输入文件:
switcher
类program
class,请替换
“/path/to/switcher.java”
用实际的文件路径值文件路径值。程序输出:
Possible solutions
Annotation processor
Let's consider an annotation processor for the type annotations (
@Target(ElementType.TYPE)
).Limitation:
Processor.process()
method: No method bodiesProcessing Code:
Overcoming limitation: Using
com.sun.source.util.TaskListener
The idea is to handle the type element analysis completion events.
Processor.init()
method: Register a task listener and handle the type element analysis completion events using the captured annotated type elements.Processor.process()
method: Capture the annotated type elements.Some related references:
Overview. Processing Code.
Inspiration. Checker Framework.
Related question. java - How to access TypeUse annotation via AnnotationProcessor - Stack Overflow.
Related question. java - JAXB bind subclasses dynamically - Stack Overflow.
Related question. Make the java compiler warn when an annotated method is used (like @deprecated) - Stack Overflow.
Note on implementation approaches
Some third-party dependencies (libraries and frameworks) may be used to implement an annotation processor.
For example, the already mentioned Checker Framework.
Some related references:
Please, note that the Checker Framework processors use
@SupportedAnnotationTypes("*")
.Draft implementation
Let's consider a draft implementation, which does not use third-party dependencies mentioned in the «Note on implementation approaches» section.
Annotation processor project
Maven project
AbstractTypeProcessor
class: Base classLet's introduce the base class that has the following abstract method:
CheckMethodBodies
class: Annotation classCheckMethodBodiesProcessor
class: Annotation processorTest project
Maven project
To be able to use the annotation class:
Switcher
class: Using annotationTesting
Execute the command for the annotation processor project:
Execute the command for the test project:
Observe the output of the annotation processor:
Stand-alone program
It is possible to use javac functionality in a stand-alone program.
It seems that it is necessary to get the tree path and then get the type mirror:
An excerpt from the documentation: TypeMirror (Java Platform SE 8 ):
Draft implementation
Input file:
Switcher
classProgram
classPlease, replace the
"/path/to/Switcher.java"
file path value with the actual file path value.The program output: