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%
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, 2022 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 java.util.Collections;
22: import java.util.HashMap;
23: import java.util.Map;
24:
25: /**
26: * Defines property resolution behavior on instances of {@link java.util.Map}.
27: *
28: * <p>
29: * This resolver handles base objects of type <code>java.util.Map</code>. It accepts any object as a property and uses
30: * that object as a key in the map. The resulting value is the value in the map that is associated with that key.
31: * </p>
32: *
33: * <p>
34: * This resolver can be constructed in read-only mode, which means that {@link #isReadOnly} will always return
35: * <code>true</code> and {@link #setValue} will always throw <code>PropertyNotWritableException</code>.
36: * </p>
37: *
38: * <p>
39: * <code>ELResolver</code>s are combined together using {@link CompositeELResolver}s, to define rich semantics for
40: * evaluating an expression. See the javadocs for {@link ELResolver} for details.
41: * </p>
42: *
43: * @see CompositeELResolver
44: * @see ELResolver
45: * @see java.util.Map
46: * @since Jakarta Server Pages 2.1
47: */
48: public class MapELResolver extends ELResolver {
49:
50: static private Class<?> theUnmodifiableMapClass = Collections.unmodifiableMap(new HashMap<>()).getClass();
51: private boolean isReadOnly;
52:
53: /**
54: * Creates a new read/write <code>MapELResolver</code>.
55: */
56: public MapELResolver() {
57: isReadOnly = false;
58: }
59:
60: /**
61: * Creates a new <code>MapELResolver</code> whose read-only status is determined by the given parameter.
62: *
63: * @param isReadOnly <code>true</code> if this resolver cannot modify maps; <code>false</code> otherwise.
64: */
65: public MapELResolver(boolean isReadOnly) {
66: this.isReadOnly = isReadOnly;
67: }
68:
69: /**
70: * If the base object is a map, returns the most general acceptable type for a value in this map.
71: *
72: * <p>
73: * If the base is a <code>Map</code>, the <code>propertyResolved</code> property of the <code>ELContext</code> object
74: * must be set to <code>true</code> by this resolver, before returning. If this property is not <code>true</code> after
75: * this method is called, the caller should ignore the return value.
76: * </p>
77: *
78: * <p>
79: * Assuming the base is a <code>Map</code>, this method will always return <code>Object.class</code> unless the
80: * resolver is constructed in read-only mode in which case {@code null} will be returned. This is because
81: * <code>Map</code>s accept any object as the value for a given key.
82: * </p>
83: *
84: * @param context The context of this evaluation.
85: * @param base The map to analyze. Only bases of type <code>Map</code> are handled by this resolver.
86: * @param property The key to return the acceptable type for. Ignored by this resolver.
87: * @return If the <code>propertyResolved</code> property of <code>ELContext</code> was set to <code>true</code>, then
88: * the most general acceptable type which must be {@code null} if the either the property or the resolver is
89: * read-only; otherwise undefined
90: * @throws NullPointerException if context is <code>null</code>
91: * @throws ELException if an exception was thrown while performing the property or variable resolution. The thrown
92: * exception must be included as the cause property of this exception, if available.
93: */
94: @Override
95: public Class<?> getType(ELContext context, Object base, Object property) {
96:• if (context == null) {
97: throw new NullPointerException();
98: }
99:
100:• if (base != null && base instanceof Map) {
101: context.setPropertyResolved(true);
102:
103: Map<?, ?> map = (Map<?, ?>) base;
104:• if (isReadOnly || map.getClass() == theUnmodifiableMapClass) {
105: return null;
106: }
107:
108: return Object.class;
109: }
110:
111: return null;
112: }
113:
114: /**
115: * If the base object is a map, returns the value associated with the given key, as specified by the
116: * <code>property</code> argument. If the key was not found, <code>null</code> is returned.
117: *
118: * <p>
119: * If the base is a <code>Map</code>, the <code>propertyResolved</code> property of the <code>ELContext</code> object
120: * must be set to <code>true</code> by this resolver, before returning. If this property is not <code>true</code> after
121: * this method is called, the caller should ignore the return value.
122: * </p>
123: *
124: * <p>
125: * Just as in {@link java.util.Map#get}, just because <code>null</code> is returned doesn't mean there is no mapping for
126: * the key; it's also possible that the <code>Map</code> explicitly maps the key to <code>null</code>.
127: * </p>
128: *
129: * @param context The context of this evaluation.
130: * @param base The map to be analyzed. Only bases of type <code>Map</code> are handled by this resolver.
131: * @param property The key whose associated value is to be returned.
132: * @return If the <code>propertyResolved</code> property of <code>ELContext</code> was set to <code>true</code>, then
133: * the value associated with the given key or <code>null</code> if the key was not found. Otherwise, undefined.
134: * @throws ClassCastException if the key is of an inappropriate type for this map (optionally thrown by the underlying
135: * <code>Map</code>).
136: * @throws NullPointerException if context is <code>null</code>, or if the key is null and this map does not permit null
137: * keys (the latter is optionally thrown by the underlying <code>Map</code>).
138: * @throws ELException if an exception was thrown while performing the property or variable resolution. The thrown
139: * exception must be included as the cause property of this exception, if available.
140: */
141: @Override
142: public Object getValue(ELContext context, Object base, Object property) {
143:• if (context == null) {
144: throw new NullPointerException();
145: }
146:
147:• if (base != null && base instanceof Map) {
148: context.setPropertyResolved(base, property);
149: Map<?, ?> map = (Map<?, ?>) base;
150: return map.get(property);
151: }
152:
153: return null;
154: }
155:
156: /**
157: * If the base object is a map, attempts to set the value associated with the given key, as specified by the
158: * <code>property</code> argument.
159: *
160: * <p>
161: * If the base is a <code>Map</code>, the <code>propertyResolved</code> property of the <code>ELContext</code> object
162: * must be set to <code>true</code> by this resolver, before returning. If this property is not <code>true</code> after
163: * this method is called, the caller can safely assume no value was set.
164: * </p>
165: *
166: * <p>
167: * If this resolver was constructed in read-only mode, this method will always throw
168: * <code>PropertyNotWritableException</code>.
169: * </p>
170: *
171: * <p>
172: * If a <code>Map</code> was created using {@link java.util.Collections#unmodifiableMap}, this method must throw
173: * <code>PropertyNotWritableException</code>. Unfortunately, there is no Collections API method to detect this. However,
174: * an implementation can create a prototype unmodifiable <code>Map</code> and query its runtime type to see if it
175: * matches the runtime type of the base object as a workaround.
176: * </p>
177: *
178: * @param context The context of this evaluation.
179: * @param base The map to be modified. Only bases of type <code>Map</code> are handled by this resolver.
180: * @param property The key with which the specified value is to be associated.
181: * @param val The value to be associated with the specified key.
182: * @throws ClassCastException if the class of the specified key or value prevents it from being stored in this map.
183: * @throws NullPointerException if context is <code>null</code>, or if this map does not permit <code>null</code> keys
184: * or values, and the specified key or value is <code>null</code>.
185: * @throws IllegalArgumentException if some aspect of this key or value prevents it from being stored in this map.
186: * @throws ELException if an exception was thrown while performing the property or variable resolution. The thrown
187: * exception must be included as the cause property of this exception, if available.
188: * @throws PropertyNotWritableException if this resolver was constructed in read-only mode, or if the put operation is
189: * not supported by the underlying map.
190: */
191: @Override
192: public void setValue(ELContext context, Object base, Object property, Object val) {
193:• if (context == null) {
194: throw new NullPointerException();
195: }
196:
197:• if (base != null && base instanceof Map) {
198: context.setPropertyResolved(base, property);
199:
200: // The cast is safe
201: @SuppressWarnings("unchecked")
202: Map<Object, Object> map = (Map<Object, Object>) base;
203:• if (isReadOnly || map.getClass() == theUnmodifiableMapClass) {
204: throw new PropertyNotWritableException();
205: }
206:
207: try {
208: map.put(property, val);
209: } catch (UnsupportedOperationException ex) {
210: throw new PropertyNotWritableException();
211: }
212: }
213: }
214:
215: /**
216: * If the base object is a map, returns whether a call to {@link #setValue} will always fail.
217: *
218: * <p>
219: * If the base is a <code>Map</code>, the <code>propertyResolved</code> property of the <code>ELContext</code> object
220: * must be set to <code>true</code> by this resolver, before returning. If this property is not <code>true</code> after
221: * this method is called, the caller should ignore the return value.
222: * </p>
223: *
224: * <p>
225: * If this resolver was constructed in read-only mode, this method will always return <code>true</code>.
226: * </p>
227: *
228: * <p>
229: * If a <code>Map</code> was created using {@link java.util.Collections#unmodifiableMap}, this method must return
230: * <code>true</code>. Unfortunately, there is no Collections API method to detect this. However, an implementation can
231: * create a prototype unmodifiable <code>Map</code> and query its runtime type to see if it matches the runtime type of
232: * the base object as a workaround.
233: * </p>
234: *
235: * @param context The context of this evaluation.
236: * @param base The map to analyze. Only bases of type <code>Map</code> are handled by this resolver.
237: * @param property The key to return the read-only status for. Ignored by this resolver.
238: * @return If the <code>propertyResolved</code> property of <code>ELContext</code> was set to <code>true</code>, then
239: * <code>true</code> if calling the <code>setValue</code> method will always fail or <code>false</code> if it is
240: * possible that such a call may succeed; otherwise undefined.
241: * @throws NullPointerException if context is <code>null</code>
242: * @throws ELException if an exception was thrown while performing the property or variable resolution. The thrown
243: * exception must be included as the cause property of this exception, if available.
244: */
245: @Override
246: public boolean isReadOnly(ELContext context, Object base, Object property) {
247:• if (context == null) {
248: throw new NullPointerException();
249: }
250:
251:• if (base != null && base instanceof Map) {
252: context.setPropertyResolved(true);
253: Map<?, ?> map = (Map<?, ?>) base;
254:• return isReadOnly || map.getClass() == theUnmodifiableMapClass;
255: }
256:
257: return false;
258: }
259:
260: /**
261: * If the base object is a map, returns the most general type that this resolver accepts for the <code>property</code>
262: * argument. Otherwise, returns <code>null</code>.
263: *
264: * <p>
265: * Assuming the base is a <code>Map</code>, this method will always return <code>Object.class</code>. This is because
266: * <code>Map</code>s accept any object as a key.
267: * </p>
268: *
269: * @param context The context of this evaluation.
270: * @param base The map to analyze. Only bases of type <code>Map</code> are handled by this resolver.
271: * @return <code>null</code> if base is not a <code>Map</code> otherwise <code>Object.class</code>.
272: */
273: @Override
274: public Class<?> getCommonPropertyType(ELContext context, Object base) {
275:• if (base != null && base instanceof Map) {
276: return Object.class;
277: }
278:
279: return null;
280: }
281:
282: }