Search in sources :

Example 41 with BeanProperty

use of cn.taketoday.beans.BeanProperty in project today-framework by TAKETODAY.

the class BeanPropertyExpressionResolver method getValue.

/**
 * If the base object is not <code>null</code>, returns the current value of the
 * given property on this bean.
 *
 * <p>
 * If the base is not <code>null</code>, the <code>propertyResolved</code>
 * property of the <code>ELContext</code> object must be set to
 * <code>true</code> by this resolver, before returning. If this property is not
 * <code>true</code> after this method is called, the caller should ignore the
 * return value.
 * </p>
 *
 * <p>
 * The provided property name will first be coerced to a <code>String</code>. If
 * the property is a readable property of the base object, as per the JavaBeans
 * specification, then return the result of the getter call. If the getter
 * throws an exception, it is propagated to the caller. If the property is not
 * found or is not readable, a <code>PropertyNotFoundException</code> is thrown.
 * </p>
 *
 * @param context The context of this evaluation.
 * @param base The bean on which to get the property.
 * @param property The name of the property to get. Will be coerced to a
 * <code>String</code>.
 * @return If the <code>propertyResolved</code> property of
 * <code>ELContext</code> was set to <code>true</code>, then the value
 * of the given property. Otherwise, undefined.
 * @throws NullPointerException if context is <code>null</code>.
 * @throws PropertyNotFoundException if <code>base</code> is not <code>null</code> and the specified
 * property does not exist or is not readable.
 * @throws ExpressionException if an exception was thrown while performing the property or
 * variable resolution. The thrown exception must be included as the
 * cause property of this exception, if available.
 */
@Override
public Object getValue(ExpressionContext context, Object base, Object property) {
    if (base == null || property == null) {
        return null;
    }
    BeanProperty beanProperty = getProperty(base, property);
    if (beanProperty != null) {
        try {
            Object value = beanProperty.getValue(base);
            context.setPropertyResolved(base, property);
            return value;
        } catch (Exception ex) {
            throw new ExpressionException("Can't get property: '" + property + "' from '" + base.getClass() + "'", ex);
        }
    }
    return null;
}
Also used : BeanProperty(cn.taketoday.beans.BeanProperty)

Example 42 with BeanProperty

use of cn.taketoday.beans.BeanProperty in project today-framework by TAKETODAY.

the class BeanPropertyExpressionResolver method setValue.

/**
 * If the base object is not <code>null</code>, attempts to set the value of the
 * given property on this bean.
 *
 * <p>
 * If the base is not <code>null</code>, the <code>propertyResolved</code>
 * property of the <code>ELContext</code> object must be set to
 * <code>true</code> by this resolver, before returning. If this property is not
 * <code>true</code> after this method is called, the caller can safely assume
 * no value was set.
 * </p>
 *
 * <p>
 * If this resolver was constructed in read-only mode, this method will always
 * throw <code>PropertyNotWritableException</code>.
 * </p>
 *
 * <p>
 * The provided property name will first be coerced to a <code>String</code>. If
 * property is a writable property of <code>base</code> (as per the JavaBeans
 * Specification), the setter method is called (passing <code>value</code>). If
 * the property exists but does not have a setter, then a
 * <code>PropertyNotFoundException</code> is thrown. If the property does not
 * exist, a <code>PropertyNotFoundException</code> is thrown.
 * </p>
 *
 * @param context The context of this evaluation.
 * @param base The bean on which to set the property.
 * @param property The name of the property to set. Will be coerced to a
 * <code>String</code>.
 * @param val The value to be associated with the specified key.
 * @throws NullPointerException if context is <code>null</code>.
 * @throws PropertyNotFoundException if <code>base</code> is not <code>null</code> and the specified
 * property does not exist.
 * @throws PropertyNotWritableException if this resolver was constructed in read-only mode, or if there
 * is no setter for the property.
 * @throws ExpressionException if an exception was thrown while performing the property or
 * variable resolution. The thrown exception must be included as the
 * cause property of this exception, if available.
 */
@Override
public void setValue(ExpressionContext context, Object base, Object property, Object val) {
    if (base == null || property == null) {
        return;
    }
    if (isReadOnly) {
        throw new PropertyNotWritableException("The ExpressionResolver for the class '" + base.getClass().getName() + "' is not writable.");
    }
    BeanProperty beanProperty = getProperty(base, property);
    if (beanProperty != null) {
        try {
            beanProperty.setValue(base, val);
            context.setPropertyResolved(base, property);
        } catch (Exception ex) {
            StringBuilder message = // 
            new StringBuilder("Can't set property '").append(// 
            property).append(// 
            "' on class '").append(// 
            base.getClass().getName()).append(// 
            "' to value '").append(// 
            val).append("'.");
            throw new ExpressionException(message.toString(), ex);
        }
    } else if (!ignoreUnknownProperty) {
        throw new PropertyNotFoundException("The class '" + base.getClass().getName() + "' does not have the property '" + property + "'.");
    }
}
Also used : BeanProperty(cn.taketoday.beans.BeanProperty)

Aggregations

BeanProperty (cn.taketoday.beans.BeanProperty)42 Test (org.junit.jupiter.api.Test)22 JdbcBeanMetadata (cn.taketoday.jdbc.result.JdbcBeanMetadata)8 BeanWrapperImpl (cn.taketoday.beans.BeanWrapperImpl)6 ArrayList (java.util.ArrayList)6 List (java.util.List)4 BeanMetadata (cn.taketoday.beans.BeanMetadata)2 NotWritablePropertyException (cn.taketoday.beans.NotWritablePropertyException)2 SimpleTypeConverter (cn.taketoday.beans.SimpleTypeConverter)2 TypeMismatchException (cn.taketoday.beans.TypeMismatchException)2 PropertyAccessor (cn.taketoday.core.reflect.PropertyAccessor)2 DataRetrievalFailureException (cn.taketoday.dao.DataRetrievalFailureException)2 InvalidDataAccessApiUsageException (cn.taketoday.dao.InvalidDataAccessApiUsageException)2 Method (java.lang.reflect.Method)2 ResultSet (java.sql.ResultSet)2 ResultSetMetaData (java.sql.ResultSetMetaData)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2