com.sun.jersey.core.reflection
Class ReflectionHelper

java.lang.Object
  extended by com.sun.jersey.core.reflection.ReflectionHelper

public class ReflectionHelper
extends java.lang.Object

Utility methods for Java reflection.

Author:
Paul.Sandoz@Sun.Com

Nested Class Summary
static class ReflectionHelper.ClassTypePair
          A tuple consisting of a class and type of the class.
static class ReflectionHelper.DeclaringClassInterfacePair
          A tuple consisting of a concrete class, declaring class that declares a generic interface type.
 
Constructor Summary
ReflectionHelper()
           
 
Method Summary
static java.lang.Class classForName(java.lang.String name)
          Get the Class from the class name.
static java.lang.Class classForName(java.lang.String name, java.lang.ClassLoader cl)
          Get the Class from the class name.
static java.lang.Class classForNameWithException(java.lang.String name)
          Get the Class from the class name.
static java.lang.Class classForNameWithException(java.lang.String name, java.lang.ClassLoader cl)
          Get the Class from the class name.
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.reflect.Method getFromStringStringMethod(java.lang.Class c)
          Get the static fromString(String ) method.
static java.lang.Class getGenericClass(java.lang.reflect.Type parameterizedType)
          Get the class that is the type argument of a parameterized 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.lang.reflect.Method getValueOfStringMethod(java.lang.Class c)
          Get the static valueOf(String ) method.
static ReflectionHelper.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
 

Constructor Detail

ReflectionHelper

public ReflectionHelper()
Method Detail

classForName

public static java.lang.Class 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.

Parameters:
name - the class name.
Returns:
the Class, otherwise null if the class cannot be found.

classForName

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

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 java.lang.Class 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 the defining class loader of this class will be utilized.

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 java.lang.Class classForNameWithException(java.lang.String name,
                                                        java.lang.ClassLoader cl)
                                                 throws java.lang.ClassNotFoundException
Get the Class from the class name.

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 privilages 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

getGenericClass

public static java.lang.Class getGenericClass(java.lang.reflect.Type parameterizedType)
                                       throws java.lang.IllegalArgumentException
Get the class that is the type argument of a parameterized type.

Parameters:
parameterizedType - must be an instance of ParameterizedType and have exactly one type argument.
Returns:
the class of the actual type argument. If the type argument is a class then the class is returned. If the type argument is a generic array type and the generic component type is a class then class of the array is returned. If the parameterizedType is not an instance of ParameterizedType or contains more than one type argument null is returned.
Throws:
java.lang.IllegalArgumentException - if the single type argument is not of a class or a generic array type, or the generic component type of the generic array type is not 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.

resolveTypeVariable

public static ReflectionHelper.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


Copyright © 2009 Sun Microsystems, Inc. All Rights Reserved.