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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
|
import java.util.regex.Pattern;
import org.checkerframework.checker.regex.qual.Regex;
public class SimpleRegex {
void regString() {
String s1 = "validRegex";
String s2 = "(InvalidRegex";
}
void validRegString() {
@Regex String s1 = "validRegex";
// :: error: (assignment.type.incompatible)
@Regex String s2 = "(InvalidRegex"; // error
}
void compileCall() {
Pattern.compile("test.*[^123]$");
// :: error: (argument.type.incompatible)
Pattern.compile("$test.*[^123"); // error
}
void requireValidReg(@Regex String reg, String nonReg) {
Pattern.compile(reg);
// :: error: (argument.type.incompatible)
Pattern.compile(nonReg); // error
}
void testAddition(@Regex String reg, String nonReg) {
@Regex String s1 = reg;
@Regex String s2 = reg + "d.*sf";
@Regex String s3 = reg + reg;
// :: error: (assignment.type.incompatible)
@Regex String n1 = nonReg; // error
// :: error: (assignment.type.incompatible)
@Regex String n2 = reg + "(df"; // error
// :: error: (assignment.type.incompatible)
@Regex String n3 = reg + nonReg; // error
// :: error: (assignment.type.incompatible)
@Regex String o1 = nonReg; // error
// :: error: (assignment.type.incompatible)
@Regex String o2 = nonReg + "sdf"; // error
// :: error: (assignment.type.incompatible)
@Regex String o3 = nonReg + reg; // error
}
@Regex String regex = "()";
String nonRegex = "()";
void testCompoundConcatenation() {
takesRegex(regex);
// :: error: (compound.assignment.type.incompatible)
regex += ")"; // error
takesRegex(regex);
nonRegex = "()";
// nonRegex is refined by flow to be a regular expression
takesRegex(nonRegex);
nonRegex += ")";
// :: error: (argument.type.incompatible)
takesRegex(nonRegex); // error
}
void takesRegex(@Regex String s) {}
void testChar() {
@Regex char c1 = 'c';
@Regex Character c2 = 'c';
// :: error: (assignment.type.incompatible)
@Regex char c3 = '('; // error
// :: error: (assignment.type.incompatible)
@Regex Character c4 = '('; // error
}
void testCharConcatenation() {
@Regex String s1 = "rege" + 'x';
@Regex String s2 = 'r' + "egex";
// :: error: (assignment.type.incompatible)
@Regex String s4 = "rege" + '('; // error
// :: error: (assignment.type.incompatible)
@Regex String s5 = "reg(" + 'x'; // error
// :: error: (assignment.type.incompatible)
@Regex String s6 = '(' + "egex"; // error
// :: error: (assignment.type.incompatible)
@Regex String s7 = 'r' + "ege("; // error
}
// TODO: Uncomment this once isValidUse works better. See RegexChecker.isValidUse for details.
// class TestAllowedTypes {
// @Regex CharSequence cs;
// @Regex String s11;
// @Regex StringBuilder sb;
// @Regex Segment s21;
// @Regex char c;
// @Regex Pattern p;
// @Regex Matcher m;
//
// // :: error: (type.invalid)
// @Regex Object o; // error
// // :: error: (type.invalid)
// @Regex List<String> l; // error
// // :: error: (type.invalid)
// ArrayList<@Regex Double> al; // error
// // :: error: (type.invalid)
// @Regex int i; // error
// // :: error: (type.invalid)
// @Regex boolean b; // error
// }
// TODO: This is not supported until the checker supports getting explicit
// annotations from local variables (instead of just fields.)
// void testAllowedTypes() {
// @Regex CharSequence cs;
// @Regex String s11;
// @Regex StringBuilder sb;
// @Regex Segment s21;
// @Regex char c;
//
// // :: error: (type.invalid)
// @Regex Object o; // error
// // :: error: (type.invalid)
// @Regex List<String> l; // error
// // :: error: (type.invalid)
// ArrayList<@Regex Double> al; // error
// // :: error: (type.invalid)
// @Regex int i; // error
// // :: error: (type.invalid)
// @Regex boolean b; // error
//
// @Regex String regex = "a";
// // :: error: (argument.type.incompatible)
// regex += "(";
//
// String nonRegex = "a";
// nonRegex += "(";
// }
void testPatternLiteral() {
Pattern.compile("non(", Pattern.LITERAL);
Pattern.compile(foo("regex"), Pattern.LITERAL);
// :: error: (argument.type.incompatible)
Pattern.compile(foo("regex("), Pattern.LITERAL); // error
// :: error: (argument.type.incompatible)
Pattern.compile("non("); // error
// :: error: (argument.type.incompatible)
Pattern.compile(foo("regex")); // error
// :: error: (argument.type.incompatible)
Pattern.compile("non(", Pattern.CASE_INSENSITIVE); // error
}
public static String foo(@Regex String s) {
return "non((";
}
// TODO: This is not supported until the framework can read explicit
// annotations from arrays.
// void testArrayAllowedTypes() {
// @Regex char[] ca1;
// char @Regex [] ca2;
// @Regex char @Regex [] ca3;
// @Regex String[] s1;
//
// // :: error: (type.invalid)
// @Regex double[] da1; // error
// // :: error: (type.invalid)
// double @Regex [] da2; // error
// // :: error: (type.invalid)
// @Regex double @Regex [] da3; // error
// // :: error: (type.invalid)
// String @Regex [] s2; // error
// }
// TODO: This is not supported until the Regex Checker supports flow
// sensitivity. See the associated comment at
// org.checkerframework.checker/regex/RegexAnnotatedTypeFactory.java:visitNewArray
// void testCharArrays(char c, @Regex char r) {
// char @Regex [] c1 = {'r', 'e', 'g', 'e', 'x'};
// char @Regex [] c2 = {'(', 'r', 'e', 'g', 'e', 'x', ')', '.', '*'};
// char @Regex [] c3 = {r, 'e', 'g', 'e', 'x'};
//
// // :: error: (assignment.type.incompatible)
// char @Regex [] c4 = {'(', 'r', 'e', 'g', 'e', 'x'}; // error
// // :: error: (assignment.type.incompatible)
// char @Regex [] c5 = {c, '.', '*'}; // error
// }
}
|