public abstract class BeanStyle extends Object
BeanStyle defines general rules about the signatures of getter and
setter methods of a bean.
Everyone knows one occurrence of a bean style, it is the style defined in the
JavaBeans Spec. That one roughly says:
Assuming you have a bean property with name xyz and type T
the related getter and setter will be
T getXyz()void setXyz(T t)BeanStyle CLASSIC.BeanStyle.
You can either choose one of the predefined bean styles or implement your own
style. The built in styles can be found as constants in this class, for
example MODERN or CLASSIC_WITH_OPTIONAL.
To create a custom style you need to create a new subclass of
BeanStyle which has five abstract methods to overwrite. To better
understand how to implement a BeanStyle this paragraph describes how
a BeanStyle is used to examine an IBean interface and how it is even
used during lifecycle of the bean.
IBeanMetaInfoParser is the class where a BeanStyle is used
most. It uses the BeanStyle to examine a new given interface
IBeanMetaInfoParser does that in following steps:
isGetterMethod(Method) and
isSetterMethod(Method).convertGetterNameToFieldName(String) and
convertSetterNameToFieldName(String).determineFieldTypeFromGetterAndSetter(Class, Method, Method).BeanStyle and that are called once for each bean interface
to collect the meta data.
Some bean styles also influence the runtime behavior of a bean.
ModernBeanStyle for example has a return type for setters other than
void and supports getters that return type Optional instead
of the field type. In such cases you also need to implement a handler that
helps the bean factory with the runtime behavior of the bean style. See
BeanStyleHandler for more information about such handlers.
| Modifier and Type | Field and Description |
|---|---|
static BeanStyle |
CLASSIC
Predefined bean style following the commonly known Java beans
specification.
|
static BeanStyle |
CLASSIC_WITH_OPTIONAL
Predefined bean style with a different naming of the setters and getters
and with
Optional support in getters. |
static BeanStyle |
MODERN
Predefined bean style mostly following the commonly known Java beans
specification with the exception of an
Optional support in
getters. |
| Constructor and Description |
|---|
BeanStyle() |
| Modifier and Type | Method and Description |
|---|---|
protected static void |
assertForBeanType(Class<?> beanType,
boolean condition,
String message)
Throws an
InvalidIBeanTypeException if a given condition is not
met. |
abstract String |
convertGetterNameToFieldName(String getterName)
Derives the name of a bean field from the name of its corresponding
getter method.
|
abstract String |
convertSetterNameToFieldName(String setterName)
Derives the name of a bean field from the name of its corresponding
setter method.
|
abstract Class<?> |
determineFieldTypeFromGetterAndSetter(Class<?> beanType,
Method getterMethod,
Method setterMethod)
Determines the type of a bean field from given corresponding getter and
setter method.
|
boolean |
equals(Object obj)
As stateless we treat all instances of one
BeanStyle sub class as
equal. |
int |
hashCode() |
abstract boolean |
isGetterMethod(Method method)
Determines if a given method is a potential getter method for this bean
type.
|
protected static boolean |
isNoParameterInMethod(Method method)
Helper method checking if a given method has no arguments.
|
protected static boolean |
isOneParameterInMethod(Method method)
Helper method checking if a given method has exactly one argument.
|
abstract boolean |
isSetterMethod(Method method)
Determines if a given method is a potential setter method for this bean
type.
|
public static final BeanStyle CLASSIC
ClassicBeanStylepublic static final BeanStyle MODERN
Optional support in
getters.ClassicBeanStyleWithOptionalSupportpublic static final BeanStyle CLASSIC_WITH_OPTIONAL
Optional support in getters.ModernBeanStyleprotected static boolean isOneParameterInMethod(Method method)
method - the method to checktrue if method has one paramprotected static boolean isNoParameterInMethod(Method method)
method - the method to checktrue if method does not have any paramsprotected static void assertForBeanType(Class<?> beanType, boolean condition, String message) throws InvalidIBeanTypeException
InvalidIBeanTypeException if a given condition is not
met.beanType - the related IBean class that will be passed to the thrown
Exception in case exception is throwncondition - if this parameter evaluates to false the
exception will be thrownmessage - the message to be passed to the exception in case exception is
thrownInvalidIBeanTypeException - if the given condition is not metpublic abstract boolean isGetterMethod(Method method)
See ClassicBeanStyle.isGetterMethod(Method) for a concrete
example.
method - the Method to testtrue if the method matches the requirements for a
getterpublic abstract boolean isSetterMethod(Method method)
See ClassicBeanStyle.isSetterMethod(Method) for a concrete
example.
method - the Method to testtrue if the method matches the requirements for a
setterpublic abstract String convertGetterNameToFieldName(String getterName) throws IllegalArgumentException
isGetterMethod(Method).getterName - the name of a method that has been identified as a potential
getterIllegalArgumentException - if a conversion is not possiblepublic abstract String convertSetterNameToFieldName(String setterName) throws IllegalArgumentException
isSetterMethod(Method).setterName - the name of a method that has been identified as a potential
setterIllegalArgumentException - if a conversion is not possiblepublic abstract Class<?> determineFieldTypeFromGetterAndSetter(Class<?> beanType, Method getterMethod, Method setterMethod) throws InvalidIBeanTypeException
beanType - the examined bean classgetterMethod - a method of the beanType that is proven to be a
potential gettersetterMethod - a method of the beanType that is proven to be a
potential setterInvalidIBeanTypeException - if either the type cannot be determined for any reason or if
the concluded types of setter and getter do not matchpublic boolean equals(Object obj)
BeanStyle sub class as
equal.equals in class ObjectObject.equals(java.lang.Object)