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.internal.introspection;
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       * Attempts to discover a MapGetExecutor.
35       *
36       * @param is the introspector
37       * @param clazz the class to find the get method from
38       * @param identifier the key to use as an argument to the get method
39       * @return the executor if found, null otherwise
40       */
41      public static MapGetExecutor discover(final Introspector is, final Class<?> clazz, final Object identifier) {
42          // we still need to ensure permissions grant access to get(...)
43          if (Map.class.isAssignableFrom(clazz) && is.getMethod(clazz, "get", identifier) != null) {
44              return new MapGetExecutor(clazz, MAP_GET, identifier);
45          }
46          return null;
47      }
48  
49      /**
50       * Creates an instance.
51       * @param clazz he class the get method applies to
52       * @param method the method held by this executor
53       * @param key the property to get
54       */
55      private MapGetExecutor(final Class<?> clazz, final java.lang.reflect.Method method, final Object key) {
56          super(clazz, method);
57          property = key;
58      }
59  
60      @Override
61      public Object getTargetProperty() {
62          return property;
63      }
64  
65      @Override
66      public Object invoke(final Object obj) {
67          @SuppressWarnings("unchecked") // ctor only allows Map instances - see discover() method
68          final Map<Object, ?> map = (Map<Object, ?>) obj;
69          return map.get(property);
70      }
71  
72      @Override
73      public Object tryInvoke(final Object obj, final Object key) {
74          if (obj != null
75              && method != null
76              && objectClass.equals(obj.getClass())
77              && ((property == null && key == null)
78                  || (property != null && key != null && property.getClass().equals(key.getClass())))) {
79              @SuppressWarnings("unchecked") // ctor only allows Map instances - see discover() method
80              final Map<Object, ?> map = (Map<Object, ?>) obj;
81              return map.get(key);
82          }
83          return TRY_FAILED;
84      }
85  }