org.jboss.webbeans.util
Class Reflections

java.lang.Object
  extended by org.jboss.webbeans.util.Reflections

public class Reflections
extends java.lang.Object

Utility class for static reflection-type operations

Author:
Pete Muir

Constructor Summary
Reflections()
           
 
Method Summary
static boolean annotationSetMatches(java.util.Set<java.lang.annotation.Annotation> annotations, java.lang.Class<? extends java.lang.annotation.Annotation>... annotationTypes)
          Checks if all annotations are in a given set of annotations
static boolean annotationTypeSetMatches(java.util.Set<java.lang.Class<? extends java.lang.annotation.Annotation>> annotations, java.lang.Class<? extends java.lang.annotation.Annotation>... annotationTypes)
          Checks if all annotations types are in a given set of annotations
static java.lang.Class<?> classForName(java.lang.String name)
          Creates an instance from a class name
static java.lang.Class<?> classForName(java.lang.String name, java.lang.ClassLoader classLoader)
          Creates an instance from a class name
static java.lang.reflect.Type[] getActualTypeArguments(java.lang.Class<?> clazz)
          Gets the actual type arguments of a class
static java.lang.Object getAndWrap(java.lang.reflect.Field field, java.lang.Object target)
          Gets value of a field and wraps exceptions
static
<T> java.util.List<java.lang.reflect.Constructor<T>>
getAnnotatedConstructors(java.lang.Class<? extends T> clazz, java.lang.Class<? extends java.lang.annotation.Annotation> annotationType)
          Gets all constructors with a given annotation
static
<T> java.lang.reflect.Constructor<T>
getConstructor(java.lang.Class<T> clazz, java.lang.Class<?>... parameterTypes)
          Gets a constructor with matching parameter types
static
<T> java.util.List<java.lang.reflect.Constructor<T>>
getConstructorsForAnnotatedParameter(java.lang.Class<? extends T> clazz, java.lang.Class<? extends java.lang.annotation.Annotation> parameterAnnotationType)
          Gets constructors with a given annotated parameter
static
<T> java.util.List<java.lang.reflect.Constructor<T>>
getConstructorsForMetaAnnotatedParameter(java.lang.Class<? extends T> clazz, java.lang.Class<? extends java.lang.annotation.Annotation> metaAnnotationType)
          Gets constructors with a given meta-annotated parameter
static java.util.List<java.lang.reflect.Method> getMethods(java.lang.Class<?> clazz, java.lang.Class<? extends java.lang.annotation.Annotation> annotationType)
          Gets all methods with a given annotation
static java.lang.String getPropertyName(java.lang.reflect.Method method)
          Gets the property name from a getter method
static java.util.Set<java.lang.Class<?>> getTypeHierachy(java.lang.Class<?> clazz)
          Gets the type hierarchy for a class A recursive function that adds the class to the set of type and then calls itself with the suprerclass as paramater until the top of the hierarchy is reached.
static java.lang.Object invokeAndWrap(java.lang.reflect.Method method, java.lang.Object instance, java.lang.Object... parameters)
          Invokes a method and wraps exceptions
static java.lang.Object invokeAndWrap(java.lang.String methodName, java.lang.Class<?>[] parameterTypes, java.lang.Object instance, java.lang.Object[] parameterValues)
          Invokes a method and wraps exceptions
static boolean isAbstract(java.lang.Class<?> clazz)
          Checks if clazz is abstract
static boolean isArrayType(java.lang.Class<?> rawType)
          Checks if raw type is array type
static boolean isBindingType(java.lang.annotation.Annotation bindingType)
          Checks the bindingType to make sure the annotation was declared properly as a binding type (annotated with @BindingType).
static boolean isFinal(java.lang.Class<?> clazz)
          Checks if class is final
static boolean isFinal(java.lang.reflect.Member member)
          Checks if member is final
static boolean isNonStaticInnerClass(java.lang.Class<?> clazz)
          Checks if class is a non-static inner one
static boolean isParameterizedType(java.lang.Class<?> type)
          Checks if type is parameterized type
static boolean isPrimitive(java.lang.Class<?> type)
          Checks if type is primitive
static boolean isProxy(java.lang.Object instance)
          Indicates if an instance is a Javassist proxy
static boolean isStatic(java.lang.Class<?> type)
          Checks if type is static
static boolean isStatic(java.lang.reflect.Member member)
          Checks if member is static
static boolean isStaticInnerClass(java.lang.Class<?> clazz)
          Checks if class is a static inner one
static boolean isTypeOrAnyMethodFinal(java.lang.Class<?> type)
          Checks if type or member is final
static java.lang.reflect.Method lookupMethod(java.lang.reflect.Method method, java.lang.Object instance)
          Looks up a method in the type hierarchy of an instance
static void setAndWrap(java.lang.reflect.Field field, java.lang.Object target, java.lang.Object value)
          Sets value of a field and wraps exceptions
static void setAndWrap(java.lang.String fieldName, java.lang.Object target, java.lang.Object value)
          Sets value of a field and wraps exceptions
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Reflections

public Reflections()
Method Detail

classForName

public static java.lang.Class<?> classForName(java.lang.String name)
                                       throws java.lang.ClassNotFoundException
Creates an instance from a class name

Parameters:
name - The class name
Returns:
The instance
Throws:
java.lang.ClassNotFoundException - If the class if not found

classForName

public static java.lang.Class<?> classForName(java.lang.String name,
                                              java.lang.ClassLoader classLoader)
                                       throws java.lang.ClassNotFoundException
Creates an instance from a class name

Parameters:
name - The class name
Returns:
The instance
Throws:
java.lang.ClassNotFoundException - If the class if not found

getPropertyName

public static java.lang.String getPropertyName(java.lang.reflect.Method method)
Gets the property name from a getter method

Parameters:
method - The getter method
Returns:
The name of the property. Returns null if method wasn't JavaBean getter-styled

isFinal

public static boolean isFinal(java.lang.Class<?> clazz)
Checks if class is final

Parameters:
clazz - The class to check
Returns:
True if final, false otherwise

isFinal

public static boolean isFinal(java.lang.reflect.Member member)
Checks if member is final

Parameters:
member - The member to check
Returns:
True if final, false otherwise

isTypeOrAnyMethodFinal

public static boolean isTypeOrAnyMethodFinal(java.lang.Class<?> type)
Checks if type or member is final

Parameters:
type - Type or member
Returns:
True if final, false otherwise

isPrimitive

public static boolean isPrimitive(java.lang.Class<?> type)
Checks if type is primitive

Parameters:
type - Type to check
Returns:
True if primitive, false otherwise

isStatic

public static boolean isStatic(java.lang.Class<?> type)
Checks if type is static

Parameters:
type - Type to check
Returns:
True if static, false otherwise

isStatic

public static boolean isStatic(java.lang.reflect.Member member)
Checks if member is static

Parameters:
member - Member to check
Returns:
True if static, false otherwise

isAbstract

public static boolean isAbstract(java.lang.Class<?> clazz)
Checks if clazz is abstract

Parameters:
clazz - Class to check
Returns:
True if abstract, false otherwise

isStaticInnerClass

public static boolean isStaticInnerClass(java.lang.Class<?> clazz)
Checks if class is a static inner one

Parameters:
clazz - Class to check
Returns:
True if static, false otherwise

isNonStaticInnerClass

public static boolean isNonStaticInnerClass(java.lang.Class<?> clazz)
Checks if class is a non-static inner one

Parameters:
clazz - Class to Check
Returns:
True if static, false otherwise

getConstructor

public static <T> java.lang.reflect.Constructor<T> getConstructor(java.lang.Class<T> clazz,
                                                                  java.lang.Class<?>... parameterTypes)
Gets a constructor with matching parameter types

Type Parameters:
T - The type
Parameters:
clazz - The class
parameterTypes - The parameter types
Returns:
The matching constructor. Null is returned if none is found

getMethods

public static java.util.List<java.lang.reflect.Method> getMethods(java.lang.Class<?> clazz,
                                                                  java.lang.Class<? extends java.lang.annotation.Annotation> annotationType)
Gets all methods with a given annotation

Parameters:
clazz - The class the examine
annotationType - The annotation type to search for
Returns:
A list of matching methods. An empty list is returned if no matches are found

getAnnotatedConstructors

public static <T> java.util.List<java.lang.reflect.Constructor<T>> getAnnotatedConstructors(java.lang.Class<? extends T> clazz,
                                                                                            java.lang.Class<? extends java.lang.annotation.Annotation> annotationType)
Gets all constructors with a given annotation

Type Parameters:
T - The type of the class
Parameters:
clazz - The class
annotationType - The annotation type
Returns:
A list of matching constructors. An empty list is returned if no matches are found

getConstructorsForAnnotatedParameter

public static <T> java.util.List<java.lang.reflect.Constructor<T>> getConstructorsForAnnotatedParameter(java.lang.Class<? extends T> clazz,
                                                                                                        java.lang.Class<? extends java.lang.annotation.Annotation> parameterAnnotationType)
Gets constructors with a given annotated parameter

Type Parameters:
T - The type
Parameters:
clazz - The class
parameterAnnotationType - The parameter annotation type
Returns:
A list of matching constructors. An empty list is returned if no matches are found

getConstructorsForMetaAnnotatedParameter

public static <T> java.util.List<java.lang.reflect.Constructor<T>> getConstructorsForMetaAnnotatedParameter(java.lang.Class<? extends T> clazz,
                                                                                                            java.lang.Class<? extends java.lang.annotation.Annotation> metaAnnotationType)
Gets constructors with a given meta-annotated parameter

Type Parameters:
T - The type
Parameters:
clazz - The class
metaAnnotationType - The parameter meta-annotation type
Returns:
A list of matching constructors. An empty list is returned if no matches are found

annotationTypeSetMatches

public static boolean annotationTypeSetMatches(java.util.Set<java.lang.Class<? extends java.lang.annotation.Annotation>> annotations,
                                               java.lang.Class<? extends java.lang.annotation.Annotation>... annotationTypes)
Checks if all annotations types are in a given set of annotations

Parameters:
annotations - The annotation set
annotationTypes - The annotation types to match
Returns:
True if match, false otherwise

annotationSetMatches

public static boolean annotationSetMatches(java.util.Set<java.lang.annotation.Annotation> annotations,
                                           java.lang.Class<? extends java.lang.annotation.Annotation>... annotationTypes)
Checks if all annotations are in a given set of annotations

Parameters:
annotations - The annotation set
annotationTypes - The annotations to match
Returns:
True if match, false otherwise

getActualTypeArguments

public static java.lang.reflect.Type[] getActualTypeArguments(java.lang.Class<?> clazz)
Gets the actual type arguments of a class

Parameters:
clazz - The class to examine
Returns:
The type arguments

isArrayType

public static boolean isArrayType(java.lang.Class<?> rawType)
Checks if raw type is array type

Parameters:
rawType - The raw type to check
Returns:
True if array, false otherwise

isParameterizedType

public static boolean isParameterizedType(java.lang.Class<?> type)
Checks if type is parameterized type

Parameters:
type - The type to check
Returns:
True if parameterized, false otherwise

invokeAndWrap

public static java.lang.Object invokeAndWrap(java.lang.reflect.Method method,
                                             java.lang.Object instance,
                                             java.lang.Object... parameters)
Invokes a method and wraps exceptions

Parameters:
method - The method to invoke
instance - The instance to invoke on
parameters - The parameters
Returns:
The return value

invokeAndWrap

public static java.lang.Object invokeAndWrap(java.lang.String methodName,
                                             java.lang.Class<?>[] parameterTypes,
                                             java.lang.Object instance,
                                             java.lang.Object[] parameterValues)
Invokes a method and wraps exceptions

Parameters:
methodName - The method name to find on the instance and invoke
parameterTypes - The method name to find on the instance and invoke
instance - The instance to invoke on
parameterValues - The parameters values
Returns:
The return value

setAndWrap

public static void setAndWrap(java.lang.reflect.Field field,
                              java.lang.Object target,
                              java.lang.Object value)
Sets value of a field and wraps exceptions

Parameters:
field - The field to set on
target - The instance to set on
value - The value to set

setAndWrap

public static void setAndWrap(java.lang.String fieldName,
                              java.lang.Object target,
                              java.lang.Object value)
Sets value of a field and wraps exceptions

Parameters:
field - The field to set on
target - The instance to set on
value - The value to set

getAndWrap

public static java.lang.Object getAndWrap(java.lang.reflect.Field field,
                                          java.lang.Object target)
Gets value of a field and wraps exceptions

Parameters:
field - The field to set on
target - The instance to set on
Returns:
The value to set

lookupMethod

public static java.lang.reflect.Method lookupMethod(java.lang.reflect.Method method,
                                                    java.lang.Object instance)
Looks up a method in the type hierarchy of an instance

Parameters:
method - The method to look for
instance - The instance to start from
Returns:
The method found, or an NoSuchMethodException if it is not found

isProxy

public static boolean isProxy(java.lang.Object instance)
Indicates if an instance is a Javassist proxy

Parameters:
instance - The instance to examine
Returns:
True if proxy, false otherwise

getTypeHierachy

public static java.util.Set<java.lang.Class<?>> getTypeHierachy(java.lang.Class<?> clazz)
Gets the type hierarchy for a class A recursive function that adds the class to the set of type and then calls itself with the suprerclass as paramater until the top of the hierarchy is reached. For each steps, adds all interfaces of the class to the set. Since the data structure is a set, duplications are eliminated

Parameters:
clazz - The class to examine
Returns:
The set of classes and interfaces in the hierarchy

isBindingType

public static boolean isBindingType(java.lang.annotation.Annotation bindingType)
Checks the bindingType to make sure the annotation was declared properly as a binding type (annotated with @BindingType).

Parameters:
bindingType - The binding type to check
Returns:
true only if the annotation is really a binding type


Copyright © 2008. All Rights Reserved.