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.jexl2.internal;
19  
20  import java.util.Map;
21  
22   /**
23   * Specialized executor to get a property from a Map.
24    * @since 2.0
25   */
26  public final class MapGetExecutor extends AbstractExecutor.Get {
27      /** The java.util.map.get method used as an active marker in MapGet. */
28      private static final java.lang.reflect.Method MAP_GET =
29              initMarker(Map.class, "get", Object.class);
30      /** The property. */
31      private final Object property;
32  
33      /**
34       * Creates an instance checking for the Map interface.
35       * @param is the introspector
36       * @param clazz the class that might implement the map interface
37       * @param key the key to use in map.get(key)
38       */
39      public MapGetExecutor(Introspector is, Class<?> clazz, Object key) {
40          super(clazz, discover(clazz));
41          property = key;
42      }
43  
44      /** {@inheritDoc} */
45      @Override
46      public Object getTargetProperty() {
47          return property;
48      }
49      
50      /**
51       * Get the property from the map.
52       * @param obj the map.
53       * @return map.get(property)
54       */
55      @Override
56      public Object execute(final Object obj) {
57          @SuppressWarnings("unchecked") // ctor only allows Map instances - see discover() method
58          final Map<Object,?> map = (Map<Object, ?>) obj;
59          return map.get(property);
60      }
61  
62      /** {@inheritDoc} */
63      @Override
64      public Object tryExecute(final Object obj, Object key) {
65          if (obj != null &&  method != null
66              && objectClass.equals(obj.getClass())
67              && (key == null || property.getClass().equals(key.getClass()))) {
68              @SuppressWarnings("unchecked") // ctor only allows Map instances - see discover() method
69              final Map<Object,?> map = (Map<Object, ?>) obj;
70              return map.get(key);
71          }
72          return TRY_FAILED;
73      }
74  
75      /**
76       * Finds the method to perform 'get' on a map.
77       * @param clazz the class to introspect
78       * @return a marker method, map.get
79       */
80      static java.lang.reflect.Method discover(Class<?> clazz) {
81          return (Map.class.isAssignableFrom(clazz))? MAP_GET : null;
82      }
83  }