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    *      http://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.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   *  Simple example to show how to access method and properties.
29   *
30   *  @since 1.0
31   */
32  public class MethodPropertyTest {
33      /**
34       * An example for method access.
35       */
36      public static void example(final Output out) throws Exception {
37          /*
38           * First step is to retrieve an instance of a JexlEngine;
39           * it might be already existing and shared or created anew.
40           */
41          final JexlEngine jexl = new JexlBuilder().create();
42          /*
43           *  Second make a jexlContext and put stuff in it
44           */
45          final JexlContext jc = new MapContext();
46  
47          /*
48           * The Java equivalents of foo and number for comparison and checking
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           *  access a method w/o args
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           *  access a method w/ args
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           * access a property
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       * Helper example class.
89       */
90      public static class Foo {
91          /**
92           * Gets foo.
93           * @return a string.
94           */
95          public String getFoo() {
96              return "This is from getFoo()";
97          }
98  
99          /**
100          * Gets an arbitrary property.
101          * @param arg property name.
102          * @return arg prefixed with 'This is the property '.
103          */
104         public String get(final String arg) {
105             return "This is the property " + arg;
106         }
107 
108         /**
109          * Gets a string from the argument.
110          * @param i a long.
111          * @return The argument prefixed with 'The value is : '
112          */
113         public String convert(final long i) {
114             return "The value is : " + i;
115         }
116     }
117 
118 
119     /**
120      * Unit test entry point.
121      * @throws Exception
122      */
123     @Test
124     public void testExample() throws Exception {
125         example(Output.JUNIT);
126     }
127 
128     /**
129      * Command line entry point.
130      * @param args command line arguments
131      * @throws Exception cos jexl does.
132      */
133     public static void main(final String[] args) throws Exception {
134         example(Output.SYSTEM);
135     }
136 }