使用 Immutables 时,将接口中的注释转发到生成的 java class

Carry forward annotation from interface to generated java class when using Immutables

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

有没有办法将@Primary 注释添加到生成的不可变对象上 java class?

更新(根据下面肖恩的建议)

我现在有一个基于

的配置

包-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 {}

主要注释

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)

参见:Style customization(解释了 @Style 注释的存储位置,但未提及 passAnnotations 机制)

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

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

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

请注意,注释位于 package 声明上方,import 位于下方。