-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement alternative JavaBean support
Add support for multiple JavaBean specification implementations. If the class java.beans.BeanInfo is available, the full JavaBeans implementation is used (same behaviour as before this patch). If the full JavaBeans implementation is not available, a built-in, stand-alone implementation is used that only provides the JavaBeans functionality that does not depend on any of the java.beans.* classes (essentially getter/setter support). Note that the configuration system properties are intended for testing only. They are NOT part of the public API.
- Loading branch information
Showing
8 changed files
with
719 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Copyright (c) 2023 Contributors to the Eclipse Foundation | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v. 2.0, which is available at | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* This Source Code may also be made available under the following Secondary | ||
* Licenses when the conditions for such availability set forth in the | ||
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License, | ||
* version 2 with the GNU Classpath Exception, which is available at | ||
* https://www.gnu.org/software/classpath/license.html. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 | ||
*/ | ||
package jakarta.el; | ||
|
||
import jakarta.el.BeanELResolver.BeanProperties; | ||
|
||
/* | ||
* Provides an abstraction so the BeanELResolver can obtain JavaBeans specification support via different | ||
* implementations. | ||
*/ | ||
abstract class BeanSupport { | ||
|
||
private static final BeanSupport beanSupport; | ||
|
||
static { | ||
// Only intended for unit tests. Not intended to be part of public API. | ||
boolean doNotCacheInstance = Boolean.getBoolean("jakarta.el.BeanSupport.doNotCacheInstance"); | ||
if (doNotCacheInstance) { | ||
beanSupport = null; | ||
} else { | ||
beanSupport = createInstance(); | ||
} | ||
} | ||
|
||
private static BeanSupport createInstance() { | ||
// Only intended for unit tests. Not intended to be part of public API. | ||
boolean useFull = !Boolean.getBoolean("jakarta.el.BeanSupport.useStandalone"); | ||
|
||
if (useFull) { | ||
// If not explicitly configured to use standalone, use the full implementation unless it is not available. | ||
try { | ||
Class.forName("java.beans.BeanInfo"); | ||
} catch (Exception e) { | ||
// Ignore: Expected if using modules and java.desktop module is not present | ||
useFull = false; | ||
} | ||
} | ||
if (useFull) { | ||
// The full implementation provided by the java.beans package | ||
return new BeanSupportFull(); | ||
} else { | ||
// The cut-down local implementation that does not depend on the java.beans package | ||
return new BeanSupportStandalone(); | ||
} | ||
} | ||
|
||
static BeanSupport getInstance() { | ||
if (beanSupport == null) { | ||
return createInstance(); | ||
} | ||
return beanSupport; | ||
} | ||
|
||
abstract BeanProperties getBeanProperties(Class<?> type); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
* Copyright (c) 2023 Contributors to the Eclipse Foundation | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v. 2.0, which is available at | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* This Source Code may also be made available under the following Secondary | ||
* Licenses when the conditions for such availability set forth in the | ||
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License, | ||
* version 2 with the GNU Classpath Exception, which is available at | ||
* https://www.gnu.org/software/classpath/license.html. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 | ||
*/ | ||
package jakarta.el; | ||
|
||
import java.beans.BeanInfo; | ||
import java.beans.IntrospectionException; | ||
import java.beans.Introspector; | ||
import java.beans.PropertyDescriptor; | ||
import java.lang.reflect.Method; | ||
|
||
import jakarta.el.BeanELResolver.BeanProperties; | ||
import jakarta.el.BeanELResolver.BeanProperty; | ||
|
||
class BeanSupportFull extends BeanSupport { | ||
|
||
@Override | ||
BeanProperties getBeanProperties(Class<?> type) { | ||
return new BeanPropertiesFull(type); | ||
} | ||
|
||
static final class BeanPropertiesFull extends BeanProperties { | ||
|
||
BeanPropertiesFull(Class<?> baseClass) throws ELException { | ||
super(baseClass); | ||
try { | ||
BeanInfo info = Introspector.getBeanInfo(this.baseClass); | ||
PropertyDescriptor[] pds = info.getPropertyDescriptors(); | ||
for (PropertyDescriptor pd : pds) { | ||
this.propertyMap.put(pd.getName(), new BeanPropertyFull(baseClass, pd)); | ||
} | ||
/* | ||
* Populating from any interfaces causes default methods to be included. | ||
*/ | ||
populateFromInterfaces(baseClass); | ||
} catch (IntrospectionException ie) { | ||
throw new ELException(ie); | ||
} | ||
} | ||
|
||
private void populateFromInterfaces(Class<?> aClass) throws IntrospectionException { | ||
Class<?> interfaces[] = aClass.getInterfaces(); | ||
if (interfaces.length > 0) { | ||
for (Class<?> ifs : interfaces) { | ||
BeanInfo info = Introspector.getBeanInfo(ifs); | ||
PropertyDescriptor[] pds = info.getPropertyDescriptors(); | ||
for (PropertyDescriptor pd : pds) { | ||
if (!this.propertyMap.containsKey(pd.getName())) { | ||
this.propertyMap.put(pd.getName(), new BeanPropertyFull(this.baseClass, pd)); | ||
} | ||
} | ||
populateFromInterfaces(ifs); | ||
} | ||
} | ||
Class<?> superclass = aClass.getSuperclass(); | ||
if (superclass != null) { | ||
populateFromInterfaces(superclass); | ||
} | ||
} | ||
} | ||
|
||
static final class BeanPropertyFull extends BeanProperty { | ||
|
||
private final PropertyDescriptor descriptor; | ||
|
||
BeanPropertyFull(Class<?> owner, PropertyDescriptor descriptor) { | ||
super(owner, descriptor.getPropertyType()); | ||
this.descriptor = descriptor; | ||
} | ||
|
||
@Override | ||
Method getWriteMethod() { | ||
return descriptor.getWriteMethod(); | ||
} | ||
|
||
@Override | ||
Method getReadMethod() { | ||
return descriptor.getReadMethod(); | ||
} | ||
} | ||
} |
Oops, something went wrong.