1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.jexl3.examples;
19
20 import org.apache.commons.jexl3.JexlBuilder;
21 import org.apache.commons.jexl3.JexlContext;
22 import org.apache.commons.jexl3.JexlEngine;
23 import org.apache.commons.jexl3.JexlExpression;
24 import org.apache.commons.jexl3.MapContext;
25 import org.junit.jupiter.api.Test;
26
27
28
29
30 public class MethodPropertyTest {
31
32
33
34 public static class Foo {
35
36
37
38
39
40 public String convert(final long i) {
41 return "The value is : " + i;
42 }
43
44
45
46
47
48
49 public String get(final String arg) {
50 return "This is the property " + arg;
51 }
52
53
54
55
56
57 public String getFoo() {
58 return "This is from getFoo()";
59 }
60 }
61
62
63
64
65 public static void example(final AbstractOutput out) throws Exception {
66
67
68
69
70 final JexlEngine jexl = new JexlBuilder().create();
71
72
73
74 final JexlContext jc = new MapContext();
75
76
77
78
79 final Foo foo = new Foo();
80 final Integer number = 10;
81
82 jc.set("foo", foo);
83 jc.set("number", number);
84
85
86
87
88 JexlExpression e = jexl.createExpression("foo.getFoo()");
89 Object o = e.evaluate(jc);
90 out.print("value returned by the method getFoo() is : ", o, foo.getFoo());
91
92
93
94
95 e = jexl.createExpression("foo.convert(1)");
96 o = e.evaluate(jc);
97 out.print("value of " + e.getParsedText() + " is : ", o, foo.convert(1));
98
99 e = jexl.createExpression("foo.convert(1+7)");
100 o = e.evaluate(jc);
101 out.print("value of " + e.getParsedText() + " is : ", o, foo.convert(1+7));
102
103 e = jexl.createExpression("foo.convert(1+number)");
104 o = e.evaluate(jc);
105 out.print("value of " + e.getParsedText() + " is : ", o, foo.convert(1+ number));
106
107
108
109
110 e = jexl.createExpression("foo.bar");
111 o = e.evaluate(jc);
112 out.print("value returned for the property 'bar' is : ", o, foo.get("bar"));
113
114 }
115
116
117
118
119
120
121 public static void main(final String[] args) throws Exception {
122 example(AbstractOutput.SYSTEM);
123 }
124
125
126
127
128
129 @Test
130 public void testExample() throws Exception {
131 example(AbstractOutput.JUNIT);
132 }
133 }