使用Immutables时,从接口到生成的java类进行注释 [英] Carry forward annotation from interface to generated java class when using Immutables

查看:755
本文介绍了使用Immutables时,从接口到生成的java类进行注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Java界面中使用Immutables( http://immutables.org )来生成构建器和不可变对象。我创建了一个名为@Primary的自定义方法级别注释(表示哪个属性是主要字段),我用它来注释Immutable接口中的一个方法。我没有看到由immutables创建的生成的java类中的注释。我试着看BYOA(带自己的注释),但这没有帮助。



有没有办法将@Primary注释放到生成的不可变java类上? / p>

更新(基于Sean的建议)



我现在有一个以下配置基于



package-info.java

  package com.mypackage; 


import com.mercuria.recon.custom.annotation.Primary;
import org.immutables.value.Value;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.PACKAGE,ElementType.TYPE})
@Retention(RetentionPolicy.CLASS)//为增量编译创建类保留
@ Value.Style( passAnnotations = Primary.class)
public @interface MyStyle {}

主要注释

  package com.mypackage.custom.annotation; 

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Primary {

}

我在package-info.json中看到一个错误,其中MyStyle应该在自己的文件中声明。我不确定上面的配置是否正确。请问您能告诉我哪里出错?

解决方案

您可以使用配置要传递的注释@Style 注释,您可以在包级别使用。



例如在任何包中创建一个名为package-info.java的文件,并使用

  @Style(passAnnotations = Primary.class)$注释它b $ b  

参见:样式自定义(解释存储 @Style 注释的位置,但未提及 passAnnotations 机制)



这是一个示例package-info.java文件:

  @Style(passAnnotations = YourAnnotation.class)
package com.yourapp;

import com.yourapp.annotations.YourAnnotation;
import org.immutables.value.Value.Style;

请注意注释高于声明,以及下面的 import


I am using Immutables (http://immutables.org) in my Java interface to generate builders and immutable object. I have created a custom method level annotation called @Primary (denoting which attribute is primary field) that I have used to annotate one of my methods in the Immutable interface. I don't see the annotation in the generated java class created by immutables. I tried looking at BYOA (Bring Your Own Annotation) but that does not help.

Is there a way to get the @Primary annotation onto the generated immutable java class?

UPDATE (Based on Sean's suggestion below)

I now have a below config based on

package-info.java

package com.mypackage;


import com.mercuria.recon.custom.annotation.Primary;
import org.immutables.value.Value;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.PACKAGE, ElementType.TYPE})
@Retention(RetentionPolicy.CLASS) // Make it class retention for incremental   compilation
@Value.Style(passAnnotations=Primary.class)
public @interface MyStyle {}

Primary Annotation

package com.mypackage.custom.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Primary {

}

I am seeing an error in package-info.json where in it says MyStyle should be declared in its own file. I am not sure the above config is correct. Please can you advise where I am going wrong?

解决方案

You can configure which annotations to pass with the @Style annotation, which you can use on package level.

E.g. create a file called package-info.java in any package and annotate it with

@Style(passAnnotations=Primary.class)

See: Style customization (explains about where to store a @Style annotation, but doesn't mention the passAnnotations mechanism)

Here's an example package-info.java file:

@Style(passAnnotations = YourAnnotation.class)
package com.yourapp;

import com.yourapp.annotations.YourAnnotation;
import org.immutables.value.Value.Style;

note that the annotations are above the package declaration, and the imports below.

这篇关于使用Immutables时,从接口到生成的java类进行注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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