Package io.microsphere.reflect
Class FieldUtils
- java.lang.Object
-
- io.microsphere.reflect.FieldUtils
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidassertFieldMatchType(java.lang.Object instance, java.lang.String fieldName, java.lang.Class<?> expectedType)Assert Field type matchstatic java.util.Set<java.lang.reflect.Field>findAllDeclaredFields(java.lang.Class<?> declaredClass, java.util.function.Predicate<? super java.lang.reflect.Field>... fieldFilters)static java.util.Set<java.lang.reflect.Field>findAllFields(java.lang.Class<?> declaredClass, java.util.function.Predicate<? super java.lang.reflect.Field>... fieldFilters)static java.lang.reflect.FieldfindField(java.lang.Class<?> klass, java.lang.String fieldName)Find the declaredFieldby its' namestatic java.lang.reflect.FieldfindField(java.lang.Class<?> klass, java.lang.String fieldName, java.lang.Class<?> fieldType)Find the declaredFieldby its' namestatic java.lang.reflect.FieldfindField(java.lang.Class<?> klass, java.lang.String fieldName, java.util.function.Predicate<? super java.lang.reflect.Field>... predicates)Find the declaredFieldby its' namestatic java.lang.reflect.FieldfindField(java.lang.Object object, java.lang.String fieldName)Find the specified objects' declaredFieldby its' namestatic java.lang.reflect.FieldgetDeclaredField(java.lang.Class<?> declaredClass, java.lang.String fieldName)Like theClass.getDeclaredField(String)method without throwing anyExceptionstatic <V> VgetFieldValue(java.lang.Object instance, java.lang.reflect.Field field)Get the value of the specifiedFieldstatic <V> VgetFieldValue(java.lang.Object instance, java.lang.String fieldName)Get the value of the specifiedFieldstatic <V> VgetFieldValue(java.lang.Object instance, java.lang.String fieldName, java.lang.Class<V> fieldType)GetFieldValuestatic <V> VgetFieldValue(java.lang.Object instance, java.lang.String fieldName, V defaultValue)GetFieldValuestatic <T> TgetStaticFieldValue(java.lang.Class<?> klass, java.lang.String fieldName)Get the staticFieldValuestatic <T> TgetStaticFieldValue(java.lang.reflect.Field field)Get the staticFieldValuestatic <V> VsetFieldValue(java.lang.Object instance, java.lang.reflect.Field field, V value)Set the value for the specifiedFieldstatic <V> VsetFieldValue(java.lang.Object instance, java.lang.String fieldName, V value)Set the value for the specifiedFieldstatic <V> VsetStaticFieldValue(java.lang.Class<?> klass, java.lang.String fieldName, V fieldValue)Set the value to staticField
-
-
-
Method Detail
-
findField
public static java.lang.reflect.Field findField(java.lang.Object object, java.lang.String fieldName)Find the specified objects' declaredFieldby its' name- Parameters:
object- theobjectto findfieldName- the name offield- Returns:
- null if not found
-
findField
public static java.lang.reflect.Field findField(java.lang.Class<?> klass, java.lang.String fieldName)Find the declaredFieldby its' name- Parameters:
klass-classto findfieldName- the name offield- Returns:
- null if not found
-
findField
public static java.lang.reflect.Field findField(java.lang.Class<?> klass, java.lang.String fieldName, java.lang.Class<?> fieldType)Find the declaredFieldby its' name- Parameters:
klass-classto findfieldName- the name offieldfieldType- thetypeoffield- Returns:
- null if not found
-
findField
public static java.lang.reflect.Field findField(java.lang.Class<?> klass, java.lang.String fieldName, java.util.function.Predicate<? super java.lang.reflect.Field>... predicates)Find the declaredFieldby its' name- Parameters:
klass-classto findfieldName- the name offieldpredicates- zero or morePredicate- Returns:
- null if not found
-
getStaticFieldValue
public static <T> T getStaticFieldValue(java.lang.Class<?> klass, java.lang.String fieldName)Get the staticFieldValue- Parameters:
klass-classto findfieldName- the name offield- Returns:
nulliffieldisnullor get failed
-
getStaticFieldValue
public static <T> T getStaticFieldValue(java.lang.reflect.Field field)
Get the staticFieldValue- Parameters:
field-Field- Returns:
nulliffieldisnullor get failed
-
setStaticFieldValue
public static <V> V setStaticFieldValue(java.lang.Class<?> klass, java.lang.String fieldName, V fieldValue)Set the value to staticField- Parameters:
klass- the class declared the fieldfieldName- the name ofFieldfieldValue- the value ofField
-
findAllFields
public static java.util.Set<java.lang.reflect.Field> findAllFields(java.lang.Class<?> declaredClass, java.util.function.Predicate<? super java.lang.reflect.Field>... fieldFilters)
-
findAllDeclaredFields
public static java.util.Set<java.lang.reflect.Field> findAllDeclaredFields(java.lang.Class<?> declaredClass, java.util.function.Predicate<? super java.lang.reflect.Field>... fieldFilters)
-
getDeclaredField
public static java.lang.reflect.Field getDeclaredField(java.lang.Class<?> declaredClass, java.lang.String fieldName)Like theClass.getDeclaredField(String)method without throwing anyException- Parameters:
declaredClass- the declared classfieldName- the name ofField- Returns:
- if can't be found, return
null - Throws:
java.lang.RuntimeException- thrown if you can't find theField
-
getFieldValue
public static <V> V getFieldValue(java.lang.Object instance, java.lang.String fieldName) throws java.lang.IllegalStateException, java.lang.IllegalArgumentExceptionGet the value of the specifiedField- Parameters:
instance- the instance whose field should be modifiedfieldName- the name ofField- Returns:
- the value of the specified
Field - Throws:
java.lang.IllegalStateException- if this Field object is enforcing Java language access control and the underlying field is inaccessible.java.lang.IllegalArgumentException- if the specified object is not an instance of the class or interface declaring the underlying field (or a subclass or implementor thereof).
-
getFieldValue
public static <V> V getFieldValue(java.lang.Object instance, java.lang.String fieldName, V defaultValue) throws java.lang.IllegalStateException, java.lang.IllegalArgumentExceptionGetFieldValue- Type Parameters:
V- field type- Parameters:
instance- the instance whose field should be modifiedfieldName- field namedefaultValue- default value- Returns:
FieldValue- Throws:
java.lang.IllegalStateException- if this Field object is enforcing Java language access control and the underlying field is inaccessible.java.lang.IllegalArgumentException- if the specified object is not an instance of the class or interface declaring the underlying field (or a subclass or implementor thereof).
-
getFieldValue
public static <V> V getFieldValue(java.lang.Object instance, java.lang.String fieldName, java.lang.Class<V> fieldType) throws java.lang.IllegalStateException, java.lang.IllegalArgumentExceptionGetFieldValue- Type Parameters:
V- field type- Parameters:
instance- the instance whose field should be modifiedfieldName- field namefieldType- field type- Returns:
FieldValue- Throws:
java.lang.IllegalStateException- if this Field object is enforcing Java language access control and the underlying field is inaccessible.java.lang.IllegalArgumentException- if the specified object is not an instance of the class or interface declaring the underlying field (or a subclass or implementor thereof).
-
getFieldValue
public static <V> V getFieldValue(java.lang.Object instance, java.lang.reflect.Field field) throws java.lang.IllegalStateException, java.lang.IllegalArgumentExceptionGet the value of the specifiedField- Parameters:
instance- the instance whose field should be modifiedfield-Field- Returns:
- the value of the specified
Field - Throws:
java.lang.IllegalStateException- if this Field object is enforcing Java language access control and the underlying field is inaccessible.java.lang.IllegalArgumentException- if the specified object is not an instance of the class or interface declaring the underlying field (or a subclass or implementor thereof).
-
setFieldValue
public static <V> V setFieldValue(java.lang.Object instance, java.lang.String fieldName, V value) throws java.lang.IllegalStateException, java.lang.IllegalArgumentExceptionSet the value for the specifiedField- Parameters:
instance- the instance whose field should be modifiedfieldName- the name ofFieldvalue- the value of field to be set- Returns:
- the previous value of the specified
Field - Throws:
java.lang.IllegalStateException- if this Field object is enforcing Java language access control and the underlying field is inaccessible.java.lang.IllegalArgumentException- if the specified object is not an instance of the class or interface declaring the underlying field (or a subclass or implementor thereof).
-
setFieldValue
public static <V> V setFieldValue(java.lang.Object instance, java.lang.reflect.Field field, V value) throws java.lang.IllegalStateException, java.lang.IllegalArgumentExceptionSet the value for the specifiedField- Parameters:
instance- the instance whose field should be modifiedfield-Fieldvalue- the value of field to be set- Returns:
- the previous value of the specified
Field - Throws:
java.lang.IllegalStateException- if this Field object is enforcing Java language access control and the underlying field is inaccessible.java.lang.IllegalArgumentException- if the specified object is not an instance of the class or interface declaring the underlying field (or a subclass or implementor thereof).
-
assertFieldMatchType
public static void assertFieldMatchType(java.lang.Object instance, java.lang.String fieldName, java.lang.Class<?> expectedType) throws java.lang.IllegalArgumentExceptionAssert Field type match- Parameters:
instance- Object or classfieldName- field nameexpectedType- expected type- Throws:
java.lang.IllegalArgumentException- if type is not matched
-
-