net.sourceforge.stripes.util.bean
Class BeanUtil

java.lang.Object
  extended by net.sourceforge.stripes.util.bean.BeanUtil

public class BeanUtil
extends Object

Provides a simple way to manipulate properties and nested properties specified by complex property expressions. It should be noted that while the interface to this class is simple it will be more efficient to use PropertyExpressionEvaluation directly if you plan to call more than one of the methods in this class with the same exact expression.

Since:
Stripes 1.4
Author:
Tim Fennell

Constructor Summary
BeanUtil()
           
 
Method Summary
static Class<?> getPropertyType(String expression, Object bean)
          Attempts to determine the type of the property specified by the property expression in the context of the supplied bean.
static Object getPropertyValue(String expression, Object bean)
          Attempts to fetch the property specified by the property expression in the context of the supplied bean.
static void setPropertyToNull(String expression, Object bean)
          Attempts to set the property to null or an equivalent value.
static void setPropertyValue(String expression, Object bean, Object value)
          Attempts to set the property specified by the property expression in the context of the supplied bean.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BeanUtil

public BeanUtil()
Method Detail

getPropertyType

public static Class<?> getPropertyType(String expression,
                                       Object bean)
                                throws ParseException,
                                       EvaluationException
Attempts to determine the type of the property specified by the property expression in the context of the supplied bean. Will return a Class if one is determinable or null if the type cannot be determined due to either there being no such property or a lack of type information.

Parameters:
expression - an expression representing a property or nested/indexed property
bean - the bean against which to evaluate the expression
Returns:
the Class representing the type of object that would be returned if getPropertyValue(String, Object) were invoked with the same parameters
Throws:
ParseException - if the expression is invalid and cannot be parsed
EvaluationException - if the expression is valid, but cannot be evaluated against this bean

getPropertyValue

public static Object getPropertyValue(String expression,
                                      Object bean)
                               throws ParseException,
                                      EvaluationException
Attempts to fetch the property specified by the property expression in the context of the supplied bean. If a value is present one will be returned. If the value or any intermediate value is null, then null will be returned.

Parameters:
expression - an expression representing a property or nested/indexed property
bean - the bean against which to evaluate the expression
Returns:
the value of the property or null
Throws:
ParseException - if the expression is invalid and cannot be parsed
EvaluationException - if the expression is valid, but cannot be evaluated against this bean

setPropertyValue

public static void setPropertyValue(String expression,
                                    Object bean,
                                    Object value)
                             throws ParseException,
                                    EvaluationException
Attempts to set the property specified by the property expression in the context of the supplied bean.

Parameters:
expression - an expression representing a property or nested/indexed property
bean - the bean against which to evaluate the expression
value - the value to be set for the property, may be null
Throws:
ParseException - if the expression is invalid and cannot be parsed
EvaluationException - if the expression is valid, but cannot be evaluated against this bean

setPropertyToNull

public static void setPropertyToNull(String expression,
                                     Object bean)
                              throws ParseException,
                                     EvaluationException
Attempts to set the property to null or an equivalent value. In most cases this leads to the property being set to null, but in the case of primitives the default value is used and in the case of Maps the key is removed from the Map.

Parameters:
expression - an expression representing a property or nested/indexed property
bean - the bean against which to evaluate the expression
Throws:
ParseException - if the expression is invalid and cannot be parsed
EvaluationException - if the expression is valid, but cannot be evaluated against this bean


? Copyright 2005-2006, Stripes Development Team.