Package org.apache.bval.util.reflection
Class Reflection
- java.lang.Object
-
- org.apache.bval.util.reflection.Reflection
-
public class Reflection extends Object
Security-agnostic "blueprint" class for reflection-related operations. Intended for use by Apache BVal code.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
Reflection.ClassHierarchy
static class
Reflection.FullHierarchy
static class
Reflection.Interfaces
Inclusivity literals forhierarchy(Class, Interfaces)
.
-
Constructor Summary
Constructors Constructor Description Reflection()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> T
find(Class<?> clazz, Function<Class<?>,T> search)
Perform a search against the class hierarchy.static Object
getAnnotationValue(Annotation annotation, String name)
Get the named value from the specifiedAnnotation
.static <T> Constructor<T>
getDeclaredConstructor(Class<T> clazz, Class<?>... parameters)
Get the declared constructor fromclazz
.static <T> Constructor<? extends T>[]
getDeclaredConstructors(Class<T> clazz)
Convenient point forPrivilizing
Class.getDeclaredConstructors()
.static Field
getDeclaredField(Class<?> clazz, String fieldName)
Get the declared field fromclazz
.static Field[]
getDeclaredFields(Class<?> clazz)
Convenient point forPrivilizing
Class.getDeclaredFields()
.static Method
getDeclaredMethod(Class<?> clazz, String name, Class<?>... parameters)
Get the declared method fromclazz
.static Method[]
getDeclaredMethods(Class<?> clazz)
Convenient point forPrivilizing
Class.getDeclaredMethods()
.static String
getProperty(String name)
Convenient point forPrivilizing
System.getProperty(String)
.static Method
getPublicMethod(Class<?> clazz, String methodName, Class<?>... parameterTypes)
static Iterable<Class<?>>
hierarchy(Class<?> type, Reflection.Interfaces interfacesBehavior)
Get anIterable
that can iterate over a class hierarchy in ascending (subclass to superclass) order.static ClassLoader
loaderFromClassOrThread(Class<?> clazz)
static ClassLoader
loaderFromThreadOrClass(Class<?> fallbackClass)
Get aClassLoader
preferringThread.getContextClassLoader()
of currentThread
over that offallbackClass
.static void
makeAccessible(AccessibleObject o)
Set the accessibility ofo
to true.static <T> T
newInstance(Class<T> cls)
Construct a new instance ofcls
using its default constructor.static Class<?>
primitiveToWrapper(Class<?> cls)
Converts the specified primitive Class object to its corresponding wrapper Class object.static Class<?>
toClass(String className)
static Class<?>
toClass(String className, boolean resolve, ClassLoader loader)
Return the class for the given string, correctly handling primitive types.static Class<?>
toClass(String className, ClassLoader loader)
Return the class for the given string, correctly handling primitive types.static Class<?>
wrapperToPrimitive(Class<?> cls)
-
-
-
Method Detail
-
primitiveToWrapper
public static Class<?> primitiveToWrapper(Class<?> cls)
Converts the specified primitive Class object to its corresponding wrapper Class object.
NOTE: From v2.2, this method handles
Void.TYPE
, returningVoid.TYPE
.- Parameters:
cls
- the class to convert, may be null- Returns:
- the wrapper class for
cls
orcls
ifcls
is not a primitive.null
if null input. - Since:
- 2.1
-
getAnnotationValue
public static Object getAnnotationValue(Annotation annotation, String name) throws IllegalAccessException, InvocationTargetException
Get the named value from the specifiedAnnotation
.- Parameters:
annotation
-name
-- Returns:
- Object value
- Throws:
IllegalAccessException
InvocationTargetException
-
loaderFromClassOrThread
public static ClassLoader loaderFromClassOrThread(Class<?> clazz)
- Parameters:
clazz
-- Returns:
ClassLoader
-
loaderFromThreadOrClass
public static ClassLoader loaderFromThreadOrClass(Class<?> fallbackClass)
Get aClassLoader
preferringThread.getContextClassLoader()
of currentThread
over that offallbackClass
.- Parameters:
fallbackClass
-- Returns:
ClassLoader
-
toClass
public static Class<?> toClass(String className) throws ClassNotFoundException
- Throws:
ClassNotFoundException
-
toClass
public static Class<?> toClass(String className, ClassLoader loader) throws ClassNotFoundException
Return the class for the given string, correctly handling primitive types. If the given class loader is null, the context loader of the current thread will be used.- Throws:
RuntimeException
- on load errorClassNotFoundException
-
toClass
public static Class<?> toClass(String className, boolean resolve, ClassLoader loader) throws ClassNotFoundException
Return the class for the given string, correctly handling primitive types. If the given class loader is null, the context loader of the current thread will be used.- Throws:
RuntimeException
- on load errorClassNotFoundException
-
getProperty
public static String getProperty(String name)
Convenient point forPrivilizing
System.getProperty(String)
.- Parameters:
name
-- Returns:
- String
-
getDeclaredField
public static Field getDeclaredField(Class<?> clazz, String fieldName)
Get the declared field fromclazz
.- Parameters:
clazz
-fieldName
-- Returns:
Field
ornull
-
getDeclaredFields
public static Field[] getDeclaredFields(Class<?> clazz)
Convenient point forPrivilizing
Class.getDeclaredFields()
.- Parameters:
clazz
-- Returns:
Field
array
-
getDeclaredConstructor
public static <T> Constructor<T> getDeclaredConstructor(Class<T> clazz, Class<?>... parameters)
Get the declared constructor fromclazz
.- Parameters:
clazz
-parameters
-- Returns:
Constructor
ornull
-
getDeclaredMethod
public static Method getDeclaredMethod(Class<?> clazz, String name, Class<?>... parameters)
Get the declared method fromclazz
.- Parameters:
clazz
-name
-parameters
-- Returns:
Method
ornull
-
getDeclaredMethods
public static Method[] getDeclaredMethods(Class<?> clazz)
Convenient point forPrivilizing
Class.getDeclaredMethods()
.- Parameters:
clazz
-- Returns:
Method
array
-
getDeclaredConstructors
public static <T> Constructor<? extends T>[] getDeclaredConstructors(Class<T> clazz)
Convenient point forPrivilizing
Class.getDeclaredConstructors()
.- Parameters:
clazz
-- Returns:
Constructor
array
-
getPublicMethod
public static Method getPublicMethod(Class<?> clazz, String methodName, Class<?>... parameterTypes)
- Parameters:
clazz
-methodName
-- Returns:
Method
ornull
-
find
public static <T> T find(Class<?> clazz, Function<Class<?>,T> search)
Perform a search against the class hierarchy.- Parameters:
clazz
-search
-- Returns:
- T or
null
-
newInstance
public static <T> T newInstance(Class<T> cls)
Construct a new instance ofcls
using its default constructor.- Parameters:
cls
-- Returns:
- T
-
makeAccessible
public static void makeAccessible(AccessibleObject o)
Set the accessibility ofo
to true.- Parameters:
o
-
-
hierarchy
public static Iterable<Class<?>> hierarchy(Class<?> type, Reflection.Interfaces interfacesBehavior)
Get anIterable
that can iterate over a class hierarchy in ascending (subclass to superclass) order. Taken from commons-lang3.- Parameters:
type
- the type to get the class hierarchy frominterfacesBehavior
- switch indicating whether to include or exclude interfaces- Returns:
- Iterable an Iterable over the class hierarchy of the given class
-
-