Package com.attivio.platform.util
Class ReflectionUtils
- java.lang.Object
-
- com.attivio.platform.util.ReflectionUtils
-
public final class ReflectionUtils extends java.lang.Object
Utility class for performing actions requiring reflection.
-
-
Constructor Summary
Constructors Constructor Description ReflectionUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> java.lang.Class<? extends T>
forName(java.lang.String className, java.lang.Class<T> type)
Lookup a class givenclassName
andtype
.static <T extends java.lang.annotation.Annotation>
TgetAnnotation(java.lang.Class<?> clazz, java.lang.Class<T> annotation)
Inspects theclazz
and returns the Annotation instance if theannotation
is present on the clazz.static java.lang.reflect.Method
getGetter(java.lang.Class<?> beanClass, java.lang.String name)
Gets a getter method.static java.lang.reflect.Method
getSetter(java.lang.Class<?> beanClass, java.lang.String name, java.lang.Class<?> type)
Gets a setter method.static java.lang.Object
invoke(java.lang.Class<?> beanClass, java.lang.Object bean, java.lang.String name, java.lang.Object... args)
Invokes a method.static java.lang.Object
invoke(java.lang.Object bean, java.lang.String name, java.lang.Object... args)
Invokes a method.static <T extends java.lang.annotation.Annotation>
booleanisAnnotationPresent(java.lang.Class<?> clazz, java.lang.Class<T> annotation)
Inspects theclazz
and returnstrue
if theannotation
is present on the clazz.static boolean
isGetter(java.lang.reflect.Method method)
Gets ifmethod
is a getter method.static boolean
isPublic(java.lang.reflect.Method method)
Gets ifmethod
is declared public.static boolean
isSetter(java.lang.reflect.Method method)
Gets ifmethod
is a setter method.static boolean
isSubclass(java.lang.String className, java.lang.Class<?> type)
static <T> T
newInstance(java.lang.Class<? extends T> type)
Intantiate a new instance oftype
.static java.lang.Object
newInstance(java.lang.String className)
Instantiate a class by name.
-
-
-
Method Detail
-
isPublic
public static boolean isPublic(java.lang.reflect.Method method)
Gets ifmethod
is declared public.
-
isGetter
public static boolean isGetter(java.lang.reflect.Method method)
Gets ifmethod
is a getter method.
-
isSetter
public static boolean isSetter(java.lang.reflect.Method method)
Gets ifmethod
is a setter method.
-
getGetter
public static java.lang.reflect.Method getGetter(java.lang.Class<?> beanClass, java.lang.String name)
Gets a getter method.- Parameters:
beanClass
- the beanClassname
- the getter name- Returns:
- the getter Method (or null if not found)
-
getSetter
public static java.lang.reflect.Method getSetter(java.lang.Class<?> beanClass, java.lang.String name, java.lang.Class<?> type)
Gets a setter method.- Parameters:
beanClass
- the bean classname
- the setter nametype
- the type- Returns:
- the setter Method (or null if not found)
-
forName
public static <T> java.lang.Class<? extends T> forName(java.lang.String className, java.lang.Class<T> type) throws AttivioException
Lookup a class givenclassName
andtype
.- Throws:
AttivioException
- if the class is not found or if the type of the class is not a subclass oftype
-
isSubclass
public static boolean isSubclass(java.lang.String className, java.lang.Class<?> type) throws AttivioException
- Parameters:
className
-type
-- Returns:
- true if
className
is a subclass or implementation oftype
(which can be an interface). - Throws:
AttivioException
- ifclassName
could not be found
-
newInstance
public static <T> T newInstance(java.lang.Class<? extends T> type) throws AttivioException
Intantiate a new instance oftype
.- Throws:
AttivioException
-
newInstance
public static java.lang.Object newInstance(java.lang.String className) throws AttivioException
Instantiate a class by name.- Throws:
AttivioException
-
invoke
public static java.lang.Object invoke(java.lang.Object bean, java.lang.String name, java.lang.Object... args) throws java.lang.reflect.InvocationTargetException, java.lang.NoSuchMethodException, java.lang.IllegalAccessException
Invokes a method.- Parameters:
bean
- the beanname
- the method nameargs
- the arguments- Returns:
- result of method invokation
- Throws:
java.lang.NoSuchMethodException
- if method could not be foundjava.lang.reflect.InvocationTargetException
java.lang.IllegalAccessException
-
invoke
public static java.lang.Object invoke(java.lang.Class<?> beanClass, java.lang.Object bean, java.lang.String name, java.lang.Object... args) throws java.lang.reflect.InvocationTargetException, java.lang.NoSuchMethodException, java.lang.IllegalAccessException
Invokes a method.- Parameters:
beanClass
- the class forbean
bean
- the beanname
- the method nameargs
- the arguments- Returns:
- result of method invokation
- Throws:
java.lang.NoSuchMethodException
- if method could not be foundjava.lang.reflect.InvocationTargetException
java.lang.IllegalAccessException
-
getAnnotation
public static <T extends java.lang.annotation.Annotation> T getAnnotation(java.lang.Class<?> clazz, java.lang.Class<T> annotation)
Inspects theclazz
and returns the Annotation instance if theannotation
is present on the clazz. This method works with classes or interfaces. All implemented interfaces are checked.- Parameters:
clazz
-annotation
-- Returns:
- the annotation if present on clazz,
null
otherwise.
-
isAnnotationPresent
public static <T extends java.lang.annotation.Annotation> boolean isAnnotationPresent(java.lang.Class<?> clazz, java.lang.Class<T> annotation)
Inspects theclazz
and returnstrue
if theannotation
is present on the clazz. This method works with classes or interfaces. All implemented interfaces are checked.- Parameters:
clazz
-annotation
-- Returns:
true
if the annotation is present on clazz,false
otherwise.
-
-