将多个注释与参数合并 [英] merge multiple annotations with parameters
问题描述
我在使用多个批注时遇到了一个问题,这些批注或多或少都说同一件事,但对不同的框架,我想将它们全部归为一个自定义批注.当前看起来像这样:
I have a problem with using multiple annotations that all say more or less the same thing but to different frameworks and I would like to group them all into one custom annotation. Currently it looks like this:
@Column(name = "bank_account_holder_name")
@XmlElement(name = "bank_account_holder_name")
@SerializedName("bank_account_holder_name")
@ApiModelProperty(name = "bank_account_holder_name", value = "The name of the recipient bank account holder")
public String bankAccountHolderName;
如您所见,它们都重复相同的String,我想将它们组合在一起,但是我还没有找到一种方法.
As you can see they all repeat the same String and I would like to combine them, but I have yet to find a way of doing so.
是否完全有可能做到这一点,还是我必须继续这样做还是更改/创建新框架?
Is it at all possible to do this, or do I have to either continue doing this or change/create a new framework?
推荐答案
答案是:可能不,这是不可能的(使用标准" java).
The answer is: probably no, this is not possible (using "standard" java).
您看到,没有 继承 用于批注,但仅是多重"继承,它可以使您表达:
You see, there is no inheritance for annotations, yet alone "multiple" inheritance which would allow you to express:
public @interface MultiAnnotiation extends Column, XmlElement, ...
最有可能的是,这些注释的工作方式如下:在运行时,相应的框架使用反射来检查某个对象是否具有.而且,如果找不到其"注释,则什么也不会发生.
And most likely, those annotations work like this: at runtime the corresponding frameworks uses reflection to check if a certain object has that annotation. And if it doesn't find "its" annotation, nothing happens.
因此,您将需要一种魔术"地将这些注释插入到您的类文件中的方法.
So you would need a way to "magically" insert those annotations into your class file.
归结为:当您编写自己的编译器并在Java之上发明某些东西时,您可以执行类似的操作.
Boiling down to: when you would write your own compiler, and invent something on top of java, then you could do something like that.
处理编译器插件附带的注释(意思是:在编译时处理的注释)时,情况可能会有所不同.也许您可以编写自己的自定义注释,然后触发相同编译时操作.
Things might be different when dealing with annotations that come with compiler-plugins (meaning: an annotation that is processed at compile time). Maybe you could write your own custom annotation then that triggers the same compile-time operations.
长话短说:所有这些听起来有趣,但高级,最有可能的是:不会生成健壮的,可用于生产的代码!
Long story short: all of this sounds interesting, but rather advanced, and most likely: not resulting in robust, production-worthy code!
这篇关于将多个注释与参数合并的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!