public final class ReflectionUtils
extends java.lang.Object
Modifier and Type | Class and Description |
---|---|
static class |
ReflectionUtils.Wildcard
Simple placeholder class that can match on anything for parameter types.
|
Modifier and Type | Method and Description |
---|---|
static java.util.Map<java.lang.String,java.lang.Object> |
convertObjectToDeepMap(java.lang.Object object)
Converts the given object to a deep map.
|
static java.util.Map<java.lang.String,java.lang.Object> |
convertObjectToMap(java.lang.Object object)
Converts the given object to a shallow map.
|
static <T> T |
createObject(java.lang.Class<?> clazz,
java.lang.Object... params)
Creates an instance object of the given class and returns it
|
static <T> T |
executeMethod(java.lang.Object object,
java.lang.String methodName,
java.lang.Object... params)
Executes a method with the given name on the given object.
|
static <T> T |
executeStaticMethod(java.lang.Class<?> clazz,
java.lang.String methodName,
java.lang.Object... params)
Executes a method with the given name on the given object.
|
static void |
filterClassesByExcludedModifier(java.util.Collection<java.lang.Class<?>> classes,
boolean requireAll,
int... modifiers)
Filters out all reflected values that don't match the given modifiers
|
static void |
filterClassesByRequiredModifier(java.util.Collection<java.lang.Class<?>> classes,
boolean requireAll,
int... modifiers)
Filters out all reflected values that don't match the given modifiers
|
static <U extends java.lang.reflect.Member> |
filterMembersByExcludedModifier(java.util.Collection<U> members,
boolean requireAll,
int... modifiers)
Filters out all reflected values that don't match the given modifiers
|
static <U extends java.lang.reflect.Member> |
filterMembersByRequiredModifier(java.util.Collection<U> members,
boolean requireAll,
int... modifiers)
Filters out all reflected values that don't match the given modifiers
|
static java.util.Set<java.lang.reflect.Constructor> |
getAllConstructorsInClass(java.lang.Class<?> clazz,
int... excludedModifiers)
Returns all constructors in the class, using modifiers to exclude specific types
|
static java.util.Set<java.lang.reflect.Field> |
getAllFieldsInClass(java.lang.Class<?> clazz,
int... excludedModifiers)
Returns all fields in the class, using modifiers to exclude specific types
|
static java.util.Set<java.lang.Class<?>> |
getAllInnerClassesInClass(java.lang.Class<?> clazz,
boolean includeMultipleLevels,
int... excludedModifiers)
Returns all inner classes in the given class.
|
static java.util.Set<java.lang.Class<?>> |
getAllInnerClassesInClassIgnoreParentClasses(java.lang.Class<?> clazz,
boolean includeMultipleLevels,
int... excludedModifiers)
Returns all inner classes in the given class.
|
static <T> java.util.List<java.lang.reflect.Constructor> |
getAllMatchingConstructorsInClass(java.lang.Class<?> clazz,
java.lang.Class<?>... paramTypes)
Gets the method with the given name in the given class.
|
static <T> java.util.List<java.lang.reflect.Constructor> |
getAllMatchingConstructorsInClass(java.lang.Class<?> clazz,
java.util.List<java.lang.Class<T>> annotations,
java.lang.Class<?>... paramTypes)
Gets the method with the given name in the given class.
|
static java.util.List<java.lang.reflect.Method> |
getAllMatchingMethodsInClass(java.lang.Class<?> clazz,
java.lang.String methodName,
java.lang.Class<?>... paramTypes)
Gets the method with the given name in the given class.
|
static java.util.Set<java.lang.reflect.Method> |
getAllMethodsInClass(java.lang.Class<?> clazz,
int... excludedModifiers)
Returns all methods in the class, using modifiers to exclude specific types
|
static java.util.Collection<java.lang.reflect.Method> |
getAllMethodsWithAnnotation(java.lang.Class<?> clazz,
java.lang.Class<? extends java.lang.annotation.Annotation> annotation)
Returns a collection of all methods that had a specific set of annotations
|
static java.util.Collection<java.lang.reflect.Method> |
getAllMethodsWithAnnotation(java.lang.Class<?> clazz,
java.util.Set<java.lang.Class<? extends java.lang.annotation.Annotation>> annotations)
Returns a collection of all methods that had a specific set of annotations
|
static java.util.Set<java.lang.Class<?>> |
getAllParentClasses(java.lang.Class<?> clazz)
Returns a set of all parent classes and interfaces in for the given class
|
static java.lang.Class<?> |
getBoxedType(java.lang.Class<?> primitive)
Converts a primitive class to it corresponding boxed type
|
static java.lang.Class<?> |
getClassFromSimpleString(java.lang.String className)
Gets a class from a simple String name (converting array notation)
|
static java.lang.Class<?> |
getClassFromSimpleStringWithImports(java.util.Collection<java.lang.String> imports,
java.lang.String className)
Attempts to resolve a class given a set of imported packages and classes.
|
static java.lang.reflect.Constructor |
getConstructorInClass(java.lang.Class<?> clazz,
java.lang.Class<?>... paramTypes)
Gets the method with the given name in the given class.
|
static <T> T |
getDataMember(java.lang.Object object,
java.lang.String fieldName)
Gets the data member off a given object.
|
static java.lang.reflect.Field |
getFieldInClass(java.lang.Class<?> clazz,
java.lang.String fieldName)
Gets the field with the given name in the given class.
|
static java.util.Set<java.lang.Class<?>> |
getImmediateParentClasses(java.lang.Class<?> clazz)
Returns a set of all immediate parent classes and interfaces in for the given class
|
static java.lang.reflect.Method |
getMethodInClass(java.lang.Class<?> clazz,
java.lang.String methodName,
java.lang.Class<?>... paramTypes)
Gets the method with the given name in the given class.
|
static java.lang.Class<?> |
getMethodReturnTypeInClass(java.lang.Class<?> clazz,
java.lang.String methodName,
java.lang.Class<?>... paramTypes)
Gets the method with the given name in the given class.
|
static java.util.Set<java.lang.reflect.Method> |
getMethodsInClass(java.lang.Class<?> clazz,
java.lang.String methodName)
Returns the methods found in the given class.
|
static java.lang.String |
getPackageName(java.lang.Class<?> clazz)
Gets the name of the package that the given class is in.
|
static java.lang.Class<?> |
getRootClassOfArray(java.lang.Class<?> clazz)
Gets the root class of an object.
|
static java.lang.String |
getSimpleClassName(java.lang.Class<?> clazz)
Returns the simple name of a class, for example, returns java.lang.String[][] rather than [[Ljava.lang.String;
|
static java.lang.String |
getSimpleClassName(java.lang.Class<?> clazz,
boolean showPackage)
Returns the simple name of a class, for example, returns java.lang.String[][] rather than [[Ljava.lang.String;
|
static <T> T |
getStaticDataMember(java.lang.Class<?> clazz,
java.lang.String fieldName)
Gets the static data member off a given object.
|
static int |
getStepsBetweenClasses(java.lang.Class<?> parent,
java.lang.Class<?> child)
Gets the number of steps it takes to go from the child to the parent.
|
static java.lang.Class<?> |
getWildcardClass()
Returns a simple placeholder class that can match on anything for parameter types, and is returned as the return type when multiple methods match, having
multiple return types, and the best match used the wildcard class.
|
static boolean |
isClassChildOfClass(java.lang.Class<?> parent,
java.lang.Class<?> child)
Checks to see if one class is a child of another.
|
static boolean |
isPrimitiveArrayType(java.lang.Class<?> clazz)
Checks to see if the array is of primitive type
|
static void |
setDataMember(java.lang.Object object,
java.lang.String fieldName,
java.lang.Object value)
Gets the data member off a given object.
|
static void |
setStaticDataMember(java.lang.Class<?> clazz,
java.lang.String fieldName,
java.lang.Object value)
Gets the static data member off a given object.
|
static boolean |
shouldFilterByExcludedModifiers(int objectModifiers,
boolean requireAll,
int... modifiers)
Checks to see if the modifiers for a given object match at least one or possibly all the modifiers listed
|
static boolean |
shouldFilterByRequiredModifiers(int objectModifiers,
boolean requireAll,
int... modifiers)
Checks to see if the modifiers for a given object match at least one or possibly all the modifiers listed
|
@NotNull public static java.lang.Class<?> getWildcardClass()
public static java.lang.String getSimpleClassName(@NotNull java.lang.Class<?> clazz)
clazz
- The class to convert to a String@NotNull public static java.lang.String getPackageName(@NotNull java.lang.Class<?> clazz)
clazz
- The class to get the package ofpublic static java.lang.String getSimpleClassName(@NotNull java.lang.Class<?> clazz, boolean showPackage)
clazz
- The class to convert to a StringshowPackage
- True if the package should be included in the class name, false otherwise@Nullable public static java.lang.Class<?> getClassFromSimpleString(@Nullable java.lang.String className) throws java.lang.ClassNotFoundException
className
- The name of the classjava.lang.ClassNotFoundException
- If the class could not be found@Nullable public static java.lang.Class<?> getClassFromSimpleStringWithImports(@Nullable java.util.Collection<java.lang.String> imports, @Nullable java.lang.String className) throws java.lang.ClassNotFoundException
imports
- A Collection of the imported packages, such as java.util.Set or java.io.*className
- The text entered as the class name, such as Set or java.util.Setjava.lang.ClassNotFoundException
- If no matching class could be foundpublic static boolean isPrimitiveArrayType(@Nullable java.lang.Class<?> clazz)
clazz
- The class to check@NotNull public static java.util.Set<java.lang.reflect.Field> getAllFieldsInClass(@Nullable java.lang.Class<?> clazz, int... excludedModifiers)
clazz
- The class to get fields forexcludedModifiers
- The modifiers of things to exclude@NotNull public static java.lang.reflect.Field getFieldInClass(@NotNull java.lang.Class<?> clazz, @Nullable java.lang.String fieldName) throws java.lang.NoSuchFieldException
clazz
- The class containing the named fieldfieldName
- The name of the field to getjava.lang.NoSuchFieldException
- If the field could not be found@Nullable public static java.lang.Class<?> getRootClassOfArray(@Nullable java.lang.Class<?> clazz)
clazz
- The class in question@NotNull public static java.util.Set<java.lang.reflect.Method> getAllMethodsInClass(@Nullable java.lang.Class<?> clazz, int... excludedModifiers)
clazz
- The class to get methods forexcludedModifiers
- The modifiers of things to exclude@NotNull public static java.util.Set<java.lang.Class<?>> getAllInnerClassesInClass(@Nullable java.lang.Class<?> clazz, boolean includeMultipleLevels, int... excludedModifiers)
clazz
- The class to get inner classes fromincludeMultipleLevels
- True if the inner classes of inner classes should be included, false otherwiseexcludedModifiers
- The modifiers of things to exclude@NotNull public static java.util.Set<java.lang.Class<?>> getAllInnerClassesInClassIgnoreParentClasses(@Nullable java.lang.Class<?> clazz, boolean includeMultipleLevels, int... excludedModifiers)
clazz
- The class to get inner classes fromincludeMultipleLevels
- True if the inner classes of inner classes should be included, false otherwiseexcludedModifiers
- The modifiers of things to exclude@NotNull public static java.util.Set<java.lang.reflect.Constructor> getAllConstructorsInClass(@Nullable java.lang.Class<?> clazz, int... excludedModifiers)
clazz
- The class to get methods forexcludedModifiers
- The modifiers of things to exclude@NotNull public static <T> java.util.List<java.lang.reflect.Constructor> getAllMatchingConstructorsInClass(java.lang.Class<?> clazz, java.lang.Class<?>... paramTypes)
clazz
- The class with the given methodparamTypes
- The types of parameters this method accepts@NotNull public static <T> java.util.List<java.lang.reflect.Constructor> getAllMatchingConstructorsInClass(java.lang.Class<?> clazz, java.util.List<java.lang.Class<T>> annotations, @NotNull java.lang.Class<?>... paramTypes)
clazz
- The class with the given methodparamTypes
- The types of parameters this method accepts@Nullable public static java.lang.reflect.Constructor getConstructorInClass(@NotNull java.lang.Class<?> clazz, @NotNull java.lang.Class<?>... paramTypes) throws java.lang.NoSuchMethodException
clazz
- The class with the given methodparamTypes
- The types of parameters this method acceptsjava.lang.NoSuchMethodException
- If no method could be found with the given signature@NotNull public static java.util.Set<java.lang.reflect.Method> getMethodsInClass(@NotNull java.lang.Class<?> clazz, @Nullable java.lang.String methodName) throws java.lang.NoSuchMethodException
clazz
- The class containing the given methodmethodName
- The name of the methodjava.lang.NoSuchMethodException
- When the method was not found@NotNull public static java.util.Collection<java.lang.reflect.Method> getAllMethodsWithAnnotation(java.lang.Class<?> clazz, java.lang.Class<? extends java.lang.annotation.Annotation> annotation)
clazz
- The class to get methods fromannotation
- An annotation that must be present@NotNull public static java.util.Collection<java.lang.reflect.Method> getAllMethodsWithAnnotation(java.lang.Class<?> clazz, @NotNull java.util.Set<java.lang.Class<? extends java.lang.annotation.Annotation>> annotations)
clazz
- The class to get methods fromannotations
- A set of the annotations that must be present@NotNull public static java.util.List<java.lang.reflect.Method> getAllMatchingMethodsInClass(@NotNull java.lang.Class<?> clazz, java.lang.String methodName, @NotNull java.lang.Class<?>... paramTypes)
clazz
- The class with the given methodmethodName
- The name of the methodparamTypes
- The types of parameters this method accepts@NotNull public static java.lang.reflect.Method getMethodInClass(@NotNull java.lang.Class<?> clazz, java.lang.String methodName, java.lang.Class<?>... paramTypes) throws java.lang.NoSuchMethodException
clazz
- The class with the given methodmethodName
- The name of the methodparamTypes
- The types of parameters this method acceptsjava.lang.NoSuchMethodException
- If no method could be found with the given signature@NotNull public static java.lang.Class<?> getMethodReturnTypeInClass(@NotNull java.lang.Class<?> clazz, java.lang.String methodName, java.lang.Class<?>... paramTypes) throws java.lang.NoSuchMethodException
clazz
- The class with the given methodmethodName
- The name of the methodparamTypes
- The types of parameters this method acceptsReflectionUtils.Wildcard
class if multiple methods matched, and
the
best match used the wildcard classjava.lang.NoSuchMethodException
- If no method could be found with the given signaturepublic static int getStepsBetweenClasses(@Nullable java.lang.Class<?> parent, @Nullable java.lang.Class<?> child)
parent
- The parent classchild
- The child classpublic static boolean isClassChildOfClass(java.lang.Class<?> parent, java.lang.Class<?> child)
parent
- The parent classchild
- The class to determine if it is a subclass or child of the parent@NotNull public static java.util.Set<java.lang.Class<?>> getAllParentClasses(@Nullable java.lang.Class<?> clazz)
clazz
- The class to get parent classes of@NotNull public static java.util.Set<java.lang.Class<?>> getImmediateParentClasses(@Nullable java.lang.Class<?> clazz)
clazz
- The class to get parent classes of@NotNull public static <T> T getDataMember(@Nullable java.lang.Object object, java.lang.String fieldName) throws java.lang.NoSuchFieldException, java.lang.IllegalAccessException
object
- The object containing the data memberfieldName
- The name of the field to get, as named from the codejava.lang.NoSuchFieldException
- If the field doesn't exist on the objectjava.lang.IllegalAccessException
- If the object doesn't allow access to the fieldjava.lang.ClassCastException
- If the type of T doesn't match the object@NotNull public static <T> T getStaticDataMember(@Nullable java.lang.Class<?> clazz, java.lang.String fieldName) throws java.lang.NoSuchFieldException, java.lang.IllegalAccessException
clazz
- The class containing the static data memberfieldName
- The name of the field to get, as named from the codejava.lang.NoSuchFieldException
- If the field doesn't exist on the objectjava.lang.IllegalAccessException
- If the object doesn't allow access to the fieldjava.lang.ClassCastException
- If the type of T doesn't match the objectpublic static void setDataMember(@Nullable java.lang.Object object, java.lang.String fieldName, java.lang.Object value) throws java.lang.NoSuchFieldException, java.lang.IllegalAccessException
object
- The object containing the data memberfieldName
- The name of the field to set, as named from the codevalue
- The value to set in the objectjava.lang.NoSuchFieldException
- If the field doesn't exist on the objectjava.lang.IllegalAccessException
- If the object doesn't allow access to the fieldjava.lang.ClassCastException
- If the type of the object given for the value isn't allowed in the fieldpublic static void setStaticDataMember(@Nullable java.lang.Class<?> clazz, java.lang.String fieldName, java.lang.Object value) throws java.lang.NoSuchFieldException, java.lang.IllegalAccessException
clazz
- The class containing the static data memberfieldName
- The name of the field to set, as named from the codevalue
- The value to set in the objectjava.lang.NoSuchFieldException
- If the field doesn't exist on the objectjava.lang.IllegalAccessException
- If the object doesn't allow access to the fieldjava.lang.ClassCastException
- If the type of the object given for the value isn't allowed in the fieldpublic static <T> T executeMethod(@Nullable java.lang.Object object, java.lang.String methodName, @NotNull java.lang.Object... params) throws java.lang.NoSuchMethodException, java.lang.reflect.InvocationTargetException, java.lang.IllegalAccessException
T
- The type to returnobject
- The object to execute the method onmethodName
- The name of the method to executeparams
- The parameters to give to the methodjava.lang.NoSuchMethodException
- If the method doesn't exist (either by name or parameter list)java.lang.reflect.InvocationTargetException
- Error invoking the methodjava.lang.IllegalAccessException
- Unable to access the methodjava.lang.ClassCastException
- The return type required is incorrectpublic static <T> T executeStaticMethod(@Nullable java.lang.Class<?> clazz, java.lang.String methodName, @NotNull java.lang.Object... params) throws java.lang.NoSuchMethodException, java.lang.reflect.InvocationTargetException, java.lang.IllegalAccessException
T
- The type to returnclazz
- The class to execute the method onmethodName
- The name of the method to executeparams
- The parameters to give to the methodjava.lang.NoSuchMethodException
- If the method doesn't exist (either by name or parameter list)java.lang.reflect.InvocationTargetException
- Error invoking the methodjava.lang.IllegalAccessException
- Unable to access the methodjava.lang.ClassCastException
- The return type required is incorrectpublic static <T> T createObject(@Nullable java.lang.Class<?> clazz, @NotNull java.lang.Object... params) throws java.lang.NoSuchMethodException, java.lang.reflect.InvocationTargetException, java.lang.IllegalAccessException, java.lang.InstantiationException
clazz
- The class type to createparams
- The parameters to give to the methodjava.lang.NoSuchMethodException
- If the constructor doesn't exist (either by parameter list)java.lang.reflect.InvocationTargetException
- Error invoking the constructorjava.lang.IllegalAccessException
- Unable to access the constructorjava.lang.InstantiationException
- An error occurred when trying to build the objectjava.lang.ClassCastException
- The return type required is incorrectpublic static <U extends java.lang.reflect.Member> void filterMembersByRequiredModifier(@NotNull java.util.Collection<U> members, boolean requireAll, int... modifiers)
members
- The collection of membersrequireAll
- True to require a field to match all modifiers, false to require at least one to matchmodifiers
- The modifiers that are allowedpublic static <U extends java.lang.reflect.Member> void filterMembersByExcludedModifier(@NotNull java.util.Collection<U> members, boolean requireAll, int... modifiers)
members
- The collection of membersrequireAll
- True to require a field to exclude all modifiers, false to require at least one to be excludedmodifiers
- The modifiers that are allowedpublic static void filterClassesByRequiredModifier(@NotNull java.util.Collection<java.lang.Class<?>> classes, boolean requireAll, int... modifiers)
classes
- The collection of classesrequireAll
- True to require a class to match all modifiers, false to require at least one to matchmodifiers
- The modifiers that are allowedpublic static void filterClassesByExcludedModifier(@NotNull java.util.Collection<java.lang.Class<?>> classes, boolean requireAll, int... modifiers)
classes
- The collection of classesrequireAll
- True to require a class to exclude all modifiers, false to require at least one to be excludedmodifiers
- The modifiers that are allowedpublic static boolean shouldFilterByRequiredModifiers(int objectModifiers, boolean requireAll, @Nullable int... modifiers)
objectModifiers
- The modifier value for the object in questionrequireAll
- True to require all modifiers to match, false to require at least one, but not necessarily all, to matchmodifiers
- The modifiers that should matchpublic static boolean shouldFilterByExcludedModifiers(int objectModifiers, boolean requireAll, @Nullable int... modifiers)
objectModifiers
- The modifier value for the object in questionrequireAll
- True to require all modifiers to match, false to require at least one, but not necessarily all, to matchmodifiers
- The modifiers that should match@NotNull public static java.util.Map<java.lang.String,java.lang.Object> convertObjectToMap(@NotNull java.lang.Object object)
object
- The object to convert to a map@NotNull public static java.util.Map<java.lang.String,java.lang.Object> convertObjectToDeepMap(@NotNull java.lang.Object object)
object
- The object to convert to a map