简体   繁体   中英

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 (eg "[\\"Value1\\", \\"Value2\\"]"). So basically the value of the annotation is a string. Eg

@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?

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM