public final class ReflectionUtils
extends java.lang.Object
Modifier and Type | Method and Description |
---|---|
static <T> T |
activate(java.lang.Class<T> clazz)
Activates an object from the type.
|
static <T> T |
activate(java.lang.Class<T> clazz,
java.lang.Object[] constructorArgs)
Activates an object from the class given, using the specified constructor arguments.
|
static java.lang.Object |
activate(java.lang.String className)
Activates an object from the class name given.
|
static java.lang.Object |
activate(java.lang.String className,
java.lang.Object[] constructorArgs)
Activates an object from the class name given.
|
static boolean |
equal(java.lang.reflect.Constructor<?> constrA,
java.lang.reflect.Constructor<?> constrB)
Compares two Constructors and returns true if they appear to be referring to the same constructor.
|
static boolean |
equal(java.lang.reflect.Field fieldA,
java.lang.reflect.Field fieldB)
Compares two Field classes and returns true if they appear to be referring to the same signature of a field.
|
static boolean |
equal(MemberInfo membA,
MemberInfo membB)
Compares two class members and returns true if they appear to be referring to the same member.
|
static boolean |
equal(java.lang.reflect.Method methodA,
java.lang.reflect.Method methodB)
Compares two Method classes and returns true if they appear to be referring to the same signature of a method.
|
static boolean |
equal(PropertyInfo propA,
PropertyInfo propB)
Compares two Method classes and returns true if they appear to be referring to the same signature of a property.
|
static java.util.List<java.lang.annotation.Annotation> |
getAnnotations(java.lang.Class<?> annotatedClass,
boolean includeInherited)
Returns all annotations from the annotated class (and its subclasses if required)
|
static <T extends java.lang.annotation.Annotation> |
getAnnotations(java.lang.Class<?> annotatedClass,
java.lang.Class<T> annotationType,
boolean includeInherited)
Returns all annotations from the annotated class (and its subclasses if required)
|
static java.lang.StackTraceElement |
getCallingStackTraceElement()
Returns the stack-trace element above the one of this method.
|
static java.util.List<java.lang.Class<?>> |
getClasses(java.lang.Package pkg)
Lists all classes inside a package.
|
static java.lang.reflect.Constructor<?> |
getConstructorDefault(java.lang.Class<?> type)
Returns null if a type has no default no-argument constructor, otherwise returns the constructor.
|
static java.lang.reflect.Constructor<?>[] |
getConstructors(java.lang.Class<?> type,
boolean includeSuperClass)
Gets all constructors found in a type.
|
static java.lang.reflect.Field |
getField(java.lang.Class<?> type,
java.lang.String name,
boolean includeInherited)
Returns the constructor information for an object's property, if found, otherwise null.
|
static java.lang.reflect.Field[] |
getFields(java.lang.Class<?> type,
boolean includeInherited)
Gets all fields found in a type.
|
static java.lang.reflect.Field[] |
getFields(java.lang.Class<?> type,
java.lang.String name,
boolean includeInherited)
Returns all fields found in a class type matching the given name.
|
static java.lang.reflect.Method |
getGetter(java.lang.Class<?> type,
java.lang.String name,
boolean includeInherited)
Returns the first encountered getter with the given name, or null if not found
|
static java.lang.reflect.Method[] |
getGetters(java.lang.Class<?> type,
boolean includeInherited)
Returns all getters found in a class type.
|
static java.lang.Class<?>[] |
getInterfaces(java.lang.Class<?> type,
boolean includeInherited)
Returns all interfaces of a type, except the specified as a generic argument one (T).
|
static java.lang.Class<?>[] |
getInterfacesExcept(java.lang.Class<?> type,
java.lang.Class<?> excludedInterface)
Returns all interfaces of a type, except the specified as a generic argument one (T).
|
static MemberInfo |
getMember(java.lang.Class<?> type,
java.lang.String name,
boolean includeInherited)
Returns the property information for an object's property, if found, otherwise null.
|
static MemberInfo[] |
getMembers(java.lang.Class<?> type,
boolean includeInherited)
Returns all methods found in a class type.
|
static MemberInfo[] |
getMembers(java.lang.Class<?> type,
java.lang.String name,
boolean includeInherited)
Returns all members found in a class type matching the given name.
|
static java.lang.reflect.Method |
getMethod(java.lang.Class<?> type,
java.lang.String name,
boolean includeInherited)
Returns the method information for an object's method, if found, otherwise null.
|
static java.util.List<java.lang.annotation.Annotation> |
getMethodAnnotations(java.lang.reflect.Method annotatedMethod,
boolean includeInherited)
Returns all annotations from the annotated method which are of specified annotation class type (or any its subclasses, if required)
|
static <T extends java.lang.annotation.Annotation> |
getMethodAnnotations(java.lang.reflect.Method annotatedMethod,
java.lang.Class<T> annotationType,
boolean includeInherited)
Returns all annotations from the annotated method which are of specified annotation class type (or any its subclasses, if required)
|
static java.lang.reflect.Method[] |
getMethods(java.lang.Class<?> type,
boolean includeInherited)
Returns all methods found in a class type.
|
static java.lang.reflect.Method[] |
getMethods(java.lang.Class<?> type,
java.lang.String name,
boolean includeInherited)
Returns all methods found in a class type matching the given name.
|
static PrimitiveType |
getPrimitiveType(java.lang.Class<?> type)
Returns true if the type specified is a primitive type such as int, boolean, etc.
|
static PropertyInfo[] |
getProperties(java.lang.Class<?> type,
boolean includeInherited)
Gets all properties found in a type.
|
static PropertyInfo[] |
getProperties(java.lang.Class<?> type,
java.lang.String name,
boolean includeInherited)
Returns all properties found in a class type matching the given name.
|
static PropertyInfo |
getProperty(java.lang.Class<?> type,
java.lang.String name,
boolean includeInherited)
Returns the property information for an object's property, if found, otherwise null.
|
static java.lang.reflect.Method |
getSetter(java.lang.Class<?> type,
java.lang.String name,
boolean includeInherited)
Returns the first encountered setter with the given name, or null if not found
|
static java.lang.reflect.Method[] |
getSetters(java.lang.Class<?> type,
boolean includeInherited)
Returns all setters found in a class type.
|
static boolean |
hasField(java.lang.Class<?> type,
java.lang.reflect.Field field,
boolean includeInherited)
Returns true if a field appears to be part of a type.
|
static boolean |
hasMember(java.lang.Class<?> type,
MemberInfo member,
boolean includeInherited)
Returns true if a member appears to be part of a type.
|
static boolean |
hasMethod(java.lang.Class<?> type,
java.lang.reflect.Method method,
boolean includeInherited)
Returns true if a method appears to be part of a type.
|
static boolean |
hasProperty(java.lang.Class<?> type,
PropertyInfo property,
boolean includeInherited)
Returns true if a method appears to be part of a type.
|
static void |
injectProperty(java.lang.Object obj,
java.lang.String propertyName,
java.lang.Object propertyValue,
boolean includeInherited)
Inject a value to a property of an object.
|
static boolean |
instanceOf(java.lang.Class<?> type,
java.lang.Class<?> baseClassOrInterfaceType)
Returns true if any of the type's base types/interfaces are equal to the given type, or if the two types are the same.
|
static boolean |
isExtending(java.lang.Class<?> type,
java.lang.Class<?> baseClassType)
Returns true if any of the type's BaseTypes are equal to the given type or if the two types are the same.
|
static boolean |
isGeneric(java.lang.Class<?> type)
Returns true if a class is generic
|
static boolean |
isGetter(java.lang.reflect.Method method)
Returns true if the method appears to be a POJO getter.
|
static boolean |
isImplementing(java.lang.Class<?> type,
java.lang.Class<?> interfaceType)
Returns true if the given type implements the interface type specified.
|
static boolean |
isLookingLike(java.lang.Class<?> type,
java.lang.Class<?> interfaceType,
boolean includeTypeInherited,
boolean includeInterfaceTypeInherited)
Returns true if the given type appears to look like a given interface (or other class).
|
static boolean |
isNested(java.lang.Class<?> type)
Returns true if the class given is taking part in a nested class hierarchy.
|
static boolean |
isOverridable(java.lang.Class<?> type)
Checks that a class is overridable, i.e.
|
static boolean |
isOverridable(java.lang.reflect.Method methodInfo)
Returns true if a method can be overridden.
|
static boolean |
isReturnTypeVoid(java.lang.reflect.Method method)
Returns true if the method return type is void.
|
static boolean |
isSetter(java.lang.reflect.Method method)
Returns true if the method appears to be a POJO setter.
|
static java.lang.Class<?> |
parse(java.lang.String value)
Parses a class from a string.
|
static <T> java.lang.Class<T> |
parse(java.lang.String value,
java.lang.Class<T> baseType)
Parses a class from a string.
|
static <T> T |
proxy(java.lang.Object obj,
java.lang.Class<T> interfaceType)
Quick-and-dirty proxy-ing utility method, allows dynamic dispatch on any object using a specified interface.
|
static <T> T |
proxy(java.lang.Object obj,
java.lang.Class<T> interfaceType,
java.lang.String[] suppressedMethodNames)
Overloaded variation of the above method, which allows certain methods to be prevented from being called.
|
public static <T> T activate(java.lang.Class<T> clazz) throws java.lang.InstantiationException, java.lang.IllegalAccessException
java.lang.NullPointerException
- An argument is null.java.lang.InstantiationException
- When instantiation fails.java.lang.IllegalAccessException
- When a member is illegally accessed.public static <T> T activate(java.lang.Class<T> clazz, java.lang.Object[] constructorArgs) throws java.lang.InstantiationException, java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException
java.lang.NullPointerException
- The class argument is null.java.lang.InstantiationException
- When instantiation fails.java.lang.IllegalAccessException
- When a member is illegally accessed.java.lang.reflect.InvocationTargetException
- When the constructor called throws an exception.java.lang.IllegalArgumentException
- When no constructor accepting this many arguments is found, or there are more than 1 constructors
found accepting the arguments given.public static java.lang.Object activate(java.lang.String className) throws java.lang.InstantiationException, java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException, java.lang.ClassNotFoundException
java.lang.InstantiationException
- When instantiation fails.java.lang.IllegalAccessException
- When a member is illegally accessed.java.lang.reflect.InvocationTargetException
- When the constructor called throws an exception.java.lang.ClassNotFoundException
- The specified class name does not correspond to a class.public static java.lang.Object activate(java.lang.String className, java.lang.Object[] constructorArgs) throws java.lang.InstantiationException, java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException, java.lang.ClassNotFoundException
java.lang.InstantiationException
- When instantiation fails.java.lang.IllegalAccessException
- When a member is illegally accessed.java.lang.reflect.InvocationTargetException
- When the constructor called throws an exception.java.lang.ClassNotFoundException
- The specified class name does not correspond to a class.public static boolean equal(java.lang.reflect.Method methodA, java.lang.reflect.Method methodB)
java.lang.NullPointerException
- An argument is nullpublic static boolean equal(java.lang.reflect.Field fieldA, java.lang.reflect.Field fieldB)
java.lang.NullPointerException
- An argument is nullpublic static boolean equal(java.lang.reflect.Constructor<?> constrA, java.lang.reflect.Constructor<?> constrB)
java.lang.NullPointerException
- An argument is nullpublic static boolean equal(MemberInfo membA, MemberInfo membB)
java.lang.NullPointerException
- An argument is nullpublic static boolean equal(PropertyInfo propA, PropertyInfo propB)
java.lang.NullPointerException
- An argument is nullpublic static java.lang.StackTraceElement getCallingStackTraceElement()
public static java.util.List<java.lang.Class<?>> getClasses(java.lang.Package pkg) throws java.lang.ClassNotFoundException
java.lang.NullPointerException
- An argument is null.java.lang.IllegalArgumentException
- A package is invalid.java.lang.ClassNotFoundException
- When finding a class from a class name fails while enumerating.public static <T extends java.lang.annotation.Annotation> java.util.List<T> getAnnotations(java.lang.Class<?> annotatedClass, java.lang.Class<T> annotationType, boolean includeInherited)
java.lang.NullPointerException
- An argument is null.public static java.util.List<java.lang.annotation.Annotation> getAnnotations(java.lang.Class<?> annotatedClass, boolean includeInherited)
java.lang.NullPointerException
- An argument is null.public static java.util.List<java.lang.annotation.Annotation> getMethodAnnotations(java.lang.reflect.Method annotatedMethod, boolean includeInherited)
java.lang.NullPointerException
- An argument is null.public static <T extends java.lang.annotation.Annotation> java.util.List<T> getMethodAnnotations(java.lang.reflect.Method annotatedMethod, java.lang.Class<T> annotationType, boolean includeInherited)
java.lang.NullPointerException
- An argument is null.public static java.lang.Class<?>[] getInterfaces(java.lang.Class<?> type, boolean includeInherited)
java.lang.NullPointerException
- An argument is null.public static java.lang.Class<?>[] getInterfacesExcept(java.lang.Class<?> type, java.lang.Class<?> excludedInterface)
java.lang.NullPointerException
- An argument is null.public static java.lang.reflect.Constructor<?>[] getConstructors(java.lang.Class<?> type, boolean includeSuperClass)
java.lang.NullPointerException
- An argument is null.java.lang.SecurityException
- Cannot perform reflection operations in this context.public static java.lang.reflect.Constructor<?> getConstructorDefault(java.lang.Class<?> type)
java.lang.NullPointerException
- An argument is null.java.lang.SecurityException
- Cannot perform reflection operations in this context.public static java.lang.reflect.Field[] getFields(java.lang.Class<?> type, boolean includeInherited)
java.lang.NullPointerException
- An argument is null.java.lang.SecurityException
- Cannot perform reflection operations in this context.public static java.lang.reflect.Field[] getFields(java.lang.Class<?> type, java.lang.String name, boolean includeInherited)
java.lang.NullPointerException
- An argument is null.java.lang.SecurityException
- Cannot perform reflection operations in this context.public static java.lang.reflect.Field getField(java.lang.Class<?> type, java.lang.String name, boolean includeInherited)
java.lang.NullPointerException
- An argument is null.java.lang.SecurityException
- Cannot perform reflection operations in this context.public static PropertyInfo[] getProperties(java.lang.Class<?> type, boolean includeInherited)
java.lang.NullPointerException
- An argument is null.java.lang.SecurityException
- Cannot perform reflection operations in this context.public static PropertyInfo[] getProperties(java.lang.Class<?> type, java.lang.String name, boolean includeInherited)
java.lang.NullPointerException
- An argument is null.java.lang.SecurityException
- Cannot perform reflection operations in this context.public static PropertyInfo getProperty(java.lang.Class<?> type, java.lang.String name, boolean includeInherited)
java.lang.NullPointerException
- An argument is null.java.lang.SecurityException
- Cannot perform reflection operations in this context.public static java.lang.reflect.Method[] getGetters(java.lang.Class<?> type, boolean includeInherited)
java.lang.NullPointerException
- An argument is null.java.lang.SecurityException
- Cannot perform reflection operations in this context.public static java.lang.reflect.Method getGetter(java.lang.Class<?> type, java.lang.String name, boolean includeInherited)
java.lang.NullPointerException
- An argument is null.java.lang.SecurityException
- Cannot perform reflection operations in this context.public static java.lang.reflect.Method[] getSetters(java.lang.Class<?> type, boolean includeInherited)
java.lang.NullPointerException
- An argument is null.java.lang.SecurityException
- Cannot perform reflection operations in this context.public static java.lang.reflect.Method getSetter(java.lang.Class<?> type, java.lang.String name, boolean includeInherited)
java.lang.NullPointerException
- An argument is null.java.lang.SecurityException
- Cannot perform reflection operations in this context.public static java.lang.reflect.Method[] getMethods(java.lang.Class<?> type, boolean includeInherited)
java.lang.NullPointerException
- An argument is null.java.lang.SecurityException
- Cannot perform reflection operations in this context.public static java.lang.reflect.Method[] getMethods(java.lang.Class<?> type, java.lang.String name, boolean includeInherited)
java.lang.NullPointerException
- An argument is null.java.lang.SecurityException
- Cannot perform reflection operations in this context.public static java.lang.reflect.Method getMethod(java.lang.Class<?> type, java.lang.String name, boolean includeInherited)
java.lang.NullPointerException
- An argument is null.java.lang.SecurityException
- Cannot perform reflection operations in this context.public static MemberInfo[] getMembers(java.lang.Class<?> type, boolean includeInherited)
java.lang.NullPointerException
- An argument is null.java.lang.SecurityException
- Cannot perform reflection operations in this context.public static MemberInfo[] getMembers(java.lang.Class<?> type, java.lang.String name, boolean includeInherited)
java.lang.NullPointerException
- An argument is null.java.lang.SecurityException
- Cannot perform reflection operations in this context.public static MemberInfo getMember(java.lang.Class<?> type, java.lang.String name, boolean includeInherited)
java.lang.NullPointerException
- An argument is null.java.lang.SecurityException
- Cannot perform reflection operations in this context.public static boolean hasMethod(java.lang.Class<?> type, java.lang.reflect.Method method, boolean includeInherited)
java.lang.NullPointerException
- An argument is null.public static boolean hasProperty(java.lang.Class<?> type, PropertyInfo property, boolean includeInherited)
java.lang.NullPointerException
- An argument is null.public static boolean hasField(java.lang.Class<?> type, java.lang.reflect.Field field, boolean includeInherited)
java.lang.NullPointerException
- An argument is null.public static boolean hasMember(java.lang.Class<?> type, MemberInfo member, boolean includeInherited)
java.lang.NullPointerException
- An argument is null.public static boolean isGeneric(java.lang.Class<?> type)
java.lang.NullPointerException
- An argument is nullpublic static boolean isGetter(java.lang.reflect.Method method)
java.lang.NullPointerException
- An argument is nullpublic static boolean isReturnTypeVoid(java.lang.reflect.Method method)
java.lang.NullPointerException
- An argument is nullpublic static boolean isSetter(java.lang.reflect.Method method)
java.lang.NullPointerException
- An argument is nullpublic static void injectProperty(java.lang.Object obj, java.lang.String propertyName, java.lang.Object propertyValue, boolean includeInherited)
java.lang.NullPointerException
- The object or property name is null.java.lang.SecurityException
- Cannot perform reflection operations in this context.public static boolean instanceOf(java.lang.Class<?> type, java.lang.Class<?> baseClassOrInterfaceType)
java.lang.NullPointerException
- When an argument is nullpublic static boolean isExtending(java.lang.Class<?> type, java.lang.Class<?> baseClassType)
java.lang.NullPointerException
- When an argument is nullpublic static boolean isLookingLike(java.lang.Class<?> type, java.lang.Class<?> interfaceType, boolean includeTypeInherited, boolean includeInterfaceTypeInherited)
public static boolean isImplementing(java.lang.Class<?> type, java.lang.Class<?> interfaceType)
java.lang.NullPointerException
- When an argument is nullpublic static boolean isNested(java.lang.Class<?> type)
java.lang.NullPointerException
- An argument is nullpublic static boolean isOverridable(java.lang.reflect.Method methodInfo)
java.lang.NullPointerException
- An argument is null.public static boolean isOverridable(java.lang.Class<?> type)
java.lang.NullPointerException
- An argument is null.java.lang.SecurityException
- Cannot perform reflection operations in this context.public static PrimitiveType getPrimitiveType(java.lang.Class<?> type)
public static java.lang.Class<?> parse(java.lang.String value) throws java.lang.ClassNotFoundException
java.lang.NullPointerException
- An argument is null.java.lang.ClassNotFoundException
- The class was not found.public static <T> java.lang.Class<T> parse(java.lang.String value, java.lang.Class<T> baseType) throws java.lang.ClassNotFoundException
java.lang.NullPointerException
- An argument is null.java.lang.ClassNotFoundException
- The class was not found.public static <T> T proxy(java.lang.Object obj, java.lang.Class<T> interfaceType)
java.lang.NullPointerException
- An argument is nulljava.lang.IllegalArgumentException
- The interface class type is not an actual interface typepublic static <T> T proxy(java.lang.Object obj, java.lang.Class<T> interfaceType, java.lang.String[] suppressedMethodNames)
java.lang.NullPointerException
- An argument is nulljava.lang.IllegalArgumentException
- The interface class type is not an actual interface type