Skip to content

Package: MapELResolver

MapELResolver

nameinstructionbranchcomplexitylinemethod
MapELResolver()
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
MapELResolver(boolean)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
getCommonPropertyType(ELContext, Object)
M: 9 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
getFeatureDescriptors(ELContext, Object)
M: 72 C: 0
0%
M: 10 C: 0
0%
M: 6 C: 0
0%
M: 21 C: 0
0%
M: 1 C: 0
0%
getType(ELContext, Object, Object)
M: 30 C: 0
0%
M: 10 C: 0
0%
M: 6 C: 0
0%
M: 9 C: 0
0%
M: 1 C: 0
0%
getValue(ELContext, Object, Object)
M: 24 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
isReadOnly(ELContext, Object, Object)
M: 30 C: 0
0%
M: 10 C: 0
0%
M: 6 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
setValue(ELContext, Object, Object, Object)
M: 41 C: 0
0%
M: 10 C: 0
0%
M: 6 C: 0
0%
M: 12 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * Copyright (c) 1997, 2021 Oracle and/or its affiliates and others.
3: * All rights reserved.
4: * Copyright 2004 The Apache Software Foundation
5: *
6: * Licensed under the Apache License, Version 2.0 (the "License");
7: * you may not use this file except in compliance with the License.
8: * You may obtain a copy of the License at
9: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing, software
13: * distributed under the License is distributed on an "AS IS" BASIS,
14: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15: * See the License for the specific language governing permissions and
16: * limitations under the License.
17: */
18:
19: package jakarta.el;
20:
21: import static java.lang.Boolean.TRUE;
22:
23: import java.beans.FeatureDescriptor;
24: import java.util.ArrayList;
25: import java.util.Collections;
26: import java.util.HashMap;
27: import java.util.Iterator;
28: import java.util.List;
29: import java.util.Map;
30:
31: /**
32: * Defines property resolution behavior on instances of {@link java.util.Map}.
33: *
34: * <p>
35: * This resolver handles base objects of type <code>java.util.Map</code>. It accepts any object as a property and uses
36: * that object as a key in the map. The resulting value is the value in the map that is associated with that key.
37: * </p>
38: *
39: * <p>
40: * This resolver can be constructed in read-only mode, which means that {@link #isReadOnly} will always return
41: * <code>true</code> and {@link #setValue} will always throw <code>PropertyNotWritableException</code>.
42: * </p>
43: *
44: * <p>
45: * <code>ELResolver</code>s are combined together using {@link CompositeELResolver}s, to define rich semantics for
46: * evaluating an expression. See the javadocs for {@link ELResolver} for details.
47: * </p>
48: *
49: * @see CompositeELResolver
50: * @see ELResolver
51: * @see java.util.Map
52: * @since Jakarta Server Pages 2.1
53: */
54: public class MapELResolver extends ELResolver {
55:
56: static private Class<?> theUnmodifiableMapClass = Collections.unmodifiableMap(new HashMap<>()).getClass();
57: private boolean isReadOnly;
58:
59: /**
60: * Creates a new read/write <code>MapELResolver</code>.
61: */
62: public MapELResolver() {
63: isReadOnly = false;
64: }
65:
66: /**
67: * Creates a new <code>MapELResolver</code> whose read-only status is determined by the given parameter.
68: *
69: * @param isReadOnly <code>true</code> if this resolver cannot modify maps; <code>false</code> otherwise.
70: */
71: public MapELResolver(boolean isReadOnly) {
72: this.isReadOnly = isReadOnly;
73: }
74:
75: /**
76: * If the base object is a map, returns the most general acceptable type for a value in this map.
77: *
78: * <p>
79: * If the base is a <code>Map</code>, the <code>propertyResolved</code> property of the <code>ELContext</code> object
80: * must be set to <code>true</code> by this resolver, before returning. If this property is not <code>true</code> after
81: * this method is called, the caller should ignore the return value.
82: * </p>
83: *
84: * <p>
85: * Assuming the base is a <code>Map</code>, this method will always return <code>Object.class</code> unless the
86: * resolver is constructed in read-only mode in which case {@code null} will be returned. This is because
87: * <code>Map</code>s accept any object as the value for a given key.
88: * </p>
89: *
90: * @param context The context of this evaluation.
91: * @param base The map to analyze. Only bases of type <code>Map</code> are handled by this resolver.
92: * @param property The key to return the acceptable type for. Ignored by this resolver.
93: * @return If the <code>propertyResolved</code> property of <code>ELContext</code> was set to <code>true</code>, then
94: * the most general acceptable type which must be {@code null} if the either the property or the resolver is
95: * read-only; otherwise undefined
96: * @throws NullPointerException if context is <code>null</code>
97: * @throws ELException if an exception was thrown while performing the property or variable resolution. The thrown
98: * exception must be included as the cause property of this exception, if available.
99: */
100: @Override
101: public Class<?> getType(ELContext context, Object base, Object property) {
102:• if (context == null) {
103: throw new NullPointerException();
104: }
105:
106:• if (base != null && base instanceof Map) {
107: context.setPropertyResolved(true);
108:
109: Map<?, ?> map = (Map<?, ?>) base;
110:• if (isReadOnly || map.getClass() == theUnmodifiableMapClass) {
111: return null;
112: }
113:
114: return Object.class;
115: }
116:
117: return null;
118: }
119:
120: /**
121: * If the base object is a map, returns the value associated with the given key, as specified by the
122: * <code>property</code> argument. If the key was not found, <code>null</code> is returned.
123: *
124: * <p>
125: * If the base is a <code>Map</code>, the <code>propertyResolved</code> property of the <code>ELContext</code> object
126: * must be set to <code>true</code> by this resolver, before returning. If this property is not <code>true</code> after
127: * this method is called, the caller should ignore the return value.
128: * </p>
129: *
130: * <p>
131: * Just as in {@link java.util.Map#get}, just because <code>null</code> is returned doesn't mean there is no mapping for
132: * the key; it's also possible that the <code>Map</code> explicitly maps the key to <code>null</code>.
133: * </p>
134: *
135: * @param context The context of this evaluation.
136: * @param base The map to be analyzed. Only bases of type <code>Map</code> are handled by this resolver.
137: * @param property The key whose associated value is to be returned.
138: * @return If the <code>propertyResolved</code> property of <code>ELContext</code> was set to <code>true</code>, then
139: * the value associated with the given key or <code>null</code> if the key was not found. Otherwise, undefined.
140: * @throws ClassCastException if the key is of an inappropriate type for this map (optionally thrown by the underlying
141: * <code>Map</code>).
142: * @throws NullPointerException if context is <code>null</code>, or if the key is null and this map does not permit null
143: * keys (the latter is optionally thrown by the underlying <code>Map</code>).
144: * @throws ELException if an exception was thrown while performing the property or variable resolution. The thrown
145: * exception must be included as the cause property of this exception, if available.
146: */
147: @Override
148: public Object getValue(ELContext context, Object base, Object property) {
149:• if (context == null) {
150: throw new NullPointerException();
151: }
152:
153:• if (base != null && base instanceof Map) {
154: context.setPropertyResolved(base, property);
155: Map<?, ?> map = (Map<?, ?>) base;
156: return map.get(property);
157: }
158:
159: return null;
160: }
161:
162: /**
163: * If the base object is a map, attempts to set the value associated with the given key, as specified by the
164: * <code>property</code> argument.
165: *
166: * <p>
167: * If the base is a <code>Map</code>, the <code>propertyResolved</code> property of the <code>ELContext</code> object
168: * must be set to <code>true</code> by this resolver, before returning. If this property is not <code>true</code> after
169: * this method is called, the caller can safely assume no value was set.
170: * </p>
171: *
172: * <p>
173: * If this resolver was constructed in read-only mode, this method will always throw
174: * <code>PropertyNotWritableException</code>.
175: * </p>
176: *
177: * <p>
178: * If a <code>Map</code> was created using {@link java.util.Collections#unmodifiableMap}, this method must throw
179: * <code>PropertyNotWritableException</code>. Unfortunately, there is no Collections API method to detect this. However,
180: * an implementation can create a prototype unmodifiable <code>Map</code> and query its runtime type to see if it
181: * matches the runtime type of the base object as a workaround.
182: * </p>
183: *
184: * @param context The context of this evaluation.
185: * @param base The map to be modified. Only bases of type <code>Map</code> are handled by this resolver.
186: * @param property The key with which the specified value is to be associated.
187: * @param val The value to be associated with the specified key.
188: * @throws ClassCastException if the class of the specified key or value prevents it from being stored in this map.
189: * @throws NullPointerException if context is <code>null</code>, or if this map does not permit <code>null</code> keys
190: * or values, and the specified key or value is <code>null</code>.
191: * @throws IllegalArgumentException if some aspect of this key or value prevents it from being stored in this map.
192: * @throws ELException if an exception was thrown while performing the property or variable resolution. The thrown
193: * exception must be included as the cause property of this exception, if available.
194: * @throws PropertyNotWritableException if this resolver was constructed in read-only mode, or if the put operation is
195: * not supported by the underlying map.
196: */
197: @Override
198: public void setValue(ELContext context, Object base, Object property, Object val) {
199:• if (context == null) {
200: throw new NullPointerException();
201: }
202:
203:• if (base != null && base instanceof Map) {
204: context.setPropertyResolved(base, property);
205:
206: // The cast is safe
207: @SuppressWarnings("unchecked")
208: Map<Object, Object> map = (Map<Object, Object>) base;
209:• if (isReadOnly || map.getClass() == theUnmodifiableMapClass) {
210: throw new PropertyNotWritableException();
211: }
212:
213: try {
214: map.put(property, val);
215: } catch (UnsupportedOperationException ex) {
216: throw new PropertyNotWritableException();
217: }
218: }
219: }
220:
221: /**
222: * If the base object is a map, returns whether a call to {@link #setValue} will always fail.
223: *
224: * <p>
225: * If the base is a <code>Map</code>, the <code>propertyResolved</code> property of the <code>ELContext</code> object
226: * must be set to <code>true</code> by this resolver, before returning. If this property is not <code>true</code> after
227: * this method is called, the caller should ignore the return value.
228: * </p>
229: *
230: * <p>
231: * If this resolver was constructed in read-only mode, this method will always return <code>true</code>.
232: * </p>
233: *
234: * <p>
235: * If a <code>Map</code> was created using {@link java.util.Collections#unmodifiableMap}, this method must return
236: * <code>true</code>. Unfortunately, there is no Collections API method to detect this. However, an implementation can
237: * create a prototype unmodifiable <code>Map</code> and query its runtime type to see if it matches the runtime type of
238: * the base object as a workaround.
239: * </p>
240: *
241: * @param context The context of this evaluation.
242: * @param base The map to analyze. Only bases of type <code>Map</code> are handled by this resolver.
243: * @param property The key to return the read-only status for. Ignored by this resolver.
244: * @return If the <code>propertyResolved</code> property of <code>ELContext</code> was set to <code>true</code>, then
245: * <code>true</code> if calling the <code>setValue</code> method will always fail or <code>false</code> if it is
246: * possible that such a call may succeed; otherwise undefined.
247: * @throws NullPointerException if context is <code>null</code>
248: * @throws ELException if an exception was thrown while performing the property or variable resolution. The thrown
249: * exception must be included as the cause property of this exception, if available.
250: */
251: @Override
252: public boolean isReadOnly(ELContext context, Object base, Object property) {
253:• if (context == null) {
254: throw new NullPointerException();
255: }
256:
257:• if (base != null && base instanceof Map) {
258: context.setPropertyResolved(true);
259: Map<?, ?> map = (Map<?, ?>) base;
260:• return isReadOnly || map.getClass() == theUnmodifiableMapClass;
261: }
262:
263: return false;
264: }
265:
266: /**
267: * If the base object is a map, returns an <code>Iterator</code> containing the set of keys available in the
268: * <code>Map</code>. Otherwise, returns <code>null</code>.
269: *
270: * <p>
271: * The <code>Iterator</code> returned must contain zero or more instances of {@link java.beans.FeatureDescriptor}. Each
272: * info object contains information about a key in the Map, and is initialized as follows:
273: * <ul>
274: * <li>displayName - The return value of calling the <code>toString</code> method on this key, or <code>"null"</code> if
275: * the key is <code>null</code>.</li>
276: * <li>name - Same as displayName property.</li>
277: * <li>shortDescription - Empty string</li>
278: * <li>expert - <code>false</code></li>
279: * <li>hidden - <code>false</code></li>
280: * <li>preferred - <code>true</code></li>
281: * </ul>
282: *
283: * In addition, the following named attributes must be set in the returned <code>FeatureDescriptor</code>s:
284: * <ul>
285: * <li>{@link ELResolver#TYPE} - The return value of calling the <code>getClass()</code> method on this key, or
286: * <code>null</code> if the key is <code>null</code>.</li>
287: * <li>{@link ELResolver#RESOLVABLE_AT_DESIGN_TIME} - <code>true</code></li>
288: * </ul>
289: *
290: *
291: * @param context The context of this evaluation.
292: * @param base The map whose keys are to be iterated over. Only bases of type <code>Map</code> are handled by this
293: * resolver.
294: * @return An <code>Iterator</code> containing zero or more (possibly infinitely more) <code>FeatureDescriptor</code>
295: * objects, each representing a key in this map, or <code>null</code> if the base object is not a map.
296: *
297: * @deprecated This method will be removed without replacement in EL 6.0
298: */
299: @Deprecated(forRemoval = true, since = "5.0")
300: @Override
301: public Iterator<FeatureDescriptor> getFeatureDescriptors(ELContext context, Object base) {
302:• if (base != null && base instanceof Map) {
303: Map<?, ?> map = (Map<?, ?>) base;
304: Iterator<?> iter = map.keySet().iterator();
305: List<FeatureDescriptor> list = new ArrayList<>();
306:
307:• while (iter.hasNext()) {
308: Object key = iter.next();
309: FeatureDescriptor descriptor = new FeatureDescriptor();
310:• String name = key == null ? null : key.toString();
311: descriptor.setName(name);
312: descriptor.setDisplayName(name);
313: descriptor.setShortDescription("");
314: descriptor.setExpert(false);
315: descriptor.setHidden(false);
316: descriptor.setPreferred(true);
317:
318:• if (key != null) {
319: descriptor.setValue("type", key.getClass());
320: }
321:
322: descriptor.setValue("resolvableAtDesignTime", TRUE);
323: list.add(descriptor);
324: }
325:
326: return list.iterator();
327: }
328:
329: return null;
330: }
331:
332: /**
333: * If the base object is a map, returns the most general type that this resolver accepts for the <code>property</code>
334: * argument. Otherwise, returns <code>null</code>.
335: *
336: * <p>
337: * Assuming the base is a <code>Map</code>, this method will always return <code>Object.class</code>. This is because
338: * <code>Map</code>s accept any object as a key.
339: * </p>
340: *
341: * @param context The context of this evaluation.
342: * @param base The map to analyze. Only bases of type <code>Map</code> are handled by this resolver.
343: * @return <code>null</code> if base is not a <code>Map</code> otherwise <code>Object.class</code>.
344: */
345: @Override
346: public Class<?> getCommonPropertyType(ELContext context, Object base) {
347:• if (base != null && base instanceof Map) {
348: return Object.class;
349: }
350:
351: return null;
352: }
353:
354: }