在带注释的字段中获取初始化程序 [英] Getting initializer in annotated field

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

问题描述

假设我有这门课:

@Annotate class MyClass {MyField 字段 = new MyField(params);}

当我处理我的注解(new 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.

推荐答案

你不能.

是否使用反射apt,您无法访问代码块.

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

  • 通过 apt,您可以访问可以注释的任何内容,但不能对初始化块进行注释(尽管您可以访问初始化块内的带注释的局部变量).
  • 通过反射无法访问代码块(只有构造函数、字段、方法和类)

如果您对此感到绝望,则需要使用像 javaparser 这样的源解析器或者像 asm 这样的字节码工具.两者都理解树结构(前者使用源树,后者使用字节码树)并且可以处理所有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 而它是 initialization(ConstructorSignature) 切入点.切入点页面上有一些参考,但要真正掌握它,您可能需要阅读AspectJ in Action by Ramnivas Laddad.

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天全站免登陆