gnodet commented on code in PR #12038:
URL: https://github.com/apache/maven/pull/12038#discussion_r3243410873
##########
impl/maven-impl/src/main/java/org/apache/maven/impl/model/profile/ConditionParser.java:
##########
@@ -129,18 +129,21 @@ private List<String> tokenize(String expression) {
}
quoteType = c;
sb.append(c);
- } else if (c == ' ' || c == '(' || c == ')' || c == ',' || c ==
'+' || c == '>' || c == '<' || c == '='
- || c == '!') {
+ } else if (c == ' ' || c == '\n' || c == '\r' || c == '\t' || c ==
'(' || c == ')' || c == ',' || c == '+'
+ || c == '>' || c == '<' || c == '=' || c == '!' || c ==
'&') {
Review Comment:
Use `Character.isWhitespace(c)` instead of enumerating whitespace characters
— it is cleaner, handles edge cases like form feed, and is the standard Java
idiom. Also add `'|'` to the operator list so `||` can be properly tokenized
(see comment below).
```suggestion
} else if (Character.isWhitespace(c) || c == '(' || c == ')' ||
c == ',' || c == '+'
|| c == '>' || c == '<' || c == '=' || c == '!' || c ==
'&' || c == '|') {
```
##########
impl/maven-impl/src/main/java/org/apache/maven/impl/model/profile/ConditionParser.java:
##########
@@ -129,18 +129,21 @@ private List<String> tokenize(String expression) {
}
quoteType = c;
sb.append(c);
- } else if (c == ' ' || c == '(' || c == ')' || c == ',' || c ==
'+' || c == '>' || c == '<' || c == '='
- || c == '!') {
+ } else if (c == ' ' || c == '\n' || c == '\r' || c == '\t' || c ==
'(' || c == ')' || c == ',' || c == '+'
+ || c == '>' || c == '<' || c == '=' || c == '!' || c ==
'&') {
if (!sb.isEmpty()) {
tokens.add(sb.toString());
sb.setLength(0);
}
- if (c != ' ') {
- if ((c == '>' || c == '<' || c == '=' || c == '!')
+ if (c != ' ' && c != '\n' && c != '\r' && c != '\t') {
Review Comment:
Same here — use `Character.isWhitespace(c)` for consistency.
```suggestion
if (!Character.isWhitespace(c)) {
```
##########
impl/maven-impl/src/test/java/org/apache/maven/impl/model/profile/ConditionParserTest.java:
##########
@@ -262,6 +262,32 @@ void testPropertyAlias() {
assertThrows(RuntimeException.class, () -> parser.parse("${unclosed"));
}
+ @Test
+ void testAmpersandAmpersandTokenizerMultiline() {
+ // Regression test for https://github.com/apache/maven/issues/11882
+ // The && operator was not being tokenized correctly when a line break
appeared before it.
+ // Uses ${os.name} and ${os.arch} which work reliably in the test
environment.
+ // ${os.name} == 'windows' && ${os.arch} == 'amd64' evaluates to true.
Review Comment:
These values come from the mock context in `createMockContext()` (lines
64-68), not from the actual OS. The comment is misleading.
```suggestion
// Uses ${os.name} and ${os.arch} which are set to 'windows' and
'amd64' in the mock context.
```
##########
impl/maven-impl/src/main/java/org/apache/maven/impl/model/profile/ConditionParser.java:
##########
@@ -129,18 +129,21 @@ private List<String> tokenize(String expression) {
}
quoteType = c;
sb.append(c);
- } else if (c == ' ' || c == '(' || c == ')' || c == ',' || c ==
'+' || c == '>' || c == '<' || c == '='
- || c == '!') {
+ } else if (c == ' ' || c == '\n' || c == '\r' || c == '\t' || c ==
'(' || c == ')' || c == ',' || c == '+'
+ || c == '>' || c == '<' || c == '=' || c == '!' || c ==
'&') {
if (!sb.isEmpty()) {
tokens.add(sb.toString());
sb.setLength(0);
}
- if (c != ' ') {
- if ((c == '>' || c == '<' || c == '=' || c == '!')
+ if (c != ' ' && c != '\n' && c != '\r' && c != '\t') {
+ if ((c == '>' || c == '<' || c == '=' || c == '!' || c ==
'&')
&& i + 1 < expression.length()
&& expression.charAt(i + 1) == '=') {
tokens.add(c + "=");
i++; // Skip the next character
+ } else if (c == '&' && i + 1 < expression.length() &&
expression.charAt(i + 1) == '&') {
+ tokens.add("&&");
+ i++; // Skip the next character
Review Comment:
Two issues:
1. **`&` in the `X=` group**: `&` was added to the `>=`/`<=`/`==`/`!=`
lookahead, which means `&=` would be tokenized as a valid two-char operator.
But `&=` is not valid in this language — remove `|| c == '&'` from this
condition.
2. **`||` not handled**: The `||` operator has the exact same tokenization
gap that `&&` had. The `|` character is not in the operator list, so `||` only
works when surrounded by spaces. Handle it symmetrically with `&&`.
```suggestion
if ((c == '>' || c == '<' || c == '=' || c == '!')
&& i + 1 < expression.length()
&& expression.charAt(i + 1) == '=') {
tokens.add(c + "=");
i++; // Skip the next character
} else if (c == '&' && i + 1 < expression.length() &&
expression.charAt(i + 1) == '&') {
tokens.add("&&");
i++; // Skip the next character
} else if (c == '|' && i + 1 < expression.length() &&
expression.charAt(i + 1) == '|') {
tokens.add("||");
i++; // Skip the next character
```
Please also add corresponding test cases for `||` with newlines, similar to
the `&&` tests.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]