public final class ReflectionUtils extends Object
| Modifier and Type | Method and Description |
|---|---|
static Class<?> |
getClass(String className)
Returns the class object for the specified qualified class name.
|
static Field |
getField(Class<?> clazz,
String fieldName)
Returns the field object found for the given field name in the given
class.
|
static Class<?> |
getFieldType(Class<?> clazz,
String fieldName)
Returns the type of a field if it exists within the class.
|
static Method |
getGetter(Class<?> clazz,
String fieldName)
Retrieves the getter method of the given class for the specified field
name.
|
static Field[] |
getInstanceFields(Class<?> clazz)
Returns the static field objects that are declared in the given class or
any of it's super types.
|
static Field[] |
getMatchingFields(Class<?> clazz,
int modifiers)
Returns the field objects that are declared in the given class or any of
it's super types that have any of the given modifiers.
|
static Method |
getMethod(Class<?> clazz,
Class<? extends Annotation> annotation)
Returns the method object for a method which is annotated with the
given annotation of the given class.
|
static Method |
getMethod(Class<?> clazz,
String methodName,
Class<?>... parameterTypes)
Returns the method object found for the given method name in the given
class.
|
static MethodException[] |
getMethodExceptions(Class<?> clazz,
Method m) |
static MethodException[] |
getMethodExceptions(Class<?> clazz,
String methodName,
Class<?>... parameterTypes) |
static Class<?>[] |
getMethodExceptionTypes(Class<?> clazz,
String methodName,
Class<?>... parameterTypes) |
static MethodParameter[] |
getMethodParameters(Class<?> clazz,
Method m) |
static MethodParameter[] |
getMethodParameters(Class<?> clazz,
String methodName,
Class<?>... parameterTypes) |
static Class<?>[] |
getMethodParameterTypes(Class<?> clazz,
String methodName,
Class<?>... parameterTypes) |
static Class<?> |
getMethodReturnType(Class<?> clazz,
String methodName,
Class<?>... parameterTypes)
Returns the return type of a method if it exists within the class.
|
static List<Method> |
getMethods(Class<?> clazz,
Class<? extends Annotation> annotation)
Returns the method objects for methods which are annotated with the
given annotation of the given class.
|
static Field[] |
getNonMatchingFields(Class<?> clazz,
int modifiers)
Returns the field objects that are declared in the given class or any of
it's super types that have none of the given modifiers.
|
static Class<?> |
getObjectClassOfPrimitve(Class<?> primitive)
Returns the wrapper class of the given primitive class or the given class.
|
static Class<?> |
getPrimitiveClassOfWrapper(Class<?> wrapperClass)
Returns the primitive class of the given wrapper class or null.
|
static Class<?> |
getResolvedFieldType(Class<?> clazz,
Field f) |
static Class<?> |
getResolvedFieldType(Class<?> clazz,
String fieldName) |
static Class<?>[] |
getResolvedFieldTypeArguments(Class<?> clazz,
Field f) |
static Class<?>[] |
getResolvedFieldTypeArguments(Class<?> clazz,
String fieldName) |
static Class<?>[] |
getResolvedMethodExceptionTypes(Class<?> clazz,
Method m) |
static Class<?>[] |
getResolvedMethodExceptionTypes(Class<?> clazz,
String methodName,
Class<?>... parameterTypes) |
static Class<?>[] |
getResolvedMethodParameterTypes(Class<?> clazz,
Method m) |
static Class<?>[] |
getResolvedMethodParameterTypes(Class<?> clazz,
String methodName,
Class<?>... parameterTypes) |
static Class<?>[][] |
getResolvedMethodParameterTypesArguments(Class<?> clazz,
Method method) |
static Class<?>[][] |
getResolvedMethodParameterTypesArguments(Class<?> clazz,
String methodName,
Class<?>... parameterTypes) |
static Class<?> |
getResolvedMethodReturnType(Class<?> clazz,
Method m) |
static Class<?> |
getResolvedMethodReturnType(Class<?> clazz,
String methodName,
Class<?>... parameterTypes) |
static Class<?>[] |
getResolvedMethodReturnTypeArguments(Class<?> clazz,
Method m) |
static Class<?>[] |
getResolvedMethodReturnTypeArguments(Class<?> clazz,
String methodName,
Class<?>... parameterTypes) |
static Method |
getSetter(Class<?> clazz,
String fieldName)
Retrieves the setter method of the given class for the specified field
name.
|
static Field[] |
getStaticFields(Class<?> clazz)
Returns the static field objects that are declared in the given class or
any of it's super types.
|
static Set<Class<?>> |
getSuperTypes(Class<?> clazz)
Retrieves all super types of the given class type.
|
static Set<Class<?>> |
getSuperTypes(Class<?> clazz,
Class<?> commonSuperType) |
static int |
getTypeVariablePosition(GenericDeclaration genericDeclartion,
TypeVariable<?> typeVariable)
Tries to find the position of the given type variable in the type
parameters of the given class.
|
static int |
getTypeVariablePosition(TypeVariable<?> typeVariable)
Returns the position of the type variable for the class in which it is
declared.
|
static Class<?> |
getWrapperClassOfPrimitve(Class<?> primitive)
Returns the wrapper class of the given primitive class or null.
|
static boolean |
isGetter(Method m) |
static boolean |
isSetter(Method m) |
static boolean |
isSubtype(Class<?> targetClazz,
Class<?> superType)
Checks if the target class is a subtype of the supertype or not.
|
static Type |
resolve(Class<?> concreteClass,
Type type) |
static Class<?> |
resolveType(Class<?> concreteClass,
Type type) |
static Class<?>[] |
resolveTypeArguments(Class<?> concreteClass,
ParameterizedType parameterizedType) |
static Class<?>[] |
resolveTypeArguments(Class<?> concreteClass,
Type type) |
static Class<?> |
resolveTypeVariable(Class<?> concreteClass,
TypeVariable<?> typeVariable)
Tries to resolve the type variable against the concrete class.
|
static Type |
resolveTypeVariableType(Class<?> concreteClass,
TypeVariable<?> typeVariable)
Tries to resolve the type variable against the concrete class.
|
public static Class<?> getClass(String className) throws ClassNotFoundException
Class.forName(java.lang.String) except
that also primitive types can be get via this method. The names for the
primitive types needed for this method are equal to the type literals
used in the java language.
Example:
ReflectionUtil.getClass("void").equals(void.class)
ReflectionUtil.getClass("int").equals(int.class)
className - ClassNotFoundExceptionpublic static Class<?> getObjectClassOfPrimitve(Class<?> primitive)
primitive - The primitive classpublic static Class<?> getWrapperClassOfPrimitve(Class<?> primitive)
primitive - The primitive classpublic static Class<?> getPrimitiveClassOfWrapper(Class<?> wrapperClass)
wrapperClass - The wrapper classpublic static boolean isSubtype(Class<?> targetClazz, Class<?> superType)
targetClazz - the class to check wether it is a subtype of the supertype or
notsuperType - the supertype classpublic static Set<Class<?>> getSuperTypes(Class<?> clazz)
clazz - The class from which the super types should be retrievedpublic static Class<?> getFieldType(Class<?> clazz, String fieldName)
getField(java.lang.Class, java.lang.String) with
a null check and finally return the type via getType().clazz - The class within to look for the field with the given field
namefieldName - The name of the field to be returnedgetField(java.lang.Class, java.lang.String)public static Class<?>[] getResolvedFieldTypeArguments(Class<?> clazz, String fieldName)
public static Class<?>[] getResolvedFieldTypeArguments(Class<?> clazz, Field f)
public static Class<?>[] resolveTypeArguments(Class<?> concreteClass, Type type)
public static Class<?>[] resolveTypeArguments(Class<?> concreteClass, ParameterizedType parameterizedType)
public static Type resolveTypeVariableType(Class<?> concreteClass, TypeVariable<?> typeVariable)
concreteClass - The class which is used to resolve the type. The type for the
type variable must be bound in this class or a superclass.typeVariable - The type variable to resolve.IllegalArgumentException - Is thrown when the concrete class is not a subtype of the
class in which the type variable has been declared.public static Class<?> resolveTypeVariable(Class<?> concreteClass, TypeVariable<?> typeVariable)
concreteClass - The class which is used to resolve the type. The type for the
type variable must be bound in this class or a superclass.typeVariable - The type variable to resolve.IllegalArgumentException - Is thrown when the concrete class is not a subtype of the
class in which the type variable has been declared.public static int getTypeVariablePosition(TypeVariable<?> typeVariable)
typeVariable - The type variable for which the position should be retrievedpublic static int getTypeVariablePosition(GenericDeclaration genericDeclartion, TypeVariable<?> typeVariable)
genericDeclartion - The generic declartion type in which to look for the type
variabletypeVariable - The type variable to look for in the given class type
parameterspublic static Field[] getInstanceFields(Class<?> clazz)
getNonMatchingFields(Class, int) with the
modifiers Modifier.STATIC.clazz - The class within to look for the fields with the given
modifierspublic static Field[] getStaticFields(Class<?> clazz)
getMatchingFields(Class, int) with the modifiers
Modifier.STATIC.clazz - The class within to look for the fields with the given
modifierspublic static Field[] getMatchingFields(Class<?> clazz, int modifiers)
clazz - The class within to look for the fields with the given
modifiersmodifiers - The OR-ed together modifiers that a field must match to be
included into the resultpublic static Field[] getNonMatchingFields(Class<?> clazz, int modifiers)
clazz - The class within to look for the fields with the given
modifiersmodifiers - The OR-ed together modifiers that a field must not match to be
included into the resultpublic static Field getField(Class<?> clazz, String fieldName)
clazz - The class within to look for the field with the given field
namefieldName - The name of the field to be returnedpublic static Class<?> getMethodReturnType(Class<?> clazz, String methodName, Class<?>... parameterTypes)
getMethod(Class, String, Class[]) with
a null check and finally return the type via getReturnType().clazz - The class within to look for the method with the given method
namemethodName - The name of the method to be returnedparameterTypes - The accepting parameter types of the methodgetMethod(Class, String, Class[])public static Class<?>[] getMethodParameterTypes(Class<?> clazz, String methodName, Class<?>... parameterTypes)
public static Class<?>[] getMethodExceptionTypes(Class<?> clazz, String methodName, Class<?>... parameterTypes)
public static Class<?> getResolvedMethodReturnType(Class<?> clazz, String methodName, Class<?>... parameterTypes)
public static Class<?> getResolvedMethodReturnType(Class<?> clazz, Method m)
public static Class<?>[] getResolvedMethodReturnTypeArguments(Class<?> clazz, String methodName, Class<?>... parameterTypes)
public static Class<?>[] getResolvedMethodReturnTypeArguments(Class<?> clazz, Method m)
public static Class<?>[] getResolvedMethodParameterTypes(Class<?> clazz, String methodName, Class<?>... parameterTypes)
public static Class<?>[] getResolvedMethodParameterTypes(Class<?> clazz, Method m)
public static Class<?>[][] getResolvedMethodParameterTypesArguments(Class<?> clazz, String methodName, Class<?>... parameterTypes)
public static Class<?>[][] getResolvedMethodParameterTypesArguments(Class<?> clazz, Method method)
public static Class<?>[] getResolvedMethodExceptionTypes(Class<?> clazz, String methodName, Class<?>... parameterTypes)
public static Class<?>[] getResolvedMethodExceptionTypes(Class<?> clazz, Method m)
public static MethodParameter[] getMethodParameters(Class<?> clazz, String methodName, Class<?>... parameterTypes)
public static MethodParameter[] getMethodParameters(Class<?> clazz, Method m)
public static MethodException[] getMethodExceptions(Class<?> clazz, String methodName, Class<?>... parameterTypes)
public static MethodException[] getMethodExceptions(Class<?> clazz, Method m)
public static Method getMethod(Class<?> clazz, String methodName, Class<?>... parameterTypes)
clazz - The class within to look for the method with the given method
namemethodName - The name of the method to be returnedparameterTypes - The accepting parameter types of the methodpublic static Method getMethod(Class<?> clazz, Class<? extends Annotation> annotation)
getMethods(java.lang.Class, java.lang.Class)clazz - The class within to look for the methodannotation - The annotation type a method must be annotated with to be
returnedpublic static List<Method> getMethods(Class<?> clazz, Class<? extends Annotation> annotation)
clazz - The class within to look for the methodsannotation - The annotation type a method must be annotated with to be
included in the listpublic static Method getGetter(Class<?> clazz, String fieldName)
This method uses #
getMethodReturnType(Class, String, Class...) to
retrieve the getter.
A getter must not have any parameters and must have a return type that is different from void.
clazz - The class within to look for the getter methodfieldName - The field name for which to find the getter methodpublic static boolean isGetter(Method m)
public static Method getSetter(Class<?> clazz, String fieldName)
A setter must have void return type and accept exactly one parameter.
clazz - The class within to look for the setter methodfieldName - The field name for which to find the setter methodpublic static boolean isSetter(Method m)
Copyright © 2020 Blazebit. All rights reserved.