org.jboss.weld.util.reflection
Class SecureReflections

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

public class SecureReflections
extends Object

Author:
Nicklas Karlsson, Ales Justin

Utility class for SecurityManager aware reflection operations with the "weld.reflection" permission


Constructor Summary
SecureReflections()
           
 
Method Summary
static
<T extends AccessibleObject>
T
ensureAccessible(T accessibleObject)
          Makes an object accessible.
static Class<?>[] extractValues(Annotation annotation)
          Helper class for reading the value of an annotation
static Constructor<?> getConstructor(Class<?> clazz, Class<?>... parameterTypes)
          Gets a constructor from a class
static Constructor<?>[] getConstructors(Class<?> clazz)
          Gets all constructors from a class
static
<T> Constructor<T>
getDeclaredConstructor(Class<T> clazz, Class<?>... parameterTypes)
          Gets a declared constructor from a class
static Constructor<?>[] getDeclaredConstructors(Class<?> clazz)
          Gets all declared constructors from a class
static Field getDeclaredField(Class<?> clazz, String fieldName)
          Returns a named, declared field from a class
static Field[] getDeclaredFields(Class<?> clazz)
          Returns all declared fields of a class
static Method getDeclaredMethod(Class<?> clazz, String methodName, Class<?>... parameterTypes)
          Returns a named, declared method of a class
static Method[] getDeclaredMethods(Class<?> clazz)
          Returns all declared methods of a class
static Field getField(Class<?> clazz, String fieldName)
          Return a named field from a class
static Field[] getFields(Class<?> clazz)
          Returns all fields of a class
static Method getMethod(Class<?> clazz, String methodName, Class<?>... parameterTypes)
          Returns a named method of a class
static Method[] getMethods(Class<?> clazz)
          Returns all methods of a class
static
<T> T
invoke(Object instance, Method method, Object... parameters)
          Invokes a given method with given parameters on an instance
static
<T> T
invoke(Object instance, String methodName, Object... parameters)
          Invokes a given method with given parameters on an instance
static boolean isMethodExists(Class<?> clazz, String methodName, Class<?>... parameterTypes)
          Checks if a method is found in a class
static Method lookupMethod(Class<?> clazz, String methodName, Class<?>... parameterTypes)
          Returns a method from the class or any class/interface in the inheritance hierarchy
static Method lookupMethod(Object instance, Method method)
          Looks up a method in an inheritance hierarchy
static
<T> T
newInstance(Class<T> clazz)
          Creates a new instance of a class
static
<T> T
newUnsafeInstance(Class<T> clazz)
          Creates a new instance of a class using unportable methods, if available
 
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 Field getField(Class<?> clazz,
                             String fieldName)
                      throws 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:
NoSuchFieldException - If the field cannot be found
See Also:
java.lang.Class#getField(String))

getDeclaredField

public static Field getDeclaredField(Class<?> clazz,
                                     String fieldName)
                              throws 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:
NoSuchFieldException - If the field cannot be found
See Also:
Class.getDeclaredField(String)

getFields

public static Field[] getFields(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 Field[] getDeclaredFields(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 Method getMethod(Class<?> clazz,
                               String methodName,
                               Class<?>... parameterTypes)
                        throws 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:
NoSuchMethodException - If the method cannot be found
See Also:
Class.getMethod(String, Class...)

getDeclaredMethod

public static Method getDeclaredMethod(Class<?> clazz,
                                       String methodName,
                                       Class<?>... parameterTypes)
                                throws 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:
NoSuchMethodException - If the method cannot be found
See Also:
Class.getDeclaredMethods()

getMethods

public static Method[] getMethods(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 Method[] getDeclaredMethods(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 Constructor<?> getConstructor(Class<?> clazz,
                                            Class<?>... parameterTypes)
                                     throws NoSuchMethodException
Gets a constructor from a class

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

getDeclaredConstructor

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

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

getConstructors

public static Constructor<?>[] getConstructors(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 Constructor<?>[] getDeclaredConstructors(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(Object instance,
                           Method method,
                           Object... parameters)
                throws IllegalArgumentException,
                       IllegalAccessException,
                       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:
IllegalArgumentException - If there was an illegal argument passed
IllegalAccessException - If there was an illegal access attempt
InvocationTargetException - If there was another error invoking the method
See Also:
Method.invoke(Object, Object...)

ensureAccessible

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

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

invoke

public static <T> T invoke(Object instance,
                           String methodName,
                           Object... parameters)
                throws IllegalArgumentException,
                       IllegalAccessException,
                       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:
IllegalArgumentException - If there was an illegal argument passed
IllegalAccessException - If there was an illegal access attempt
InvocationTargetException - If there was another error invoking the method
See Also:
Method.invoke(Object, Object...)

newInstance

public static <T> T newInstance(Class<T> clazz)
                     throws InstantiationException,
                            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:
InstantiationException - If the instance could not be create
IllegalAccessException - If there was an illegal access attempt
See Also:
Class.newInstance()

newUnsafeInstance

public static <T> T newUnsafeInstance(Class<T> clazz)
                           throws InstantiationException,
                                  IllegalAccessException
Creates a new instance of a class using unportable methods, if available

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

lookupMethod

public static Method lookupMethod(Object instance,
                                  Method method)
                           throws 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:
NoSuchMethodException - if the method could not be found

lookupMethod

public static Method lookupMethod(Class<?> clazz,
                                  String methodName,
                                  Class<?>... parameterTypes)
                           throws 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:
NoSuchMethodException - If the method could not be found

extractValues

public static Class<?>[] extractValues(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(Class<?> clazz,
                                     String methodName,
                                     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 © 2013 Seam Framework. All Rights Reserved.