Package: MapELResolver
MapELResolver
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
MapELResolver() |
|
|
|
|
|
||||||||||||||||||||
MapELResolver(boolean) |
|
|
|
|
|
||||||||||||||||||||
getCommonPropertyType(ELContext, Object) |
|
|
|
|
|
||||||||||||||||||||
getFeatureDescriptors(ELContext, Object) |
|
|
|
|
|
||||||||||||||||||||
getType(ELContext, Object, Object) |
|
|
|
|
|
||||||||||||||||||||
getValue(ELContext, Object, Object) |
|
|
|
|
|
||||||||||||||||||||
isReadOnly(ELContext, Object, Object) |
|
|
|
|
|
||||||||||||||||||||
setValue(ELContext, Object, Object, Object) |
|
|
|
|
|
||||||||||||||||||||
static {...} |
|
|
|
|
|
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>. This is because
86: * <code>Map</code>s accept any object as the value for a given key.
87: * </p>
88: *
89: * @param context The context of this evaluation.
90: * @param base The map to analyze. Only bases of type <code>Map</code> are handled by this resolver.
91: * @param property The key to return the acceptable type for. Ignored by this resolver.
92: * @return If the <code>propertyResolved</code> property of <code>ELContext</code> was set to <code>true</code>, then
93: * the most general acceptable type; otherwise undefined.
94: * @throws NullPointerException if context is <code>null</code>
95: * @throws ELException if an exception was thrown while performing the property or variable resolution. The thrown
96: * exception must be included as the cause property of this exception, if available.
97: */
98: @Override
99: public Class<?> getType(ELContext context, Object base, Object property) {
100:• if (context == null) {
101: throw new NullPointerException();
102: }
103:
104:• if (base != null && base instanceof Map) {
105: context.setPropertyResolved(true);
106: return Object.class;
107: }
108:
109: return null;
110: }
111:
112: /**
113: * If the base object is a map, returns the value associated with the given key, as specified by the
114: * <code>property</code> argument. If the key was not found, <code>null</code> is returned.
115: *
116: * <p>
117: * If the base is a <code>Map</code>, the <code>propertyResolved</code> property of the <code>ELContext</code> object
118: * must be set to <code>true</code> by this resolver, before returning. If this property is not <code>true</code> after
119: * this method is called, the caller should ignore the return value.
120: * </p>
121: *
122: * <p>
123: * Just as in {@link java.util.Map#get}, just because <code>null</code> is returned doesn't mean there is no mapping for
124: * the key; it's also possible that the <code>Map</code> explicitly maps the key to <code>null</code>.
125: * </p>
126: *
127: * @param context The context of this evaluation.
128: * @param base The map to be analyzed. Only bases of type <code>Map</code> are handled by this resolver.
129: * @param property The key whose associated value is to be returned.
130: * @return If the <code>propertyResolved</code> property of <code>ELContext</code> was set to <code>true</code>, then
131: * the value associated with the given key or <code>null</code> if the key was not found. Otherwise, undefined.
132: * @throws ClassCastException if the key is of an inappropriate type for this map (optionally thrown by the underlying
133: * <code>Map</code>).
134: * @throws NullPointerException if context is <code>null</code>, or if the key is null and this map does not permit null
135: * keys (the latter is optionally thrown by the underlying <code>Map</code>).
136: * @throws ELException if an exception was thrown while performing the property or variable resolution. The thrown
137: * exception must be included as the cause property of this exception, if available.
138: */
139: @Override
140: public Object getValue(ELContext context, Object base, Object property) {
141:• if (context == null) {
142: throw new NullPointerException();
143: }
144:
145:• if (base != null && base instanceof Map) {
146: context.setPropertyResolved(base, property);
147: Map<?, ?> map = (Map<?, ?>) base;
148: return map.get(property);
149: }
150:
151: return null;
152: }
153:
154: /**
155: * If the base object is a map, attempts to set the value associated with the given key, as specified by the
156: * <code>property</code> argument.
157: *
158: * <p>
159: * If the base is a <code>Map</code>, the <code>propertyResolved</code> property of the <code>ELContext</code> object
160: * must be set to <code>true</code> by this resolver, before returning. If this property is not <code>true</code> after
161: * this method is called, the caller can safely assume no value was set.
162: * </p>
163: *
164: * <p>
165: * If this resolver was constructed in read-only mode, this method will always throw
166: * <code>PropertyNotWritableException</code>.
167: * </p>
168: *
169: * <p>
170: * If a <code>Map</code> was created using {@link java.util.Collections#unmodifiableMap}, this method must throw
171: * <code>PropertyNotWritableException</code>. Unfortunately, there is no Collections API method to detect this. However,
172: * an implementation can create a prototype unmodifiable <code>Map</code> and query its runtime type to see if it
173: * matches the runtime type of the base object as a workaround.
174: * </p>
175: *
176: * @param context The context of this evaluation.
177: * @param base The map to be modified. Only bases of type <code>Map</code> are handled by this resolver.
178: * @param property The key with which the specified value is to be associated.
179: * @param val The value to be associated with the specified key.
180: * @throws ClassCastException if the class of the specified key or value prevents it from being stored in this map.
181: * @throws NullPointerException if context is <code>null</code>, or if this map does not permit <code>null</code> keys
182: * or values, and the specified key or value is <code>null</code>.
183: * @throws IllegalArgumentException if some aspect of this key or value prevents it from being stored in this map.
184: * @throws ELException if an exception was thrown while performing the property or variable resolution. The thrown
185: * exception must be included as the cause property of this exception, if available.
186: * @throws PropertyNotWritableException if this resolver was constructed in read-only mode, or if the put operation is
187: * not supported by the underlying map.
188: */
189: @Override
190: public void setValue(ELContext context, Object base, Object property, Object val) {
191:• if (context == null) {
192: throw new NullPointerException();
193: }
194:
195:• if (base != null && base instanceof Map) {
196: context.setPropertyResolved(base, property);
197:
198: // The cast is safe
199: @SuppressWarnings("unchecked")
200: Map<Object, Object> map = (Map<Object, Object>) base;
201:• if (isReadOnly || map.getClass() == theUnmodifiableMapClass) {
202: throw new PropertyNotWritableException();
203: }
204:
205: try {
206: map.put(property, val);
207: } catch (UnsupportedOperationException ex) {
208: throw new PropertyNotWritableException();
209: }
210: }
211: }
212:
213: /**
214: * If the base object is a map, returns whether a call to {@link #setValue} will always fail.
215: *
216: * <p>
217: * If the base is a <code>Map</code>, the <code>propertyResolved</code> property of the <code>ELContext</code> object
218: * must be set to <code>true</code> by this resolver, before returning. If this property is not <code>true</code> after
219: * this method is called, the caller should ignore the return value.
220: * </p>
221: *
222: * <p>
223: * If this resolver was constructed in read-only mode, this method will always return <code>true</code>.
224: * </p>
225: *
226: * <p>
227: * If a <code>Map</code> was created using {@link java.util.Collections#unmodifiableMap}, this method must return
228: * <code>true</code>. Unfortunately, there is no Collections API method to detect this. However, an implementation can
229: * create a prototype unmodifiable <code>Map</code> and query its runtime type to see if it matches the runtime type of
230: * the base object as a workaround.
231: * </p>
232: *
233: * @param context The context of this evaluation.
234: * @param base The map to analyze. Only bases of type <code>Map</code> are handled by this resolver.
235: * @param property The key to return the read-only status for. Ignored by this resolver.
236: * @return If the <code>propertyResolved</code> property of <code>ELContext</code> was set to <code>true</code>, then
237: * <code>true</code> if calling the <code>setValue</code> method will always fail or <code>false</code> if it is
238: * possible that such a call may succeed; otherwise undefined.
239: * @throws NullPointerException if context is <code>null</code>
240: * @throws ELException if an exception was thrown while performing the property or variable resolution. The thrown
241: * exception must be included as the cause property of this exception, if available.
242: */
243: @Override
244: public boolean isReadOnly(ELContext context, Object base, Object property) {
245:• if (context == null) {
246: throw new NullPointerException();
247: }
248:
249:• if (base != null && base instanceof Map) {
250: context.setPropertyResolved(true);
251: Map<?, ?> map = (Map<?, ?>) base;
252:• return isReadOnly || map.getClass() == theUnmodifiableMapClass;
253: }
254:
255: return false;
256: }
257:
258: /**
259: * If the base object is a map, returns an <code>Iterator</code> containing the set of keys available in the
260: * <code>Map</code>. Otherwise, returns <code>null</code>.
261: *
262: * <p>
263: * The <code>Iterator</code> returned must contain zero or more instances of {@link java.beans.FeatureDescriptor}. Each
264: * info object contains information about a key in the Map, and is initialized as follows:
265: * <ul>
266: * <li>displayName - The return value of calling the <code>toString</code> method on this key, or <code>"null"</code> if
267: * the key is <code>null</code>.</li>
268: * <li>name - Same as displayName property.</li>
269: * <li>shortDescription - Empty string</li>
270: * <li>expert - <code>false</code></li>
271: * <li>hidden - <code>false</code></li>
272: * <li>preferred - <code>true</code></li>
273: * </ul>
274: *
275: * In addition, the following named attributes must be set in the returned <code>FeatureDescriptor</code>s:
276: * <ul>
277: * <li>{@link ELResolver#TYPE} - The return value of calling the <code>getClass()</code> method on this key, or
278: * <code>null</code> if the key is <code>null</code>.</li>
279: * <li>{@link ELResolver#RESOLVABLE_AT_DESIGN_TIME} - <code>true</code></li>
280: * </ul>
281: *
282: *
283: * @param context The context of this evaluation.
284: * @param base The map whose keys are to be iterated over. Only bases of type <code>Map</code> are handled by this
285: * resolver.
286: * @return An <code>Iterator</code> containing zero or more (possibly infinitely more) <code>FeatureDescriptor</code>
287: * objects, each representing a key in this map, or <code>null</code> if the base object is not a map.
288: *
289: * @deprecated This method will be removed without replacement in EL 6.0
290: */
291: @Deprecated(forRemoval = true, since = "5.0")
292: @Override
293: public Iterator<FeatureDescriptor> getFeatureDescriptors(ELContext context, Object base) {
294:• if (base != null && base instanceof Map) {
295: Map<?, ?> map = (Map<?, ?>) base;
296: Iterator<?> iter = map.keySet().iterator();
297: List<FeatureDescriptor> list = new ArrayList<>();
298:
299:• while (iter.hasNext()) {
300: Object key = iter.next();
301: FeatureDescriptor descriptor = new FeatureDescriptor();
302:• String name = key == null ? null : key.toString();
303: descriptor.setName(name);
304: descriptor.setDisplayName(name);
305: descriptor.setShortDescription("");
306: descriptor.setExpert(false);
307: descriptor.setHidden(false);
308: descriptor.setPreferred(true);
309:
310:• if (key != null) {
311: descriptor.setValue("type", key.getClass());
312: }
313:
314: descriptor.setValue("resolvableAtDesignTime", TRUE);
315: list.add(descriptor);
316: }
317:
318: return list.iterator();
319: }
320:
321: return null;
322: }
323:
324: /**
325: * If the base object is a map, returns the most general type that this resolver accepts for the <code>property</code>
326: * argument. Otherwise, returns <code>null</code>.
327: *
328: * <p>
329: * Assuming the base is a <code>Map</code>, this method will always return <code>Object.class</code>. This is because
330: * <code>Map</code>s accept any object as a key.
331: * </p>
332: *
333: * @param context The context of this evaluation.
334: * @param base The map to analyze. Only bases of type <code>Map</code> are handled by this resolver.
335: * @return <code>null</code> if base is not a <code>Map</code> otherwise <code>Object.class</code>.
336: */
337: @Override
338: public Class<?> getCommonPropertyType(ELContext context, Object base) {
339:• if (base != null && base instanceof Map) {
340: return Object.class;
341: }
342:
343: return null;
344: }
345:
346: }