简体   繁体   English

将多个注释与参数合并

[英]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;

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? 是否可以这样做,或者我是否必须继续这样做或更改/创建新框架?

The answer is: probably no, this is not possible (using "standard" java). 答案是: 可能不,这是不可能的(使用“标准”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. 所以你需要一种方法来“神奇地”将这些注释插入到你的类文件中。

Boiling down to: when you would write your own compiler, and invent something on top of java, then you could do something like that. 归结为:当你编写自己的编译器,并在java之上发明一些东西时,你可以做类似的事情。

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 is possible to merge some annotations to only one annotation. 可以将一些注释合并到仅一个注释。 Spring for example do it in the SpringBootApplication-Annotation: 例如,Spring在SpringBootApplication-Annotation中执行它:

/**
 * Indicates a {@link Configuration configuration} class that declares one or more
 * {@link Bean @Bean} methods and also triggers {@link EnableAutoConfiguration
 * auto-configuration} and {@link ComponentScan component scanning}. This is a convenience
 * annotation that is equivalent to declaring {@code @Configuration},
 * {@code @EnableAutoConfiguration} and {@code @ComponentScan}.
 *
 * @author Phillip Webb
 * @since 1.2.0
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Configuration
@EnableAutoConfiguration
@ComponentScan
public @interface SpringBootApplication {

    /**
     * Exclude specific auto-configuration classes such that they will never be applied.
     * @return the classes to exclude
     */
    Class<?>[] exclude() default {};

}

But i don't know if it is possible to set a value to the inner annotations from the merged annotation. 但我不知道是否可以从合并注释中设置内部注释的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM