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 void
assertFieldMatchType(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.Field
findField(java.lang.Class<?> klass, java.lang.String fieldName)
Find the declaredField
by its' namestatic java.lang.reflect.Field
findField(java.lang.Class<?> klass, java.lang.String fieldName, java.lang.Class<?> fieldType)
Find the declaredField
by its' namestatic 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 declaredField
by its' namestatic java.lang.reflect.Field
findField(java.lang.Object object, java.lang.String fieldName)
Find the specified objects' declaredField
by its' namestatic java.lang.reflect.Field
getDeclaredField(java.lang.Class<?> declaredClass, java.lang.String fieldName)
Like theClass.getDeclaredField(String)
method without throwing anyException
static <V> V
getFieldValue(java.lang.Object instance, java.lang.reflect.Field field)
Get the value of the specifiedField
static <V> V
getFieldValue(java.lang.Object instance, java.lang.String fieldName)
Get the value of the specifiedField
static <V> V
getFieldValue(java.lang.Object instance, java.lang.String fieldName, java.lang.Class<V> fieldType)
GetField
Valuestatic <V> V
getFieldValue(java.lang.Object instance, java.lang.String fieldName, V defaultValue)
GetField
Valuestatic <T> T
getStaticFieldValue(java.lang.Class<?> klass, java.lang.String fieldName)
Get the staticField
Valuestatic <T> T
getStaticFieldValue(java.lang.reflect.Field field)
Get the staticField
Valuestatic <V> V
setFieldValue(java.lang.Object instance, java.lang.reflect.Field field, V value)
Set the value for the specifiedField
static <V> V
setFieldValue(java.lang.Object instance, java.lang.String fieldName, V value)
Set the value for the specifiedField
static <V> V
setStaticFieldValue(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' declaredField
by its' name- Parameters:
object
- theobject
to 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 declaredField
by its' name- Parameters:
klass
-class
to 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 declaredField
by its' name- Parameters:
klass
-class
to findfieldName
- the name offield
fieldType
- thetype
offield
- 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 declaredField
by its' name- Parameters:
klass
-class
to findfieldName
- the name offield
predicates
- zero or morePredicate
- Returns:
- null if not found
-
getStaticFieldValue
public static <T> T getStaticFieldValue(java.lang.Class<?> klass, java.lang.String fieldName)
Get the staticField
Value- Parameters:
klass
-class
to findfieldName
- the name offield
- Returns:
null
iffield
isnull
or get failed
-
getStaticFieldValue
public static <T> T getStaticFieldValue(java.lang.reflect.Field field)
Get the staticField
Value- Parameters:
field
-Field
- Returns:
null
iffield
isnull
or 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 ofField
fieldValue
- 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.IllegalArgumentException
Get 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.IllegalArgumentException
GetField
Value- Type Parameters:
V
- field type- Parameters:
instance
- the instance whose field should be modifiedfieldName
- field namedefaultValue
- default value- Returns:
Field
Value- 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.IllegalArgumentException
GetField
Value- Type Parameters:
V
- field type- Parameters:
instance
- the instance whose field should be modifiedfieldName
- field namefieldType
- field type- Returns:
Field
Value- 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.IllegalArgumentException
Get 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.IllegalArgumentException
Set the value for the specifiedField
- Parameters:
instance
- the instance whose field should be modifiedfieldName
- the name ofField
value
- 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.IllegalArgumentException
Set the value for the specifiedField
- Parameters:
instance
- the instance whose field should be modifiedfield
-Field
value
- 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.IllegalArgumentException
Assert Field type match- Parameters:
instance
- Object or classfieldName
- field nameexpectedType
- expected type- Throws:
java.lang.IllegalArgumentException
- if type is not matched
-
-