I use groovy and like the @Immutable annotation. The problem is, that this annotation does not only create constructors with the specified class fields, but also creates an empty constructor and allows the fields to partly stay null (or take a default value). I want to prevent this.
Example:
@Immutable class User { int age}
can be called like
User jasmin = new User(30)
println "I am ${jasmin.age} years old" // I am 30 years old
but also like
User james = new User() //Uhoh, I have no age
println "I am ${james.age} years old" // I am 0 years old - maybe not expected
My question is therefor: are there any annotations or other ways that prevent calling the empty constructor? Possibly throwing an exception when there is no age
passed (or null passed for it) at runtime. Bonus points if I get eclipse IDE support so that an empty constructor call is complained by eclipse at compile time.
I did not find something like @NotNull for groovy, but for java i found different annotations as the ones in this question. Would using one of these a good idea? How to decide? Or is it better to write my own custom annotation - and can I get IDE help by doing that?