Skip to contentMethod: ArrayELResolver()
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.lang.reflect.Array;
22:
23: /**
24: * Defines property resolution behavior on arrays.
25: *
26: * <p>
27: * This resolver handles base objects that are Java language arrays. It accepts any object as a property and coerces
28: * that object into an integer index into the array. The resulting value is the value in the array at that index.
29: * </p>
30: *
31: * <p>
32: * This resolver can be constructed in read-only mode, which means that {@link #isReadOnly} will always return
33: * <code>true</code> and {@link #setValue} will always throw <code>PropertyNotWritableException</code>.
34: * </p>
35: *
36: * <p>
37: * <code>ELResolver</code>s are combined together using {@link CompositeELResolver}s, to define rich semantics for
38: * evaluating an expression. See the javadocs for {@link ELResolver} for details.
39: * </p>
40: *
41: * @see CompositeELResolver
42: * @see ELResolver
43: *
44: * @since Jakarta Server Pages 2.1
45: */
46: public class ArrayELResolver extends ELResolver {
47:
48: private static final String LENGTH_PROPERTY_NAME = "length";
49:
50: /**
51: * Creates a new read/write <code>ArrayELResolver</code>.
52: */
53: public ArrayELResolver() {
54: this.isReadOnly = false;
55: }
56:
57: /**
58: * Creates a new <code>ArrayELResolver</code> whose read-only status is determined by the given parameter.
59: *
60: * @param isReadOnly <code>true</code> if this resolver cannot modify arrays; <code>false</code> otherwise.
61: */
62: public ArrayELResolver(boolean isReadOnly) {
63: this.isReadOnly = isReadOnly;
64: }
65:
66: /**
67: * If the base object is an array, returns the most general acceptable type for a value in this array.
68: *
69: * <p>
70: * If the base is a <code>array</code>, the <code>propertyResolved</code> property of the <code>ELContext</code> object
71: * must be set to <code>true</code> by this resolver, before returning. If this property is not <code>true</code> after
72: * this method is called, the caller should ignore the return value.
73: * </p>
74: *
75: * <p>
76: * Assuming the base is an <code>array</code> and that this resolver was not constructed in read-only mode, this
77: * method will return <code>base.getClass().getComponentType()</code>, which is the most general type of component
78: * that can be stored at any given index in the array.
79: * </p>
80: *
81: * @param context The context of this evaluation.
82: * @param base The array to analyze. Only bases that are Java language arrays are handled by this resolver.
83: * @param property The index of the element in the array to return the acceptable type for. Will be coerced into an
84: * integer, but otherwise ignored by this resolver.
85: * @return If the <code>propertyResolved</code> property of <code>ELContext</code> was set to <code>true</code>, then
86: * the most general acceptable type which must be {@code null} if the either the property or the resolver is
87: * read-only; otherwise undefined
88: * @throws PropertyNotFoundException if the given index is out of bounds for this array.
89: * @throws NullPointerException if context is <code>null</code>
90: * @throws ELException if an exception was thrown while performing the property or variable resolution. The thrown
91: * exception must be included as the cause property of this exception, if available.
92: */
93: @Override
94: public Class<?> getType(ELContext context, Object base, Object property) {
95:
96: if (context == null) {
97: throw new NullPointerException();
98: }
99:
100: if (base != null && base.getClass().isArray()) {
101: context.setPropertyResolved(true);
102: int index = toInteger(property);
103: if (index < 0 || index >= Array.getLength(base)) {
104: throw new PropertyNotFoundException();
105: }
106:
107: /*
108: * The resolver may have been created in read-only mode but the
109: * array and its elements will always be read-write.
110: */
111: if (isReadOnly) {
112: return null;
113: }
114:
115: return base.getClass().getComponentType();
116: }
117: return null;
118: }
119:
120: /**
121: * If the base object is a Java language array, returns the length of the array or the value at the given index. If
122: * the {@code property} argument is the case sensitive string {@code "length"}, then the length of the array is
123: * returned. Otherwise, the {@code property} argument is coerced into an integer and used as the index of the value
124: * to be returned. If the coercion could not be performed, an {@code IllegalArgumentException} is thrown. If the
125: * index is out of bounds, {@code null} is returned.
126: *
127: * <p>
128: * If the base is a Java language array, the {@code propertyResolved} property of the {@code ELContext} object must
129: * be set to {@code true} by this resolver, before returning. If this property is not {@code true} after this method
130: * is called, the caller should ignore the return value.
131: * </p>
132: *
133: * @param context The context of this evaluation.
134: * @param base The array to analyze. Only bases that are Java language arrays are handled by this resolver.
135: * @param property Either the string {@code "length"} or the index of the value to be returned. An index value will
136: * be coerced into an integer.
137: * @return If the {@code propertyResolved} property of {@code ELContext} was set to {@code true}, then the length of
138: * the array, the value at the given index or {@code null} if the index was out of bounds. Otherwise, undefined.
139: * @throws IllegalArgumentException if the property was not the string {@code "length"} and could not be coerced
140: * into an integer.
141: * @throws NullPointerException if context is {@code null}.
142: * @throws ELException if an exception was thrown while performing the property or variable resolution. The thrown
143: * exception must be included as the cause property of this exception, if available.
144: */
145: @Override
146: public Object getValue(ELContext context, Object base, Object property) {
147:
148: if (context == null) {
149: throw new NullPointerException();
150: }
151:
152: if (base != null && base.getClass().isArray()) {
153: context.setPropertyResolved(base, property);
154: if (LENGTH_PROPERTY_NAME.equals(property)) {
155: return Integer.valueOf(Array.getLength(base));
156: }
157: int index = toInteger(property);
158: if (index >= 0 && index < Array.getLength(base)) {
159: return Array.get(base, index);
160: }
161: }
162: return null;
163: }
164:
165: /**
166: * If the base object is a Java language array, attempts to set the value at the given index with the given value. The
167: * index is specified by the <code>property</code> argument, and coerced into an integer. If the coercion could not be
168: * performed, an <code>IllegalArgumentException</code> is thrown. If the index is out of bounds, a
169: * <code>PropertyNotFoundException</code> is thrown.
170: *
171: * <p>
172: * If the base is a Java language array, the <code>propertyResolved</code> property of the <code>ELContext</code> object
173: * must be set to <code>true</code> by this resolver, before returning. If this property is not <code>true</code> after
174: * this method is called, the caller can safely assume no value was set.
175: * </p>
176: *
177: * <p>
178: * If this resolver was constructed in read-only mode, this method will always throw
179: * <code>PropertyNotWritableException</code>.
180: * </p>
181: *
182: * @param context The context of this evaluation.
183: * @param base The array to be modified. Only bases that are Java language arrays are handled by this resolver.
184: * @param property The index of the value to be set. Will be coerced into an integer.
185: * @param val The value to be set at the given index.
186: * @throws ClassCastException if the class of the specified element prevents it from being added to this array.
187: * @throws NullPointerException if context is <code>null</code>.
188: * @throws IllegalArgumentException if the property could not be coerced into an integer, or if some aspect of the
189: * specified element prevents it from being added to this array.
190: * @throws PropertyNotWritableException if this resolver was constructed in read-only mode.
191: * @throws PropertyNotFoundException if the given index is out of bounds for this array.
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: */
195: @Override
196: public void setValue(ELContext context, Object base, Object property, Object val) {
197:
198: if (context == null) {
199: throw new NullPointerException();
200: }
201:
202: if (base != null && base.getClass().isArray()) {
203: context.setPropertyResolved(base, property);
204: if (isReadOnly) {
205: throw new PropertyNotWritableException();
206: }
207: Class<?> type = base.getClass().getComponentType();
208: if (val != null && !type.isAssignableFrom(val.getClass())) {
209: throw new ClassCastException();
210: }
211: int index = toInteger(property);
212: if (index < 0 || index >= Array.getLength(base)) {
213: throw new PropertyNotFoundException();
214: }
215: Array.set(base, index, val);
216: }
217: }
218:
219: /**
220: * If the base object is a Java language array, returns whether a call to {@link #setValue} will always fail.
221: *
222: * <p>
223: * If the base is a Java language array, the <code>propertyResolved</code> property of the <code>ELContext</code> object
224: * must be set to <code>true</code> by this resolver, before returning. If this property is not <code>true</code> after
225: * this method is called, the caller should ignore the return value.
226: * </p>
227: *
228: * <p>
229: * If this resolver was constructed in read-only mode, this method will always return <code>true</code>. Otherwise, it
230: * returns <code>false</code>.
231: * </p>
232: *
233: * @param context The context of this evaluation.
234: * @param base The array to analyze. Only bases that are a Java language array are handled by this resolver.
235: * @param property The index of the element in the array to return the acceptable type for. Will be coerced into an
236: * integer, but otherwise ignored by this resolver.
237: * @return If the <code>propertyResolved</code> property of <code>ELContext</code> was set to <code>true</code>, then
238: * <code>true</code> if calling the <code>setValue</code> method will always fail or <code>false</code> if it is
239: * possible that such a call may succeed; otherwise undefined.
240: * @throws PropertyNotFoundException if the given index is out of bounds for this array.
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:
248: if (context == null) {
249: throw new NullPointerException();
250: }
251:
252: if (base != null && base.getClass().isArray()) {
253: context.setPropertyResolved(true);
254: int index = toInteger(property);
255: if (index < 0 || index >= Array.getLength(base)) {
256: throw new PropertyNotFoundException();
257: }
258: }
259: return isReadOnly;
260: }
261:
262: /**
263: * If the base object is a Java language array, returns the most general type that this resolver accepts for the
264: * <code>property</code> argument. Otherwise, returns <code>null</code>.
265: *
266: * <p>
267: * Assuming the base is an array, this method will always return <code>Integer.class</code>. This is because arrays
268: * accept integers for their index.
269: * </p>
270: *
271: * @param context The context of this evaluation.
272: * @param base The array to analyze. Only bases that are a Java language array are handled by this resolver.
273: * @return <code>null</code> if base is not a Java language array; otherwise <code>Integer.class</code>.
274: */
275: @Override
276: public Class<?> getCommonPropertyType(ELContext context, Object base) {
277:
278: if (base != null && base.getClass().isArray()) {
279: return Integer.class;
280: }
281: return null;
282: }
283:
284: private int toInteger(Object p) {
285:
286: if (p instanceof Integer) {
287: return ((Integer) p).intValue();
288: }
289: if (p instanceof Character) {
290: return ((Character) p).charValue();
291: }
292: if (p instanceof Boolean) {
293: return ((Boolean) p).booleanValue() ? 1 : 0;
294: }
295: if (p instanceof Number) {
296: return ((Number) p).intValue();
297: }
298: if (p instanceof String) {
299: return Integer.parseInt((String) p);
300: }
301: throw new IllegalArgumentException();
302: }
303:
304: private boolean isReadOnly;
305: }