1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
|
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.checkerframework.checker.regex.RegexUtil;
import org.checkerframework.checker.regex.qual.*;
class TestIsRegex {
void test1(String str1) throws Exception {
if (!RegexUtil.isRegex(str1)) {
throw new Exception();
}
Pattern.compile(str1);
}
void test2(String str2) throws Exception {
if (!RegexUtil.isRegex(str2)) {
// :: error: (argument.type.incompatible)
Pattern.compile(str2);
}
}
void test3(String str3) throws Exception {
if (RegexUtil.isRegex(str3)) {
Pattern.compile(str3);
} else {
throw new Exception();
}
}
void test4(String str4) throws Exception {
if (RegexUtil.isRegex(str4)) {
Pattern.compile(str4);
} else {
// :: error: (argument.type.incompatible)
Pattern.compile(str4);
}
}
void test5(String str5) throws Exception {
if (!RegexUtil.isRegex(str5, 3)) {
throw new Exception();
}
Pattern.compile(str5).matcher("test").group(3);
}
void test6(String str6) throws Exception {
if (RegexUtil.isRegex(str6, 4)) {
Pattern.compile(str6).matcher("4kdfj").group(4);
} else {
// :: error: (argument.type.incompatible)
Pattern.compile(str6);
}
}
void test7(String str7) throws Exception {
if (RegexUtil.isRegex(str7, 5)) {
// :: error: (group.count.invalid)
Pattern.compile(str7).matcher("4kdfj").group(6);
}
}
@Regex Pattern test8(String input) {
String datePattern = null;
if (input != null) {
datePattern = "regexkdafj";
if (!RegexUtil.isRegex(datePattern, 1)) {
throw new Error(
"error parsing regex "
+ datePattern
+ ": "
+ RegexUtil.regexError(datePattern));
}
return Pattern.compile(datePattern);
}
@Regex(1) String dp = datePattern;
if (input != null) { // just some test...
Pattern pattern = datePattern != null ? Pattern.compile(datePattern) : null;
return pattern;
} else {
Pattern pattern = datePattern != null ? Pattern.compile(dp) : null;
return pattern;
}
}
@Regex(1) Pattern test9(String input) {
String datePattern = null;
if (input != null) {
datePattern = "regexkdafj";
if (!RegexUtil.isRegex(datePattern, 1)) {
throw new Error(
"error parsing regex "
+ datePattern
+ ": "
+ RegexUtil.regexError(datePattern));
}
return Pattern.compile(datePattern);
}
@Regex(1) String dp = datePattern;
if (input != null) { // just some test...
Pattern pattern = datePattern != null ? Pattern.compile(datePattern) : null;
return pattern;
} else {
Pattern pattern = datePattern != null ? Pattern.compile(dp) : null;
return pattern;
}
}
void test10(String s) throws Exception {
if (!RegexUtil.isRegex(s, 2)) {
throw new Exception();
}
Pattern p = Pattern.compile(s);
Matcher m = p.matcher("abc");
String g = m.group(1);
}
void test11(String s) throws Exception {
@Regex(2) String l1 = RegexUtil.asRegex(s, 2);
@Regex(1) String l2 = RegexUtil.asRegex(s, 2);
@Regex String l3 = RegexUtil.asRegex(s, 2);
// :: error: (assignment.type.incompatible)
@Regex(3) String l4 = RegexUtil.asRegex(s, 2);
}
@Regex(2) String test12(String s, boolean b) throws Exception {
return b ? null : RegexUtil.asRegex(s, 2);
}
}
|