public final class ReflectionHelper extends Object
Modifier and Type | Class and Description |
---|---|
static class |
ReflectionHelper.DeclaringClassInterfacePair
A tuple consisting of a concrete class and a declaring class that declares a
generic interface type.
|
Modifier and Type | Method and Description |
---|---|
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)
|
static GenericType |
genericTypeFor(Object instance)
Create a
generic type information for a given
Java instance . |
static PrivilegedAction<Field[]> |
getAllFieldsPA(Class<?> clazz)
Get privileged action to obtain fields on given class, recursively through inheritance hierarchy.
|
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<Constructor<?>> |
getDeclaredConstructorPA(Class<?> clazz,
Class<?>... params)
Get privileged action to obtain declared constructor of given class with given parameters.
|
static PrivilegedAction<Constructor<?>[]> |
getDeclaredConstructorsPA(Class<?> clazz)
Get privileged action to obtain declared constructors of given class.
|
static PrivilegedAction<Field[]> |
getDeclaredFieldsPA(Class<?> clazz)
Get privileged action to obtain fields declared on given class.
|
static PrivilegedAction<Collection<? extends 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<?> clazz)
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 Class<?> |
getRawClass(Type type)
Given the type parameter gets the raw type represented by the type, or null if this has no associated raw class.
|
static InputStream |
getResourceAsStream(ClassLoader loader,
Class<?> originClass,
String name)
Lookup resource by given name.
|
static PrivilegedAction<Constructor> |
getStringConstructorPA(Class<?> clazz)
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<?> clazz)
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 |
isJaxbAvailable()
Returns true iff JAX-B API is available on classpath.
|
static boolean |
isPrimitive(Type type)
Check if the given type is a primitive type.
|
static boolean |
isPublic(Class<?> clazz)
Return
true iff the method is public. |
static boolean |
isPublic(Executable executable)
Return
true iff the executable is public. |
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 boolean |
isXmlTransformAvailable()
Returns true iff javax.xml.transform package is available on classpath.
|
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.
|
static Class<?> |
theMostSpecificTypeOf(Set<Type> contractTypes)
Determine the most specific type from given set.
|
public static Class<?> getDeclaringClass(AccessibleObject ao)
Supported are Method
, Field
and Constructor
accessible object types.
ao
- an accessible object.IllegalArgumentException
- in case the type of the accessible object
is not supported.public static String objectToString(Object o)
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())
o
- the object.public static String methodInstanceToString(Object o, Method m)
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, those method returns a
string equal to the value of:
o.getClass().getName() + '@' + Integer.toHexString(o.hashCode()) + '.' + m.getName() + '(' + <parameters> + ')'.
o
- the object whose class implements m
.m
- the method.public static <T> PrivilegedAction<Class<T>> classForNamePA(String 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.
T
- class type.name
- class name.null
if the class cannot be found.AccessController.doPrivileged(java.security.PrivilegedAction)
public static <T> PrivilegedAction<Class<T>> classForNamePA(String name, ClassLoader cl)
T
- class type.name
- class name.cl
- class loader to use, if null
then the defining class loader
of this class will be utilized.null
if the class cannot be found.AccessController.doPrivileged(java.security.PrivilegedAction)
public static PrivilegedAction<ClassLoader> getClassLoaderPA(Class<?> clazz)
clazz
- class for which to get class loader.clazz
class.AccessController.doPrivileged(java.security.PrivilegedAction)
public static PrivilegedAction<Field[]> getDeclaredFieldsPA(Class<?> clazz)
clazz
- class for which to get the declared fields.clazz
class.AccessController.doPrivileged(java.security.PrivilegedAction)
public static PrivilegedAction<Field[]> getAllFieldsPA(Class<?> clazz)
clazz
- class for which to get fields.clazz
class.AccessController.doPrivileged(java.security.PrivilegedAction)
public static PrivilegedAction<Collection<? extends Method>> getDeclaredMethodsPA(Class<?> clazz)
clazz
- class for which to get the declared methods.clazz
class.AccessController.doPrivileged(java.security.PrivilegedAction)
public static <T> PrivilegedExceptionAction<Class<T>> classForNameWithExceptionPEA(String name) throws ClassNotFoundException
T
- class type.name
- class name.ClassNotFoundException
or return null
if the class cannot be found.ClassNotFoundException
- in case the class cannot be loaded with the context class loader.AccessController.doPrivileged(java.security.PrivilegedExceptionAction)
public static <T> PrivilegedExceptionAction<Class<T>> classForNameWithExceptionPEA(String name, ClassLoader cl) throws ClassNotFoundException
T
- class type.name
- class name.cl
- class loader to use, if null
then the defining class loader
of the calling class will be utilized.null
,
or throws ClassNotFoundException
in case the class loader has been specified.ClassNotFoundException
- in case the class cannot be loaded with the specified class loader.AccessController.doPrivileged(java.security.PrivilegedExceptionAction)
public static PrivilegedAction<ClassLoader> getContextClassLoaderPA()
null
if the context class loader has not been set.AccessController.doPrivileged(java.security.PrivilegedAction)
public static PrivilegedAction setContextClassLoaderPA(ClassLoader classLoader)
classLoader
- context class loader to be set.AccessController.doPrivileged(java.security.PrivilegedAction)
public static PrivilegedAction setAccessibleMethodPA(Method m)
m
- method to be set as accessible.AccessController.doPrivileged(java.security.PrivilegedAction)
public static boolean isPublic(Class<?> clazz)
true
iff the method is public.clazz
- The method in questiontrue
if mod includes the public modifier; false
otherwise.public static boolean isPublic(Executable executable)
true
iff the executable is public.executable
- The executable in questiontrue
if the executable includes the public modifier; false
otherwise.public static List<Class<?>> getGenericTypeArgumentClasses(Type type) throws IllegalArgumentException
parameterized
input type.
For any given argument in the returned list, following rules apply:
type
is not an instance of ParameterizedType an empty
list is returned.type
- parameterized type.null
.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.public static List<ClassTypePair> getTypeArgumentAndClass(Type type) throws IllegalArgumentException
parameterized
input type.
For any given class
part of each pair
in the returned list, following rules apply:
type
is not an instance of ParameterizedType an empty
list is returned.type
- parameterized type.null
.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.public static boolean isPrimitive(Type type)
type
- type to be checked.true
in case the type represents a primitive type, otherwise returns false
.public static Type[] getTypeArguments(Type type)
parameterized type
,
the method returns null
.type
- parameterized type.null
in case the input type is not a parameterized type.public static Type getTypeArgument(Type type, int index)
parameterized type
,
the method returns null
.type
- parameterized type.index
- type parameter index.null
in case the input type is not
a parameterized type.public static <T> Class<T> erasure(Type type)
Class
representation of the given type.
This corresponds to the notion of the erasure in JSR-14.type
- type to provide the erasure for.public static boolean isSubClassOf(Type subType, Type superType)
subType
is a sub-type of superType
.subType
- sub-type type.superType
- super-type type.true
in case the subType
is a sub-type of superType
,
false
otherwise.public static boolean isArray(Type type)
type
- type to check.true
in case the type is an array type, false
otherwise.public static boolean isArrayOfType(Type type, Class<?> componentType)
type
- type to check.componentType
- array component type.true
in case the type is an array type of a given component type,
false
otherwise.public static Type getArrayComponentType(Type type)
type
- must be an array.IllegalArgumentException
- in case the type is not an array type.public static Class<?> getArrayForComponentType(Class<?> c)
c
- the component class of the arraypublic static PrivilegedAction<Method> getValueOfStringMethodPA(Class<?> clazz)
clazz
- class to obtain the method.null
if the method is not present.AccessController.doPrivileged(java.security.PrivilegedAction)
public static PrivilegedAction<Method> getFromStringStringMethodPA(Class<?> clazz)
clazz
- class for which to get the method.null
if the method is not present.AccessController.doPrivileged(java.security.PrivilegedAction)
public static PrivilegedAction<Constructor> getStringConstructorPA(Class<?> clazz)
clazz
- The class for which to obtain the constructor.null
if the constructor is not present.AccessController.doPrivileged(java.security.PrivilegedAction)
public static PrivilegedAction<Constructor<?>[]> getDeclaredConstructorsPA(Class<?> clazz)
clazz
- The class for which to obtain the constructors.AccessController.doPrivileged(java.security.PrivilegedAction)
public static PrivilegedAction<Constructor<?>> getDeclaredConstructorPA(Class<?> clazz, Class<?>... params)
clazz
- The class for which to obtain the constructor.params
- constructor parameters.null
, when constructor with given parameters
is not found.AccessController.doPrivileged(java.security.PrivilegedAction)
public static Collection<Class<? extends Annotation>> getAnnotationTypes(AnnotatedElement annotatedElement, Class<? extends Annotation> metaAnnotation)
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).public static boolean isGetter(Method method)
getter
.method
- method to be examined.true
if the method is getter
, false
otherwise.public static GenericType genericTypeFor(Object instance)
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.
instance
- Java instance for which the GenericType
description should be created.GenericType
describing the Java instance
.public static boolean isSetter(Method method)
setter
.method
- method to be examined.true
if the method is setter
, false
otherwise.public static String getPropertyName(Method method)
method
- method to be examined.public static Class<?> theMostSpecificTypeOf(Set<Type> contractTypes)
contractTypes
- to be taken into account.public static Class[] getParameterizedClassArguments(ReflectionHelper.DeclaringClassInterfacePair p)
p
- the declaring classpublic static Type[] getParameterizedTypeArguments(ReflectionHelper.DeclaringClassInterfacePair p)
p
- the declaring classpublic static ReflectionHelper.DeclaringClassInterfacePair getClass(Class<?> concrete, Class<?> iface)
concrete
- the concrete class than directly or indirectly
implements or extends an interface class.iface
- the interface class.public static ClassTypePair resolveGenericType(Class concreteClass, Class declaringClass, Class rawResolvedType, Type genericResolvedType)
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.public static ClassTypePair resolveTypeVariable(Class<?> c, Class<?> dc, TypeVariable tv)
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.public static PrivilegedAction<Method> findMethodOnClassPA(Class<?> c, Method m)
TypeVariable
then that public method is
returned from the action.c
- the class to search for a public methodm
- the method to findAccessController.doPrivileged(java.security.PrivilegedAction)
public static PrivilegedAction<Method[]> getMethodsPA(Class<?> c)
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.
c
- class for which the methods should be returned.Method
objects representing the
public methods of the class.AccessController.doPrivileged(java.security.PrivilegedAction)
public static Method findOverridingMethodOnClass(Class<?> clazz, Method method)
clazz
- class to find overriding method on.method
- an abstract method to find implementing method for.public static OsgiRegistry getOsgiRegistryInstance()
OsgiRegistry
instance.OsgiRegistry
instance or null
if the class cannot be instantiated (not in OSGi environment).public static InputStream getResourceAsStream(ClassLoader loader, Class<?> originClass, String name)
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.public static Class<?> getRawClass(Type type)
type
- the type to find the raw class on.public static boolean isJaxbAvailable()
public static boolean isXmlTransformAvailable()
Copyright © 2007-2024, Oracle and/or its affiliates. All Rights Reserved. Use is subject to license terms.