Package de.qytera.qtaf.core.reflection
Class FieldHelper
java.lang.Object
de.qytera.qtaf.core.reflection.FieldHelper
Class that provides helper methods for reflective field access and manipulation.
-
Method Summary
Modifier and TypeMethodDescriptionstatic booleanCheck if field can be accessed.getDeclaredFieldsRecursively(Class<?> clazz) Get declared fields of a class and its superclasses.static FieldgetFieldByName(String name, Class<?> clazz) Get a field reflection object of a given object by a class object.static ObjectgetFieldValue(Object object, Field field) Get value of an object field using the Reflection API.static ObjectgetFieldValue(Object object, String fieldName) Get field value of given object.static booleansetFieldValue(Object object, Field field, Object value) Set value of an object field using the Reflection API.static booleansetFieldValue(Object object, String fieldName, Object value) Set value of an object field using the Reflection API.
-
Method Details
-
getDeclaredFieldsRecursively
Get declared fields of a class and its superclasses.- Parameters:
clazz- class- Returns:
- fields
-
getFieldValue
Get field value of given object. This method can be used to access private fields of objects. If the field is static this method will return null.- Parameters:
object- ObjectfieldName- Field name- Returns:
- Field value on success, null otherwise
-
getFieldByName
Get a field reflection object of a given object by a class object.- Parameters:
name- field nameclazz- Object class- Returns:
- Field reflection object on success, null otherwise
-
canAccess
Check if field can be accessed.- Parameters:
field- Field reflection objectobject- Object that has the field- Returns:
- true if accessible, false otherwise
- Throws:
IllegalArgumentException- Thrown when trying to access static fields
-
getFieldValue
Get value of an object field using the Reflection API.- Parameters:
field- Field reflection objectobject- Object that has the field- Returns:
- Field value on success, null otherwise
-
setFieldValue
Set value of an object field using the Reflection API.- Parameters:
object- Object that has the fieldfield- Field reflection objectvalue- New value of the field- Returns:
- Field value on success, null otherwise
-
setFieldValue
Set value of an object field using the Reflection API.- Parameters:
object- Object that has the fieldfieldName- Field's namevalue- New value of the field- Returns:
- Field value on success, null otherwise
-