Skip to contentMethod: put(Class, BeanELResolver.BeanProperties)
1: /*
2: * Copyright (c) 1997, 2023 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 jakarta.el.ELUtil.getExceptionMessageString;
22:
23: import java.lang.ref.ReferenceQueue;
24: import java.lang.ref.SoftReference;
25: import java.lang.reflect.InvocationTargetException;
26: import java.lang.reflect.Method;
27: import java.util.HashMap;
28: import java.util.Map;
29: import java.util.concurrent.ConcurrentHashMap;
30:
31: /**
32: * Defines property resolution behavior on objects using the JavaBeans component architecture.
33: *
34: * <p>
35: * If the {@code java.beans.*} package is available (part of the {@code java.desktop} module) the JavaBeans
36: * implementation provided by the JRE is used. If the {@code java.beans.*} package is not available, a built-in
37: * stand-alone implementation is used that just provides getter/setter support (as everything else requires classes from
38: * {@code java.beans.*}).
39: *
40: * <p>
41: * This resolver handles base objects of any type, as long as the base is not <code>null</code>. It accepts any object
42: * as a property or method, and coerces it to a string.
43: *
44: * <p>
45: * For property resolution, the property string is used to find a JavaBeans compliant property on the base object. The
46: * value is accessed using JavaBeans getters and setters.
47: * </p>
48: *
49: * <p>
50: * For method resolution, the method string is the name of the method in the bean. The parameter types can be optionally
51: * specified to identify the method. If the parameter types are not specified, the parameter objects are used in the
52: * method resolution.
53: * </p>
54: *
55: * <p>
56: * The JavaBeans specification predates the introduction of default method implementations defined on an interface. In
57: * addition to the JavaBeans specification requirements for looking up property getters, property setters and methods,
58: * this resolver also considers default methods and includes them in the results.
59: * </p>
60: *
61: * <p>
62: * The JavaBeans specification predates the introduction of Modules. In addition to the JavaBeans specification
63: * requirements for looking up property getters, property setters and methods, this resolver also considers module
64: * visibility.
65: * </p>
66: *
67: * <p>
68: * This resolver can be constructed in read-only mode, which means that {@link #isReadOnly} will always return
69: * <code>true</code> and {@link #setValue} will always throw <code>PropertyNotWritableException</code>.
70: * </p>
71: *
72: * <p>
73: * <code>ELResolver</code>s are combined together using {@link CompositeELResolver}s, to define rich semantics for
74: * evaluating an expression. See the javadocs for {@link ELResolver} for details.
75: * </p>
76: *
77: * <p>
78: * Because this resolver handles base objects of any type, it should be placed near the end of a composite resolver.
79: * Otherwise, it will claim to have resolved a property before any resolvers that come after it get a chance to test if
80: * they can do so as well.
81: * </p>
82: *
83: * @see CompositeELResolver
84: * @see ELResolver
85: *
86: * @since Jakarta Server Pages 2.1
87: */
88: public class BeanELResolver extends ELResolver {
89:
90: static private class BPSoftReference extends SoftReference<BeanProperties> {
91: final Class<?> key;
92:
93: BPSoftReference(Class<?> key, BeanProperties beanProperties, ReferenceQueue<BeanProperties> refQ) {
94: super(beanProperties, refQ);
95: this.key = key;
96: }
97: }
98:
99: static private class SoftConcurrentHashMap extends ConcurrentHashMap<Class<?>, BeanProperties> {
100:
101: private static final long serialVersionUID = -178867497897782229L;
102: private static final int CACHE_INIT_SIZE = 1024;
103: private ConcurrentHashMap<Class<?>, BPSoftReference> map = new ConcurrentHashMap<>(CACHE_INIT_SIZE);
104: private ReferenceQueue<BeanProperties> refQ = new ReferenceQueue<>();
105:
106: // Remove map entries that have been placed on the queue by GC.
107: private void cleanup() {
108: BPSoftReference BPRef = null;
109: while ((BPRef = (BPSoftReference) refQ.poll()) != null) {
110: map.remove(BPRef.key);
111: }
112: }
113:
114: @Override
115: public BeanProperties put(Class<?> key, BeanProperties value) {
116: cleanup();
117: BPSoftReference prev = map.put(key, new BPSoftReference(key, value, refQ));
118:• return prev == null ? null : prev.get();
119: }
120:
121: @Override
122: public BeanProperties putIfAbsent(Class<?> key, BeanProperties value) {
123: cleanup();
124: BPSoftReference prev = map.putIfAbsent(key, new BPSoftReference(key, value, refQ));
125: return prev == null ? null : prev.get();
126: }
127:
128: @Override
129: public BeanProperties get(Object key) {
130: cleanup();
131: BPSoftReference BPRef = map.get(key);
132: if (BPRef == null) {
133: return null;
134: }
135: if (BPRef.get() == null) {
136: // value has been garbage collected, remove entry in map
137: map.remove(key);
138: return null;
139: }
140: return BPRef.get();
141: }
142: }
143:
144: private boolean isReadOnly;
145:
146: private final SoftConcurrentHashMap properties = new SoftConcurrentHashMap();
147:
148: /*
149: * Defines a property for a bean.
150: */
151: abstract static class BeanProperty {
152:
153: final private Class<?> baseClass;
154: final private Class<?> type;
155: private Method readMethod;
156: private Method writeMethod;
157:
158: public BeanProperty(Class<?> baseClass, Class<?> type) {
159: this.baseClass = baseClass;
160: this.type = type;
161: }
162:
163: public Class<?> getPropertyType() {
164: return this.type;
165: }
166:
167: public boolean isReadOnly(Object base) {
168: return getWriteMethod(base) == null;
169: }
170:
171: public Method getReadMethod(Object base) {
172: if (readMethod == null) {
173: readMethod = ELUtil.getMethod(baseClass, base, getReadMethod());
174: }
175: return readMethod;
176: }
177:
178: public Method getWriteMethod(Object base) {
179: if (writeMethod == null) {
180: writeMethod = ELUtil.getMethod(baseClass, base, getWriteMethod());
181: }
182: return writeMethod;
183: }
184:
185: abstract Method getWriteMethod();
186:
187: abstract Method getReadMethod();
188: }
189:
190: /*
191: * Defines the properties for a bean.
192: */
193: abstract static class BeanProperties {
194:
195: protected final Map<String, BeanProperty> propertyMap = new HashMap<>();
196: protected final Class<?> baseClass;
197:
198: BeanProperties(Class<?> baseClass) {
199: this.baseClass = baseClass;
200: }
201:
202: public BeanProperty getBeanProperty(String property) {
203: return propertyMap.get(property);
204: }
205: }
206:
207: /**
208: * Creates a new read/write <code>BeanELResolver</code>.
209: */
210: public BeanELResolver() {
211: this.isReadOnly = false;
212: }
213:
214: /**
215: * Creates a new <code>BeanELResolver</code> whose read-only status is determined by the given parameter.
216: *
217: * @param isReadOnly <code>true</code> if this resolver cannot modify beans; <code>false</code> otherwise.
218: */
219: public BeanELResolver(boolean isReadOnly) {
220: this.isReadOnly = isReadOnly;
221: }
222:
223: /**
224: * If the base object is not <code>null</code>, returns the most general acceptable type that can be set on this bean
225: * property.
226: *
227: * <p>
228: * If the base is not <code>null</code>, the <code>propertyResolved</code> property of the <code>ELContext</code> object
229: * must be set to <code>true</code> by this resolver, before returning. If this property is not <code>true</code> after
230: * this method is called, the caller should ignore the return value.
231: * </p>
232: *
233: * <p>
234: * The provided property will first be coerced to a <code>String</code>. If there is a <code>BeanInfoProperty</code>
235: * for this property, there were no errors retrieving it and neither the property nor the resolver are read-only,
236: * the <code>propertyType</code> of the <code>propertyDescriptor</code> is returned. If the property is resolved but
237: * either the property or the resolver is read-only then {@code null} will be returned. Otherwise, a
238: * <code>PropertyNotFoundException</code> is thrown.
239: * </p>
240: *
241: * @param context The context of this evaluation.
242: * @param base The bean to analyze.
243: * @param property The name of the property to analyze. Will be coerced to a <code>String</code>.
244: * @return If the <code>propertyResolved</code> property of <code>ELContext</code> was set to <code>true</code>, then
245: * the most general acceptable type which must be {@code null} if the either the property or the resolver is
246: * read-only; otherwise undefined
247: * @throws NullPointerException if context is <code>null</code>
248: * @throws PropertyNotFoundException if <code>base</code> is not <code>null</code> and the specified property does not
249: * exist or is not readable.
250: * @throws ELException if an exception was thrown while performing the property or variable resolution. The thrown
251: * exception must be included as the cause property of this exception, if available.
252: */
253: @Override
254: public Class<?> getType(ELContext context, Object base, Object property) {
255: if (context == null) {
256: throw new NullPointerException();
257: }
258:
259: if (base == null || property == null) {
260: return null;
261: }
262:
263: BeanProperty beanProperty = getBeanProperty(context, base, property);
264: context.setPropertyResolved(true);
265:
266: if (isReadOnly || beanProperty.isReadOnly(base)) {
267: return null;
268: }
269:
270: return beanProperty.getPropertyType();
271: }
272:
273: /**
274: * If the base object is not <code>null</code>, returns the current value of the given property on this bean.
275: *
276: * <p>
277: * If the base is not <code>null</code>, the <code>propertyResolved</code> property of the <code>ELContext</code> object
278: * must be set to <code>true</code> by this resolver, before returning. If this property is not <code>true</code> after
279: * this method is called, the caller should ignore the return value.
280: * </p>
281: *
282: * <p>
283: * The provided property name will first be coerced to a <code>String</code>. If the property is a readable property of
284: * the base object, as per the JavaBeans specification, then return the result of the getter call. If the getter throws
285: * an exception, it is propagated to the caller. If the property is not found or is not readable, a
286: * <code>PropertyNotFoundException</code> is thrown.
287: * </p>
288: *
289: * @param context The context of this evaluation.
290: * @param base The bean on which to get the property.
291: * @param property The name of the property to get. Will be coerced to a <code>String</code>.
292: * @return If the <code>propertyResolved</code> property of <code>ELContext</code> was set to <code>true</code>, then
293: * the value of the given property. Otherwise, undefined.
294: * @throws NullPointerException if context is <code>null</code>.
295: * @throws PropertyNotFoundException if <code>base</code> is not <code>null</code> and the specified property does not
296: * exist or is not readable.
297: * @throws ELException if an exception was thrown while performing the property or variable resolution. The thrown
298: * exception must be included as the cause property of this exception, if available.
299: */
300: @Override
301: public Object getValue(ELContext context, Object base, Object property) {
302: if (context == null) {
303: throw new NullPointerException();
304: }
305:
306: if (base == null || property == null) {
307: return null;
308: }
309:
310: Method method = getBeanProperty(context, base, property).getReadMethod(base);
311: if (method == null) {
312: throw new PropertyNotFoundException(
313: getExceptionMessageString(context, "propertyNotReadable", new Object[] { base.getClass().getName(), property.toString() }));
314: }
315:
316: Object value;
317: try {
318: value = method.invoke(base, new Object[0]);
319: context.setPropertyResolved(base, property);
320: } catch (ELException ex) {
321: throw ex;
322: } catch (InvocationTargetException ite) {
323: throw new ELException(ite.getCause());
324: } catch (Exception ex) {
325: throw new ELException(ex);
326: }
327:
328: return value;
329: }
330:
331: /**
332: * If the base object is not <code>null</code>, attempts to set the value of the given property on this bean.
333: *
334: * <p>
335: * If the base is not <code>null</code>, the <code>propertyResolved</code> property of the <code>ELContext</code> object
336: * must be set to <code>true</code> by this resolver, before returning. If this property is not <code>true</code> after
337: * this method is called, the caller can safely assume no value was set.
338: * </p>
339: *
340: * <p>
341: * If this resolver was constructed in read-only mode, this method will always throw
342: * <code>PropertyNotWritableException</code>.
343: * </p>
344: *
345: * <p>
346: * The provided property name will first be coerced to a <code>String</code>. If property is a writable property of
347: * <code>base</code> (as per the JavaBeans Specification), the setter method is called (passing <code>value</code>). If
348: * the property exists but does not have a setter, then a <code>PropertyNotFoundException</code> is thrown. If the
349: * property does not exist, a <code>PropertyNotFoundException</code> is thrown.
350: * </p>
351: *
352: * @param context The context of this evaluation.
353: * @param base The bean on which to set the property.
354: * @param property The name of the property to set. Will be coerced to a <code>String</code>.
355: * @param val The value to be associated with the specified key.
356: * @throws NullPointerException if context is <code>null</code>.
357: * @throws PropertyNotFoundException if <code>base</code> is not <code>null</code> and the specified property does not
358: * exist.
359: * @throws PropertyNotWritableException if this resolver was constructed in read-only mode, or if there is no setter for
360: * the property.
361: * @throws ELException if an exception was thrown while performing the property or variable resolution. The thrown
362: * exception must be included as the cause property of this exception, if available.
363: */
364: @Override
365: public void setValue(ELContext context, Object base, Object property, Object val) {
366: if (context == null) {
367: throw new NullPointerException();
368: }
369:
370: if (base == null || property == null) {
371: return;
372: }
373:
374: if (isReadOnly) {
375: throw new PropertyNotWritableException(getExceptionMessageString(context, "resolverNotwritable", new Object[] { base.getClass().getName() }));
376: }
377:
378: Method method = getBeanProperty(context, base, property).getWriteMethod(base);
379: if (method == null) {
380: throw new PropertyNotWritableException(
381: getExceptionMessageString(context, "propertyNotWritable", new Object[] { base.getClass().getName(), property.toString() }));
382: }
383:
384: try {
385: method.invoke(base, new Object[] { val });
386: context.setPropertyResolved(base, property);
387: } catch (ELException ex) {
388: throw ex;
389: } catch (InvocationTargetException ite) {
390: throw new ELException(ite.getCause());
391: } catch (Exception ex) {
392: if (null == val) {
393: val = "null";
394: }
395: String message = getExceptionMessageString(context, "setPropertyFailed", new Object[] { property.toString(), base.getClass().getName(), val });
396: throw new ELException(message, ex);
397: }
398: }
399:
400: /**
401: * If the base object is not <code>null</code>, invoke the method, with the given parameters on this bean. The return
402: * value from the method is returned.
403: *
404: * <p>
405: * If the base is not <code>null</code>, the <code>propertyResolved</code> property of the <code>ELContext</code> object
406: * must be set to <code>true</code> by this resolver, before returning. If this property is not <code>true</code> after
407: * this method is called, the caller should ignore the return value.
408: * </p>
409: *
410: * <p>
411: * The provided method object will first be coerced to a <code>String</code>. The methods in the bean is then examined
412: * and an attempt will be made to select one for invocation. If no suitable can be found, a
413: * <code>MethodNotFoundException</code> is thrown.
414: *
415: * If the given paramTypes is not <code>null</code>, select the method with the given name and parameter types.
416: *
417: * Else select the method with the given name that has the same number of parameters. If there are more than one such
418: * method, the method selection process is undefined.
419: *
420: * Else select the method with the given name that takes a variable number of arguments.
421: *
422: * Note the resolution for overloaded methods will likely be clarified in a future version of the spec.
423: *
424: * The provide parameters are coerced to the corresponding parameter types of the method, and the method is then
425: * invoked.
426: *
427: * @param context The context of this evaluation.
428: * @param base The bean on which to invoke the method
429: * @param methodName The simple name of the method to invoke. Will be coerced to a <code>String</code>. If method is
430: * "<init>"or "<clinit>" a MethodNotFoundException is thrown.
431: * @param paramTypes An array of Class objects identifying the method's formal parameter types, in declared order. Use
432: * an empty array if the method has no parameters. Can be <code>null</code>, in which case the method's formal parameter
433: * types are assumed to be unknown.
434: * @param params The parameters to pass to the method, or <code>null</code> if no parameters.
435: * @return The result of the method invocation (<code>null</code> if the method has a <code>void</code> return type).
436: * @throws MethodNotFoundException if no suitable method can be found.
437: * @throws ELException if an exception was thrown while performing (base, method) resolution. The thrown exception must
438: * be included as the cause property of this exception, if available. If the exception thrown is an
439: * <code>InvocationTargetException</code>, extract its <code>cause</code> and pass it to the <code>ELException</code>
440: * constructor.
441: * @since Jakarta Expression Language 2.2
442: */
443: @Override
444: public Object invoke(ELContext context, Object base, Object methodName, Class<?>[] paramTypes, Object[] params) {
445: if (base == null || methodName == null) {
446: return null;
447: }
448:
449: Method method = ELUtil.findMethod(base.getClass(), base, methodName.toString(), paramTypes, params, false);
450:
451: for (Object param : params) {
452: // If the parameters is a LambdaExpression, set the ELContext
453: // for its evaluation
454: if (param instanceof LambdaExpression) {
455: ((LambdaExpression) param).setELContext(context);
456: }
457: }
458:
459: Object ret = ELUtil.invokeMethod(context, method, base, params);
460: context.setPropertyResolved(base, methodName);
461: return ret;
462: }
463:
464: /**
465: * If the base object is not <code>null</code>, returns whether a call to {@link #setValue} will always fail.
466: *
467: * <p>
468: * If the base is not <code>null</code>, the <code>propertyResolved</code> property of the <code>ELContext</code> object
469: * must be set to <code>true</code> by this resolver, before returning. If this property is not <code>true</code> after
470: * this method is called, the caller can safely assume no value was set.
471: * </p>
472: *
473: * <p>
474: * If this resolver was constructed in read-only mode, this method will always return <code>true</code>.
475: * </p>
476: *
477: * <p>
478: * The provided property name will first be coerced to a <code>String</code>. If property is a writable property of
479: * <code>base</code>, <code>false</code> is returned. If the property is found but is not writable, <code>true</code> is
480: * returned. If the property is not found, a <code>PropertyNotFoundException</code> is thrown.
481: * </p>
482: *
483: * @param context The context of this evaluation.
484: * @param base The bean to analyze.
485: * @param property The name of the property to analyzed. Will be coerced to a <code>String</code>.
486: * @return If the <code>propertyResolved</code> property of <code>ELContext</code> was set to <code>true</code>, then
487: * <code>true</code> if calling the <code>setValue</code> method will always fail or <code>false</code> if it is
488: * possible that such a call may succeed; otherwise undefined.
489: * @throws NullPointerException if context is <code>null</code>
490: * @throws PropertyNotFoundException if <code>base</code> is not <code>null</code> and the specified property does not
491: * exist.
492: * @throws ELException if an exception was thrown while performing the property or variable resolution. The thrown
493: * exception must be included as the cause property of this exception, if available.
494: */
495: @Override
496: public boolean isReadOnly(ELContext context, Object base, Object property) {
497: if (context == null) {
498: throw new NullPointerException();
499: }
500:
501: if (base == null || property == null) {
502: return false;
503: }
504:
505: context.setPropertyResolved(true);
506: if (isReadOnly) {
507: return true;
508: }
509:
510: return getBeanProperty(context, base, property).isReadOnly(base);
511: }
512:
513: /**
514: * If the base object is not <code>null</code>, returns the most general type that this resolver accepts for the
515: * <code>property</code> argument. Otherwise, returns <code>null</code>.
516: *
517: * <p>
518: * Assuming the base is not <code>null</code>, this method will always return <code>Object.class</code>. This is because
519: * any object is accepted as a key and is coerced into a string.
520: * </p>
521: *
522: * @param context The context of this evaluation.
523: * @param base The bean to analyze.
524: * @return <code>null</code> if base is <code>null</code> otherwise <code>Object.class</code>.
525: */
526: @Override
527: public Class<?> getCommonPropertyType(ELContext context, Object base) {
528: if (base == null) {
529: return null;
530: }
531:
532: return Object.class;
533: }
534:
535: private BeanProperty getBeanProperty(ELContext context, Object base, Object prop) {
536: String property = prop.toString();
537: Class<?> baseClass = base.getClass();
538:
539: BeanProperties beanProperties = properties.get(baseClass);
540: if (beanProperties == null) {
541: beanProperties = BeanSupport.getInstance().getBeanProperties(baseClass);
542: properties.put(baseClass, beanProperties);
543: }
544:
545: BeanProperty beanProperty = beanProperties.getBeanProperty(property);
546: if (beanProperty == null) {
547: throw new PropertyNotFoundException(getExceptionMessageString(context, "propertyNotFound", new Object[] { baseClass.getName(), property }));
548: }
549:
550: return beanProperty;
551: }
552: }