Top Description Inners Constructors Methods
org.glassfish.tyrus.core

public Class ReflectionHelper

extends Object
Class Inheritance
Imports
java.lang.reflect.AccessibleObject, .Array, .Constructor, .Field, .GenericArrayType, .Method, .Modifier, .ParameterizedType, .Type, .TypeVariable, java.security.AccessController, .PrivilegedAction, .PrivilegedExceptionAction, java.util.HashMap, .Map, jakarta.websocket.DeploymentException, org.glassfish.tyrus.core.l10n.LocalizationMessages

Utility methods for Java reflection.
Author
[email protected]

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
ReflectionHelper.ClassTypePair

A tuple consisting of a class and type of the class.

public static class
ReflectionHelper.DeclaringClassInterfacePair

A tuple consisting of a concrete class, declaring class that declares a generic interface type.

public static class

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public static Class<T>

Returns:

the Class, otherwise null if the class cannot be found.
classForName
(String
the class name.
name
)

Get the Class from the class name.

public static Class<T>

Returns:

the Class, otherwise null if the class cannot be found.
classForName
(String
the class name.
name
,
ClassLoader
the class loader to use, if null then the defining class loader of this class will be utilized.
cl
)

Get the Class from the class name.

public static Class<T>

Returns:

the Class, otherwise null if the class cannot be found.
classForNameWithException
(String
the class name.
name
)

Get the Class from the class name.

public static Class<T>

Returns:

the Class, otherwise null if the class cannot be found.
classForNameWithException
(String
the class name.
name
,
ClassLoader
the class loader to use, if null then the defining class loader of this class will be utilized.
cl
)

Get the Class from the class name.

public static <
class type.
T
>
PrivilegedExceptionAction<Class<T>>

Returns:

privileged exception action to obtain the Class. The action could throw ClassNotFoundException or return null if the class cannot be found.
classForNameWithExceptionPEA
(final String
class name.
name
)

Get privileged exception action to obtain Class from given class name.

public static <
class type.
T
>
PrivilegedExceptionAction<Class<T>>

Returns:

privileged exception action to obtain the Class. The action throws ClassNotFoundException or returns null if the class cannot be found.
classForNameWithExceptionPEA
(final String
class name.
name
,
final ClassLoader
class loader to use, if null then the defining class loader of the calling class will be utilized.
cl
)

Get privileged exception action to obtain Class from given class name.

private static boolean
public static Method

Returns:

the found public method.
findMethodOnClass
(Class<T>
the class to search for a public method
c
,
Method
the method to find
m
)

Find a method on a class given an existing method.

public static Class<T>

Returns:

the array class.
getArrayClass
(Class<T>
the component class of the array
c
)

Get Array class of component class.

public static ReflectionHelper.DeclaringClassInterfacePair

Returns:

the tuple of the declaring class and the generic interface type.
getClass
(Class<T>
the concrete class than directly or indirectly implements or extends an interface class.
concrete
,
Class<T>
the interface class.
iface
)

Find the declaring class that implements or extends an interface.

private static ReflectionHelper.DeclaringClassInterfacePair
getClass(Class<T> concrete, Class<T> iface, Class<T> c)

private static Class<T>
public static Class<?>

Returns:

type of the class.
getClassType
(Class<?>
Class whose type is searched for.
inspectedClass
,
Class<?>
Class relatively to which the search is performed.
superClass
)

Find a type of the class given it's Superclass.

private static ClassLoader

Returns:

the context class loader, otherwise null if not set.
getContextClassLoader
()

Get the context class loader.

public static PrivilegedAction<ClassLoader>

Returns:

privileged action to obtain the actual context class loader. The action could return null if context class loader has not been set.
getContextClassLoaderPA
()

Get privileged action to obtain context class loader.

public static Class<T>

Returns:

declaring class of provided object.
getDeclaringClass
(AccessibleObject
object for which the declared class will be returned.
ao
)

Get declaring class of provided field, method or constructor.

public static Method

Returns:

the method, otherwise null if the method is not present.
getFromStringStringMethod
(Class<T>
The class to obtain the method.
c
)

Get the static fromString(String ) method.

public static Class<T>

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.
getGenericClass
(Type
must be an instance of ParameterizedType and have exactly one type argument.
parameterizedType
)

Get the class that is the type argument of a parameterized type.

public static <
type.
T
>
T

Returns:

new instance of Class.
getInstance
(Class<T>
Class whose instance is going to be created
c
,
ErrorCollector
ErrorCollector which collects the Exceptions.
collector
)

Creates an instance of Class c using Class#newInstance().

public static <
type.
T
>
T

Returns:

new instance of Class.
getInstance
(Class<T>
Class whose instance is going to be created
c
)

Creates an instance of Class c using Class#newInstance().

public static OsgiRegistry

Returns:

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

Returns an OsgiRegistry instance.

public static Class<T>[]

Returns:

the parameterized class arguments, or null if the generic interface type is not a parameterized type.
getParameterizedClassArguments
(ReflectionHelper.DeclaringClassInterfacePair
the declaring class
p
)

Get the parameterized class arguments for a declaring class that declares a generic interface type.

public static Type[]

Returns:

the parameterized type arguments, or null if the generic interface type is not a parameterized type.
getParameterizedTypeArguments
(ReflectionHelper.DeclaringClassInterfacePair
the declaring class
p
)

Get the parameterized type arguments for a declaring class that declares a generic interface type.

public static Constructor<T>

Returns:

the constructor, otherwise null if the constructor is not present.
getStringConstructor
(Class<T>
The class to obtain the constructor.
c
)

Get the constructor that has a single parameter of String.

private static ReflectionHelper.DeclaringClassInterfacePair
getType(Class<T> concrete, Class<T> iface, Class<T> c, Type[] ts)

private static ReflectionHelper.DeclaringClassInterfacePair
getType(Class<T> concrete, Class<T> iface, Class<T> c, Type t)

public static ReflectionHelper.TypeClassPair
getTypeArgumentAndClass(Type parameterizedType)

private static Type
private static String
getTypeName(Class<T> type)

public static Method

Returns:

the method, otherwise null if the method is not present.
getValueOfStringMethod
(Class<T>
The class to obtain the method.
c
)

Get the static valueOf(String ) method.

public static String

Returns:

the string representation of the method and instance.
methodInstanceToString
(Object
the object whose class implements m.
o
,
Method
the method.
m
)

Create a string representation of a method and an instance whose class implements the method.

public static String

Returns:

the string representation of the object.
objectToString
(Object
the object.
o
)

Create a string representation of an object.

public static ReflectionHelper.ClassTypePair

Returns:

the resolved Java class and type, otherwise null if the type variable could not be resolved
resolveTypeVariable
(Class<T>
the concrete class from which all type variables are resolved
c
,
Class<T>
the declaring class where the type variable was defined
dc
,
TypeVariable<D extends GenericDeclaration>
the type variable
tv
)

Given a type variable resolve the Java class of that variable.

private static ReflectionHelper.ClassTypePair
private static ReflectionHelper.ClassTypePair
public static void
setAccessibleMethod(final Method
the method to be set as accessible
m
)

Set a method to be accessible.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

ReflectionHelperback to summary
public ReflectionHelper()

Method Detail

classForNameback to summary
public static Class<T> classForName(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:String

the class name.

Returns:Class<T>

the Class, otherwise null if the class cannot be found.

classForNameback to summary
public static Class<T> classForName(String name, ClassLoader cl)

Get the Class from the class name.

Parameters
name:String

the class name.

cl:ClassLoader

the class loader to use, if null then the defining class loader of this class will be utilized.

Returns:Class<T>

the Class, otherwise null if the class cannot be found.

classForNameWithExceptionback to summary
public static Class<T> classForNameWithException(String name) throws 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.

Parameters
name:String

the class name.

Returns:Class<T>

the Class, otherwise null if the class cannot be found.

Exceptions
ClassNotFoundException:
if the class cannot be found.
classForNameWithExceptionback to summary
public static Class<T> classForNameWithException(String name, ClassLoader cl) throws ClassNotFoundException

Get the Class from the class name.

Parameters
name:String

the class name.

cl:ClassLoader

the class loader to use, if null then the defining class loader of this class will be utilized.

Returns:Class<T>

the Class, otherwise null if the class cannot be found.

Exceptions
ClassNotFoundException:
if the class cannot be found.
classForNameWithExceptionPEAback to summary
public static <T> PrivilegedExceptionAction<Class<T>> classForNameWithExceptionPEA(final 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.

Parameters
<T>
class type.
name:String

class name.

Returns:PrivilegedExceptionAction<Class<T>>

privileged exception action to obtain the Class. The action could throw ClassNotFoundException or return null if the class cannot be found.

Exceptions
ClassNotFoundException:
when provided string contains classname of unknown class.
See Also
AccessController#doPrivileged(java.security.PrivilegedExceptionAction)
classForNameWithExceptionPEAback to summary
public static <T> PrivilegedExceptionAction<Class<T>> classForNameWithExceptionPEA(final String name, final 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.

Parameters
<T>
class type.
name:String

class name.

cl:ClassLoader

class loader to use, if null then the defining class loader of the calling class will be utilized.

Returns:PrivilegedExceptionAction<Class<T>>

privileged exception action to obtain the Class. The action throws ClassNotFoundException or returns null if the class cannot be found.

Annotations
@SuppressWarnings:unchecked
Exceptions
ClassNotFoundException:
when provided string contains classname of unknown class.
See Also
AccessController#doPrivileged(java.security.PrivilegedExceptionAction)
compareParameterTypesback to summary
private static boolean compareParameterTypes(Type[] ts, Type[] _ts)
findMethodOnClassback to summary
public static Method findMethodOnClass(Class<T> c, 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:Class<T>

the class to search for a public method

m:Method

the method to find

Returns:Method

the found public method.

getArrayClassback to summary
public static Class<T> getArrayClass(Class<T> c)

Get Array class of component class.

Parameters
c:Class<T>

the component class of the array

Returns:Class<T>

the array class.

getClassback to summary
public static ReflectionHelper.DeclaringClassInterfacePair getClass(Class<T> concrete, Class<T> iface)

Find the declaring class that implements or extends an interface.

Parameters
concrete:Class<T>

the concrete class than directly or indirectly implements or extends an interface class.

iface:Class<T>

the interface class.

Returns:ReflectionHelper.DeclaringClassInterfacePair

the tuple of the declaring class and the generic interface type.

getClassback to summary
private static ReflectionHelper.DeclaringClassInterfacePair getClass(Class<T> concrete, Class<T> iface, Class<T> c)
getClassOfTypeback to summary
private static Class<T> getClassOfType(Type type)
getClassTypeback to summary
public static Class<?> getClassType(Class<?> inspectedClass, Class<?> superClass)

Find a type of the class given it's Superclass.

Parameters
inspectedClass:Class<?>

Class whose type is searched for.

superClass:Class<?>

Class relatively to which the search is performed.

Returns:Class<?>

type of the class.

getContextClassLoaderback to summary
private static ClassLoader getContextClassLoader()

Get the context class loader.

Returns:ClassLoader

the context class loader, otherwise null if not set.

getContextClassLoaderPAback to summary
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:PrivilegedAction<ClassLoader>

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)
getDeclaringClassback to summary
public static Class<T> getDeclaringClass(AccessibleObject ao)

Get declaring class of provided field, method or constructor.

Parameters
ao:AccessibleObject

object for which the declared class will be returned.

Returns:Class<T>

declaring class of provided object.

getFromStringStringMethodback to summary
public static Method getFromStringStringMethod(Class<T> c)

Get the static fromString(String ) method.

Parameters
c:Class<T>

The class to obtain the method.

Returns:Method

the method, otherwise null if the method is not present.

Annotations
@SuppressWarnings:unchecked
getGenericClassback to summary
public static Class<T> getGenericClass(Type parameterizedType) throws IllegalArgumentException

Get the class that is the type argument of a parameterized type.

Parameters
parameterizedType:Type

must be an instance of ParameterizedType and have exactly one type argument.

Returns:Class<T>

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.

Exceptions
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.
getInstanceback to summary
public static <T> T getInstance(Class<T> c, ErrorCollector collector)

Creates an instance of Class c using Class#newInstance(). Exceptions are logged to ErrorCollector.

Parameters
<T>
type.
c:Class<T>

Class whose instance is going to be created

collector:ErrorCollector

ErrorCollector which collects the Exceptions.

Returns:T

new instance of Class.

getInstanceback to summary
public static <T> T getInstance(Class<T> c) throws IllegalAccessException, InstantiationException

Creates an instance of Class c using Class#newInstance().

Parameters
<T>
type.
c:Class<T>

Class whose instance is going to be created

Returns:T

new instance of Class.

Exceptions
IllegalAccessException:
if the class or its nullary constructor is not accessible.
InstantiationException:
if this Class represents an abstract class, an interface, an array class, a primitive type, or void; or if the class has no nullary constructor; or if the instantiation fails for some other reason.
getOsgiRegistryInstanceback to summary
public static OsgiRegistry getOsgiRegistryInstance()

Returns an OsgiRegistry instance.

Returns:OsgiRegistry

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

getParameterizedClassArgumentsback to summary
public static Class<T>[] getParameterizedClassArguments(ReflectionHelper.DeclaringClassInterfacePair p)

Get the parameterized class arguments for a declaring class that declares a generic interface type.

Parameters
p:ReflectionHelper.DeclaringClassInterfacePair

the declaring class

Returns:Class<T>[]

the parameterized class arguments, or null if the generic interface type is not a parameterized type.

getParameterizedTypeArgumentsback to summary
public static Type[] getParameterizedTypeArguments(ReflectionHelper.DeclaringClassInterfacePair p)

Get the parameterized type arguments for a declaring class that declares a generic interface type.

Parameters
p:ReflectionHelper.DeclaringClassInterfacePair

the declaring class

Returns:Type[]

the parameterized type arguments, or null if the generic interface type is not a parameterized type.

getStringConstructorback to summary
public static Constructor<T> getStringConstructor(Class<T> c)

Get the constructor that has a single parameter of String.

Parameters
c:Class<T>

The class to obtain the constructor.

Returns:Constructor<T>

the constructor, otherwise null if the constructor is not present.

Annotations
@SuppressWarnings:unchecked
getTypeback to summary
private static ReflectionHelper.DeclaringClassInterfacePair getType(Class<T> concrete, Class<T> iface, Class<T> c, Type[] ts)
getTypeback to summary
private static ReflectionHelper.DeclaringClassInterfacePair getType(Class<T> concrete, Class<T> iface, Class<T> c, Type t)
getTypeArgumentAndClassback to summary
public static ReflectionHelper.TypeClassPair getTypeArgumentAndClass(Type parameterizedType) throws IllegalArgumentException
getTypeArgumentOfParameterizedTypeback to summary
private static Type getTypeArgumentOfParameterizedType(Type parameterizedType)
getTypeNameback to summary
private static String getTypeName(Class<T> type)
getValueOfStringMethodback to summary
public static Method getValueOfStringMethod(Class<T> c)

Get the static valueOf(String ) method.

Parameters
c:Class<T>

The class to obtain the method.

Returns:Method

the method, otherwise null if the method is not present.

Annotations
@SuppressWarnings:unchecked
methodInstanceToStringback to summary
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 '@', 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:Object

the object whose class implements m.

m:Method

the method.

Returns:String

the string representation of the method and instance.

objectToStringback to summary
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 '@', 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:Object

the object.

Returns:String

the string representation of the object.

resolveTypeVariableback to summary
public static ReflectionHelper.ClassTypePair resolveTypeVariable(Class<T> c, Class<T> dc, TypeVariable<D extends GenericDeclaration> tv)

Given a type variable resolve the Java class of that variable.

Parameters
c:Class<T>

the concrete class from which all type variables are resolved

dc:Class<T>

the declaring class where the type variable was defined

tv:TypeVariable<D extends GenericDeclaration>

the type variable

Returns:ReflectionHelper.ClassTypePair

the resolved Java class and type, otherwise null if the type variable could not be resolved

resolveTypeVariableback to summary
private static ReflectionHelper.ClassTypePair resolveTypeVariable(Class<T> c, Class<T> dc, TypeVariable<D extends GenericDeclaration> tv, Map<TypeVariable<D extends GenericDeclaration>, Type> map)
resolveTypeVariableback to summary
private static ReflectionHelper.ClassTypePair resolveTypeVariable(ParameterizedType pt, Class<T> c, Class<T> dc, TypeVariable<D extends GenericDeclaration> tv, Map<TypeVariable<D extends GenericDeclaration>, Type> map)
setAccessibleMethodback to summary
public static void setAccessibleMethod(final Method m)

Set a method to be accessible.

Parameters
m:Method

the method to be set as accessible

org.glassfish.tyrus.core back to summary

public Class ReflectionHelper.ClassTypePair

extends Object
Class Inheritance

A tuple consisting of a class and type of the class.

Field Summary

Modifier and TypeField and Description
public final Class<T>
c

The class.

public final Type
t

The type of the class.

Constructor Summary

AccessConstructor and Description
public
public

Method Summary

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

cback to summary
public final Class<T> c

The class.

tback to summary
public final Type t

The type of the class.

Constructor Detail

ClassTypePairback to summary
public ClassTypePair(Class<T> c)
ClassTypePairback to summary
public ClassTypePair(Class<T> c, Type t)
org.glassfish.tyrus.core back to summary

public Class ReflectionHelper.DeclaringClassInterfacePair

extends Object
Class Inheritance
  • java.lang.Object
  • org.glassfish.tyrus.core.ReflectionHelper.DeclaringClassInterfacePair

A tuple consisting of a concrete class, declaring class that declares a generic interface type.

Field Summary

Modifier and TypeField and Description
public final Class<T>
public final Class<T>
public final Type

Constructor Summary

AccessConstructor and Description
private
DeclaringClassInterfacePair(Class<T> concreteClass, Class<T> declaringClass, Type genericInteface)

Method Summary

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

concreteClassback to summary
public final Class<T> concreteClass
declaringClassback to summary
public final Class<T> declaringClass
genericInterfaceback to summary
public final Type genericInterface

Constructor Detail

DeclaringClassInterfacePairback to summary
private DeclaringClassInterfacePair(Class<T> concreteClass, Class<T> declaringClass, Type genericInteface)
org.glassfish.tyrus.core back to summary

public final Class ReflectionHelper.TypeClassPair

extends Object
Class Inheritance

Field Summary

Modifier and TypeField and Description
public final Class<T>
c

public final Type
t

Constructor Summary

AccessConstructor and Description
public

Method Summary

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

cback to summary
public final Class<T> c
tback to summary
public final Type t

Constructor Detail

TypeClassPairback to summary
public TypeClassPair(Type t, Class<T> c)