获得初始的注解的字段 [英] Getting initializer in annotated field

查看:148
本文介绍了获得初始的注解的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我有这个类:

@Annotate MyClass类{
  MyField的字段=新MyField的(PARAMS);
}

我怎样才能让我的类字段的初始化一部分,当我处理我的注释(即新MyField的(PARAMS)部分)?

How can I get the initializing part of my class field when I process my annotation (the new MyField(params) part)?

我看不到在 FieldDeclaration 类相关的任何事情。

I can't see anything related in the FieldDeclaration class.

推荐答案

您不能。

无论您使用反射容易,你不能访问code块。

Whether you use reflection or apt, you cannot access code blocks.


  • 通过容易,你可以访问任何你可以注释,但你不能标注初始化块(尽管你可以访问一个初始化块内的注释局部变量)。

  • 通过反思就没有办法访问code块(只有构造函数,字段,方法和类)

如果你有足够的这个绝望的,你需要使用源代码分析器如 JavaParser类或字节code的工具,像 ASM 。这两种理解的树结构(前者使用的源代码树,后者字节code树),并且可以处理所有的Java结构,包括初始化块。

If you are desperate enough about this, you need to use a source parser like javaparser or a byte code tool like asm. Both understand tree structures (the former uses source trees, the latter byte code trees) and can deal with all java structures, including initializer blocks.

但你最好的选择可能是的AspectJ 和它的初始化(ConstructorSignature) 切入点。有在切入点页面但要真正有一定的参考抓住它,你可能必须通过Ramnivas Laddad 阅读 AspectJ的行动。

But your best bet is probably AspectJ and it's initialization(ConstructorSignature) pointcut. There is some reference on the pointcuts page but to really grasp it you would probably have to read AspectJ in Action by Ramnivas Laddad.

这篇关于获得初始的注解的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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