Package: BeanNameELResolver
BeanNameELResolver
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
BeanNameELResolver(BeanNameResolver) |
|
|
|
|
|
||||||||||||||||||||
getCommonPropertyType(ELContext, Object) |
|
|
|
|
|
||||||||||||||||||||
getFeatureDescriptors(ELContext, Object) |
|
|
|
|
|
||||||||||||||||||||
getType(ELContext, Object, Object) |
|
|
|
|
|
||||||||||||||||||||
getValue(ELContext, Object, Object) |
|
|
|
|
|
||||||||||||||||||||
isReadOnly(ELContext, Object, Object) |
|
|
|
|
|
||||||||||||||||||||
setValue(ELContext, Object, Object, Object) |
|
|
|
|
|
Coverage
1: /*
2: * Copyright (c) 2012, 2021 Oracle and/or its affiliates and others.
3: * All rights reserved.
4: *
5: * This program and the accompanying materials are made available under the
6: * terms of the Eclipse Public License v. 2.0, which is available at
7: * http://www.eclipse.org/legal/epl-2.0.
8: *
9: * This Source Code may also be made available under the following Secondary
10: * Licenses when the conditions for such availability set forth in the
11: * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
12: * version 2 with the GNU Classpath Exception, which is available at
13: * https://www.gnu.org/software/classpath/license.html.
14: *
15: * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
16: */
17:
18: package jakarta.el;
19:
20: import java.beans.FeatureDescriptor;
21: import java.util.Iterator;
22:
23: /**
24: * <p>
25: * An <code>ELResolver</code> for resolving user or container managed beans.
26: * </p>
27: * <p>
28: * A {@link BeanNameResolver} is required for its proper operation. The following example creates an
29: * <code>ELResolver</code> that resolves the name "bean" to an instance of MyBean.
30: *
31: * <pre>
32: * <code>
33: * ELResovler elr = new BeanNameELResolver(new BeanNameResolver {
34: * public boolean isNameResolved(String beanName) {
35: * return "bean".equals(beanName);
36: * }
37: * public Object getBean(String beanName) {
38: * return "bean".equals(beanName)? new MyBean(): null;
39: * }
40: * });
41: * </code>
42: * </pre>
43: *
44: * @since Jakarta Expression Language 3.0
45: */
46: public class BeanNameELResolver extends ELResolver {
47:
48: private BeanNameResolver beanNameResolver;
49:
50: /**
51: * Constructor
52: *
53: * @param beanNameResolver The {@link BeanNameResolver} that resolves a bean name.
54: */
55: public BeanNameELResolver(BeanNameResolver beanNameResolver) {
56: this.beanNameResolver = beanNameResolver;
57: }
58:
59: /**
60: * If the base object is <code>null</code> and the property is a name that is resolvable by the BeanNameResolver,
61: * returns the value resolved by the BeanNameResolver.
62: *
63: * <p>
64: * If name is resolved by the BeanNameResolver, the <code>propertyResolved</code> property of the <code>ELContext</code>
65: * object must be set to <code>true</code> by this resolver, before returning. If this property is not <code>true</code>
66: * after this method is called, the caller should ignore the return value.
67: * </p>
68: *
69: * @param context The context of this evaluation.
70: * @param base <code>null</code>
71: * @param property The name of the bean.
72: * @return If the <code>propertyResolved</code> property of <code>ELContext</code> was set to <code>true</code>, then
73: * the value of the bean with the given name. Otherwise, undefined.
74: * @throws NullPointerException if context is <code>null</code>.
75: * @throws ELException if an exception was thrown while performing the property or variable resolution. The thrown
76: * exception must be included as the cause property of this exception, if available.
77: */
78: @Override
79: public Object getValue(ELContext context, Object base, Object property) {
80:• if (context == null) {
81: throw new NullPointerException();
82: }
83:
84:• if (base == null && property instanceof String) {
85:• if (beanNameResolver.isNameResolved((String) property)) {
86: context.setPropertyResolved(base, property);
87: return beanNameResolver.getBean((String) property);
88: }
89: }
90:
91: return null;
92: }
93:
94: /**
95: * If the base is null and the property is a name that is resolvable by the BeanNameResolver, the bean in the
96: * BeanNameResolver is set to the given value.
97: *
98: * <p>
99: * If the name is resolvable by the BeanNameResolver, or if the BeanNameResolver allows creating a new bean, the
100: * <code>propertyResolved</code> property of the <code>ELContext</code> object must be set to <code>true</code> by the
101: * resolver, before returning. If this property is not <code>true</code> after this method is called, the caller can
102: * safely assume no value has been set.
103: * </p>
104: *
105: * @param context The context of this evaluation.
106: * @param base <code>null</code>
107: * @param property The name of the bean
108: * @param value The value to set the bean with the given name to.
109: * @throws NullPointerException if context is <code>null</code>
110: * @throws PropertyNotWritableException if the BeanNameResolver does not allow the bean to be modified.
111: * @throws ELException if an exception was thrown while attempting to set the bean with the given name. The thrown
112: * exception must be included as the cause property of this exception, if available.
113: */
114: @Override
115: public void setValue(ELContext context, Object base, Object property, Object value) {
116:• if (context == null) {
117: throw new NullPointerException();
118: }
119:
120:• if (base == null && property instanceof String) {
121: String beanName = (String) property;
122:• if (beanNameResolver.isNameResolved(beanName) || beanNameResolver.canCreateBean(beanName)) {
123: beanNameResolver.setBeanValue(beanName, value);
124: context.setPropertyResolved(base, property);
125: }
126: }
127: }
128:
129: /**
130: * If the base is null and the property is a name resolvable by the BeanNameResolver, return the type of the bean.
131: *
132: * <p>
133: * If the name is resolvable by the BeanNameResolver, the <code>propertyResolved</code> property of the
134: * <code>ELContext</code> object must be set to <code>true</code> by the resolver, before returning. If this property is
135: * not <code>true</code> after this method is called, the caller can safely assume no value has been set.
136: * </p>
137: *
138: * @param context The context of this evaluation.
139: * @param base <code>null</code>
140: * @param property The name of the bean.
141: * @return If the <code>propertyResolved</code> property of <code>ELContext</code> was set to <code>true</code>, then
142: * the type of the bean with the given name. Otherwise, undefined.
143: * @throws NullPointerException if context is <code>null</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 Class<?> getType(ELContext context, Object base, Object property) {
149:• if (context == null) {
150: throw new NullPointerException();
151: }
152:
153:• if (base == null && property instanceof String) {
154:• if (beanNameResolver.isNameResolved((String) property)) {
155: context.setPropertyResolved(true);
156: return beanNameResolver.getBean((String) property).getClass();
157: }
158: }
159:
160: return null;
161: }
162:
163: /**
164: * If the base is null and the property is a name resolvable by the BeanNameResolver, attempts to determine if the bean
165: * is writable.
166: *
167: * <p>
168: * If the name is resolvable by the BeanNameResolver, the <code>propertyResolved</code> property of the
169: * <code>ELContext</code> object must be set to <code>true</code> by the resolver, before returning. If this property is
170: * not <code>true</code> after this method is called, the caller can safely assume no value has been set.
171: * </p>
172: *
173: * @param context The context of this evaluation.
174: * @param base <code>null</code>
175: * @param property The name of the bean.
176: * @return If the <code>propertyResolved</code> property of <code>ELContext</code> was set to <code>true</code>, then
177: * <code>true</code> if the property is read-only or <code>false</code> if not; otherwise undefined.
178: * @throws NullPointerException if context is <code>null</code>.
179: * @throws ELException if an exception was thrown while performing the property or variable resolution. The thrown
180: * exception must be included as the cause property of this exception, if available.
181: */
182: @Override
183: public boolean isReadOnly(ELContext context, Object base, Object property) {
184:• if (context == null) {
185: throw new NullPointerException();
186: }
187:
188:• if (base == null && property instanceof String) {
189:• if (beanNameResolver.isNameResolved((String) property)) {
190: context.setPropertyResolved(true);
191: return beanNameResolver.isReadOnly((String) property);
192: }
193: }
194:
195: return false;
196: }
197:
198: /**
199: * Always returns <code>null</code>, since there is no reason to iterate through a list of one element: bean name.
200: *
201: * @param context The context of this evaluation.
202: * @param base <code>null</code>.
203: * @return <code>null</code>.
204: *
205: * @deprecated This method will be removed without replacement in EL 6.0
206: */
207: @Deprecated(forRemoval = true, since = "5.0")
208: @Override
209: public Iterator<FeatureDescriptor> getFeatureDescriptors(ELContext context, Object base) {
210: return null;
211: }
212:
213: /**
214: * Always returns <code>String.class</code>, since a bean name is a String.
215: *
216: * @param context The context of this evaluation.
217: * @param base <code>null</code>.
218: * @return <code>String.class</code>.
219: */
220: @Override
221: public Class<?> getCommonPropertyType(ELContext context, Object base) {
222: return String.class;
223: }
224: }