@Incubating class RegexChecker extends TypeCheckingDSL
Checks at compile-time for cases of invalid regex usage where the actual regex string can be identified (e.g. inline or defined by a local variable with an initial value or a field with an initial value). A number of errors which would normally surface only at runtime are handled.
Pattern#compile method:
~/\w{3/ // missing closing repetition quantifier brace
~"(.)o(.*" // missing closing group bracket
Pattern.compile(/?/) // dangling meta character '?' (Java longhand)
These examples illustrate explicitly defined constant strings but local variable
or field definitions where a constant string can be determined are also checked.
Pattern#matches method:
'foobar' =~ /f[o]{2/ // missing closing repetition quantifier brace
'foobar' ==~ /(foo/ // missing closing group bracket
Pattern.matches(/?/, 'foo') // dangling meta character '?' (Java longhand)
def m = 'foobar' =~ /(...)(...)/
assert m[0][1] == 'foo' // okay
assert m[0][3] // type error: only two groups in regex
And similarly for Java long-hand variants:
Pattern p = Pattern.compile('(...)(...)')
Matcher m = p.matcher('foobar')
assert m.find()
assert m.group(1) == 'foo' // okay
assert m.group(3) // type error: only two groups in regex
matcher[0], the getAt extension method for Matcher
is called. This may return a String (if no groups occur within the regex) or a list of String values
if grouping is used, hence the declared return type of the mentioned getAt method is Object
to account for these two possibilities. When using RegexChecker, the inferred type will be either
String or List<String> when a regex string can be identified.
Over time, the idea would be to support more cases as per:
https://checkerframework.org/manual/#regex-checker
https://homes.cs.washington.edu/~mernst/pubs/regex-types-ftfjp2012.pdf
https://github.com/typetools/checker-framework/tree/master/checker/src/main/java/org/checkerframework/checker/regex
| Modifiers | Name | Description |
|---|---|---|
class |
RegexChecker.1 |
|
class |
RegexChecker.2 |
| Constructor and description |
|---|
RegexChecker() |
Copyright © 2003-2022 The Apache Software Foundation. All rights reserved.