Package com.github.javaxcel.util
Class FieldUtils
java.lang.Object
com.github.javaxcel.util.FieldUtils
public final class FieldUtils extends Object
Utilities for reflection.
-
Method Summary
Modifier and Type Method Description static String
convertIfFaulty(String maybeFaulty, String defaultValue, Field field)
If the value is null or empty string, converts a value to default value.static <T> Object
getFieldValue(T model, Field field)
Returns value of the field.static List<Field>
getInheritedFields(Class<?> type)
Gets fields of the type including its inherited fields.static List<Field>
getTargetedFields(Class<?> type)
Gets the targeted fields depending on the policy.static <T> T
instantiate(Class<T> type)
Returns instance of type.static <T> void
setFieldValue(T model, Field field, Object value)
Sets up value into the field.static List<String>
toHeaderNames(List<Field> fields)
Converts fields to header names.static <T> Map<String,Object>
toMap(T model, List<Field> fields)
Converts fields to a map.
-
Method Details
-
getTargetedFields
Gets the targeted fields depending on the policy.If the targeting policy depended on
ExcelModel.includeSuper()
istrue
, this returns its own declared fields. Otherwise, this returns its own declared fields including super classes' fields.This doesn't return the fields annotated with
ExcelIgnore
.If the model class'
ExcelModel.explicit()
istrue
, this excludes the fields not annotated withExcelColumn
.- Parameters:
type
- type of the object- Returns:
- targeted fields
- See Also:
ExcelModel.includeSuper()
,ExcelModel.explicit()
,ExcelIgnore
-
getInheritedFields
Gets fields of the type including its inherited fields.- Parameters:
type
- type of the object- Returns:
- inherited and own fields
- See Also:
Class.getDeclaredFields()
,Class.getSuperclass()
-
toHeaderNames
Converts fields to header names.This checks whether the field is annotated with
ExcelColumn
or not. IfExcelColumn.name()
is not null and not empty, this returns a header name defined in the field. Otherwise returns name of the field.- Parameters:
fields
- targeted fields- Returns:
- list of
ExcelColumn.name()
orField.getName()
-
toMap
Converts fields to a map.- Type Parameters:
T
- type of the object- Parameters:
model
- model in listfields
- targeted fields- Returns:
Map
in which key is the model's field name and value is the model's field value- See Also:
Field.getName()
,getFieldValue(Object, Field)
-
getFieldValue
Returns value of the field.- Type Parameters:
T
- type of the object- Parameters:
model
- model in listfield
- targeted field- Returns:
- field value
-
setFieldValue
Sets up value into the field.- Type Parameters:
T
- type of the object- Parameters:
model
- model in listfield
- targeted fieldvalue
- value to be set into field
-
convertIfFaulty
@Nullable public static String convertIfFaulty(String maybeFaulty, String defaultValue, Field field)If the value is null or empty string, converts a value to default value.- Parameters:
maybeFaulty
- value that may be null or emptydefaultValue
- 1.AbstractExcelWriter.defaultValue(String)
2.ExcelColumn.defaultValue()
3.ExcelModel.defaultValue()
field
- field of model- Returns:
- origin value or default value
-
instantiate
Returns instance of type.this can instantiate the type that has constructor without parameter.
- Type Parameters:
T
- type of instance- Parameters:
type
- class- Returns:
- instance of type
- Throws:
NoTargetedConstructorException
- if the type doesn't have default constructor
-