Package net.obvj.confectory.util
Class PropertyUtils
- java.lang.Object
-
- net.obvj.confectory.util.PropertyUtils
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Optional<Field>findFieldByPropertyKey(Class<?> type, String name)Find a field matching the following criteria in the specified type, ornullif no match found: the field is marked with the@Propertyannotation; and the annotation defines a property key which is equal to the one specified in the parameterstatic FieldfindFieldByPropertyKeyOrName(Class<?> type, String name)Find a field matching any of the following criteria in the specified type, ornullif no match found: the field is marked with the@Propertyannotation, and the annotation defines a custom name equal to the one specified in the parameter; or the field name is equal to the specifiednameparameterstatic StringgetPropertyKey(Property property)Safely returns the property key defined in the@Propertyannotation, or an empty string if the annotation is null.static StringgetPropertyKeyOrFieldName(Property property, Field field)Returns either the property key specified in the@Propertyannotation or, if not present, the field name.static booleanisFieldAnnotated(Field field, String name)Returnstrueif the field is annotated with a Property annotation which key matches the specified name.static ObjectparseValue(String string, Class<?> targetType, Property property, ObjectFactory objectFactory)Parse the specified value into an object of the specified type.static ObjectparseValue(String string, Field field, ObjectFactory objectFactory)Parse the specified value into an object based on the field type.
-
-
-
Method Detail
-
getPropertyKeyOrFieldName
public static String getPropertyKeyOrFieldName(Property property, Field field)
Returns either the property key specified in the@Propertyannotation or, if not present, the field name.- Parameters:
property- thePropertyannotation to be evaluated (null is allowed)field- theFieldto be evaluated (not null)- Returns:
- the field name, or the key specified in the
@Propertyannotation, 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@Propertyannotation, or an empty string if the annotation is null.- Parameters:
property- thePropertyannotation 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, ornullif no match found:- the field is marked with the
@Propertyannotation, and the annotation defines a custom name equal to the one specified in the parameter; or - the field name is equal to the specified
nameparameter
- Parameters:
type- the class to reflect; not nullname- the field name to obtain; not null- Returns:
- the
Fieldobject; can be null
- the field is marked with the
-
findFieldByPropertyKey
public static Optional<Field> findFieldByPropertyKey(Class<?> type, String name)
Find a field matching the following criteria in the specified type, ornullif no match found:- the field is marked with the
@Propertyannotation; and - the annotation defines a property key which is equal to the one specified in the parameter
- Parameters:
type- the class to check; notnullname- the field name to obtain;null- Returns:
- an Optional possibly containing a
Fieldobject; can be empty
- the field is marked with the
-
isFieldAnnotated
public static boolean isFieldAnnotated(Field field, String name)
Returnstrueif the field is annotated with a Property annotation which key matches the specified name.- Parameters:
field- the field to be checked; not nullname- the name to be tested- Returns:
trueif 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
TypeConverterclass configured in thePropertyannotation (if not null), or standard conversion by using theTypeFactoryby default.- Parameters:
string- the string to be parsedfield- the target field (not null)objectFactory- theObjectFactoryto 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 operationParseException- 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
TypeConverterclass configured in thePropertyannotation (if not null), or standard conversion by using theTypeFactoryby default.- Parameters:
string- the string to be parsedtargetType- the target type (not null)property- thePropertyannotation to be evaluated (null is allowed)objectFactory- theObjectFactoryto 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 operationParseException- if an error is encountered while parsing
-
-