如何从ExecutableElement获取方法主体 [英] How to get method body from ExecutableElement

查看:128
本文介绍了如何从ExecutableElement获取方法主体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的AbstractProcessor中,我可以从已创建注释的带有注释的类中获取所有方法:

In my AbstractProcessor I am able to get all methods from a class annotated with some annotation, I have created:

List<? extends Element> allElements = processingEnv.getElementUtils().getAllMembers((TypeElement) bean);
List<ExecutableElement> methods = ElementFilter.methodsIn(allElements);

是否可以获取方法的主体/ExecutableElement?该API似乎只处理签名和修饰符.

Is is possible to get the body of the method/ExecutableElement? The API only seem to deal with the signature and modifiers.

我可能会使用此答案的一些变体: https://stackoverflow.com/a/34568708/6095334 ,以访问专有的* .sun.**包中的类,例如com.sun.tools.javac.tree.JCTree$MethodTree:

I could probably use some variation of this answer: https://stackoverflow.com/a/34568708/6095334, to access classes from the proprietary *.sun.** package, such as com.sun.tools.javac.tree.JCTree$MethodTree:

MethodTree methodTree = trees.getTree(executableElement);

其中,trees是在AbstractProcessor的init()方法中设置的com.sun.source.util.Trees的实例,如下所示:trees = Trees.instance(processingEnv);
但是这些类带有警告:这不是任何受支持的API的一部分.如果编写依赖于此的代码,则后果自负.该代码及其内部接口如有更改或删除,恕不另行通知.

where trees is an instance of com.sun.source.util.Trees set in the AbstractProcessor's init() method as so: trees = Trees.instance(processingEnv);
But these classes come with a warning: This is NOT part of any supported API. If you write code that depends on this, you do so at your own risk. This code and its internal interfaces are subject to change or deletion without notice.

我希望可以从更通用的注释处理框架中访问注释方法的主体.

I would hope that it was possible to access an annotated method's body from within the more general annotation processing framework.

推荐答案

据我所知,注释框架不支持访问ExecutableElement的正文.调用getEnclosedElements()很诱人,但正如javadoc所述:

To my knowledge, the annotation framework does not support accessing an ExecutableElement´s body. It would be tempting to call getEnclosedElements(), but as the javadoc states:

返回松散地说直接由该元素包围的元素.类或接口被认为封装了直接声明的字段,方法,构造函数和成员类型.包将顶级类和接口包含在其中,但不认为包含子包.当前不认为其他类型的元素可以包含任何元素.但是,随着API或编程语言的发展,这种情况可能会发生变化.

Returns the elements that are, loosely speaking, directly enclosed by this element. A class or interface is considered to enclose the fields, methods, constructors, and member types that it directly declares. A package encloses the top-level classes and interfaces within it, but is not considered to enclose subpackages. Other kinds of elements are not currently considered to enclose any elements; however, that may change as this API or the programming language evolves.

对于我的项目,我设法从方法主体中提取了所需的信息,如下所示:

For my project, I managed to extract the information I needed from the method body as follows:

MethodTree methodTree = trees.getTree(executableElement);
BlockTree blockTree = methodTree.getBody();
for (StatementTree statementTree : blockTree.getStatements()) {
  // *do something with the statements*
}

其中,com.sun.source.util.Trees trees = Trees.instance(processingEnv);是我在AbstractProcessorinit()方法中设置的实例字段.

where com.sun.source.util.Trees trees = Trees.instance(processingEnv); is an instance field I set in the AbstractProcessor's init() method.

请参阅以下答案,以获取有关对引用的jdk工具类的依赖性的信息: https://stackoverflow.com/a/29585979 /6095334

See this answer, for information about the dependency to the referenced jdk tool classes: https://stackoverflow.com/a/29585979/6095334

这篇关于如何从ExecutableElement获取方法主体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆