简体   繁体   English

Checkstyle匹配并允许合法的注释参数值

[英]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\\"]"). 我有一个注释,该注释的参数值可以作为字符串数组的字符串表示形式(例如“ [\\“ 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. 我想要一种@MyAnn ,以便当某人使用@MyAnn该值必须为4之一。任何其他情况都应报告为违规。

The problem I am having is how to specify the regex for the invalid values (mine is for valid values). 我遇到的问题是如何为无效值指定正则表达式(mine是有效值)。 Because the checkstyle needs me to specify the regex for the illegal values. 因为checkstyle需要我为非法值指定正则表达式。

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? 请参阅Checkstyle-如何使用正则表达式排除任何文件(但以'Impl.java'结尾的文件除外)?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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