How to make a 3rd Party Annotation Inheritable

68 views Asked by At

I am trying to use @AvroName annotation in java POJO fields. However, I want these POJO fields to be inherited by the subclasses. But @AvroName annotation does not have the @Inherited annotation to it.

Is there a way to make the @AvroName annotation have @Inherited?

I was wondering if I can make a wrapper annotation but not very sure.

public abstract class Abc {

  @AvroName(“field1”)
  public String field1;
}
public class Xyz extends Abc {

  @AvroName(“field2”)
  public String field2;
}

I want my Xyz object to have both field1 and field2 with @AvroName properties.

2

There are 2 answers

0
F_sants_ On

I am not too sure if I understood the problem correctly. The fields in you Xyz class are both annotated with @AvroName, you don't need this annotation to be annotated with @Inherited. @Inherited only has effect on classes.

Maybe this can clear it up for you.

0
Thomas Kläger On

The class Xyz has the fields field2 and field1 and both are annotated with an AvroName annotation.

The following code shows that:

public class AnnTester {
    public static void main(String[] args) {
        Field[] fields = Xyz.class.getFields();
        for (Field f: fields) {
            System.out.printf("%s: %s%n", f.getName(), Arrays.toString(f.getAnnotations()));
        }
    }
}

The output from this code is:

field2: [@org.apache.avro.reflect.AvroName("field2")]
field1: [@org.apache.avro.reflect.AvroName("field1")]

The problem is that the client you want to use does not correctly handle inherited fields.


Note that even if you could add the @Inherited annotation to the AvroName annotation the behaviour would not change.

The documentation of the @Inherited annotataion explicitly states:

Note that this meta-annotation interface has no effect if the annotated interface is used to annotate anything other than a class.