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.
static class ReflectionHelper.TypeClassPair
           
 
Constructor Summary
ReflectionHelper()
           
 
Method Summary
static java.security.PrivilegedAction<java.lang.Class<?>> classForNamePA(java.lang.String name)
          Get privileged action to obtain Class from given class name.
static java.security.PrivilegedAction<java.lang.Class<?>> classForNamePA(java.lang.String name, java.lang.ClassLoader cl)
          Get privileged action to obtain Class from given class name.
static
<T> java.security.PrivilegedExceptionAction<java.lang.Class<T>>
classForNameWithExceptionPEA(java.lang.String name)
          Get privileged exception action to obtain Class from given class name.
static
<T> java.security.PrivilegedExceptionAction<java.lang.Class<T>>
classForNameWithExceptionPEA(java.lang.String name, java.lang.ClassLoader cl)
          Get privileged exception action to obtain Class from given class name.
static java.security.PrivilegedAction<java.lang.reflect.Method> findMethodOnClassPA(java.lang.Class<?> c, java.lang.reflect.Method m)
          Get privileged action to 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.security.PrivilegedAction<java.lang.ClassLoader> getContextClassLoaderPA()
          Get privileged action to obtain context class loader.
static java.lang.Class getDeclaringClass(java.lang.reflect.AccessibleObject ao)
           
static java.security.PrivilegedAction<java.lang.reflect.Method> getFromStringStringMethodPA(java.lang.Class<?> c)
          Get privileged action to 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 OsgiRegistry getOsgiRegistryInstance()
          Returns an OsgiRegistry instance.
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.security.PrivilegedAction<java.lang.reflect.Constructor> getStringConstructorPA(java.lang.Class<?> c)
          Get privileged action to obtain constructor that has a single parameter of String.
static ReflectionHelper.TypeClassPair getTypeArgumentAndClass(java.lang.reflect.Type parameterizedType)
           
static java.security.PrivilegedAction<java.lang.reflect.Method> getValueOfStringMethodPA(java.lang.Class<?> c)
          Get privileged action to obtain 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 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 java.security.PrivilegedAction setAccessibleMethodPA(java.lang.reflect.Method m)
          Get privileged action to 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

getDeclaringClass

public static java.lang.Class getDeclaringClass(java.lang.reflect.AccessibleObject ao)

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.

classForNamePA

public static java.security.PrivilegedAction<java.lang.Class<?>> classForNamePA(java.lang.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.

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 java.security.PrivilegedAction<java.lang.Class<?>> classForNamePA(java.lang.String name,
                                                                                java.lang.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.

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)

classForNameWithExceptionPEA

public static <T> java.security.PrivilegedExceptionAction<java.lang.Class<T>> classForNameWithExceptionPEA(java.lang.String name)
                                                                                                throws java.lang.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:
java.lang.ClassNotFoundException
See Also:
AccessController.doPrivileged(java.security.PrivilegedExceptionAction)

classForNameWithExceptionPEA

public static <T> java.security.PrivilegedExceptionAction<java.lang.Class<T>> classForNameWithExceptionPEA(java.lang.String name,
                                                                                                           java.lang.ClassLoader cl)
                                                                                                throws java.lang.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:
java.lang.ClassNotFoundException
See Also:
AccessController.doPrivileged(java.security.PrivilegedExceptionAction)

getContextClassLoaderPA

public static java.security.PrivilegedAction<java.lang.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)

setAccessibleMethodPA

public static java.security.PrivilegedAction setAccessibleMethodPA(java.lang.reflect.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)

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 type argument is a parameterized type and it's raw type is a class then that class 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, or not a parameterized type with a raw type that is not a class.

getTypeArgumentAndClass

public static ReflectionHelper.TypeClassPair getTypeArgumentAndClass(java.lang.reflect.Type parameterizedType)
                                                              throws java.lang.IllegalArgumentException
Throws:
java.lang.IllegalArgumentException

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.

getValueOfStringMethodPA

public static java.security.PrivilegedAction<java.lang.reflect.Method> getValueOfStringMethodPA(java.lang.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 java.security.PrivilegedAction<java.lang.reflect.Method> getFromStringStringMethodPA(java.lang.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 java.security.PrivilegedAction<java.lang.reflect.Constructor> getStringConstructorPA(java.lang.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)

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

findMethodOnClassPA

public static java.security.PrivilegedAction<java.lang.reflect.Method> findMethodOnClassPA(java.lang.Class<?> c,
                                                                                           java.lang.reflect.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)

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).


Copyright © 2014 Oracle Corporation. All Rights Reserved.