Class 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)
    This is also the default bean style used in IBean and can be found in predefined BeanStyle CLASSIC.
    ibean also allows other styles where getters and setters have different naming convention or other signatures. These styles are defined in subclasses of 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

    • to determine if the provided interface is a valid IBean interface and
    • to parse all fields with corresponding getters and setters from this interface.
    The IBeanMetaInfoParser does that in following steps: The methods mentioned in the previous steps are the abstract methods that define a 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.

    • 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 an Optional 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 with Optional support in getters.
        See Also:
        ModernBeanStyle
    • Constructor Detail

      • BeanStyle

        public BeanStyle()
    • 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 an InvalidIBeanTypeException 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 thrown
        condition - if this parameter evaluates to false the exception will be thrown
        message - 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 - the Method 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 - the Method 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 via isGetterMethod(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 via isSetterMethod(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 class
        getterMethod - a method of the beanType that is proven to be a potential getter
        setterMethod - a method of the beanType 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
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object