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.JexlExpression;
21 import org.apache.commons.jexl3.JexlContext;
22 import org.apache.commons.jexl3.JexlEngine;
23 import org.apache.commons.jexl3.MapContext;
24
25 import java.util.List;
26 import java.util.ArrayList;
27 import org.apache.commons.jexl3.JexlBuilder;
28 import org.junit.Test;
29
30
31
32
33
34
35 public class ArrayTest {
36
37
38
39 static void example(final Output out) throws Exception {
40
41
42
43
44 final JexlEngine jexl = new JexlBuilder().create();
45
46
47
48 final JexlContext jc = new MapContext();
49
50 final List<Object> l = new ArrayList<>();
51 l.add("Hello from location 0");
52 final Integer two = 2;
53 l.add(two);
54 jc.set("array", l);
55
56 JexlExpression e = jexl.createExpression("array[1]");
57 Object o = e.evaluate(jc);
58 out.print("Object @ location 1 = ", o, two);
59
60 e = jexl.createExpression("array[0].length()");
61 o = e.evaluate(jc);
62
63 out.print("The length of the string at location 0 is : ", o, 21);
64 }
65
66
67
68
69
70 @Test
71 public void testExample() throws Exception {
72 example(Output.JUNIT);
73 }
74
75
76
77
78
79
80 public static void main(final String[] args) throws Exception {
81 example(Output.SYSTEM);
82 }
83 }