org.jboss.weld.util.reflection
Class SecureReflections

java.lang.Object
  extended by org.jboss.weld.util.reflection.SecureReflections

public class SecureReflections
extends java.lang.Object

Author:
Nicklas Karlsson Utility class for SecurityManager aware reflection operations with the "weld.reflection" permission

Constructor Summary
SecureReflections()
           
 
Method Summary
static
<T extends java.lang.reflect.AccessibleObject>
T
ensureAccessible(T accessibleObject)
          Makes an object accessible.
static java.lang.Class<?>[] extractValues(java.lang.annotation.Annotation annotation)
          Helper class for reading the value of an annotation
static java.lang.reflect.Constructor<?> getConstructor(java.lang.Class<?> clazz, java.lang.Class<?>... parameterTypes)
          Gets a constructor from a class
static java.lang.reflect.Constructor<?>[] getConstructors(java.lang.Class<?> clazz)
          Gets all constructors from a class
static java.lang.reflect.Constructor<?> getDeclaredConstructor(java.lang.Class<?> clazz, java.lang.Class<?>... parameterTypes)
          Gets a declared constructor from a class
static java.lang.reflect.Constructor<?>[] getDeclaredConstructors(java.lang.Class<?> clazz)
          Gets all declared constructors from a class
static java.lang.reflect.Field getDeclaredField(java.lang.Class<?> clazz, java.lang.String fieldName)
          Returns a named, declared field from a class
static java.lang.reflect.Field[] getDeclaredFields(java.lang.Class<?> clazz)
          Returns all declared fields of a class
static java.lang.reflect.Method getDeclaredMethod(java.lang.Class<?> clazz, java.lang.String methodName, java.lang.Class<?>... parameterTypes)
          Returns a named, declared method of a class
static java.lang.reflect.Method[] getDeclaredMethods(java.lang.Class<?> clazz)
          Returns all declared methods of a class
static java.lang.reflect.Field getField(java.lang.Class<?> clazz, java.lang.String fieldName)
          Return a named field from a class
static java.lang.reflect.Field[] getFields(java.lang.Class<?> clazz)
          Returns all fields of a class
static java.lang.reflect.Method getMethod(java.lang.Class<?> clazz, java.lang.String methodName, java.lang.Class<?>... parameterTypes)
          Returns a named method of a class
static java.lang.reflect.Method[] getMethods(java.lang.Class<?> clazz)
          Returns all methods of a class
static
<T> T
invoke(java.lang.Object instance, java.lang.reflect.Method method, java.lang.Object... parameters)
          Invokes a given method with given parameters on an instance
static
<T> T
invoke(java.lang.Object instance, java.lang.String methodName, java.lang.Object... parameters)
          Invokes a given method with given parameters on an instance
static boolean isMethodExists(java.lang.Class<?> clazz, java.lang.String methodName, java.lang.Class<?>... parameterTypes)
          Checks if a method is found in a class
static java.lang.reflect.Method lookupMethod(java.lang.Class<?> clazz, java.lang.String methodName, java.lang.Class<?>... parameterTypes)
          Returns a method from the class or any class/interface in the inheritance hierarchy
static java.lang.reflect.Method lookupMethod(java.lang.Object instance, java.lang.reflect.Method method)
          Looks up a method in an inheritance hierarchy
static
<T> T
newInstance(java.lang.Class<T> clazz)
          Creates a new instance of a class
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SecureReflections

public SecureReflections()
Method Detail

getField

public static java.lang.reflect.Field getField(java.lang.Class<?> clazz,
                                               java.lang.String fieldName)
                                        throws java.lang.NoSuchFieldException
Return a named field from a class

Parameters:
clazz - The class to operate on
fieldName - The name of the field
Returns:
The field
Throws:
java.lang.NoSuchFieldException - If the field cannot be found
See Also:
java.lang.Class#getField(String))

getDeclaredField

public static java.lang.reflect.Field getDeclaredField(java.lang.Class<?> clazz,
                                                       java.lang.String fieldName)
                                                throws java.lang.NoSuchFieldException
Returns a named, declared field from a class

Parameters:
clazz - The class to operate on
fieldName - The name of the field
Returns:
The field
Throws:
java.lang.NoSuchFieldException - If the field cannot be found
See Also:
Class.getDeclaredField(String)

getFields

public static java.lang.reflect.Field[] getFields(java.lang.Class<?> clazz)
Returns all fields of a class

Parameters:
clazz - The class to operate on
Returns:
The fields
See Also:
Class.getFields()

getDeclaredFields

public static java.lang.reflect.Field[] getDeclaredFields(java.lang.Class<?> clazz)
Returns all declared fields of a class

Parameters:
clazz - The class to operate on
Returns:
The fields
See Also:
Class.getDeclaredFields()

getMethod

public static java.lang.reflect.Method getMethod(java.lang.Class<?> clazz,
                                                 java.lang.String methodName,
                                                 java.lang.Class<?>... parameterTypes)
                                          throws java.lang.NoSuchMethodException
Returns a named method of a class

Parameters:
clazz - The class to operate on
methodName - The name of the method
parameterTypes - The method parameter types
Returns:
The method
Throws:
java.lang.NoSuchMethodException - If the method cannot be found
See Also:
Class.getMethod(String, Class...)

getDeclaredMethod

public static java.lang.reflect.Method getDeclaredMethod(java.lang.Class<?> clazz,
                                                         java.lang.String methodName,
                                                         java.lang.Class<?>... parameterTypes)
                                                  throws java.lang.NoSuchMethodException
Returns a named, declared method of a class

Parameters:
clazz - The class to operate on
methodName - The name of the method
parameterTypes - The method parameter types
Returns:
The method
Throws:
java.lang.NoSuchMethodException - If the method cannot be found
See Also:
Class.getDeclaredMethods()

getMethods

public static java.lang.reflect.Method[] getMethods(java.lang.Class<?> clazz)
Returns all methods of a class

Parameters:
clazz - The class to operate on
Returns:
The methods
See Also:
Class.getMethods()

getDeclaredMethods

public static java.lang.reflect.Method[] getDeclaredMethods(java.lang.Class<?> clazz)
Returns all declared methods of a class

Parameters:
clazz - The class to operate on
Returns:
The methods
See Also:
Class.getDeclaredMethods()

getConstructor

public static java.lang.reflect.Constructor<?> getConstructor(java.lang.Class<?> clazz,
                                                              java.lang.Class<?>... parameterTypes)
                                                       throws java.lang.NoSuchMethodException
Gets a constructor from a class

Parameters:
clazz - The class to operate on
parameterTypes - The constructor parameter types
Returns:
The constructor
Throws:
java.lang.NoSuchMethodException - If the constructor cannot be found
See Also:
Class.getConstructor(Class...)

getDeclaredConstructor

public static java.lang.reflect.Constructor<?> getDeclaredConstructor(java.lang.Class<?> clazz,
                                                                      java.lang.Class<?>... parameterTypes)
                                                               throws java.lang.NoSuchMethodException
Gets a declared constructor from a class

Parameters:
clazz - The class to operate on
parameterTypes - The constructor parameter types
Returns:
The constructor
Throws:
java.lang.NoSuchMethodException - If the constructor cannot be found
See Also:
Class.getDeclaredConstructor(Class...)

getConstructors

public static java.lang.reflect.Constructor<?>[] getConstructors(java.lang.Class<?> clazz)
Gets all constructors from a class

Parameters:
clazz - The class to operate on
Returns:
The constructors
See Also:
Class.getConstructors()

getDeclaredConstructors

public static java.lang.reflect.Constructor<?>[] getDeclaredConstructors(java.lang.Class<?> clazz)
Gets all declared constructors from a class

Parameters:
clazz - The class to operate on
Returns:
The constructors
See Also:
Class.getDeclaredConstructor(Class...)

invoke

public static <T> T invoke(java.lang.Object instance,
                           java.lang.reflect.Method method,
                           java.lang.Object... parameters)
                throws java.lang.IllegalArgumentException,
                       java.lang.IllegalAccessException,
                       java.lang.reflect.InvocationTargetException
Invokes a given method with given parameters on an instance

Parameters:
instance - The instance to invoke on
method - The method to invoke
parameters - The method parameters
Returns:
The return value of the method
Throws:
java.lang.IllegalArgumentException - If there was an illegal argument passed
java.lang.IllegalAccessException - If there was an illegal access attempt
java.lang.reflect.InvocationTargetException - If there was another error invoking the method
See Also:
Method.invoke(Object, Object...)

ensureAccessible

public static <T extends java.lang.reflect.AccessibleObject> T ensureAccessible(T accessibleObject)
Makes an object accessible.

Parameters:
accessibleObjects - The object to manipulate
Returns:
The accessible object

invoke

public static <T> T invoke(java.lang.Object instance,
                           java.lang.String methodName,
                           java.lang.Object... parameters)
                throws java.lang.IllegalArgumentException,
                       java.lang.IllegalAccessException,
                       java.lang.reflect.InvocationTargetException
Invokes a given method with given parameters on an instance

Parameters:
instance - The instance to invoke on
methodName - The name of the method to invoke
parameters - The method parameters
Returns:
The return value of the method
Throws:
java.lang.IllegalArgumentException - If there was an illegal argument passed
java.lang.IllegalAccessException - If there was an illegal access attempt
java.lang.reflect.InvocationTargetException - If there was another error invoking the method
See Also:
Method.invoke(Object, Object...)

newInstance

public static <T> T newInstance(java.lang.Class<T> clazz)
                     throws java.lang.InstantiationException,
                            java.lang.IllegalAccessException
Creates a new instance of a class

Type Parameters:
T - The type of the instance
Parameters:
clazz - The class to construct from
Returns:
The new instance
Throws:
java.lang.InstantiationException - If the instance could not be create
java.lang.IllegalAccessException - If there was an illegal access attempt
See Also:
Class.newInstance()

lookupMethod

public static java.lang.reflect.Method lookupMethod(java.lang.Object instance,
                                                    java.lang.reflect.Method method)
                                             throws java.lang.NoSuchMethodException
Looks up a method in an inheritance hierarchy

Parameters:
instance - The instance (class) to start from
method - The method to look up
Returns:
The method
Throws:
java.lang.NoSuchMethodException - if the method could not be found

lookupMethod

public static java.lang.reflect.Method lookupMethod(java.lang.Class<?> clazz,
                                                    java.lang.String methodName,
                                                    java.lang.Class<?>... parameterTypes)
                                             throws java.lang.NoSuchMethodException
Returns a method from the class or any class/interface in the inheritance hierarchy

Parameters:
clazz - The class to search
methodName - The method name
parameterTypes - The method parameter types
Returns:
The method
Throws:
java.lang.NoSuchMethodException - If the method could not be found

extractValues

public static java.lang.Class<?>[] extractValues(java.lang.annotation.Annotation annotation)
Helper class for reading the value of an annotation

Parameters:
annotation - The annotation to inspect
Returns:
The array of classes

isMethodExists

public static boolean isMethodExists(java.lang.Class<?> clazz,
                                     java.lang.String methodName,
                                     java.lang.Class<?>... parameterTypes)
Checks if a method is found in a class

Parameters:
clazz - The class to inspect
methodName - The name of the method
parameterTypes - The parameter types of the method
Returns:
true if method is present, false otherwise


Copyright © 2008-2010 Seam Framework. All Rights Reserved.