我可以从一个或的TypeVariable向VariableElement对基础类方法的列表获得注释处理器在编译时 [英] Can I get from a TypeVariable or VariableElement to a list of Methods on the underlying class In an annotation processor at compile time

查看:818
本文介绍了我可以从一个或的TypeVariable向VariableElement对基础类方法的列表获得注释处理器在编译时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个注解的类:

public class CacheMessageHolder<TestMessage> implements MessageHolder<TestMessage> {
    protected @MessageHolderType TestMessage message;
    @Override
    @SendProtoAll (proto ="protoMessageClass", matchType=MatchType.PARTIAL)
    public void setMessage( TestMessage msg) {
        this.message = msg;     
    }
}

在我的注释处理器我想对传递到setMessage方法的对象的getter方法​​的清单,这些信息将被用于code产生。

In my annotation processor I want to get a list of the getter methods on the Object passed into the setMessage method, this information will then be used for code generation.

我延长ElementScanner6和管理来获取似乎举行的参数VariableElement,但我不知道在哪里可以从这里走。

I extend ElementScanner6 and manage to get a VariableElement that seems to hold the parameter but I do not know where to go from here.

因此​​,在这个例子中,我想在的TestMessage类中的所有方法,在编译时。

So in this example I want to get all the methods in the TestMessage class at compile time.

任何想法

推荐答案

注释处理是相当繁琐的,一个可能会丢失相当快..我想你应该得到相应的这个参数元素的类型,然后得到相应的元素这种类型的,然后得到其成员和过滤。尝试用下面的code打,让我知道,如果它的工作:

Annotation Processing is quite cumbersome, and one can get lost quite fast.. I think you should get the type corresponding to this parameter element, then get the element corresponding to this type, then get its members and filter them. Try to play with the following code, and let me know if it work :

VariableElement parameterElement;
ProcessingEnvironment processingEnv;

TypeMirror parameterType = parameterElement.asType();
Types typeUtils = processingEnv.getTypeUtils();
TypeElement typeElement = (TypeElement) typeUtils.asElement(parameterType);
Elements elementUtils = processingEnv.getElementUtils()
List<? extends Element> elementMembers = elementUtils.getAllMembers(typeElement);
List<ExecutableElement> elementMethods = ElementFilter.methodsIn(elementMembers);
for(ExecutableElement methodElement : elementMethods) {
    if (methodElement.getParameters().size()==0 && methodElement.getSimpleName().toString().startsWith("get")) {
      // do something
    }
}

我觉得它应该工作,但不会是100%肯定它是一个getter,因为你不能查什么方法体中完成。我以为是吸你的意思是开始搞定的方法,并且没有参数。

I think it should work, but won't be sure 100% that it's a getter, since you can't check what's done inside a method body. I assumed by "getter" you meant a method starting with "get", and with no parameter.

这是否回答你的问题?

这篇关于我可以从一个或的TypeVariable向VariableElement对基础类方法的列表获得注释处理器在编译时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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