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.jexl.util;
19  
20  import java.lang.reflect.InvocationTargetException;
21  
22  import org.apache.commons.logging.Log;
23  
24  
25  /**
26   * Executor that simply tries to execute a get(key)
27   * operation. This will try to find a get(key) method
28   * for any type of object, not just objects that
29   * implement the Map interface as was previously
30   * the case.
31   *
32   * @since 1.0
33   * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
34   * @version $Id: GetExecutor.java 584046 2007-10-12 05:14:37Z proyal $
35   */
36  public class GetExecutor extends AbstractExecutor {
37      /**
38       * Container to hold the 'key' part of
39       * get(key).
40       */
41      private final Object[] args;
42  
43      /**
44       * Default constructor.
45       *
46       * @param r The instance log.
47       * @param ispect The JEXL introspector.
48       * @param c The class being examined.
49       * @param key The key for the get(key) operation.
50       * @throws Exception Failure while trying to obtain the pertinent method.
51       */
52      public GetExecutor(Log r,
53              org.apache.commons.jexl.util.introspection.Introspector ispect,
54              Class c, String key) throws Exception {
55          rlog = r;
56          // If you passed in null as property, we don't use the value
57          // for parameter lookup. Instead we just look for get() without
58          // any parameters.
59          //
60          // In any other case, the following condition will set up an array
61          // for looking up get(String) on the class.
62  
63          if (key != null)
64          {
65              args = new Object[] { key };
66          } else {
67              args = null;
68          }
69          method = ispect.getMethod(c, "get", args);
70      }
71  
72      /**
73       * {@inheritDoc}
74       */
75      public Object execute(Object o)
76      throws IllegalAccessException, InvocationTargetException {
77          if (method == null) {
78              return null;
79          }
80  
81          return method.invoke(o, args);
82      }
83  
84  }
85