public class ReflectionHelper extends Object
| Modifier and Type | Class and Description |
|---|---|
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 and Description |
|---|
ReflectionHelper() |
| Modifier and Type | Method and Description |
|---|---|
static Class |
classForName(String name)
Get the Class from the class name.
|
static Class |
classForName(String name,
ClassLoader cl)
Get the Class from the class name.
|
static Class |
classForNameWithException(String name)
Get the Class from the class name.
|
static Class |
classForNameWithException(String name,
ClassLoader cl)
Get the Class from the 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 Method |
findMethodOnClass(Class c,
Method m)
Find a method on a class given an existing method.
|
static Class |
getArrayClass(Class c)
Get Array class of component class.
|
static ReflectionHelper.DeclaringClassInterfacePair |
getClass(Class concrete,
Class iface)
Find the declaring class that implements or extends an interface.
|
static Class<?> |
getClassType(Class<?> inspectedClass,
Class<?> superClass)
Find a type of the class given it's Superclass.
|
static PrivilegedAction<ClassLoader> |
getContextClassLoaderPA()
Get privileged action to obtain context class loader.
|
static Class |
getDeclaringClass(AccessibleObject ao)
Get declaring class of provided field, method or constructor.
|
static Method |
getFromStringStringMethod(Class c)
Get the static fromString(String ) method.
|
static Class |
getGenericClass(Type parameterizedType)
Get the class that is the type argument of a parameterized type.
|
static <T> T |
getInstance(Class<T> c)
Creates an instance of
Class c using Class.newInstance(). |
static <T> T |
getInstance(Class<T> c,
ErrorCollector collector)
Creates an instance of
Class c using Class.newInstance(). |
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 Constructor |
getStringConstructor(Class c)
Get the constructor that has a single parameter of String.
|
static ReflectionHelper.TypeClassPair |
getTypeArgumentAndClass(Type parameterizedType) |
static Method |
getValueOfStringMethod(Class c)
Get the static valueOf(String ) method.
|
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 ReflectionHelper.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.
|
public static Class getDeclaringClass(AccessibleObject ao)
ao - object for which the declared class will be returned.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, thos 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 Class 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.
name - the class name.public static Class classForName(String name, ClassLoader cl)
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 Class 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.
name - the class name.ClassNotFoundException - if the class cannot be found.public static Class classForNameWithException(String name, ClassLoader cl) throws ClassNotFoundException
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 <T> PrivilegedExceptionAction<Class<T>> classForNameWithExceptionPEA(String name) throws ClassNotFoundException
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.
T - class type.name - class name.ClassNotFoundException or return null if the class cannot be found.ClassNotFoundException - when provided string contains classname of unknown class.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.ClassNotFoundException
or returns null if the class cannot be found.ClassNotFoundException - when provided string contains classname of unknown class.AccessController.doPrivileged(java.security.PrivilegedExceptionAction)public static PrivilegedAction<ClassLoader> getContextClassLoaderPA()
null if context class loader has not been set.AccessController.doPrivileged(java.security.PrivilegedAction)public static void setAccessibleMethod(Method m)
m - the method to be set as accessiblepublic static Class getGenericClass(Type parameterizedType) throws IllegalArgumentException
parameterizedType - must be an instance of ParameterizedType
and have exactly one type argument.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.public static ReflectionHelper.TypeClassPair getTypeArgumentAndClass(Type parameterizedType) throws IllegalArgumentException
IllegalArgumentExceptionpublic static Class getArrayClass(Class c)
c - the component class of the arraypublic 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 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 ReflectionHelper.ClassTypePair resolveTypeVariable(Class c, Class dc, TypeVariable tv)
c - the concrete class from which all type variables are resolveddc - the declaring class where the type variable was definedtv - the type variablepublic 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 findpublic static Class<?> getClassType(Class<?> inspectedClass, Class<?> superClass)
inspectedClass - Class whose type is searched for.superClass - Class relatively to which the search is performed.public static OsgiRegistry getOsgiRegistryInstance()
OsgiRegistry instance.OsgiRegistry instance or null if the class cannot be instantiated (not in OSGi
environment).public static <T> T getInstance(Class<T> c, ErrorCollector collector)
T - type.c - Class whose instance is going to be createdcollector - ErrorCollector which collects the Exceptions.Class.public static <T> T getInstance(Class<T> c) throws IllegalAccessException, InstantiationException
Class c using Class.newInstance().T - type.c - Class whose instance is going to be createdClass.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.Copyright © 2012-2017, Oracle and/or its affiliates. All Rights Reserved. Use is subject to license terms.