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.internal.introspection;
19
20 import java.util.Map;
21
22 /**
23 * Specialized executor to get a property from a Map.
24 *
25 * @since 2.0
26 */
27 public final class MapGetExecutor extends AbstractExecutor.Get {
28
29 /** The java.util.map.get method used as an active marker in MapGet. */
30 private static final java.lang.reflect.Method MAP_GET =
31 initMarker(Map.class, "get", Object.class);
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 /** The property. */
50 private final Object property;
51
52 /**
53 * Creates an instance.
54 *
55 * @param clazz he class the get method applies to
56 * @param method the method held by this executor
57 * @param key the property to get
58 */
59 private MapGetExecutor(final Class<?> clazz, final java.lang.reflect.Method method, final Object key) {
60 super(clazz, method);
61 property = key;
62 }
63
64 @Override
65 public Object getTargetProperty() {
66 return property;
67 }
68
69 @Override
70 public Object invoke(final Object obj) {
71 @SuppressWarnings("unchecked") // ctor only allows Map instances - see discover() method
72 final Map<Object, ?> map = (Map<Object, ?>) obj;
73 return map.get(property);
74 }
75
76 @Override
77 public Object tryInvoke(final Object obj, final Object key) {
78 if (obj != null
79 && method != null
80 && objectClass.equals(obj.getClass())
81 && (property == null && key == null
82 || property != null && key != null && property.getClass().equals(key.getClass()))) {
83 @SuppressWarnings("unchecked") // ctor only allows Map instances - see discover() method
84 final Map<Object, ?> map = (Map<Object, ?>) obj;
85 return map.get(key);
86 }
87 return TRY_FAILED;
88 }
89 }