org.glassfish.jersey.internal.util
Class ReflectionHelper

java.lang.Object
  extended by org.glassfish.jersey.internal.util.ReflectionHelper

public class ReflectionHelper
extends Object

Utility methods for Java reflection.

Author:
Paul Sandoz (paul.sandoz at oracle.com), Jakub Podlesak (jakub.podlesak at oracle.com)

Nested Class Summary
static class ReflectionHelper.DeclaringClassInterfacePair
          A tuple consisting of a concrete class and a declaring class that declares a generic interface type.
 
Method Summary
static
<T> PrivilegedAction<Class<T>>
classForNamePA(String name)
          Get privileged action to obtain Class from given class name.
static
<T> PrivilegedAction<Class<T>>
classForNamePA(String name, ClassLoader cl)
          Get privileged action to obtain Class from given class name.
static
<T> PrivilegedExceptionAction<Class<T>>
classForNameWithExceptionPEA(String name)
          Get privileged exception action to obtain Class from given class name.
static
<T> PrivilegedExceptionAction<Class<T>>
classForNameWithExceptionPEA(String name, ClassLoader cl)
          Get privileged exception action to obtain Class from given class name.
static
<T> Class<T>
erasure(Type type)
          Get the Class representation of the given type.
static PrivilegedAction<Method> findMethodOnClassPA(Class<?> c, Method m)
          Get privileged action to find a method on a class given an existing method.
static Method findOverridingMethodOnClass(Class<?> clazz, Method method)
          Find a method that overrides the given method on the given class.
static javax.ws.rs.core.GenericType genericTypeFor(Object instance)
          Create a generic type information for a given Java instance.
static Collection<Class<? extends Annotation>> getAnnotationTypes(AnnotatedElement annotatedElement, Class<? extends Annotation> metaAnnotation)
          Returns collection of all annotation types attached to a given annotated element that have the provided meta annotation attached.
static Type getArrayComponentType(Type type)
          Gets the component type of the array.
static Class<?> getArrayForComponentType(Class<?> c)
          Get Array class of component type.
static ReflectionHelper.DeclaringClassInterfacePair getClass(Class<?> concrete, Class<?> iface)
          Find the declaring class that implements or extends an interface.
static PrivilegedAction<ClassLoader> getClassLoaderPA(Class<?> clazz)
          Get privileged action to obtain class loader for given class.
static PrivilegedAction<ClassLoader> getContextClassLoaderPA()
          Get privileged action to obtain context class loader.
static PrivilegedAction<Field[]> getDeclaredFieldsPA(Class<?> clazz)
          Get privileged action to obtain fields declared on given class.
static PrivilegedAction<Method[]> getDeclaredMethodsPA(Class<?> clazz)
          Get privileged action to obtain methods declared on given class.
static Class<?> getDeclaringClass(AccessibleObject ao)
          Get the declaring class of an accessible object.
static PrivilegedAction<Method> getFromStringStringMethodPA(Class<?> c)
          Get privileged action to get the static fromString(String ) method.
static List<Class<?>> getGenericTypeArgumentClasses(Type type)
          Get the list of classes that represent the type arguments of a parameterized input type.
static PrivilegedAction<Method[]> getMethodsPA(Class<?> c)
          Get privileged action to return an array containing Method objects reflecting all the public member methods of the supplied class or interface object, including those declared by the class or interface and those inherited from superclasses and superinterfaces.
static OsgiRegistry getOsgiRegistryInstance()
          Returns an OsgiRegistry instance.
static Class[] getParameterizedClassArguments(ReflectionHelper.DeclaringClassInterfacePair p)
          Get the parameterized class arguments for a declaring class that declares a generic interface type.
static Type[] getParameterizedTypeArguments(ReflectionHelper.DeclaringClassInterfacePair p)
          Get the parameterized type arguments for a declaring class that declares a generic interface type.
static String getPropertyName(Method method)
          Determine property (field) name from given getter/setter method.
static InputStream getResourceAsStream(ClassLoader loader, Class<?> originClass, String name)
          Lookup resource by given name.
static PrivilegedAction<Constructor> getStringConstructorPA(Class<?> c)
          Get privileged action to obtain constructor that has a single parameter of String.
static Type getTypeArgument(Type type, int index)
          Get a type argument at particular index for a parameterized type.
static List<ClassTypePair> getTypeArgumentAndClass(Type type)
          Get the list of class-type pairs that represent the type arguments of a parameterized input type.
static Type[] getTypeArguments(Type type)
          Get the type arguments for a parameterized type.
static PrivilegedAction<Method> getValueOfStringMethodPA(Class<?> c)
          Get privileged action to obtain the static valueOf(String ) method.
static boolean isArray(Type type)
          Checks if the type is an array type.
static boolean isArrayOfType(Type type, Class<?> componentType)
          Checks if the type is an array of a given component type.
static boolean isGetter(Method method)
          Determine whether a given method is getter.
static boolean isPrimitive(Type type)
          Check if the given type is a primitive type.
static boolean isSetter(Method method)
          Determine whether a given method is setter.
static boolean isSubClassOf(Type subType, Type superType)
          Check if subType is a sub-type of superType.
static String methodInstanceToString(Object o, Method m)
          Create a string representation of a method and an instance whose class implements the method.
static String objectToString(Object o)
          Create a string representation of an object.
static ClassTypePair resolveGenericType(Class concreteClass, Class declaringClass, Class rawResolvedType, Type genericResolvedType)
          Resolve generic type parameter(s) of a raw class and it's generic type based on the class that declares the generic type parameter(s) to be resolved and a concrete implementation of the declaring class.
static ClassTypePair resolveTypeVariable(Class<?> c, Class<?> dc, TypeVariable tv)
          Given a type variable resolve the Java class of that variable.
static PrivilegedAction setAccessibleMethodPA(Method m)
          Get privileged action to set a method to be accessible.
static PrivilegedAction setContextClassLoaderPA(ClassLoader classLoader)
          Get privileged action to set the actual context class loader.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getDeclaringClass

public static Class<?> getDeclaringClass(AccessibleObject ao)
Get the declaring class of an accessible object. Supported are Method, Field and Constructor accessible object types.

Parameters:
ao - an accessible object.
Returns:
the declaring class of an accessible object.
Throws:
IllegalArgumentException - in case the type of the accessible object is not supported.

objectToString

public static String objectToString(Object o)
Create a string representation of an object.

Returns a string consisting of the name of the class of which the object is an instance, the at-sign character '&#64;', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:

 o.getClass().getName() + '@' + Integer.toHexString(o.hashCode())
 

Parameters:
o - the object.
Returns:
the string representation of the object.

methodInstanceToString

public static String methodInstanceToString(Object o,
                                            Method m)
Create a string representation of a method and an instance whose class implements the method.

Returns a string consisting of the name of the class of which the object is an instance, the at-sign character '&#64;', the unsigned hexadecimal representation of the hash code of the object, the character '.', the name of the method, the character '(', the list of method parameters, and the character ')'. In other words, those method returns a string equal to the value of:

 o.getClass().getName() + '@' + Integer.toHexString(o.hashCode()) +
         '.' + m.getName() + '(' + <parameters> + ')'.

Parameters:
o - the object whose class implements m.
m - the method.
Returns:
the string representation of the method and instance.

classForNamePA

public static <T> PrivilegedAction<Class<T>> classForNamePA(String name)
Get privileged action to obtain Class from given class name. If run using security manager, the returned privileged action must be invoked within a doPrivileged block.

The context class loader will be utilized if accessible and non-null. Otherwise the defining class loader of this class will be utilized.

Type Parameters:
T - class type.
Parameters:
name - class name.
Returns:
privileged action to obtain desired Class. The action could return null if the class cannot be found.
See Also:
AccessController.doPrivileged(java.security.PrivilegedAction)

classForNamePA

public static <T> PrivilegedAction<Class<T>> classForNamePA(String name,
                                                            ClassLoader cl)
Get privileged action to obtain Class from given class name. If run using security manager, the returned privileged action must be invoked within a doPrivileged block.

Type Parameters:
T - class type.
Parameters:
name - class name.
cl - class loader to use, if null then the defining class loader of this class will be utilized.
Returns:
privileged action to obtain desired Class. The action could return null if the class cannot be found.
See Also:
AccessController.doPrivileged(java.security.PrivilegedAction)

getClassLoaderPA

public static PrivilegedAction<ClassLoader> getClassLoaderPA(Class<?> clazz)
Get privileged action to obtain class loader for given class. If run using security manager, the returned privileged action must be invoked within a doPrivileged block.

Parameters:
clazz - class for which to get class loader.
Returns:
privileged action to obtain class loader for the clazz class.
See Also:
AccessController.doPrivileged(java.security.PrivilegedAction)

getDeclaredFieldsPA

public static PrivilegedAction<Field[]> getDeclaredFieldsPA(Class<?> clazz)
Get privileged action to obtain fields declared on given class. If run using security manager, the returned privileged action must be invoked within a doPrivileged block.

Parameters:
clazz - class for which to get the declared fields.
Returns:
privileged action to obtain fields declared on the clazz class.
See Also:
AccessController.doPrivileged(java.security.PrivilegedAction)

getDeclaredMethodsPA

public static PrivilegedAction<Method[]> getDeclaredMethodsPA(Class<?> clazz)
Get privileged action to obtain methods declared on given class. If run using security manager, the returned privileged action must be invoked within a doPrivileged block.

Parameters:
clazz - class for which to get the declared methods.
Returns:
privileged action to obtain methods declared on the clazz class.
See Also:
AccessController.doPrivileged(java.security.PrivilegedAction)

classForNameWithExceptionPEA

public static <T> PrivilegedExceptionAction<Class<T>> classForNameWithExceptionPEA(String name)
                                                                        throws ClassNotFoundException
Get privileged exception action to obtain Class from given class name. If run using security manager, the returned privileged exception action must be invoked within a doPrivileged block.

The actual context class loader will be utilized if accessible and non-null. Otherwise the defining class loader of the calling class will be utilized.

Type Parameters:
T - class type.
Parameters:
name - class name.
Returns:
privileged exception action to obtain the Class. The action could throw ClassNotFoundException or return null if the class cannot be found.
Throws:
ClassNotFoundException
See Also:
AccessController.doPrivileged(java.security.PrivilegedExceptionAction)

classForNameWithExceptionPEA

public static <T> PrivilegedExceptionAction<Class<T>> classForNameWithExceptionPEA(String name,
                                                                                   ClassLoader cl)
                                                                        throws ClassNotFoundException
Get privileged exception action to obtain Class from given class name. If run using security manager, the returned privileged exception action must be invoked within a doPrivileged block.

Type Parameters:
T - class type.
Parameters:
name - class name.
cl - class loader to use, if null then the defining class loader of the calling class will be utilized.
Returns:
privileged exception action to obtain the Class. The action throws ClassNotFoundException or returns null if the class cannot be found.
Throws:
ClassNotFoundException
See Also:
AccessController.doPrivileged(java.security.PrivilegedExceptionAction)

getContextClassLoaderPA

public static PrivilegedAction<ClassLoader> getContextClassLoaderPA()
Get privileged action to obtain context class loader. If run using security manager, the returned privileged action must be invoked within a doPrivileged block.

Returns:
privileged action to obtain the actual context class loader. The action could return null if context class loader has not been set.
See Also:
AccessController.doPrivileged(java.security.PrivilegedAction)

setContextClassLoaderPA

public static PrivilegedAction setContextClassLoaderPA(ClassLoader classLoader)
Get privileged action to set the actual context class loader. If run using security manager, the returned privileged action must be invoked within a doPrivileged block.

Parameters:
classLoader - context class loader to be set.
Returns:
privileged action to set context class loader.
See Also:
AccessController.doPrivileged(java.security.PrivilegedAction)

setAccessibleMethodPA

public static PrivilegedAction setAccessibleMethodPA(Method m)
Get privileged action to set a method to be accessible. If run using security manager, the returned privileged action must be invoked within a doPrivileged block.

Parameters:
m - method to be set as accessible.
Returns:
privileged action to set the method to be accessible.
See Also:
AccessController.doPrivileged(java.security.PrivilegedAction)

getGenericTypeArgumentClasses

public static List<Class<?>> getGenericTypeArgumentClasses(Type type)
                                                    throws IllegalArgumentException
Get the list of classes that represent the type arguments of a parameterized input type.

For any given argument in the returned list, following rules apply:

If the type is not an instance of ParameterizedType an empty list is returned.

Parameters:
type - parameterized type.
Returns:
the list of classed representing the actual type arguments. May be empty, but may never be null.
Throws:
IllegalArgumentException - if any of the generic type arguments is not a class, or a generic array type, or the generic component type of the generic array type is not class, or not a parameterized type with a raw type that is not a class.

getTypeArgumentAndClass

public static List<ClassTypePair> getTypeArgumentAndClass(Type type)
                                                   throws IllegalArgumentException
Get the list of class-type pairs that represent the type arguments of a parameterized input type.

For any given class part of each pair in the returned list, following rules apply:

If the type is not an instance of ParameterizedType an empty list is returned.

Parameters:
type - parameterized type.
Returns:
the list of class-type pairs representing the actual type arguments. May be empty, but may never be null.
Throws:
IllegalArgumentException - if any of the generic type arguments is not a class, or a generic array type, or the generic component type of the generic array type is not class, or not a parameterized type with a raw type that is not a class.

isPrimitive

public static boolean isPrimitive(Type type)
Check if the given type is a primitive type.

Parameters:
type - type to be checked.

getTypeArguments

public static Type[] getTypeArguments(Type type)
Get the type arguments for a parameterized type.

In case the type is not a parameterized type, the method returns null.

Parameters:
type - parameterized type.
Returns:
type arguments for a parameterized type, or null in case the input type is not a parameterized type.

getTypeArgument

public static Type getTypeArgument(Type type,
                                   int index)
Get a type argument at particular index for a parameterized type.

In case the type is not a parameterized type, the method returns null.

Parameters:
type - parameterized type.
index - type parameter index.
Returns:
type argument for a parameterized type at a given index, or null in case the input type is not a parameterized type.

erasure

public static <T> Class<T> erasure(Type type)
Get the Class representation of the given type.

This corresponds to the notion of the erasure in JSR-14.

Parameters:
type - type to provide the erasure for.
Returns:
the given type's erasure.

isSubClassOf

public static boolean isSubClassOf(Type subType,
                                   Type superType)
Check if subType is a sub-type of superType.

Parameters:
subType - sub-type type.
superType - super-type type.
Returns:
true in case the subType is a sub-type of superType, false otherwise.

isArray

public static boolean isArray(Type type)
Checks if the type is an array type.

Parameters:
type - type to check.
Returns:
true in case the type is an array type, false otherwise.

isArrayOfType

public static boolean isArrayOfType(Type type,
                                    Class<?> componentType)
Checks if the type is an array of a given component type.

Parameters:
type - type to check.
componentType - array component type.
Returns:
true in case the type is an array type of a given component type, false otherwise.

getArrayComponentType

public static Type getArrayComponentType(Type type)
Gets the component type of the array.

Parameters:
type - must be an array.
Returns:
array component type.
Throws:
IllegalArgumentException - in case the type is not an array type.

getArrayForComponentType

public static Class<?> getArrayForComponentType(Class<?> c)
Get Array class of component type.

Parameters:
c - the component class of the array
Returns:
the array class.

getValueOfStringMethodPA

public static PrivilegedAction<Method> getValueOfStringMethodPA(Class<?> c)
Get privileged action to obtain the static valueOf(String ) method. If run using security manager, the returned privileged action must be invoked within a doPrivileged block.

Parameters:
c - The class to obtain the method.
Returns:
privileged action to get the method. The action could return null if the method is not present.
See Also:
AccessController.doPrivileged(java.security.PrivilegedAction)

getFromStringStringMethodPA

public static PrivilegedAction<Method> getFromStringStringMethodPA(Class<?> c)
Get privileged action to get the static fromString(String ) method. If run using security manager, the returned privileged action must be invoked within a doPrivileged block.

Parameters:
c - class for which to get the method.
Returns:
privileged action to obtain the method. The action could return null if the method is not present.
See Also:
AccessController.doPrivileged(java.security.PrivilegedAction)

getStringConstructorPA

public static PrivilegedAction<Constructor> getStringConstructorPA(Class<?> c)
Get privileged action to obtain constructor that has a single parameter of String. If run using security manager, the returned privileged action must be invoked within a doPrivileged block.

Parameters:
c - The class for which to obtain the constructor.
Returns:
privileged action to obtain the constructor. The action could return null if the constructor is not present.
See Also:
AccessController.doPrivileged(java.security.PrivilegedAction)

getAnnotationTypes

public static Collection<Class<? extends Annotation>> getAnnotationTypes(AnnotatedElement annotatedElement,
                                                                         Class<? extends Annotation> metaAnnotation)
Returns collection of all annotation types attached to a given annotated element that have the provided meta annotation attached.

Parameters:
annotatedElement - annotated element.
metaAnnotation - meta annotation attached to the annotation types we are looking for (if null, annotation types of all attached annotations will be returned).
Returns:
list of annotation types with a given meta annotation

isGetter

public static boolean isGetter(Method method)
Determine whether a given method is getter.

Parameters:
method - method to be examined.
Returns:
true if the method is getter, false otherwise.

genericTypeFor

public static javax.ws.rs.core.GenericType genericTypeFor(Object instance)
Create a generic type information for a given Java instance.

If the supplied instance is an instance of GenericEntity, the generic type information will be computed using the GenericEntity.getType() information. Otherwise the instance.getClass() will be used.

Parameters:
instance - Java instance for which the GenericType description should be created.
Returns:
GenericType describing the Java instance.

isSetter

public static boolean isSetter(Method method)
Determine whether a given method is setter.

Parameters:
method - method to be examined.
Returns:
true if the method is setter, false otherwise.

getPropertyName

public static String getPropertyName(Method method)
Determine property (field) name from given getter/setter method.

Parameters:
method - method to be examined.
Returns:
property (field) name.

getParameterizedClassArguments

public static Class[] getParameterizedClassArguments(ReflectionHelper.DeclaringClassInterfacePair p)
Get the parameterized class arguments for a declaring class that declares a generic interface type.

Parameters:
p - the declaring class
Returns:
the parameterized class arguments, or null if the generic interface type is not a parameterized type.

getParameterizedTypeArguments

public static Type[] getParameterizedTypeArguments(ReflectionHelper.DeclaringClassInterfacePair p)
Get the parameterized type arguments for a declaring class that declares a generic interface type.

Parameters:
p - the declaring class
Returns:
the parameterized type arguments, or null if the generic interface type is not a parameterized type.

getClass

public static ReflectionHelper.DeclaringClassInterfacePair getClass(Class<?> concrete,
                                                                    Class<?> iface)
Find the declaring class that implements or extends an interface.

Parameters:
concrete - the concrete class than directly or indirectly implements or extends an interface class.
iface - the interface class.
Returns:
the tuple of the declaring class and the generic interface type.

resolveGenericType

public static ClassTypePair resolveGenericType(Class concreteClass,
                                               Class declaringClass,
                                               Class rawResolvedType,
                                               Type genericResolvedType)
Resolve generic type parameter(s) of a raw class and it's generic type based on the class that declares the generic type parameter(s) to be resolved and a concrete implementation of the declaring class.

Parameters:
concreteClass - concrete implementation of the declaring class.
declaringClass - class declaring the generic type parameter(s) to be resolved.
rawResolvedType - raw class of the generic type to be resolved.
genericResolvedType - generic type information of th type to be resolved.
Returns:
a pair of class and the generic type values with the the resolved generic parameter types.

resolveTypeVariable

public static ClassTypePair resolveTypeVariable(Class<?> c,
                                                Class<?> dc,
                                                TypeVariable tv)
Given a type variable resolve the Java class of that variable.

Parameters:
c - the concrete class from which all type variables are resolved.
dc - the declaring class where the type variable was defined.
tv - the type variable.
Returns:
the resolved Java class and type, otherwise null if the type variable could not be resolved.

findMethodOnClassPA

public static PrivilegedAction<Method> findMethodOnClassPA(Class<?> c,
                                                           Method m)
Get privileged action to find a method on a class given an existing method. If run using security manager, the returned privileged action must be invoked within a doPrivileged block.

If there exists a public method on the class that has the same name and parameters as the existing method then that public method is returned from the action.

Otherwise, if there exists a public method on the class that has the same name and the same number of parameters as the existing method, and each generic parameter type, in order, of the public method is equal to the generic parameter type, in the same order, of the existing method or is an instance of TypeVariable then that public method is returned from the action.

Parameters:
c - the class to search for a public method
m - the method to find
Returns:
privileged action to return public method found.
See Also:
AccessController.doPrivileged(java.security.PrivilegedAction)

getMethodsPA

public static PrivilegedAction<Method[]> getMethodsPA(Class<?> c)
Get privileged action to return an array containing Method objects reflecting all the public member methods of the supplied class or interface object, including those declared by the class or interface and those inherited from superclasses and superinterfaces. Array classes return all the (public) member methods inherited from the Object class. The elements in the array returned are not sorted and are not in any particular order. This method returns action providing an array of length 0 if this Class object represents a class or interface that has no public member methods, or if this Class object represents a primitive type or void.

The class initialization method <clinit> is not included in the returned array. If the class declares multiple public member methods with the same parameter types, they are all included in the returned array.

See The Java Language Specification, sections 8.2 and 8.4.

Parameters:
c - class for which the methods should be returned.
Returns:
privileged action to obtain an array of Method objects representing the public methods of the class.
See Also:
AccessController.doPrivileged(java.security.PrivilegedAction)

findOverridingMethodOnClass

public static Method findOverridingMethodOnClass(Class<?> clazz,
                                                 Method method)
Find a method that overrides the given method on the given class.

Parameters:
clazz - class to find overriding method on.
method - an abstract method to find implementing method for.
Returns:
method that overrides the given method or the given method itself if a better alternative cannot be found.

getOsgiRegistryInstance

public static OsgiRegistry getOsgiRegistryInstance()
Returns an OsgiRegistry instance.

Returns:
an OsgiRegistry instance or null if the class cannot be instantiated (not in OSGi environment).

getResourceAsStream

public static InputStream getResourceAsStream(ClassLoader loader,
                                              Class<?> originClass,
                                              String name)
Lookup resource by given name. If OSGi runtime is detected and the originClass parameter is not null, an attempt will be made to get the resource input stream via OSGi API from the bundle where originClass is included. Otherwise (non OSGi environment) or if OSGi fails to provide the input stream, the return value will be taken from the provided loader getResourceAsStream method.

Parameters:
loader - class loader where to lookup the resource in non-OSGi environment or if OSGi means fail.
originClass - if not null, and OSGi environment is detected, the resource will be taken from the bundle including the originClass type.
name - filename of the desired resource.
Returns:
an input stream corresponding to the required resource or null if the resource could not be found.


Copyright © 2007-2014, Oracle and/or its affiliates. All Rights Reserved. Use is subject to license terms.