org.squirrelframework.foundation.util
Class ReflectUtils

java.lang.Object
  extended by org.squirrelframework.foundation.util.ReflectUtils

public class ReflectUtils
extends Object


Nested Class Summary
static interface ReflectUtils.FieldCallback
          Callback interface invoked on each field in the hierarchy.
static interface ReflectUtils.FieldFilter
          Callback optionally used to filter fields to be operated on by a field callback.
static interface ReflectUtils.MethodCallback
          Action to take on each method.
static interface ReflectUtils.MethodFilter
          Callback optionally used to filter methods to be operated on by a method callback.
 
Method Summary
static void doWithFields(Class<?> clazz, ReflectUtils.FieldCallback fc)
          Invoke the given callback on all fields in the target class, going up the class hierarchy to get all declared fields.
static void doWithFields(Class<?> clazz, ReflectUtils.FieldCallback fc, ReflectUtils.FieldFilter ff)
          Invoke the given callback on all fields in the target class, going up the class hierarchy to get all declared fields.
static void doWithMethods(Class<?> clazz, ReflectUtils.MethodCallback mc)
          Perform the given callback operation on all matching methods of the given class and superclasses.
static void doWithMethods(Class<?> clazz, ReflectUtils.MethodCallback mc, ReflectUtils.MethodFilter mf)
          Perform the given callback operation on all matching methods of the given class and superclasses (or given interface and super-interfaces).
static Object get(Field field, Object object)
           
static Set<Field> getAllDeclaredFields(Class<?> theClass)
           
static Field[] getAnnotatedFields(Class<?> targetClass, Class<? extends Annotation> annotationClass)
           
static List<Method> getAnnotatedMethods(Class<?> targetClass, Class<? extends Annotation> annotationClass)
           
static
<T extends Annotation>
T
getAnnotation(Class<?> theClass, Class<T> theAnnotation)
          Return the given annotation from the class.
static Class<?> getClass(String className)
           
static
<T> Constructor<T>
getConstructor(Class<T> type, Class<?>[] parameterTypes)
           
static Field getField(Class<?> clazz, String fieldName)
          searches for the field in the given class and in its super classes
static Method getFirstMethodOfName(Class<?> clazz, String name)
           
static Method getMethod(Class<?> clazz, String methodName, Class<?>[] parameterTypes)
          searches for the method in the given class and in its super classes
static String getPackageName(String className)
           
static Object getStatic(Field field)
           
static Class<?>[] getSuperclasses(Class<?> type)
          Returns all superclasses.
static boolean hasAnnotation(Class<?> theClass, Class<? extends Annotation> theAnnotation)
          Return whether or not the class has the given annotation.
static Object invoke(Method method, Object target)
           
static Object invoke(Method method, Object target, Object[] args)
           
static Object invokeStatic(Method method)
           
static Object invokeStatic(Method method, Object[] args)
           
static boolean isAnnotatedWith(Object obj, Class<? extends Annotation> theAnnotation)
           
static boolean isBeanProperty(Method method)
          Returns true if method is a bean property.
static boolean isUserDefinedMethod(Method method)
          Returns true if method is user defined and not defined in Object class.
static String logMethod(Method method)
           
static
<T> T
newInstance(Class<T> clazz)
           
static
<T> T
newInstance(Constructor<T> constructor)
           
static
<T> T
newInstance(Constructor<T> constructor, Object[] args)
           
static
<T> T
newInstance(String className)
           
static void set(Field field, Object object, Object value)
           
static void setStatic(Field field, Object value)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getAllDeclaredFields

public static Set<Field> getAllDeclaredFields(Class<?> theClass)

getField

public static Field getField(Class<?> clazz,
                             String fieldName)
searches for the field in the given class and in its super classes


getMethod

public static Method getMethod(Class<?> clazz,
                               String methodName,
                               Class<?>[] parameterTypes)
searches for the method in the given class and in its super classes


logMethod

public static String logMethod(Method method)

getAnnotation

public static <T extends Annotation> T getAnnotation(Class<?> theClass,
                                                     Class<T> theAnnotation)
Return the given annotation from the class. If the class does not have the annotation, it's parent class and any interfaces will also be checked.

Parameters:
theClass - the class to inspect
theAnnotation - the annotation to retrieve
Returns:
the class's annotation, or it's "inherited" annotation, or null if the annotation cannot be found.

hasAnnotation

public static boolean hasAnnotation(Class<?> theClass,
                                    Class<? extends Annotation> theAnnotation)
Return whether or not the class has the given annotation. If the class itself does not have the annotation, it's super class and the interfaces it implements are checked.

Parameters:
theClass - the class to check
theAnnotation - the annotation to look for
Returns:
if the class has the annotation, or one of its parents does and it "inherited" the annotation, false otherwise

getAnnotatedMethods

public static List<Method> getAnnotatedMethods(Class<?> targetClass,
                                               Class<? extends Annotation> annotationClass)

getAnnotatedFields

public static Field[] getAnnotatedFields(Class<?> targetClass,
                                         Class<? extends Annotation> annotationClass)

isAnnotatedWith

public static boolean isAnnotatedWith(Object obj,
                                      Class<? extends Annotation> theAnnotation)

getFirstMethodOfName

public static Method getFirstMethodOfName(Class<?> clazz,
                                          String name)

getConstructor

public static <T> Constructor<T> getConstructor(Class<T> type,
                                                Class<?>[] parameterTypes)

newInstance

public static <T> T newInstance(String className)

newInstance

public static <T> T newInstance(Class<T> clazz)

newInstance

public static <T> T newInstance(Constructor<T> constructor)

newInstance

public static <T> T newInstance(Constructor<T> constructor,
                                Object[] args)

getStatic

public static Object getStatic(Field field)

get

public static Object get(Field field,
                         Object object)

setStatic

public static void setStatic(Field field,
                             Object value)

set

public static void set(Field field,
                       Object object,
                       Object value)

invokeStatic

public static Object invokeStatic(Method method)

invokeStatic

public static Object invokeStatic(Method method,
                                  Object[] args)

invoke

public static Object invoke(Method method,
                            Object target)

invoke

public static Object invoke(Method method,
                            Object target,
                            Object[] args)

doWithFields

public static void doWithFields(Class<?> clazz,
                                ReflectUtils.FieldCallback fc)
                         throws IllegalArgumentException
Invoke the given callback on all fields in the target class, going up the class hierarchy to get all declared fields.

Parameters:
clazz - the target class to analyze
fc - the callback to invoke for each field
Throws:
IllegalArgumentException

doWithFields

public static void doWithFields(Class<?> clazz,
                                ReflectUtils.FieldCallback fc,
                                ReflectUtils.FieldFilter ff)
                         throws IllegalArgumentException
Invoke the given callback on all fields in the target class, going up the class hierarchy to get all declared fields.

Parameters:
clazz - the target class to analyze
fc - the callback to invoke for each field
ff - the filter that determines the fields to apply the callback to
Throws:
IllegalArgumentException

doWithMethods

public static void doWithMethods(Class<?> clazz,
                                 ReflectUtils.MethodCallback mc)
                          throws IllegalArgumentException
Perform the given callback operation on all matching methods of the given class and superclasses.

The same named method occurring on subclass and superclass will appear twice, unless excluded by a ReflectUtils.MethodFilter.

Parameters:
clazz - class to start looking at
mc - the callback to invoke for each method
Throws:
IllegalArgumentException
See Also:
doWithMethods(Class, MethodCallback, MethodFilter)

doWithMethods

public static void doWithMethods(Class<?> clazz,
                                 ReflectUtils.MethodCallback mc,
                                 ReflectUtils.MethodFilter mf)
                          throws IllegalArgumentException
Perform the given callback operation on all matching methods of the given class and superclasses (or given interface and super-interfaces).

The same named method occurring on subclass and superclass will appear twice, unless excluded by the specified ReflectUtils.MethodFilter.

Parameters:
clazz - class to start looking at
mc - the callback to invoke for each method
mf - the filter that determines the methods to apply the callback to
Throws:
IllegalArgumentException

getSuperclasses

public static Class<?>[] getSuperclasses(Class<?> type)
Returns all superclasses.


isUserDefinedMethod

public static boolean isUserDefinedMethod(Method method)
Returns true if method is user defined and not defined in Object class.


isBeanProperty

public static boolean isBeanProperty(Method method)
Returns true if method is a bean property.


getPackageName

public static String getPackageName(String className)

getClass

public static Class<?> getClass(String className)


Copyright © 2013-2014. All Rights Reserved.