For instance, in the follwing annotation:
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface EndOfTheCurrentDay {
//some staff
}
it is obviously that we can't apply the annotation to the field of the type, say, Integer
. But in the way it was implemented, the usage of the annotation may be unsafe. How can I prevet applying the annotaion to the field other than java.util.Date
? Is it even posible?
No, you cannot reliably restrict this and generate errors during compilation - annotation processors can be disabled. If you want to be absolutely certain you need to validate it in runtime when you process the annotation: