Skip to content

Commit 5e362dd

Browse files
committed
Refactor: requested changes
1 parent 22a1a72 commit 5e362dd

7 files changed

Lines changed: 28 additions & 30 deletions

File tree

client-java/instrumentation-shared/src/main/java/org/evomaster/client/java/instrumentation/shared/StringSpecializationInfo.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class StringSpecializationInfo implements Serializable {
2121
* Only meaningful when stringSpecialization is a regex type.
2222
* Defaults to 0 (no external flags).
2323
*/
24-
private final int regexFlags;
24+
private final int externalRegexFlagsBitmask;
2525

2626
public StringSpecializationInfo(StringSpecialization stringSpecialization, String value) {
2727
this(stringSpecialization, value, TaintType.FULL_MATCH);
@@ -31,14 +31,14 @@ public StringSpecializationInfo(StringSpecialization stringSpecialization, Strin
3131
this(stringSpecialization, value, taintType, 0);
3232
}
3333

34-
public StringSpecializationInfo(StringSpecialization stringSpecialization, String value, TaintType taintType, int regexFlags) {
34+
public StringSpecializationInfo(StringSpecialization stringSpecialization, String value, TaintType taintType, int externalRegexFlagsBitmask) {
3535
this.stringSpecialization = Objects.requireNonNull(stringSpecialization);
3636
this.value = value;
3737
if(taintType == null || taintType == TaintType.NONE){
3838
throw new IllegalArgumentException("Invalid type: "+taintType);
3939
}
4040
this.type = taintType;
41-
this.regexFlags = regexFlags;
41+
this.externalRegexFlagsBitmask = externalRegexFlagsBitmask;
4242
}
4343

4444
public StringSpecialization getStringSpecialization() {
@@ -57,7 +57,7 @@ public TaintType getType() {
5757
* Getter for regex flags bitmask, only meaningful when stringSpecialization is regex type. Defaults to 0 (no flags)
5858
* @return Integer bitmask for the regex flags associated to the string.
5959
*/
60-
public int getRegexFlags() { return regexFlags; }
60+
public int getExternalRegexFlagsBitmask() { return externalRegexFlagsBitmask; }
6161

6262
@Override
6363
public boolean equals(Object o) {
@@ -67,11 +67,11 @@ public boolean equals(Object o) {
6767
return stringSpecialization == that.stringSpecialization &&
6868
Objects.equals(value, that.value) &&
6969
type == that.type &&
70-
regexFlags == that.regexFlags;
70+
externalRegexFlagsBitmask == that.externalRegexFlagsBitmask;
7171
}
7272

7373
@Override
7474
public int hashCode() {
75-
return Objects.hash(stringSpecialization, value, type, regexFlags);
75+
return Objects.hash(stringSpecialization, value, type, externalRegexFlagsBitmask);
7676
}
7777
}

client-java/instrumentation/src/main/java/org/evomaster/client/java/instrumentation/coverage/methodreplacement/PatternMatchingHelper.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,16 @@ public static boolean matches(String regex, String input, String idTemplate) {
2626
/**
2727
* Invocation to Pattern.matches() is free of side-effects.
2828
*/
29-
public static boolean matches(String regex, int flags, String input, String idTemplate) {
29+
public static boolean matches(String regex, int externalRegexFlagsBitmask, String input, String idTemplate) {
3030
Objects.requireNonNull(regex);
3131
Objects.requireNonNull(input);
3232

3333
if (ExecutionTracer.isTaintInput(input)) {
3434
ExecutionTracer.addStringSpecialization(input,
35-
new StringSpecializationInfo(StringSpecialization.REGEX_WHOLE, regex, TaintType.FULL_MATCH, flags));
35+
new StringSpecializationInfo(StringSpecialization.REGEX_WHOLE, regex, TaintType.FULL_MATCH, externalRegexFlagsBitmask));
3636
}
3737

38-
Pattern p = Pattern.compile(regex, flags);
38+
Pattern p = Pattern.compile(regex, externalRegexFlagsBitmask);
3939
Matcher m = p.matcher(input);
4040
boolean matches = m.matches();
4141

client-java/instrumentation/src/main/java/org/evomaster/client/java/instrumentation/coverage/methodreplacement/classes/MatcherClassReplacement.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ public static boolean matches(Matcher caller, String idTemplate) {
4848

4949
String text = getText(caller);
5050
String pattern = caller.pattern().toString();
51-
int regexFlags = caller.pattern().flags();
51+
int externalRegexFlagsBitmask = caller.pattern().flags();
5252

53-
boolean patternMatchesResult = PatternMatchingHelper.matches(pattern, regexFlags, text, idTemplate);
53+
boolean patternMatchesResult = PatternMatchingHelper.matches(pattern, externalRegexFlagsBitmask, text, idTemplate);
5454

5555
TaintType taintType = ExecutionTracer.getTaintType(text);
5656

@@ -60,7 +60,7 @@ public static boolean matches(Matcher caller, String idTemplate) {
6060
*/
6161
String regex = caller.pattern().toString();
6262
ExecutionTracer.addStringSpecialization(text,
63-
new StringSpecializationInfo(StringSpecialization.REGEX_WHOLE, regex, taintType, regexFlags));
63+
new StringSpecializationInfo(StringSpecialization.REGEX_WHOLE, regex, taintType, externalRegexFlagsBitmask));
6464
}
6565
boolean matcherMatchesResults = caller.matches();
6666
assert (patternMatchesResult == matcherMatchesResults);
@@ -73,7 +73,7 @@ public static boolean find(Matcher caller, String idTemplate) {
7373

7474
String input = getText(caller);
7575
String regex = caller.pattern().toString();
76-
int regexFlags = caller.pattern().flags();
76+
int externalRegexFlagsBitmask = caller.pattern().flags();
7777
int end;
7878
try {
7979
end = caller.end();
@@ -106,7 +106,7 @@ match the regex, and find() only requires
106106
TaintType taintType = ExecutionTracer.getTaintType(substring);
107107
if (taintType.isTainted()) {
108108
ExecutionTracer.addStringSpecialization(substring,
109-
new StringSpecializationInfo(StringSpecialization.REGEX_PARTIAL, regex, taintType, regexFlags));
109+
new StringSpecializationInfo(StringSpecialization.REGEX_PARTIAL, regex, taintType, externalRegexFlagsBitmask));
110110
}
111111

112112
String anyPositionRegexMatch = RegexSharedUtils.handlePartialMatch(regex);

core/src/main/kotlin/org/evomaster/core/search/gene/string/StringGene.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -817,7 +817,7 @@ class StringGene(
817817
} else {
818818
RegexSharedUtils.handlePartialMatch(it.value)
819819
}
820-
RegexWithFlags(regex, it.regexFlags)
820+
RegexWithFlags(regex, it.externalRegexFlagsBitmask)
821821
}
822822
//.joinToString("|")
823823
.forEach {(regex, flags) ->

core/src/main/kotlin/org/evomaster/core/utils/RegexFlags.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,14 @@ data class RegexFlags(
6666
* [java.util.regex.Pattern.compile] to be preserved and applied when building
6767
* the gene tree, mirroring the behaviour of the Java regex engine.
6868
*/
69-
fun fromJavaFlags(flags: Int): RegexFlags = RegexFlags(
70-
caseInsensitive = flags and Pattern.CASE_INSENSITIVE != 0,
71-
unicodeCase = flags and Pattern.UNICODE_CASE != 0,
72-
dotAll = flags and Pattern.DOTALL != 0,
73-
multiline = flags and Pattern.MULTILINE != 0,
74-
unixLines = flags and Pattern.UNIX_LINES != 0,
75-
unicodeCharacterClass = flags and Pattern.UNICODE_CHARACTER_CLASS != 0,
76-
comments = flags and Pattern.COMMENTS != 0
69+
fun fromExternalJavaRegexFlagBitmask(externalRegexFlagsBitmask: Int): RegexFlags = RegexFlags(
70+
caseInsensitive = externalRegexFlagsBitmask and Pattern.CASE_INSENSITIVE != 0,
71+
unicodeCase = externalRegexFlagsBitmask and Pattern.UNICODE_CASE != 0,
72+
dotAll = externalRegexFlagsBitmask and Pattern.DOTALL != 0,
73+
multiline = externalRegexFlagsBitmask and Pattern.MULTILINE != 0,
74+
unixLines = externalRegexFlagsBitmask and Pattern.UNIX_LINES != 0,
75+
unicodeCharacterClass = externalRegexFlagsBitmask and Pattern.UNICODE_CHARACTER_CLASS != 0,
76+
comments = externalRegexFlagsBitmask and Pattern.COMMENTS != 0
7777
)
7878
}
7979

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package org.evomaster.core.utils
22

33
data class RegexWithFlags(val regex: String, val regexFlags: RegexFlags) {
4-
constructor (regex: String, regexFlagInt: Int) : this(regex, RegexFlags.fromJavaFlags(regexFlagInt))
4+
constructor (regex: String, regexFlagInt: Int) : this(regex, RegexFlags.fromExternalJavaRegexFlagBitmask(regexFlagInt))
55
}

core/src/test/kotlin/org/evomaster/core/parser/RegexHandlerTest.kt

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,13 @@ package org.evomaster.core.parser
33
import org.antlr.v4.runtime.misc.ParseCancellationException
44
import org.evomaster.client.java.instrumentation.heuristic.ValidatorHeuristics
55
import org.evomaster.client.java.instrumentation.shared.RegexSharedUtils
6-
import org.evomaster.core.search.gene.regex.RegexGene
76
import org.evomaster.core.search.service.AdaptiveParameterControl
87
import org.evomaster.core.search.service.Randomness
98
import org.evomaster.core.search.service.mutator.MutationWeightControl
109
import org.evomaster.core.utils.RegexFlags
1110
import org.junit.jupiter.api.Assertions.*
1211
import org.junit.jupiter.api.Disabled
1312
import org.junit.jupiter.api.Test
14-
import org.junit.jupiter.api.assertThrows
1513
import java.util.regex.Pattern
1614

1715
internal class RegexHandlerTest{
@@ -176,7 +174,7 @@ internal class RegexHandlerTest{
176174
fun testJVMExternalCaseInsensitiveFlagWithUnicodeCase() {
177175
// \u03A1 is greek capital Rho, \u03C1 is lowercase rho
178176
val regex = "\u03A1+"
179-
val flags = RegexFlags.fromJavaFlags(Pattern.CASE_INSENSITIVE or Pattern.UNICODE_CASE)
177+
val flags = RegexFlags.fromExternalJavaRegexFlagBitmask(Pattern.CASE_INSENSITIVE or Pattern.UNICODE_CASE)
180178
val gene = RegexHandler.createGeneForJVM(regex, flags)
181179
val pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE or Pattern.UNICODE_CASE)
182180
val rand = Randomness()
@@ -193,7 +191,7 @@ internal class RegexHandlerTest{
193191

194192
val regex = "^abc$"
195193
val noFlags = RegexHandler.createGeneForJVM(regex)
196-
val withCI = RegexHandler.createGeneForJVM(regex, RegexFlags.fromJavaFlags(Pattern.CASE_INSENSITIVE))
194+
val withCI = RegexHandler.createGeneForJVM(regex, RegexFlags.fromExternalJavaRegexFlagBitmask(Pattern.CASE_INSENSITIVE))
197195

198196
val rand = Randomness()
199197
val noFlagsSamples = (1..200).map {
@@ -214,7 +212,7 @@ internal class RegexHandlerTest{
214212
fun testJVMInlineFlagNotDoubledByExternalFlag() {
215213

216214
val regex = "(?i:abc)"
217-
val withExternalCI = RegexHandler.createGeneForJVM(regex, RegexFlags.fromJavaFlags(Pattern.CASE_INSENSITIVE))
215+
val withExternalCI = RegexHandler.createGeneForJVM(regex, RegexFlags.fromExternalJavaRegexFlagBitmask(Pattern.CASE_INSENSITIVE))
218216
val pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE)
219217
val rand = Randomness()
220218

@@ -229,7 +227,7 @@ internal class RegexHandlerTest{
229227
fun testJVMInlineCanDisableExternalFlag() {
230228

231229
val regex = "^(?-i:abc)$"
232-
val withExternalCI = RegexHandler.createGeneForJVM(regex, RegexFlags.fromJavaFlags(Pattern.CASE_INSENSITIVE))
230+
val withExternalCI = RegexHandler.createGeneForJVM(regex, RegexFlags.fromExternalJavaRegexFlagBitmask(Pattern.CASE_INSENSITIVE))
233231
val rand = Randomness()
234232

235233
repeat(200) {

0 commit comments

Comments
 (0)