Class PropertyUtils


  • public class PropertyUtils
    extends Object
    Common methods for working with the Property annotation.
    Since:
    2.5.0
    Author:
    oswaldo.bapvic.jr (Oswaldo Junior)
    • Method Detail

      • getPropertyKeyOrFieldName

        public static String getPropertyKeyOrFieldName​(Property property,
                                                       Field field)
        Returns either the property key specified in the @Property annotation or, if not present, the field name.
        Parameters:
        property - the Property annotation to be evaluated (null is allowed)
        field - the Field to be evaluated (not null)
        Returns:
        the field name, or the key specified in the @Property annotation, if present in the field.
        Throws:
        NullPointerException - if the field is null
      • getPropertyKey

        public static String getPropertyKey​(Property property)
        Safely returns the property key defined in the @Property annotation, or an empty string if the annotation is null.
        Parameters:
        property - the Property annotation to be evaluated (null is allowed)
        Returns:
        the property key defined in the annotation, or an empty string (not null)
      • findFieldByPropertyKeyOrName

        public static Field findFieldByPropertyKeyOrName​(Class<?> type,
                                                         String name)
        Find a field matching any of the following criteria in the specified type, or null if no match found:
        • the field is marked with the @Property annotation, and the annotation defines a custom name equal to the one specified in the parameter; or
        • the field name is equal to the specified name parameter
        Parameters:
        type - the class to reflect; not null
        name - the field name to obtain; not null
        Returns:
        the Field object; can be null
      • findFieldByPropertyKey

        public static Optional<Field> findFieldByPropertyKey​(Class<?> type,
                                                             String name)
        Find a field matching the following criteria in the specified type, or null if no match found:
        • the field is marked with the @Property annotation; and
        • the annotation defines a property key which is equal to the one specified in the parameter
        Parameters:
        type - the class to check; not null
        name - the field name to obtain; null
        Returns:
        an Optional possibly containing a Field object; can be empty
      • isFieldAnnotated

        public static boolean isFieldAnnotated​(Field field,
                                               String name)
        Returns true if the field is annotated with a Property annotation which key matches the specified name.
        Parameters:
        field - the field to be checked; not null
        name - the name to be tested
        Returns:
        true if the field is annotated with a Property annotation which key matches the specified name
      • parseValue

        public static Object parseValue​(String string,
                                        Field field,
                                        ObjectFactory objectFactory)
                                 throws ReflectiveOperationException,
                                        ParseException
        Parse the specified value into an object based on the field type.

        This method may apply custom conversion logic by invoking the TypeConverter class configured in the Property annotation (if not null), or standard conversion by using the TypeFactory by default.

        Parameters:
        string - the string to be parsed
        field - the target field (not null)
        objectFactory - the ObjectFactory to produce a new instance of the custom converter class, if specified in the annotation (not null)
        Returns:
        the object resulting from the parse operation
        Throws:
        ReflectiveOperationException - if an error occurs in the reflective operation
        ParseException - if an error is encountered while parsing
      • parseValue

        public static Object parseValue​(String string,
                                        Class<?> targetType,
                                        Property property,
                                        ObjectFactory objectFactory)
                                 throws ReflectiveOperationException,
                                        ParseException
        Parse the specified value into an object of the specified type.

        This method may apply custom conversion logic by invoking the TypeConverter class configured in the Property annotation (if not null), or standard conversion by using the TypeFactory by default.

        Parameters:
        string - the string to be parsed
        targetType - the target type (not null)
        property - the Property annotation to be evaluated (null is allowed)
        objectFactory - the ObjectFactory to produce a new instance of the custom converter class, if specified in the annotation (not null)
        Returns:
        the object resulting from the parse operation
        Throws:
        ReflectiveOperationException - if an error occurs in the reflective operation
        ParseException - if an error is encountered while parsing