Class BeanStyle
- java.lang.Object
-
- org.coliper.ibean.BeanStyle
-
- Direct Known Subclasses:
ClassicBeanStyle
,ModernBeanStyle
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- (getter)
T getXyz()
- (setter)
void setXyz(T t)
BeanStyle
CLASSIC
.
ibean also allows other styles where getters and setters have different naming convention or other signatures. These styles are defined in subclasses ofBeanStyle
.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
orCLASSIC_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 aBeanStyle
this paragraph describes how aBeanStyle
is used to examine an IBean interface and how it is even used during lifecycle of the bean.
IBeanMetaInfoParser
is the class where aBeanStyle
is used most. It uses theBeanStyle
to examine a new given interface- to determine if the provided interface is a valid IBean interface and
- to parse all fields with corresponding getters and setters from this interface.
IBeanMetaInfoParser
does that in following steps:- It iterates over all method of the interface and checks if they are
potential setters or getters by calling
isGetterMethod(Method)
andisSetterMethod(Method)
. - From all found getters and setters it retrieves their matching field name
via calling
convertGetterNameToFieldName(String)
andconvertSetterNameToFieldName(String)
. - Finally it combines all getters and setters that have the same field name
and determines the type of the field by calling
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 thanvoid
and supports getters that return typeOptional
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. SeeBeanStyleHandler
for more information about such handlers.
-
-
Field Summary
Fields Modifier and Type Field 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 withOptional
support in getters.static BeanStyle
MODERN
Predefined bean style mostly following the commonly known Java beans specification with the exception of anOptional
support in getters.
-
Constructor Summary
Constructors Constructor Description BeanStyle()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected static void
assertForBeanType(Class<?> beanType, boolean condition, String message)
Throws anInvalidIBeanTypeException
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 oneBeanStyle
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.
-
-
-
Field Detail
-
CLASSIC
public static final BeanStyle CLASSIC
Predefined bean style following the commonly known Java beans specification.- See Also:
ClassicBeanStyle
-
MODERN
public static final BeanStyle MODERN
Predefined bean style mostly following the commonly known Java beans specification with the exception of anOptional
support in getters.- See Also:
ClassicBeanStyleWithOptionalSupport
-
CLASSIC_WITH_OPTIONAL
public static final BeanStyle CLASSIC_WITH_OPTIONAL
Predefined bean style with a different naming of the setters and getters and withOptional
support in getters.- See Also:
ModernBeanStyle
-
-
Method Detail
-
isOneParameterInMethod
protected static boolean isOneParameterInMethod(Method method)
Helper method checking if a given method has exactly one argument.- Parameters:
method
- the method to check- Returns:
true
if method has one param
-
isNoParameterInMethod
protected static boolean isNoParameterInMethod(Method method)
Helper method checking if a given method has no arguments.- Parameters:
method
- the method to check- Returns:
true
if method does not have any params
-
assertForBeanType
protected static void assertForBeanType(Class<?> beanType, boolean condition, String message) throws InvalidIBeanTypeException
Throws anInvalidIBeanTypeException
if a given condition is not met.- Parameters:
beanType
- the related IBean class that will be passed to the thrown Exception in case exception is throwncondition
- if this parameter evaluates tofalse
the exception will be thrownmessage
- the message to be passed to the exception in case exception is thrown- Throws:
InvalidIBeanTypeException
- if the given condition is not met
-
isGetterMethod
public abstract boolean isGetterMethod(Method method)
Determines if a given method is a potential getter method for this bean type. Typically it checks if the method confirms to the required signature and naming convention of the style. This method should not do any checks about the type the method belongs to.See
ClassicBeanStyle.isGetterMethod(Method)
for a concrete example.- Parameters:
method
- theMethod
to test- Returns:
true
if the method matches the requirements for a getter
-
isSetterMethod
public abstract boolean isSetterMethod(Method method)
Determines if a given method is a potential setter method for this bean type. Typically it checks if the method confirms to the required signature and naming convention of the style. This method should not do any checks about the type the method belongs to.See
ClassicBeanStyle.isSetterMethod(Method)
for a concrete example.- Parameters:
method
- theMethod
to test- Returns:
true
if the method matches the requirements for a setter
-
convertGetterNameToFieldName
public abstract String convertGetterNameToFieldName(String getterName) throws IllegalArgumentException
Derives the name of a bean field from the name of its corresponding getter method. This method can assume that the given getter name has been checked for compliance viaisGetterMethod(Method)
.- Parameters:
getterName
- the name of a method that has been identified as a potential getter- Returns:
- the name of the bean field; names are case sensitive
- Throws:
IllegalArgumentException
- if a conversion is not possible
-
convertSetterNameToFieldName
public abstract String convertSetterNameToFieldName(String setterName) throws IllegalArgumentException
Derives the name of a bean field from the name of its corresponding setter method. This method can assume that the given setter name has been checked for compliance viaisSetterMethod(Method)
.- Parameters:
setterName
- the name of a method that has been identified as a potential setter- Returns:
- the name of the bean field; names are case sensitive
- Throws:
IllegalArgumentException
- if a conversion is not possible
-
determineFieldTypeFromGetterAndSetter
public abstract Class<?> determineFieldTypeFromGetterAndSetter(Class<?> beanType, Method getterMethod, Method setterMethod) throws InvalidIBeanTypeException
Determines the type of a bean field from given corresponding getter and setter method. Implementations of this method can assume that both given methods have been checked for getter and setter compliance and that both methods match in terms of method names. Implementations need to determine the type of the field and need to check if both getter and setter match to the type. That means that for example not only the given setter should be looked at to find out the field type.- Parameters:
beanType
- the examined bean classgetterMethod
- a method of thebeanType
that is proven to be a potential gettersetterMethod
- a method of thebeanType
that is proven to be a potential setter- Returns:
- the type of the bean field
- Throws:
InvalidIBeanTypeException
- if either the type cannot be determined for any reason or if the concluded types of setter and getter do not match
-
equals
public boolean equals(Object obj)
As stateless we treat all instances of oneBeanStyle
sub class as equal.- Overrides:
equals
in classObject
- See Also:
Object.equals(java.lang.Object)
-
-