Class BeanStyle
- java.lang.Object
-
- org.coliper.ibean.BeanStyle
-
- Direct Known Subclasses:
ClassicBeanStyle,ModernBeanStyle
public abstract class BeanStyle extends Object
BeanStyledefines 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)
BeanStyleCLASSIC.
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
MODERNorCLASSIC_WITH_OPTIONAL.To create a custom style you need to create a new subclass of
BeanStylewhich has five abstract methods to overwrite. To better understand how to implement aBeanStylethis paragraph describes how aBeanStyleis used to examine an IBean interface and how it is even used during lifecycle of the bean.
IBeanMetaInfoParseris the class where aBeanStyleis used most. It uses theBeanStyleto 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.
IBeanMetaInfoParserdoes 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).
BeanStyleand that are called once for each bean interface to collect the meta data.Some bean styles also influence the runtime behavior of a bean.
ModernBeanStylefor example has a return type for setters other thanvoidand supports getters that return typeOptionalinstead 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. SeeBeanStyleHandlerfor more information about such handlers.
-
-
Field Summary
Fields Modifier and Type Field Description static BeanStyleCLASSICPredefined bean style following the commonly known Java beans specification.static BeanStyleCLASSIC_WITH_OPTIONALPredefined bean style with a different naming of the setters and getters and withOptionalsupport in getters.static BeanStyleMODERNPredefined bean style mostly following the commonly known Java beans specification with the exception of anOptionalsupport 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 voidassertForBeanType(Class<?> beanType, boolean condition, String message)Throws anInvalidIBeanTypeExceptionif a given condition is not met.abstract StringconvertGetterNameToFieldName(String getterName)Derives the name of a bean field from the name of its corresponding getter method.abstract StringconvertSetterNameToFieldName(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.booleanequals(Object obj)As stateless we treat all instances of oneBeanStylesub class as equal.inthashCode()abstract booleanisGetterMethod(Method method)Determines if a given method is a potential getter method for this bean type.protected static booleanisNoParameterInMethod(Method method)Helper method checking if a given method has no arguments.protected static booleanisOneParameterInMethod(Method method)Helper method checking if a given method has exactly one argument.abstract booleanisSetterMethod(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 anOptionalsupport 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 withOptionalsupport 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:
trueif 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:
trueif method does not have any params
-
assertForBeanType
protected static void assertForBeanType(Class<?> beanType, boolean condition, String message) throws InvalidIBeanTypeException
Throws anInvalidIBeanTypeExceptionif 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 tofalsethe 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- theMethodto test- Returns:
trueif 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- theMethodto test- Returns:
trueif 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 thebeanTypethat is proven to be a potential gettersetterMethod- a method of thebeanTypethat 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 oneBeanStylesub class as equal.- Overrides:
equalsin classObject- See Also:
Object.equals(java.lang.Object)
-
-