1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
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 * Simple example to show how to access method and properties.
29 */
30 class MethodPropertyTest {
31 /**
32 * Helper example class.
33 */
34 public static class Foo {
35 /**
36 * Gets a string from the argument.
37 * @param i a long.
38 * @return The argument prefixed with 'The value is : '
39 */
40 public String convert(final long i) {
41 return "The value is : " + i;
42 }
43
44 /**
45 * Gets an arbitrary property.
46 * @param arg property name.
47 * @return arg prefixed with 'This is the property '.
48 */
49 public String get(final String arg) {
50 return "This is the property " + arg;
51 }
52
53 /**
54 * Gets foo.
55 * @return a string.
56 */
57 public String getFoo() {
58 return "This is from getFoo()";
59 }
60 }
61
62 /**
63 * An example for method access.
64 */
65 public static void example(final AbstractOutput out) throws Exception {
66 /*
67 * First step is to retrieve an instance of a JexlEngine;
68 * it might be already existing and shared or created anew.
69 */
70 final JexlEngine jexl = new JexlBuilder().create();
71 /*
72 * Second make a jexlContext and put stuff in it
73 */
74 final JexlContext jc = new MapContext();
75
76 /*
77 * The Java equivalents of foo and number for comparison and checking
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 * access a method w/o args
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 * access a method w/ args
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 * access a property
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 * Command line entry point.
118 * @param args command line arguments
119 * @throws Exception cos jexl does.
120 */
121 public static void main(final String[] args) throws Exception {
122 example(AbstractOutput.SYSTEM);
123 }
124
125 /**
126 * Unit test entry point.
127 * @throws Exception
128 */
129 @Test
130 void testExample() throws Exception {
131 example(AbstractOutput.JUNIT);
132 }
133 }