View Javadoc
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      /**
33       * Helper example class.
34       */
35      public static class Foo {
36  
37          /**
38           * Gets a string from the argument.
39           * @param i a long.
40           * @return The argument prefixed with 'The value is : '
41           */
42          public String convert(final long i) {
43              return "The value is : " + i;
44          }
45  
46          /**
47           * Gets an arbitrary property.
48           * @param arg property name.
49           * @return arg prefixed with 'This is the property '.
50           */
51          public String get(final String arg) {
52              return "This is the property " + arg;
53          }
54  
55          /**
56           * Gets foo.
57           * @return a string.
58           */
59          public String getFoo() {
60              return "This is from getFoo()";
61          }
62      }
63  
64      /**
65       * An example for method access.
66       */
67      public static void example(final AbstractOutput out) throws Exception {
68          /*
69           * First step is to retrieve an instance of a JexlEngine;
70           * it might be already existing and shared or created anew.
71           */
72          final JexlEngine jexl = new JexlBuilder().create();
73          /*
74           *  Second make a jexlContext and put stuff in it
75           */
76          final JexlContext jc = new MapContext();
77  
78          /*
79           * The Java equivalents of foo and number for comparison and checking
80           */
81          final Foo foo = new Foo();
82          final Integer number = 10;
83  
84          jc.set("foo", foo);
85          jc.set("number", number);
86  
87          /*
88           *  access a method w/o args
89           */
90          JexlExpression e = jexl.createExpression("foo.getFoo()");
91          Object o = e.evaluate(jc);
92          out.print("value returned by the method getFoo() is : ", o, foo.getFoo());
93  
94          /*
95           *  access a method w/ args
96           */
97          e = jexl.createExpression("foo.convert(1)");
98          o = e.evaluate(jc);
99          out.print("value of " + e.getParsedText() + " is : ", o, foo.convert(1));
100 
101         e = jexl.createExpression("foo.convert(1+7)");
102         o = e.evaluate(jc);
103         out.print("value of " + e.getParsedText() + " is : ", o, foo.convert(1+7));
104 
105         e = jexl.createExpression("foo.convert(1+number)");
106         o = e.evaluate(jc);
107         out.print("value of " + e.getParsedText() + " is : ", o, foo.convert(1+ number));
108 
109         /*
110          * access a property
111          */
112         e = jexl.createExpression("foo.bar");
113         o = e.evaluate(jc);
114         out.print("value returned for the property 'bar' is : ", o, foo.get("bar"));
115 
116     }
117 
118     /**
119      * Command line entry point.
120      * @param args command line arguments
121      * @throws Exception cos jexl does.
122      */
123     public static void main(final String[] args) throws Exception {
124         example(AbstractOutput.SYSTEM);
125     }
126 
127     /**
128      * Unit test entry point.
129      * @throws Exception
130      */
131     @Test
132     void testExample() throws Exception {
133         example(AbstractOutput.JUNIT);
134     }
135 }