In this code construct:
public MyClass(Integer... numbers) {
do_something_with(numbers[]);
}
is it possible to require that numbers contains at least one entry in such a way, that this is checked at compile-time? (At run-time, of course, I can just check numbers.length.)
Clearly I could do this:
public MyClass(Integer number, Integer... more_numbers) {
do_something_with(number, more_numbers[]);
}
but this isn't going to be very elegant.
The reason I would like to do this is to make sure that a sub-class does not simply forget to call this constructor at all, which will default to a call to super() with no numbers in the list. In this case, I would rather like to get the familiar error message: Implicit super constructor is undefined. Must explicitly invoke another constructor.
Could there be another way to achieve the same, like some @-annotation that marks this constructor as non-implicit?
解决方案
I suppose one incredibly hacky way to do this is to create a no-args method and mark it as deprecated. Then compile with these two flags: -Xlint:deprecation -Werror. This will cause any use of a deprecated method to be a compile time error.