You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by gn...@apache.org on 2026/08/24 05:21:57 UTC

(maven) 01/01: Support the logical not operator in profile activation conditions (#12737)

This is an automated email from the ASF dual-hosted git repository.

gnodet pushed a commit to branch backport/12737-to-maven-4.0.x
in repository https://gitbox.apache.org/repos/asf/maven.git

commit 127c027ccaebb0715f31643efdc54aef33527199
Author: Sepuri Sai Krishna <sa...@gmail.com>
AuthorDate: Mon Aug 24 10:32:59 2026 +0530

    Support the logical not operator in profile activation conditions (#12737)
    
    Add handling for the `!` operator in ConditionParser.parseUnary() so that
    `!expr` works as sugar for `not(expr)`, using the same toBoolean coercion.
    The tokenizer already emits a bare `!` token but no parser rule consumed it,
    causing a confusing "Unknown variable: !" error. The `!=` operator is
    unaffected since the tokenizer emits it as a single token.
    
    Fixes #12736
---
 .../maven/impl/model/profile/ConditionParser.java  | 12 +++-
 .../impl/model/profile/ConditionParserTest.java    | 65 ++++++++++++++++++++++
 2 files changed, 76 insertions(+), 1 deletion(-)

diff --git a/impl/maven-impl/src/main/java/org/apache/maven/impl/model/profile/ConditionParser.java b/impl/maven-impl/src/main/java/org/apache/maven/impl/model/profile/ConditionParser.java
index 4b73b5217e..a34e6e23cd 100644
--- a/impl/maven-impl/src/main/java/org/apache/maven/impl/model/profile/ConditionParser.java
+++ b/impl/maven-impl/src/main/java/org/apache/maven/impl/model/profile/ConditionParser.java
@@ -288,11 +288,21 @@ private Object parseMultiplyDivide() {
     }
 
     /**
-     * Parses unary operations (negation).
+     * Parses unary operations: logical not ({@code !}) and arithmetic negation ({@code -}).
+     * <p>
+     * {@code !} is equivalent to the {@code not()} function and uses the same {@link #toBoolean}
+     * coercion. Being handled here gives both operators a higher precedence than multiplication,
+     * addition, comparison and the logical operators, so {@code !a && b} parses as
+     * {@code (!a) && b}.
      *
      * @return the result of parsing unary operations
      */
     private Object parseUnary() {
+        if (current < tokens.size() && tokens.get(current).equals("!")) {
+            current++;
+            Object value = parseUnary();
+            return !toBoolean(value);
+        }
         if (current < tokens.size() && tokens.get(current).equals("-")) {
             current++;
             Object value = parseUnary();
diff --git a/impl/maven-impl/src/test/java/org/apache/maven/impl/model/profile/ConditionParserTest.java b/impl/maven-impl/src/test/java/org/apache/maven/impl/model/profile/ConditionParserTest.java
index be1da09919..c1eb0a5735 100644
--- a/impl/maven-impl/src/test/java/org/apache/maven/impl/model/profile/ConditionParserTest.java
+++ b/impl/maven-impl/src/test/java/org/apache/maven/impl/model/profile/ConditionParserTest.java
@@ -159,6 +159,71 @@ void testComplexExpression() {
         assertEquals("SUCCESS!", parser.parse(expression));
     }
 
+    /**
+     * The tokenizer emits a bare {@code !} token, so the parser has to be able to consume it.
+     * The {@code contains(..)} cases also cover a {@code !} inside a string literal, which the
+     * tokenizer must leave alone.
+     */
+    @Test
+    void testLogicalNotOperator() {
+        assertFalse((Boolean) parser.parse("!true"));
+        assertTrue((Boolean) parser.parse("!false"));
+        assertTrue((Boolean) parser.parse("!!true"));
+        assertFalse((Boolean) parser.parse("!!!true"));
+        assertTrue((Boolean) parser.parse("!contains('Hello, World!', 'OpenAI')"));
+        assertFalse((Boolean) parser.parse("!contains('Hello, World!', 'World')"));
+    }
+
+    /**
+     * {@code !x} is sugar for the {@code not(x)} function and must agree with it.
+     */
+    @Test
+    void testLogicalNotOperatorMatchesNotFunction() {
+        assertEquals(
+                parser.parse("not(contains('Hello, World!', 'OpenAI'))"),
+                parser.parse("!contains('Hello, World!', 'OpenAI')"));
+        assertEquals(
+                parser.parse("not(contains('Hello, World!', 'World'))"),
+                parser.parse("!contains('Hello, World!', 'World')"));
+        assertEquals(parser.parse("not(true)"), parser.parse("!true"));
+    }
+
+    /**
+     * {@code !} binds tighter than comparison and the logical operators, as it does in Java.
+     */
+    @Test
+    void testLogicalNotOperatorPrecedence() {
+        assertTrue((Boolean) parser.parse("!false && true"));
+        assertFalse((Boolean) parser.parse("!true && false"));
+        assertTrue((Boolean) parser.parse("!true || true"));
+        assertTrue((Boolean) parser.parse("!(true && false)"));
+        assertTrue((Boolean) parser.parse("!(1 > 2)"));
+        assertFalse((Boolean) parser.parse("!('a' != 'b')"));
+    }
+
+    /**
+     * Coercion of non-boolean operands must match what the {@code not()} function does.
+     */
+    @Test
+    void testLogicalNotOperatorCoercion() {
+        assertFalse((Boolean) parser.parse("!'abc'"));
+        assertTrue((Boolean) parser.parse("!''"));
+        assertFalse((Boolean) parser.parse("!length('ab')"));
+        assertTrue((Boolean) parser.parse("!0"));
+        assertFalse((Boolean) parser.parse("!1"));
+    }
+
+    /**
+     * A bare {@code !} must not disturb the {@code !=} operator, which the tokenizer emits as a
+     * single token.
+     */
+    @Test
+    void testNotEqualsStillParsesAsOneOperator() {
+        assertTrue((Boolean) parser.parse("1 != 2"));
+        assertFalse((Boolean) parser.parse("'abc' != 'abc'"));
+        assertTrue((Boolean) parser.parse("'abc' != 'cdf'"));
+    }
+
     @Test
     void testStringComparison() {
         assertTrue((Boolean) parser.parse("'abc' != 'cdf'"));