org.glassfish.jersey.internal.util
Class ReflectionHelper

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

public class ReflectionHelper
extends java.lang.Object

Utility methods for Java reflection.

Author:
Paul Sandoz

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> java.lang.Class<T>
classForName(java.lang.String name)
          Get the Class from the class name.
static
<T> java.lang.Class<T>
classForName(java.lang.String name, java.lang.ClassLoader cl)
          Get the Class from the class name.
static
<T> java.lang.Class<T>
classForNameWithException(java.lang.String name)
          Get the Class from the class name.
static
<T> java.lang.Class<T>
classForNameWithException(java.lang.String name, java.lang.ClassLoader cl)
          Get the Class from the class name.
static java.lang.reflect.Method findMethodOnClass(java.lang.Class<?> c, java.lang.reflect.Method m)
          Find a method on a class given an existing method.
static java.lang.Class<?> getArrayClass(java.lang.Class<?> c)
          Get Array class of component class.
static ReflectionHelper.DeclaringClassInterfacePair getClass(java.lang.Class<?> concrete, java.lang.Class<?> iface)
          Find the declaring class that implements or extends an interface.
static java.lang.ClassLoader getContextClassLoader()
          Get the context class loader.
static java.lang.Class<?> getDeclaringClass(java.lang.reflect.AccessibleObject ao)
          Get the declaring class of an accessible object.
static java.lang.reflect.Method getFromStringStringMethod(java.lang.Class<?> c)
          Get the static fromString(String ) method.
static java.util.List<java.lang.Class<?>> getGenericTypeArgumentClasses(java.lang.reflect.Type type)
          Get the list of classes that represent the type arguments of a parameterized input type.
static java.lang.Class[] getParameterizedClassArguments(ReflectionHelper.DeclaringClassInterfacePair p)
          Get the parameterized class arguments for a declaring class that declares a generic interface type.
static java.lang.reflect.Type[] getParameterizedTypeArguments(ReflectionHelper.DeclaringClassInterfacePair p)
          Get the parameterized type arguments for a declaring class that declares a generic interface type.
static java.lang.reflect.Constructor getStringConstructor(java.lang.Class<?> c)
          Get the constructor that has a single parameter of String.
static java.util.List<ClassTypePair> getTypeArgumentAndClass(java.lang.reflect.Type type)
          Get the list of class-type pairs that represent the type arguments of a parameterized input type.
static java.lang.reflect.Method getValueOfStringMethod(java.lang.Class<?> c)
          Get the static valueOf(String ) method.
static java.lang.String methodInstanceToString(java.lang.Object o, java.lang.reflect.Method m)
          Create a string representation of a method and an instance whose class implements the method.
static java.lang.String objectToString(java.lang.Object o)
          Create a string representation of an object.
static ClassTypePair resolveGenericType(java.lang.Class concreteClass, java.lang.Class declaringClass, java.lang.Class rawResolvedType, java.lang.reflect.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(java.lang.Class<?> c, java.lang.Class<?> dc, java.lang.reflect.TypeVariable tv)
          Given a type variable resolve the Java class of that variable.
static void setAccessibleMethod(java.lang.reflect.Method m)
          Set a method to be accessible.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getDeclaringClass

public static java.lang.Class<?> getDeclaringClass(java.lang.reflect.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:
java.lang.IllegalArgumentException - in case the type of the accessible object is not supported.

objectToString

public static java.lang.String objectToString(java.lang.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 '@', 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 java.lang.String methodInstanceToString(java.lang.Object o,
                                                      java.lang.reflect.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 '@', 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, thos 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.

classForName

public static <T> java.lang.Class<T> classForName(java.lang.String name)
Get the Class from the class name.

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 - the class name.
Returns:
the Class, otherwise null if the class cannot be found.

classForName

public static <T> java.lang.Class<T> classForName(java.lang.String name,
                                                  java.lang.ClassLoader cl)
Get the Class from the class name.

Type Parameters:
T - class type.
Parameters:
name - the class name.
cl - the class loader to use, if null then the defining class loader of this class will be utilized.
Returns:
the Class, otherwise null if the class cannot be found.

classForNameWithException

public static <T> java.lang.Class<T> classForNameWithException(java.lang.String name)
                                                    throws java.lang.ClassNotFoundException
Get the Class from the class name.

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 - the class name.
Returns:
the Class, otherwise null if the class cannot be found.
Throws:
java.lang.ClassNotFoundException - if the class cannot be found.

classForNameWithException

public static <T> java.lang.Class<T> classForNameWithException(java.lang.String name,
                                                               java.lang.ClassLoader cl)
                                                    throws java.lang.ClassNotFoundException
Get the Class from the class name.

Type Parameters:
T - class type.
Parameters:
name - the class name.
cl - the class loader to use, if null then the defining class loader of this class will be utilized.
Returns:
the Class, otherwise null if the class cannot be found.
Throws:
java.lang.ClassNotFoundException - if the class cannot be found.

getContextClassLoader

public static java.lang.ClassLoader getContextClassLoader()
Get the context class loader.

Returns:
the context class loader, otherwise null security privileges are not set.

setAccessibleMethod

public static void setAccessibleMethod(java.lang.reflect.Method m)
Set a method to be accessible.

Parameters:
m - the method to be set as accessible

getGenericTypeArgumentClasses

public static java.util.List<java.lang.Class<?>> getGenericTypeArgumentClasses(java.lang.reflect.Type type)
                                                                        throws java.lang.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:
java.lang.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 java.util.List<ClassTypePair> getTypeArgumentAndClass(java.lang.reflect.Type type)
                                                             throws java.lang.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:
java.lang.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.

getArrayClass

public static java.lang.Class<?> getArrayClass(java.lang.Class<?> c)
Get Array class of component class.

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

getValueOfStringMethod

public static java.lang.reflect.Method getValueOfStringMethod(java.lang.Class<?> c)
Get the static valueOf(String ) method.

Parameters:
c - The class to obtain the method.
Returns:
the method, otherwise null if the method is not present.

getFromStringStringMethod

public static java.lang.reflect.Method getFromStringStringMethod(java.lang.Class<?> c)
Get the static fromString(String ) method.

Parameters:
c - The class to obtain the method.
Returns:
the method, otherwise null if the method is not present.

getStringConstructor

public static java.lang.reflect.Constructor getStringConstructor(java.lang.Class<?> c)
Get the constructor that has a single parameter of String.

Parameters:
c - The class to obtain the constructor.
Returns:
the constructor, otherwise null if the constructor is not present.

getParameterizedClassArguments

public static java.lang.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 java.lang.reflect.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(java.lang.Class<?> concrete,
                                                                    java.lang.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(java.lang.Class concreteClass,
                                               java.lang.Class declaringClass,
                                               java.lang.Class rawResolvedType,
                                               java.lang.reflect.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(java.lang.Class<?> c,
                                                java.lang.Class<?> dc,
                                                java.lang.reflect.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.

findMethodOnClass

public static java.lang.reflect.Method findMethodOnClass(java.lang.Class<?> c,
                                                         java.lang.reflect.Method m)
Find a method on a class given an existing method.

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.

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.

Parameters:
c - the class to search for a public method
m - the method to find
Returns:
the found public method.


Copyright © 2007-2012 Oracle Corporation. All Rights Reserved. Use is subject to license terms.