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 classReflection.ClassHierarchystatic classReflection.FullHierarchystatic classReflection.InterfacesInclusivity 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> Tfind(Class<?> clazz, Function<Class<?>,T> search)Perform a search against the class hierarchy.static ObjectgetAnnotationValue(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 forPrivilizingClass.getDeclaredConstructors().static FieldgetDeclaredField(Class<?> clazz, String fieldName)Get the declared field fromclazz.static Field[]getDeclaredFields(Class<?> clazz)Convenient point forPrivilizingClass.getDeclaredFields().static MethodgetDeclaredMethod(Class<?> clazz, String name, Class<?>... parameters)Get the declared method fromclazz.static Method[]getDeclaredMethods(Class<?> clazz)Convenient point forPrivilizingClass.getDeclaredMethods().static StringgetProperty(String name)Convenient point forPrivilizingSystem.getProperty(String).static MethodgetPublicMethod(Class<?> clazz, String methodName, Class<?>... parameterTypes)static Iterable<Class<?>>hierarchy(Class<?> type, Reflection.Interfaces interfacesBehavior)Get anIterablethat can iterate over a class hierarchy in ascending (subclass to superclass) order.static ClassLoaderloaderFromClassOrThread(Class<?> clazz)static ClassLoaderloaderFromThreadOrClass(Class<?> fallbackClass)Get aClassLoaderpreferringThread.getContextClassLoader()of currentThreadover that offallbackClass.static voidmakeAccessible(AccessibleObject o)Set the accessibility ofoto true.static <T> TnewInstance(Class<T> cls)Construct a new instance ofclsusing 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
clsorclsifclsis not a primitive.nullif 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:
IllegalAccessExceptionInvocationTargetException
-
loaderFromClassOrThread
public static ClassLoader loaderFromClassOrThread(Class<?> clazz)
- Parameters:
clazz-- Returns:
ClassLoader
-
loaderFromThreadOrClass
public static ClassLoader loaderFromThreadOrClass(Class<?> fallbackClass)
Get aClassLoaderpreferringThread.getContextClassLoader()of currentThreadover 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 forPrivilizingSystem.getProperty(String).- Parameters:
name-- Returns:
- String
-
getDeclaredField
public static Field getDeclaredField(Class<?> clazz, String fieldName)
Get the declared field fromclazz.- Parameters:
clazz-fieldName-- Returns:
Fieldornull
-
getDeclaredFields
public static Field[] getDeclaredFields(Class<?> clazz)
Convenient point forPrivilizingClass.getDeclaredFields().- Parameters:
clazz-- Returns:
Fieldarray
-
getDeclaredConstructor
public static <T> Constructor<T> getDeclaredConstructor(Class<T> clazz, Class<?>... parameters)
Get the declared constructor fromclazz.- Parameters:
clazz-parameters-- Returns:
Constructorornull
-
getDeclaredMethod
public static Method getDeclaredMethod(Class<?> clazz, String name, Class<?>... parameters)
Get the declared method fromclazz.- Parameters:
clazz-name-parameters-- Returns:
Methodornull
-
getDeclaredMethods
public static Method[] getDeclaredMethods(Class<?> clazz)
Convenient point forPrivilizingClass.getDeclaredMethods().- Parameters:
clazz-- Returns:
Methodarray
-
getDeclaredConstructors
public static <T> Constructor<? extends T>[] getDeclaredConstructors(Class<T> clazz)
Convenient point forPrivilizingClass.getDeclaredConstructors().- Parameters:
clazz-- Returns:
Constructorarray
-
getPublicMethod
public static Method getPublicMethod(Class<?> clazz, String methodName, Class<?>... parameterTypes)
- Parameters:
clazz-methodName-- Returns:
Methodornull
-
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 ofclsusing its default constructor.- Parameters:
cls-- Returns:
- T
-
makeAccessible
public static void makeAccessible(AccessibleObject o)
Set the accessibility ofoto true.- Parameters:
o-
-
hierarchy
public static Iterable<Class<?>> hierarchy(Class<?> type, Reflection.Interfaces interfacesBehavior)
Get anIterablethat 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
-
-