Skip to contentMethod: getBeanProperties(Class)
1: /*
2: * Copyright (c) 2023 Contributors to the Eclipse Foundation
3: *
4: * This program and the accompanying materials are made available under the
5: * terms of the Eclipse Public License v. 2.0, which is available at
6: * http://www.eclipse.org/legal/epl-2.0.
7: *
8: * This Source Code may also be made available under the following Secondary
9: * Licenses when the conditions for such availability set forth in the
10: * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11: * version 2 with the GNU Classpath Exception, which is available at
12: * https://www.gnu.org/software/classpath/license.html.
13: *
14: * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15: */
16: package jakarta.el;
17:
18: import java.beans.BeanInfo;
19: import java.beans.IntrospectionException;
20: import java.beans.Introspector;
21: import java.beans.PropertyDescriptor;
22: import java.lang.reflect.Method;
23:
24: import jakarta.el.BeanELResolver.BeanProperties;
25: import jakarta.el.BeanELResolver.BeanProperty;
26:
27: class BeanSupportFull extends BeanSupport {
28:
29: @Override
30: BeanProperties getBeanProperties(Class<?> type) {
31: return new BeanPropertiesFull(type);
32: }
33:
34: static final class BeanPropertiesFull extends BeanProperties {
35:
36: BeanPropertiesFull(Class<?> baseClass) throws ELException {
37: super(baseClass);
38: try {
39: BeanInfo info = Introspector.getBeanInfo(this.baseClass);
40: PropertyDescriptor[] pds = info.getPropertyDescriptors();
41: for (PropertyDescriptor pd : pds) {
42: this.propertyMap.put(pd.getName(), new BeanPropertyFull(baseClass, pd));
43: }
44: /*
45: * Populating from any interfaces causes default methods to be included.
46: */
47: populateFromInterfaces(baseClass);
48: } catch (IntrospectionException ie) {
49: throw new ELException(ie);
50: }
51: }
52:
53: private void populateFromInterfaces(Class<?> aClass) throws IntrospectionException {
54: Class<?> interfaces[] = aClass.getInterfaces();
55: if (interfaces.length > 0) {
56: for (Class<?> ifs : interfaces) {
57: BeanInfo info = Introspector.getBeanInfo(ifs);
58: PropertyDescriptor[] pds = info.getPropertyDescriptors();
59: for (PropertyDescriptor pd : pds) {
60: if (!this.propertyMap.containsKey(pd.getName())) {
61: this.propertyMap.put(pd.getName(), new BeanPropertyFull(this.baseClass, pd));
62: }
63: }
64: populateFromInterfaces(ifs);
65: }
66: }
67: Class<?> superclass = aClass.getSuperclass();
68: if (superclass != null) {
69: populateFromInterfaces(superclass);
70: }
71: }
72: }
73:
74: static final class BeanPropertyFull extends BeanProperty {
75:
76: private final PropertyDescriptor descriptor;
77:
78: BeanPropertyFull(Class<?> owner, PropertyDescriptor descriptor) {
79: super(owner, descriptor.getPropertyType());
80: this.descriptor = descriptor;
81: }
82:
83: @Override
84: Method getWriteMethod() {
85: return descriptor.getWriteMethod();
86: }
87:
88: @Override
89: Method getReadMethod() {
90: return descriptor.getReadMethod();
91: }
92: }
93: }