Class BeanMorpher

  • All Implemented Interfaces:
    Morpher, ObjectMorpher

    public final class BeanMorpher
    extends java.lang.Object
    implements ObjectMorpher
    Converts a JavaBean into another JavaBean or DynaBean.
    This Morpher will try to match every property from the target JavaBean's class to the properties of the source JavaBean. If any target property differs in type from the source property, it will try to morph it. If a Morpher is not found for that type, the conversion will be aborted with a MorphException; this may be changed by setting the Morpher to be lenient, in that way it will ignore the property (the resulting value will be null).
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.lang.Object morph​(java.lang.Object sourceBean)
      Morphs the input object into an output object of the supported type.
      java.lang.Class morphsTo()
      Returns the target Class for conversion.
      private void setProperty​(java.lang.Object targetBean, java.lang.String name, java.lang.Class sourceType, java.lang.Class targetType, java.lang.Object value)  
      boolean supports​(java.lang.Class clazz)
      Returns true if the Morpher supports conversion from this Class.
      private void validateClass​(java.lang.Class clazz)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • log

        private static final org.apache.commons.logging.Log log
      • beanClass

        private final java.lang.Class beanClass
      • lenient

        private boolean lenient
    • Constructor Detail

      • BeanMorpher

        public BeanMorpher​(java.lang.Class beanClass,
                           MorpherRegistry morpherRegistry)
        Parameters:
        beanClass - the target class to morph to
        morpherRegistry - a registry of morphers
      • BeanMorpher

        public BeanMorpher​(java.lang.Class beanClass,
                           MorpherRegistry morpherRegistry,
                           boolean lenient)
        Parameters:
        beanClass - the target class to morph to
        morpherRegistry - a registry of morphers
        lenient - if an exception should be raised if no morpher is found for a target property
    • Method Detail

      • morph

        public java.lang.Object morph​(java.lang.Object sourceBean)
        Description copied from interface: ObjectMorpher
        Morphs the input object into an output object of the supported type.
        Specified by:
        morph in interface ObjectMorpher
        Parameters:
        sourceBean - The input value to be morphed
      • morphsTo

        public java.lang.Class morphsTo()
        Description copied from interface: Morpher
        Returns the target Class for conversion.
        Specified by:
        morphsTo in interface Morpher
        Returns:
        the target Class for conversion.
      • supports

        public boolean supports​(java.lang.Class clazz)
        Description copied from interface: Morpher
        Returns true if the Morpher supports conversion from this Class.
        Specified by:
        supports in interface Morpher
        Parameters:
        clazz - the source Class
        Returns:
        true if clazz is supported by this morpher, false otherwise.
      • setProperty

        private void setProperty​(java.lang.Object targetBean,
                                 java.lang.String name,
                                 java.lang.Class sourceType,
                                 java.lang.Class targetType,
                                 java.lang.Object value)
                          throws java.lang.IllegalAccessException,
                                 java.lang.reflect.InvocationTargetException,
                                 java.lang.NoSuchMethodException
        Throws:
        java.lang.IllegalAccessException
        java.lang.reflect.InvocationTargetException
        java.lang.NoSuchMethodException
      • validateClass

        private void validateClass​(java.lang.Class clazz)