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 <T> ObjectgetFieldValue(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 ObjectinitialValueOf(Class<?> type)Gets initial value of the type.static <T> Tinstantiate(Class<T> type)Returns instance of type.static <T> voidsetFieldValue(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()
-
initialValueOf
Gets initial value of the type.- Parameters:
type- type of the object- Returns:
- initial value of the type
- See Also:
TypeClassifier.isNumericPrimitive(Class)
-
toHeaderNames
Converts fields to header names.This checks whether the field is annotated with
ExcelColumnor 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:
Mapin 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
-
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
-