Constructor annotation on java records

1.8k views Asked by At

Is there a way to get an annotation like ConstructorProperties that has @Target(CONSTRUCTOR) to annotate the generated constructor of a java 16 record? E.g.:

@ConstructorProperties({"id", "filename"})
public record Person(long id, String filename) {}

This ^ causes the following error:

java: annotation type not applicable to this kind of declaration
1

There are 1 answers

4
pearcemerritt On

This worked:

public record Person(long id, String filename) {
    @ConstructorProperties({"id", "filename"})
    public Person {}
}

My understanding is that the inner constructor with no parameter list is a way of adding logic to the default constructor created using the component list. Apparently, adding a constructor annotation to that has the end result I was after :)