diff --git a/platform-app/src/main/java/com/eip4/platform/app/script/juel/JuelScriptEngine.java b/platform-app/src/main/java/com/eip4/platform/app/script/juel/JuelScriptEngine.java index 1f3b25b..7cf3551 100644 --- a/platform-app/src/main/java/com/eip4/platform/app/script/juel/JuelScriptEngine.java +++ b/platform-app/src/main/java/com/eip4/platform/app/script/juel/JuelScriptEngine.java @@ -63,7 +63,12 @@ public class JuelScriptEngine extends AbstractScriptEngine implements Compilable return null; } try { - ELContext elContext = new BindingsELContext(resolveBindings(context)); + // 直接持有 ENGINE/GLOBAL 两层 Bindings,经 ELResolver 惰性 get() 解析顶层变量—— + // 不可 putAll/复制:真实服务流传入的 FlowScriptBindings 的 entrySet/keySet/putAll + // 抛 UnsupportedOperationException(仅 get/put/containsKey 可用),复制会崩(QA dogfooding 实证)。 + ELContext elContext = new BindingsELContext( + context.getBindings(ScriptContext.ENGINE_SCOPE), + context.getBindings(ScriptContext.GLOBAL_SCOPE)); ValueExpression expr = EXPRESSION_FACTORY.createValueExpression(elContext, script, Object.class); return expr.getValue(elContext); } catch (ELException e) { @@ -96,24 +101,6 @@ public class JuelScriptEngine extends AbstractScriptEngine implements Compilable return factory; } - /** - * 合并 ENGINE_SCOPE 与 GLOBAL_SCOPE 的 bindings(ENGINE_SCOPE 优先),作为 EL 顶层变量来源。 - * 与 framework 行为一致:调用方经 {@code evaluate(script, "juel", bindings)} 传入的 - * bindings 落在 ENGINE_SCOPE。 - */ - @SuppressWarnings({"unchecked", "rawtypes"}) - private Map resolveBindings(ScriptContext context) { - SimpleBindings merged = new SimpleBindings(); - Bindings global = context.getBindings(ScriptContext.GLOBAL_SCOPE); - if (global != null) { - merged.putAll(global); - } - Bindings engine = context.getBindings(ScriptContext.ENGINE_SCOPE); - if (engine != null) { - merged.putAll(engine); - } - return (Map) merged; - } private static String readFully(Reader reader) throws ScriptException { if (reader == null) { @@ -162,9 +149,9 @@ public class JuelScriptEngine extends AbstractScriptEngine implements Compilable private final VariableMapper variableMapper = new NoopVariableMapper(); private final FunctionMapper functionMapper = new NoopFunctionMapper(); - private BindingsELContext(Map bindings) { + private BindingsELContext(Bindings engineBindings, Bindings globalBindings) { CompositeELResolver composite = new CompositeELResolver(); - composite.add(new BindingsELResolver(bindings)); + composite.add(new BindingsELResolver(engineBindings, globalBindings)); composite.add(new MapELResolver()); composite.add(new ListELResolver()); composite.add(new ArrayELResolver()); @@ -201,17 +188,34 @@ public class JuelScriptEngine extends AbstractScriptEngine implements Compilable */ private static final class BindingsELResolver extends ELResolver { - private final Map bindings; + private final Bindings engineBindings; + private final Bindings globalBindings; - private BindingsELResolver(Map bindings) { - this.bindings = bindings; + private BindingsELResolver(Bindings engineBindings, Bindings globalBindings) { + this.engineBindings = engineBindings; + this.globalBindings = globalBindings; + } + + /** + * 仅用 {@code get()/containsKey()} 解析顶层变量(ENGINE 优先,缺则 GLOBAL)。 + * 关键:严禁 entrySet/keySet/putAll——真实服务流的 FlowScriptBindings 这些方法抛 + * UnsupportedOperationException,仅 get/put/containsKey 实现(QA dogfooding 实证)。 + */ + private Object lookup(String name) { + if (engineBindings != null && engineBindings.containsKey(name)) { + return engineBindings.get(name); + } + if (globalBindings != null && globalBindings.containsKey(name)) { + return globalBindings.get(name); + } + return null; // 未绑定 → null(保持 JUEL 宽松语义) } @Override public Object getValue(ELContext context, Object base, Object property) { if (base == null && property instanceof String) { context.setPropertyResolved(true); - return bindings.get(property); + return lookup((String) property); } return null; } @@ -220,7 +224,7 @@ public class JuelScriptEngine extends AbstractScriptEngine implements Compilable public Class getType(ELContext context, Object base, Object property) { if (base == null && property instanceof String) { context.setPropertyResolved(true); - Object value = bindings.get(property); + Object value = lookup((String) property); return value == null ? Object.class : value.getClass(); } return null; @@ -230,7 +234,9 @@ public class JuelScriptEngine extends AbstractScriptEngine implements Compilable public void setValue(ELContext context, Object base, Object property, Object value) { if (base == null && property instanceof String) { context.setPropertyResolved(true); - bindings.put((String) property, value); + if (engineBindings != null) { + engineBindings.put((String) property, value); // put 可用 + } } } diff --git a/platform-app/src/test/java/com/eip4/platform/app/script/juel/JuelScriptEngineSeamTest.java b/platform-app/src/test/java/com/eip4/platform/app/script/juel/JuelScriptEngineSeamTest.java index cb34e04..023181f 100644 --- a/platform-app/src/test/java/com/eip4/platform/app/script/juel/JuelScriptEngineSeamTest.java +++ b/platform-app/src/test/java/com/eip4/platform/app/script/juel/JuelScriptEngineSeamTest.java @@ -126,4 +126,32 @@ class JuelScriptEngineSeamTest { assertTrue(result instanceof Number); assertEquals(6, ((Number) result).intValue()); } + + /** + * 模拟保真层 FlowScriptBindings:get/put/containsKey 可用, + * entrySet/keySet/values/size/putAll 抛 UnsupportedOperationException(与真实类一致)。 + */ + static class FlowScriptBindingsLike extends SimpleBindings { + @Override public java.util.Set> entrySet() { throw new UnsupportedOperationException(); } + @Override public java.util.Set keySet() { throw new UnsupportedOperationException(); } + @Override public java.util.Collection values() { throw new UnsupportedOperationException(); } + @Override public int size() { throw new UnsupportedOperationException(); } + @Override public void putAll(java.util.Map m) { throw new UnsupportedOperationException(); } + } + + @Test + @DisplayName("(g) 真实流 FlowScriptBindings(entrySet/putAll 抛 UOE)下仍可求值——仅用 get/containsKey") + void flowScriptBindingsLike_noEntrySet() throws Exception { + // 回归:B1 原 resolveBindings 用 putAll 合并 → 触 FlowScriptBindings.entrySet UOE, + // 致所有带 conditionExpression 的 EL 写流崩(QA dogfooding 实证)。修后只走 get/containsKey。 + FlowScriptBindingsLike b = new FlowScriptBindingsLike(); + b.put("x", 5); + b.put("isSave", Boolean.TRUE); + ScriptEngine engine = new ScriptEngineManager().getEngineByName("juel"); + assertNotNull(engine); + assertEquals(6, ((Number) engine.eval("${x + 1}", b)).intValue()); + assertEquals(Boolean.TRUE, engine.eval("${isSave}", b)); // ${isSave} 转移条件:真实写流核心 + assertEquals(Boolean.TRUE, engine.eval("${x > 3}", b)); + assertNull(engine.eval("${nope}", b)); // 未绑定 → null,不抛、不触 entrySet + } }