From ecd652c6064b40908d3e51117d6587aba059f614 Mon Sep 17 00:00:00 2001 From: Bauke Scholtz Date: Sat, 27 Jul 2024 16:46:29 -0400 Subject: [PATCH] Fix #5460: regression after fixing #5417: the composite component attribute default value wasn't anymore available when explicitly calling getAttributes().get(name) rather than relying on statehelper -- this has been fixed --- .../applicationimpl/InstanceFactory.java | 19 ------------- .../faces/component/UIComponentBase.java | 28 ++++++++++++++++++- 2 files changed, 27 insertions(+), 20 deletions(-) diff --git a/impl/src/main/java/com/sun/faces/application/applicationimpl/InstanceFactory.java b/impl/src/main/java/com/sun/faces/application/applicationimpl/InstanceFactory.java index fc19300977..ef1256c981 100644 --- a/impl/src/main/java/com/sun/faces/application/applicationimpl/InstanceFactory.java +++ b/impl/src/main/java/com/sun/faces/application/applicationimpl/InstanceFactory.java @@ -828,25 +828,6 @@ private void pushDeclaredDefaultValuesToAttributesMap(FacesContext context, Bean } } - /** - * Helper method to convert a value to a type as defined in PropertyDescriptor(s) - * - * @param name - * @param value - * @param propertyDescriptors - * @return value - */ - private Object convertValueToTypeIfNecessary(String name, Object value, PropertyDescriptor[] propertyDescriptors, ExpressionFactory expressionFactory) { - for (PropertyDescriptor propertyDescriptor : propertyDescriptors) { - if (propertyDescriptor.getName().equals(name)) { - value = expressionFactory.coerceToType(value, propertyDescriptor.getPropertyType()); - break; - } - } - - return value; - } - /** *

* To enable EL Coercion to use Faces Custom converters, this method will call diff --git a/impl/src/main/java/jakarta/faces/component/UIComponentBase.java b/impl/src/main/java/jakarta/faces/component/UIComponentBase.java index 69a1ed7835..6f51537e46 100644 --- a/impl/src/main/java/jakarta/faces/component/UIComponentBase.java +++ b/impl/src/main/java/jakarta/faces/component/UIComponentBase.java @@ -31,6 +31,7 @@ import static java.util.Collections.unmodifiableList; import static java.util.logging.Level.FINE; +import java.beans.BeanInfo; import java.beans.IntrospectionException; import java.beans.PropertyDescriptor; import java.io.IOException; @@ -1978,10 +1979,35 @@ public Object get(Object keyObj) { } } } - + if (result == null && attributes != null && isCompositeComponent(component)) { + result = getCompositeComponentAttributeDefaultValue(key, attributes); + } return result; } + private Object getCompositeComponentAttributeDefaultValue(String name, Map attributes) { + var metadata = (BeanInfo) attributes.get(UIComponent.BEANINFO_KEY); + + if (metadata != null) { + for (var propertyDescriptor : metadata.getPropertyDescriptors()) { + if (propertyDescriptor.getName().equals(name)) { + var defaultValue = propertyDescriptor.getValue("default"); + + if (defaultValue != null) { + if (defaultValue instanceof ValueExpression) { + defaultValue = ((ValueExpression) defaultValue).getValue(component.getFacesContext().getELContext()); + } + + var expressionFactory = component.getFacesContext().getApplication().getExpressionFactory(); + return expressionFactory.coerceToType(defaultValue, propertyDescriptor.getPropertyType()); + } + } + } + } + + return null; + } + @Override public Object put(String keyValue, Object value) { if (keyValue == null) {