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