|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectorg.glassfish.jersey.internal.util.ReflectionHelper
public class ReflectionHelper
Utility methods for Java reflection.
| 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
|
classForName(String name)
Get the Class from the class name. |
|
static
|
classForName(String name,
ClassLoader cl)
Get the Class from the class name. |
|
static
|
classForNameWithException(String name)
Get the Class from the class name. |
|
static
|
classForNameWithException(String name,
ClassLoader cl)
Get the Class from the class name. |
|
static
|
erasure(Type type)
Get the Class representation of the given type. |
|
static Method |
findMethodOnClass(Class<?> c,
Method m)
Find a method on a class given an existing method. |
|
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 ClassLoader |
getContextClassLoader()
Get the context class loader. |
|
static Class<?> |
getDeclaringClass(AccessibleObject ao)
Get the declaring class of an accessible object. |
|
static Method |
getFromStringStringMethod(Class<?> c)
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 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 InputStream |
getResourceAsStream(ClassLoader loader,
Class<?> originClass,
String name)
Lookup resource by given name. |
|
static Constructor |
getStringConstructor(Class<?> c)
Get the 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 Method |
getValueOfStringMethod(Class<?> c)
Get 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 |
isPrimitive(Type type)
Check if the given type is a primitive type. |
|
static boolean |
isSubClassOf(Type subType,
Type superType)
Check if subType is a sub-type of superType. |
|
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 void |
setAccessibleMethod(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 |
|---|
public static Class<?> getDeclaringClass(AccessibleObject ao)
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> Class<T> classForName(String name)
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 - the class name.
public static <T> Class<T> classForName(String name,
ClassLoader cl)
T - class type.name - the class name.cl - the class loader to use, if null then the defining class loader
of this class will be utilized.
public static <T> Class<T> classForNameWithException(String name)
throws ClassNotFoundException
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 - the class name.
ClassNotFoundException - if the class cannot be found.
public static <T> Class<T> classForNameWithException(String name,
ClassLoader cl)
throws ClassNotFoundException
T - class type.name - the class name.cl - the class loader to use, if null then the defining class loader
of this class will be utilized.
ClassNotFoundException - if the class cannot be found.public static ClassLoader getContextClassLoader()
public static void setAccessibleMethod(Method m)
m - the method to be set as accessible
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.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 array
public static Method getValueOfStringMethod(Class<?> c)
c - The class to obtain the method.
public static Method getFromStringStringMethod(Class<?> c)
c - The class to obtain the method.
public static Constructor getStringConstructor(Class<?> c)
c - The class to obtain the constructor.
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 Class[] getParameterizedClassArguments(ReflectionHelper.DeclaringClassInterfacePair p)
p - the declaring class
public static Type[] getParameterizedTypeArguments(ReflectionHelper.DeclaringClassInterfacePair p)
p - the declaring class
public 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 Method findMethodOnClass(Class<?> c,
Method m)
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.
c - the class to search for a public methodm - the method to find
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.
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||