celeritas
celeritas

Reputation: 388

Checkstyle to match and allow legal annotation parameter values

I have an annotation that can have value of a parameter as the string representation of the array of string (e.g. "[\"Value1\", \"Value2\"]"). So basically the value of the annotation is a string. E.g.

@MyAnn(value = "[\"V1\"]")

Valid set of the strings can only be either:

  1. value = "[\"V1\", \"V2\", \"\"]"
  2. value = "[ ]"
  3. value = "[]"
  4. value = ""

A legal regex that matches these strings is:

value = (?:\"\[\s*(\\\".*\")*\s*\]\"|\"\"|\"\[\s*\]\")

I'd like to have a checkstyle such that if when someone is using @MyAnn then the value must be one of the 4. Anything else should be reported as the violation.

The problem I am having is how to specify the regex for the invalid values (mine is for valid values). Because the checkstyle needs me to specify the regex for the illegal values.

What would be the way to work it out?

Upvotes: 0

Views: 74

Answers (1)

jwaddell
jwaddell

Reputation: 1934

Surround your regex with the ?! negative lookahead assertion:

^((?!(?:\"\[\s*(\\\".*\")*\s*\]\"|\"\"|\"\[\s*\]\")).)*$

See Checkstyle - How to exclude any file, but the ones ending with 'Impl.java', with a regex?

Upvotes: 0

Related Questions