Class ModelUtils


  • public final class ModelUtils
    extends java.lang.Object
    This class contains some utility methods for getting/setting field values and check for methods/field. These methods are based on reflection.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static boolean enumEquals​(HybrisEnumValue enum1, java.lang.Object obj)
      Compares enum object with another object.
      static boolean existsField​(java.lang.Class model, java.lang.String field)
      Returns true if in the given class model a field by the given fieldname exists.
      static boolean existsMethod​(java.lang.Class model, java.lang.String method)
      Returns true if in the given class model a method by the given method name exists.
      static java.lang.Class getFieldType​(java.lang.Class model, java.lang.String field)
      Returns a Class object that identifies the declared type for the given field name of the given model class.
      static java.lang.Object getFieldValue​(java.lang.Object model, java.lang.String field)
      Returns the value of the field (given by the field name) from the given model.
      static boolean isPrimitiveField​(java.lang.Class model, java.lang.String field)
      Returns (per reflection) true if in the class the field by it's given field name is a primitive type.
      static boolean setFieldValue​(java.lang.Object model, java.lang.String field, java.lang.Object value)
      Sets in the given model the given value into the field by the given field name per reflection and returns true if this was successful.
      • Methods inherited from class java.lang.Object

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

      • getFieldValue

        public static java.lang.Object getFieldValue​(java.lang.Object model,
                                                     java.lang.String field)
        Returns the value of the field (given by the field name) from the given model. per reflection. Searches all superclasses of the model.
        Parameters:
        model - the model
        field - the field name
        Returns:
        null if not such field at the model exists, otherwise the value object is returned
        Throws:
        java.lang.IllegalStateException - if an unexpected reflection exception occurs.
      • setFieldValue

        public static boolean setFieldValue​(java.lang.Object model,
                                            java.lang.String field,
                                            java.lang.Object value)
        Sets in the given model the given value into the field by the given field name per reflection and returns true if this was successful.
        Parameters:
        model - the model where the value should be set
        field - the field name at the model
        value - the value to be set
        Returns:
        false if no such fiel at the model was found
      • isPrimitiveField

        public static boolean isPrimitiveField​(java.lang.Class model,
                                               java.lang.String field)
        Returns (per reflection) true if in the class the field by it's given field name is a primitive type.
        Parameters:
        model - the class model
        field - the field name
        Returns:
        false if the field is not primitive or the field doesn't exist in the class
      • getFieldType

        public static java.lang.Class getFieldType​(java.lang.Class model,
                                                   java.lang.String field)
        Returns a Class object that identifies the declared type for the given field name of the given model class.
        Parameters:
        model - the model class
        field - the field name
        Returns:
        null if no such field was found, otherwise the type class is returned
      • existsMethod

        public static boolean existsMethod​(java.lang.Class model,
                                           java.lang.String method)
        Returns true if in the given class model a method by the given method name exists. The method can have any signature.
        Parameters:
        model - the class model
        method - the method name
        Returns:
        false if no such method exists in the given class model
      • existsField

        public static boolean existsField​(java.lang.Class model,
                                          java.lang.String field)
        Returns true if in the given class model a field by the given fieldname exists. Searches all superclasses of the given class model.
        Parameters:
        model - the class model
        field - the fieldname
        Returns:
        false if no such field exists in the given class model
      • enumEquals

        public static boolean enumEquals​(HybrisEnumValue enum1,
                                         java.lang.Object obj)
        Compares enum object with another object. Used inside generated dynamic enum values.
        Parameters:
        enum1 - the first enum
        obj - the second enum
        Returns:
        true if both objects are dynamic enum values and have the same type and code; please note that type and code are case insensitive!