3

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?

serah
  • 2,057
  • 7
  • 36
  • 56
  • package-info.json? The file is supposed to be called package-info.java. And you should not define a custom annotation there, but instead annotate the package (see my answer) – Sean Patrick Floyd Mar 06 '17 at 17:59

1 Answers1

2

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.

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
  • Hi , Thanks for pointing me to Styles. I am not sure I have understood it completely. I have updated my initial post with how I have configured. Please can you let me know where I might be going wrong. – serah Mar 06 '17 at 17:45
  • The Styles annotation configures your code generation. You can annotate your class directly with it, or if you want to apply the same settings to multiple classes, annotate the package. – Sean Patrick Floyd Mar 06 '17 at 17:46
  • I believe I am not setting up the package-info.java file correctly. What should be the contents of this file. Can you please let me know what is incorrect in the sample I pasted above? – serah Mar 06 '17 at 17:53
  • Thanks Sean, works now. Sorry, package-info.json was a typo . – serah Mar 06 '17 at 18:07