This is an automated email from the ASF dual-hosted git repository.
henrib pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-jexl.git
The following commit(s) were added to refs/heads/master by this push:
new 03450b11 JEXL: annotations on syntactic elements starting with
curly-bracket behave like their non-annotated counterparts;
03450b11 is described below
commit 03450b110e64ee76f36b0db7746e789b1802b11d
Author: Henrib <[email protected]>
AuthorDate: Mon Nov 10 19:19:59 2025 +0100
JEXL: annotations on syntactic elements starting with curly-bracket behave
like their non-annotated counterparts;
---
.../commons/jexl3/internal/TemplateEngine.java | 9 ---
.../org/apache/commons/jexl3/parser/Parser.jjt | 6 +-
.../java/org/apache/commons/jexl3/BlockTest.java | 75 ++++++++++++++++++++--
3 files changed, 74 insertions(+), 16 deletions(-)
diff --git
a/src/main/java/org/apache/commons/jexl3/internal/TemplateEngine.java
b/src/main/java/org/apache/commons/jexl3/internal/TemplateEngine.java
index 76a901c1..145fa1fd 100644
--- a/src/main/java/org/apache/commons/jexl3/internal/TemplateEngine.java
+++ b/src/main/java/org/apache/commons/jexl3/internal/TemplateEngine.java
@@ -462,11 +462,6 @@ public final class TemplateEngine extends JxltEngine {
return collector.collected();
}
- @Override
- public Scope getScope() {
- return node instanceof ASTJexlScript? ((ASTJexlScript)
node).getScope() : null;
- }
-
@Override
protected void getVariables(final Engine.VarCollector collector) {
jexl.getVariables(node instanceof ASTJexlScript? (ASTJexlScript)
node : null, node, collector);
@@ -564,10 +559,6 @@ public final class TemplateEngine extends JxltEngine {
return strb.toString();
}
- public Scope getScope() {
- return null;
- }
-
/**
* Interprets a sub-expression.
* @param interpreter a JEXL interpreter
diff --git a/src/main/java/org/apache/commons/jexl3/parser/Parser.jjt
b/src/main/java/org/apache/commons/jexl3/parser/Parser.jjt
index 59eee701..c2b87c85 100644
--- a/src/main/java/org/apache/commons/jexl3/parser/Parser.jjt
+++ b/src/main/java/org/apache/commons/jexl3/parser/Parser.jjt
@@ -413,7 +413,11 @@ void Annotation() #Annotation :
void AnnotatedStatement() #AnnotatedStatement : {}
{
- (LOOKAHEAD(2) Annotation())+ Statement()
+ ( LOOKAHEAD(2) Annotation() )+
+ // curly brackets expressions (set, map) have priority over statements
+ ( LOOKAHEAD(<LCURLY> ( <RCURLY> | <COLON> | ( Expression() ( <RCURLY>
| <COLON> | <COMMA>) ) ))
+ Expression()
+ | Statement() )
}
void Statement() #void : {}
diff --git a/src/test/java/org/apache/commons/jexl3/BlockTest.java
b/src/test/java/org/apache/commons/jexl3/BlockTest.java
index e578744a..580086cb 100644
--- a/src/test/java/org/apache/commons/jexl3/BlockTest.java
+++ b/src/test/java/org/apache/commons/jexl3/BlockTest.java
@@ -18,6 +18,9 @@ package org.apache.commons.jexl3;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.util.Collections;
import org.junit.jupiter.api.Test;
@@ -35,7 +38,7 @@ class BlockTest extends JexlTestCase {
}
@Test
- void testBlockExecutesAll() throws Exception {
+ void testBlockExecutesAll() {
final JexlScript e = JEXL.createScript("if (true) { x = 'Hello'; y =
'World';}");
final JexlContext jc = new MapContext();
final Object o = e.execute(jc);
@@ -45,7 +48,7 @@ class BlockTest extends JexlTestCase {
}
@Test
- void testBlockLastExecuted01() throws Exception {
+ void testBlockLastExecuted01() {
final JexlScript e = JEXL.createScript("if (true) { x = 1; } else { x
= 2; }");
final JexlContext jc = new MapContext();
final Object o = e.execute(jc);
@@ -53,7 +56,7 @@ class BlockTest extends JexlTestCase {
}
@Test
- void testBlockLastExecuted02() throws Exception {
+ void testBlockLastExecuted02() {
final JexlScript e = JEXL.createScript("if (false) { x = 1; } else { x
= 2; }");
final JexlContext jc = new MapContext();
final Object o = e.execute(jc);
@@ -61,7 +64,7 @@ class BlockTest extends JexlTestCase {
}
@Test
- void testBlockSimple() throws Exception {
+ void testBlockSimple() {
final JexlScript e = JEXL.createScript("if (true) { 'hello'; }");
final JexlContext jc = new MapContext();
final Object o = e.execute(jc);
@@ -69,7 +72,7 @@ class BlockTest extends JexlTestCase {
}
@Test
- void testEmptyBlock() throws Exception {
+ void testEmptyBlock() {
final JexlScript e = JEXL.createScript("if (true) { }");
final JexlContext jc = new MapContext();
final Object o = e.execute(jc);
@@ -77,10 +80,70 @@ class BlockTest extends JexlTestCase {
}
@Test
- void testNestedBlock() throws Exception {
+ void testNestedBlock() {
final JexlScript e = JEXL.createScript("if (true) { x = 'hello'; y =
'world';" + " if (true) { x; } y; }");
final JexlContext jc = new MapContext();
final Object o = e.execute(jc);
assertEquals("world", o, "Block result is wrong");
}
+
+ @Test
+ void testSetVSBlock() {
+ final AnnotationTest.AnnotationContext jc = new
AnnotationTest.AnnotationContext();
+ JexlScript e;
+ Object r;
+ // synchronized block
+ e = JEXL.createScript("let n = 41 @synchronized { n += 1; }");
+ r = e.execute(jc);
+ assertEquals(42, r);
+ assertEquals(1, jc.getCount());
+ assertTrue(jc.getNames().contains("synchronized"));
+ // synchronized set
+ e = JEXL.createScript("let n = 41 @synchronized { n += 1 }");
+ r = e.execute(jc);
+ assertEquals(Collections.singleton(42), r);
+ assertEquals(2, jc.getCount());
+ assertTrue(jc.getNames().contains("synchronized"));
+
+ e = JEXL.createScript("let n = 41 { n += 1 }");
+ r = e.execute(jc);
+ assertEquals(Collections.singleton(42), r);
+
+ e = JEXL.createScript("let n = 41 { n += 1; }");
+ r = e.execute(jc);
+ assertEquals(42, r);
+
+ e = JEXL.createScript("{'A' : 1, 'B' : 42}['B']");
+ r = e.execute(jc);
+ assertEquals(42, r);
+
+ e = JEXL.createScript("{ n = 42; }");
+ r = e.execute(jc);
+ assertEquals(42, r);
+ e = JEXL.createScript("@synchronized(y) { n = 42; }", "y");
+ r = e.execute(jc);
+ assertEquals(42, r);
+
+ e = JEXL.createScript("{ n = 42 }");
+ r = e.execute(jc);
+ assertEquals(Collections.singleton(42), r);
+ e = JEXL.createScript("@synchronized(z) { n = 42 }", "z");
+ r = e.execute(jc);
+ assertEquals(Collections.singleton(42), r);
+
+ e = JEXL.createScript("{ n = 41; m = 42 }");
+ r = e.execute(jc);
+ assertEquals(42, r);
+
+ e = JEXL.createScript("{ 20 + 22; }");
+ r = e.execute(jc);
+ assertEquals(42, r);
+ e = JEXL.createScript("@synchronized { 20 + 22; }");
+ r = e.execute(jc);
+ assertEquals(42, r);
+
+ e = JEXL.createScript("{ 6 * 7 }");
+ r = e.execute(jc);
+ assertEquals(Collections.singleton(42), r);
+ }
}