public final class ReflectionExtensions extends Object
ReflectionExtensions provides utility methods for the java reflection API| Constructor and Description |
|---|
ReflectionExtensions() |
| Modifier and Type | Method and Description |
|---|---|
static <T> void |
copyFieldValue(T source,
T target,
@NonNull String fieldName)
Copies the field value of the given source object to the given target object.
|
static String |
firstCharacterToUpperCase(@NonNull String fieldName)
Sets the first character from the given string to upper case and returns it.
|
static String[] |
getAllDeclaredFieldNames(@NonNull Class<?> cls)
Gets all the declared field names including all fields from all super classes from the given
class object
|
static String[] |
getAllDeclaredFieldNames(@NonNull Class<?> cls,
List<String> ignoreFieldNames)
Gets all the declared field names including all fields from all super classes from the given
class object minus the given ignored field names
|
static String[] |
getAllDeclaredFieldNames(@NonNull Class<?> cls,
String... ignoreFieldNames)
Gets all the declared field names including all fields from all super classes from the given
class object minus the given optional array of ignored field names
|
static Field[] |
getAllDeclaredFields(@NonNull Class<?> cls)
Gets all the declared fields including all fields from all super classes from the given class
object
|
static Field |
getDeclaredField(@NonNull Class<?> cls,
@NonNull String fieldName)
Gets the
Field that match to the given field name that exists in the given class. |
static <T> Field |
getDeclaredField(T object,
@NonNull String fieldName)
Gets the
Field that match to the given field name that exists in the given object. |
static String[] |
getDeclaredFieldNames(@NonNull Class<?> cls)
Gets all the declared field names from the given class object.
|
static String[] |
getDeclaredFieldNames(@NonNull Class<?> cls,
List<String> ignoreFieldNames)
Gets all the declared field names from the given class object minus the given ignored field
names
Note: without the field names from any superclasses
|
static String[] |
getDeclaredFieldNames(@NonNull Class<?> cls,
String... ignoreFieldNames)
Gets all the declared field names from the given class object minus the given ignored field
names
Note: without the field names from any superclasses
|
static Field[] |
getDeclaredFields(@NonNull Class<?> cls,
List<String> ignoreFieldNames)
Gets the declared fields from the given class minus the given ignored field names
|
static Field[] |
getDeclaredFields(@NonNull Class<?> cls,
String... ignoreFieldNames)
Gets the declared fields from the given class minus the given optional array of ignored field
names
|
static List<String> |
getFieldNames(@NonNull Class<?> cls)
Gets all field names from the given class as an String list.
|
static List<String> |
getFieldNames(@NonNull Class<?> cls,
List<String> ignoreFieldNames)
Gets all field names from the given class as an String list minus the given ignored field
names
|
static List<String> |
getFieldNames(@NonNull Class<?> cls,
String... ignoreFieldNames)
Gets all field names from the given class as an String list minus the given optional array of
ignored field names
|
static <T> Object |
getFieldValue(T source,
@NonNull String fieldName)
Gets the field value of the given source object over the field name.
|
static String[] |
getMethodNames(@NonNull Class<?> cls)
Gets all method names from the given class as an String array.
|
static Map<String,String> |
getMethodNamesWithPrefixFromFieldNames(@NonNull List<String> fieldNames,
String prefix)
Generates a Map with the fieldName as key and the method as value.
|
static List<String> |
getModifiers(@NonNull Field field)
Gets the modifiers from the given Field as a list of String objects.
|
static boolean |
isNotSynthetic(@NonNull Field field)
Checks if the given
Field is not synthetic |
static <T> T |
newInstance(@NonNull Class<T> clazz)
Creates a new instance from the same type as the given Class.
|
static <T> T |
newInstance(T object)
Creates a new instance from the same type as the given object.
|
static <T> T |
newInstanceWithObjenesis(@NonNull Class<T> clazz)
Creates a new instance from the same type as the given
Class |
static <T> void |
setFieldValue(@NonNull Class<?> cls,
@NonNull String fieldName,
Object newValue)
Sets the field value of the given class object over the field name.
|
static <T> void |
setFieldValue(T source,
@NonNull String fieldName,
Object newValue)
Sets the field value of the given source object over the field name.
|
public static <T> void copyFieldValue(@NonNull
T source,
@NonNull
T target,
@NonNull
@NonNull String fieldName)
throws NoSuchFieldException,
SecurityException,
IllegalAccessException
T - the generic type of the objectsource - the sourcetarget - the targetfieldName - the field nameNoSuchFieldException - is thrown if no such field exists.SecurityException - is thrown if a security manager says no.IllegalAccessException - is thrown if an illegal on create an instance or access a method.public static <T> void setFieldValue(@NonNull
T source,
@NonNull
@NonNull String fieldName,
Object newValue)
throws NoSuchFieldException,
SecurityException,
IllegalAccessException
T - the generic typesource - the sourcefieldName - the field namenewValue - the new valueNoSuchFieldException - is thrown if no such field exists.SecurityException - is thrown if a security manager says no.IllegalAccessException - is thrown if an illegal on create an instance or access a method.public static <T> Object getFieldValue(@NonNull T source, @NonNull @NonNull String fieldName) throws NoSuchFieldException, SecurityException, IllegalAccessException
T - the generic typesource - the sourcefieldName - the field nameNoSuchFieldException - is thrown if no such field exists.SecurityException - is thrown if a security manager says no.IllegalAccessException - is thrown if an illegal on create an instance or access a method.public static <T> void setFieldValue(@NonNull
@NonNull Class<?> cls,
@NonNull
@NonNull String fieldName,
Object newValue)
throws NoSuchFieldException,
SecurityException,
IllegalAccessException
T - the generic typecls - The classfieldName - the field namenewValue - the new valueNoSuchFieldException - is thrown if no such field exists.SecurityException - is thrown if a security manager says no.IllegalAccessException - is thrown if an illegal on create an instance or access a method.public static List<String> getFieldNames(@NonNull @NonNull Class<?> cls)
cls - The class object to get the field names.public static List<String> getFieldNames(@NonNull @NonNull Class<?> cls, List<String> ignoreFieldNames)
cls - The class object to get the field namesignoreFieldNames - a list with field names that shell be ignoredpublic static List<String> getFieldNames(@NonNull @NonNull Class<?> cls, String... ignoreFieldNames)
cls - The class object to get the field namesignoreFieldNames - a optional array with the field names that shell be ignoredpublic static String[] getDeclaredFieldNames(@NonNull @NonNull Class<?> cls)
cls - the class objectpublic static String[] getDeclaredFieldNames(@NonNull @NonNull Class<?> cls, String... ignoreFieldNames)
cls - the class objectignoreFieldNames - a optional array with the field names that shell be ignoredpublic static String[] getDeclaredFieldNames(@NonNull @NonNull Class<?> cls, List<String> ignoreFieldNames)
cls - the class objectignoreFieldNames - a list with field names that shell be ignoredpublic static boolean isNotSynthetic(@NonNull
@NonNull Field field)
Field is not syntheticfield - the fieldField is not synthetic otherwise falsepublic static String[] getMethodNames(@NonNull @NonNull Class<?> cls)
cls - The class object to get the method names.public static Map<String,String> getMethodNamesWithPrefixFromFieldNames(@NonNull @NonNull List<String> fieldNames, String prefix)
fieldNames - A list with the field names.prefix - The prefix for the method name.public static String firstCharacterToUpperCase(@NonNull @NonNull String fieldName)
fieldName - The String to modify.public static List<String> getModifiers(@NonNull @NonNull Field field)
field - The field to get the modifiers.public static <T> T newInstance(@NonNull
T object)
throws InstantiationException,
IllegalAccessException,
ClassNotFoundException
T - the generic typeobject - the objectClassNotFoundException - is thrown if the class cannot be locatedIllegalAccessException - is thrown if the class or its default constructor is not accessible.InstantiationException - is thrown if this Class represents an abstract class, an interface, an
array class, a primitive type, or void; or if the class has no default
constructor; or if the instantiation fails for some other reason.public static <T> T newInstance(@NonNull
@NonNull Class<T> clazz)
throws InstantiationException,
IllegalAccessException
T - the generic typeclazz - the Class objectIllegalAccessException - is thrown if the class or its default constructor is not accessible.InstantiationException - is thrown if this Class represents an abstract class, an interface, an
array class, a primitive type, or void; or if the class has no default
constructor; or if the instantiation fails for some other reason.public static <T> T newInstanceWithObjenesis(@NonNull
@NonNull Class<T> clazz)
ClassT - the generic typeclazz - the Class objectpublic static <T> Field getDeclaredField(@NonNull T object, @NonNull @NonNull String fieldName) throws NoSuchFieldException, SecurityException
Field that match to the given field name that exists in the given object.T - the generic typeobject - the objectfieldName - the field nameNoSuchFieldException - is thrown if no such field exists.SecurityException - is thrown if a security manager says no.public static Field getDeclaredField(@NonNull @NonNull Class<?> cls, @NonNull @NonNull String fieldName) throws NoSuchFieldException, SecurityException
Field that match to the given field name that exists in the given class.cls - the class objectfieldName - the field nameNoSuchFieldException - is thrown if no such field exists.SecurityException - is thrown if a security manager says no.public static Field[] getAllDeclaredFields(@NonNull @NonNull Class<?> cls)
cls - the class objectpublic static String[] getAllDeclaredFieldNames(@NonNull @NonNull Class<?> cls)
cls - the class objectpublic static String[] getAllDeclaredFieldNames(@NonNull @NonNull Class<?> cls, String... ignoreFieldNames)
cls - the class objectignoreFieldNames - a optional array with the field names that shell be ignoredpublic static String[] getAllDeclaredFieldNames(@NonNull @NonNull Class<?> cls, List<String> ignoreFieldNames)
cls - the class objectignoreFieldNames - a list with field names that shell be ignoredpublic static Field[] getDeclaredFields(@NonNull @NonNull Class<?> cls, List<String> ignoreFieldNames) throws SecurityException
cls - the class objectignoreFieldNames - a list with field names that shell be ignoredField from the given class minus the given ignored field namesSecurityException - is thrown if a security manager says nopublic static Field[] getDeclaredFields(@NonNull @NonNull Class<?> cls, String... ignoreFieldNames) throws SecurityException
cls - the class objectignoreFieldNames - a list with field names that shell be ignoredField from the given class minus the given optional array of
ignored field namesSecurityException - is thrown if a security manager says noCopyright © 2015–2019 Alpha Ro Group UG (haftungsbeschrÀngt). All rights reserved.