diff --git a/core/src/main/java/com/taobao/arthas/core/command/express/ArthasObjectPropertyAccessor.java b/core/src/main/java/com/taobao/arthas/core/command/express/ArthasObjectPropertyAccessor.java index 7562705fdef..cab20626a2a 100644 --- a/core/src/main/java/com/taobao/arthas/core/command/express/ArthasObjectPropertyAccessor.java +++ b/core/src/main/java/com/taobao/arthas/core/command/express/ArthasObjectPropertyAccessor.java @@ -5,13 +5,28 @@ import com.taobao.arthas.core.GlobalOptions; import ognl.ObjectPropertyAccessor; +import ognl.OgnlContext; import ognl.OgnlException; +import ognl.OgnlRuntime; + /** * @author hengyunabc 2022-03-24 */ public class ArthasObjectPropertyAccessor extends ObjectPropertyAccessor { + @Override + public Object getPossibleProperty(Map context, Object target, String name) throws OgnlException { + Object result; + try { + result = OgnlRuntime.getFieldValue((OgnlContext) context, target, name, true); + } catch (Exception ex) { + throw new OgnlException(name, ex); + } + + return result; + } + @Override public Object setPossibleProperty(Map context, Object target, String name, Object value) throws OgnlException { if (GlobalOptions.strict) { diff --git a/core/src/test/java/com/taobao/arthas/core/command/express/ArthasObjectPropertyAccessorTest.java b/core/src/test/java/com/taobao/arthas/core/command/express/ArthasObjectPropertyAccessorTest.java new file mode 100644 index 00000000000..c8367dc28ce --- /dev/null +++ b/core/src/test/java/com/taobao/arthas/core/command/express/ArthasObjectPropertyAccessorTest.java @@ -0,0 +1,57 @@ +package com.taobao.arthas.core.command.express; + +import com.taobao.arthas.core.advisor.Advice; +import ognl.OgnlException; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.Arrays; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; + +class ArthasObjectPropertyAccessorTest { + + private Express express; + + @BeforeEach + public void setUp () { + Fetcher fetcher = new Fetcher().add(new Fetcher.Fetch() + .add(new FlowContext("aa")) + .add(new FlowContext("bb")) + ).add(new Fetcher.Fetch() + .add(new FlowContext("cc")) + .add(new FlowContext("dd")) + .add(new FlowContext("ee")) + ); + + Object[] params = new Object[4]; + params[0] = fetcher; + Advice advice = Advice.newForAfterReturning(null, getClass(), null, null, params, null); + express = ExpressFactory.unpooledExpress(null).bind(advice); + } + + @Test + void getPossibleProperty() throws ExpressException { + assertInstanceOf(List.class, express.get("params[0].completedFetches")); + assertEquals(2, ((List) express.get("params[0].completedFetches")).size()); + assertThrows(ExpressException.class, () -> express.is("params[0].hasCompletedFetches")); + assertTrue(express.is("params[0].hasCompletedFetches()")); + assertThrows(ExpressException.class, () -> express.is("params[0].getCompletedFetches")); + assertTrue(express.is("params[0].getCompletedFetches()")); + assertInstanceOf(Fetcher.Fetch.class, express.get("params[0].completedFetches[1]")); + assertInstanceOf(List.class, express.get("params[0].completedFetches[1].flowContexts")); + assertEquals(3, ((List) express.get("params[0].completedFetches[1].flowContexts")).size()); + assertTrue(express.is("params[0].completedFetches[1].hasFlowContexts()")); + assertTrue(express.is("params[0].completedFetches[1].getFlowContexts()")); + assertInstanceOf(List.class, express.get("params[0].completedFetches[1].getFlowContexts1()")); + assertInstanceOf(List.class, express.get("params[0].completedFetches.{flowContexts.{flowAttribute.bxApp}}")); + assertIterableEquals(Arrays.asList( + Arrays.asList("aa", "bb"), + Arrays.asList("cc", "dd", "ee") + ), (Iterable) express.get("params[0].completedFetches.{flowContexts.{flowAttribute.bxApp}}")); + } + + + +} \ No newline at end of file diff --git a/core/src/test/java/com/taobao/arthas/core/command/express/Fetcher.java b/core/src/test/java/com/taobao/arthas/core/command/express/Fetcher.java new file mode 100644 index 00000000000..7462d3757c0 --- /dev/null +++ b/core/src/test/java/com/taobao/arthas/core/command/express/Fetcher.java @@ -0,0 +1,45 @@ +package com.taobao.arthas.core.command.express; + + +import java.util.ArrayList; +import java.util.List; + +public class Fetcher { + private final List completedFetches = new ArrayList<>(); + + public boolean hasCompletedFetches() { + return !completedFetches.isEmpty(); + } + + public boolean getCompletedFetches() { + return hasCompletedFetches(); + } + + public Fetcher add(Fetch fetch) { + completedFetches.add(fetch); + return this; + } + + public static class Fetch { + private final List flowContexts = new ArrayList<>(); + public boolean hasFlowContexts() { + return !flowContexts.isEmpty(); + } + + public boolean getFlowContexts() { + return !flowContexts.isEmpty(); + } + + public List getFlowContexts1() { + return flowContexts; + } + + public Fetch add(FlowContext flowContext) { + flowContexts.add(flowContext); + return this; + } + + } +} + + diff --git a/core/src/test/java/com/taobao/arthas/core/command/express/FlowAttribute.java b/core/src/test/java/com/taobao/arthas/core/command/express/FlowAttribute.java index 1848b650ca1..f52a40a69fd 100644 --- a/core/src/test/java/com/taobao/arthas/core/command/express/FlowAttribute.java +++ b/core/src/test/java/com/taobao/arthas/core/command/express/FlowAttribute.java @@ -3,6 +3,13 @@ public class FlowAttribute { private String bxApp = "aaa"; + public FlowAttribute() { + } + + public FlowAttribute(String bxApp) { + this.bxApp = bxApp; + } + public String getBxApp() { return this.bxApp ; } diff --git a/core/src/test/java/com/taobao/arthas/core/command/express/FlowContext.java b/core/src/test/java/com/taobao/arthas/core/command/express/FlowContext.java index c1991bdbe79..bf3d0211b2b 100644 --- a/core/src/test/java/com/taobao/arthas/core/command/express/FlowContext.java +++ b/core/src/test/java/com/taobao/arthas/core/command/express/FlowContext.java @@ -3,6 +3,14 @@ public class FlowContext { private FlowAttribute flowAttribute = new FlowAttribute(); + + public FlowContext() { + } + + public FlowContext(String app) { + this.flowAttribute = new FlowAttribute(app); + } + public FlowAttribute getFlowAttribute() { return this.flowAttribute ; }