rayeaster
rayeaster

Reputation: 261

How to add restriction to Annotation?

Java annotation

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface OneAnnotation{
    String aNumber();        
}

say I wanna only permit those string format which matches("[\d]+"), other string will be "failed" by either a complier error or some other notification. Is there any of doing this---add restriction on returned value on annotation's method.

valid:

@OneAnnotation(aNumber = “1234”)

invalid:

@OneAnnotation(aNumber = “XXXX”)

Upvotes: 1

Views: 2420

Answers (1)

Joachim Sauer
Joachim Sauer

Reputation: 308021

You might be able to implement this using annotation processing.

Other than that there's no way to add arbitrary restrictions to your annotations.

But: if you want a number, why don't you use a numeric type like int or long here (depending on your range requirements)?

Upvotes: 1

Related Questions