Package io.microsphere.reflect
Class ReflectionUtils
- java.lang.Object
-
- io.microsphere.util.BaseUtils
-
- io.microsphere.reflect.ReflectionUtils
-
public abstract class ReflectionUtils extends BaseUtils
Reflection Utility class , generic methods are defined fromFieldUtils,MethodUtils,ConstructorUtils- Since:
- 1.0.0
- Version:
- 1.0.0
- Author:
- Mercy
- See Also:
Method,Field,Constructor,Array,MethodUtils,FieldUtils,ConstructorUtils
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.StringSUN_REFLECT_REFLECTION_CLASS_NAMESun JDK implementation class: full name of sun.reflect.Reflection
-
Constructor Summary
Constructors Constructor Description ReflectionUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidassertArrayIndex(java.lang.Object array, int index)Assert array indexstatic voidassertArrayType(java.lang.Object array)Assert the object is array or notstatic voidassertFieldMatchType(java.lang.Object object, java.lang.String fieldName, java.lang.Class<?> expectedType)Assert Field type matchstatic <T> java.util.Set<java.lang.Class<T>>findHierarchicalTypes(java.lang.Class<?> sourceClass, java.lang.Class<T> matchType)Find the hierarchical types from the sourceclassby specifiedtype.static java.util.Set<java.lang.reflect.ParameterizedType>findParameterizedTypes(java.lang.Class<?> sourceClass)Find theSetofParameterizedTypestatic java.lang.Class<?>getCallerClass()Get caller classstatic java.lang.Class<?>getCallerClass(int invocationFrame)Get the caller classstatic java.lang.StringgetCallerClassName()Get Caller classstatic <T> TgetProperty(java.lang.Object bean, java.lang.String methodName)Get the value from the specified bean and its getter method.static booleanisSupportedSunReflectReflection()Is supported sun.reflect.Reflection or notstatic java.util.Map<java.lang.String,java.lang.Object>readFieldsAsMap(java.lang.Object object)Read fields value asMapstatic java.lang.Class[]resolveTypes(java.lang.Object... values)Resolve the types of the specified valuesstatic <T> java.util.List<T>toList(java.lang.Object array)ConvertArrayobject toList
-
-
-
Field Detail
-
SUN_REFLECT_REFLECTION_CLASS_NAME
public static final java.lang.String SUN_REFLECT_REFLECTION_CLASS_NAME
Sun JDK implementation class: full name of sun.reflect.Reflection- See Also:
- Constant Field Values
-
-
Method Detail
-
isSupportedSunReflectReflection
public static boolean isSupportedSunReflectReflection()
Is supported sun.reflect.Reflection or not- Returns:
trueif supported
-
getCallerClassName
@Nonnull public static java.lang.String getCallerClassName()
Get Caller class- Returns:
- Get the Class name that called the method
-
getCallerClass
@Nonnull public static java.lang.Class<?> getCallerClass() throws java.lang.IllegalStateExceptionGet caller class For instance,package com.acme; import ...; class Foo { public void bar(){ } }- Returns:
- Get caller class
- Throws:
java.lang.IllegalStateException- If the caller class cannot be found
-
getCallerClass
public static java.lang.Class<?> getCallerClass(int invocationFrame)
Get the caller class- Parameters:
invocationFrame- The frame of method invocation- Returns:
nullif not found
-
assertArrayIndex
public static void assertArrayIndex(java.lang.Object array, int index) throws java.lang.IllegalArgumentExceptionAssert array index- Parameters:
array- Array objectindex- index- Throws:
java.lang.IllegalArgumentException- seeassertArrayType(Object)java.lang.ArrayIndexOutOfBoundsException- Ifindexis less than 0 or equals or greater than length of array
-
assertArrayType
public static void assertArrayType(java.lang.Object array) throws java.lang.IllegalArgumentExceptionAssert the object is array or not- Parameters:
array- asserted object- Throws:
java.lang.IllegalArgumentException- if the object is not a array
-
assertFieldMatchType
public static void assertFieldMatchType(java.lang.Object object, java.lang.String fieldName, java.lang.Class<?> expectedType) throws java.lang.IllegalArgumentExceptionAssert Field type match- Parameters:
object- ObjectfieldName- field nameexpectedType- expected type- Throws:
java.lang.IllegalArgumentException- if type is not matched
-
toList
@Nonnull public static <T> java.util.List<T> toList(java.lang.Object array) throws java.lang.IllegalArgumentExceptionConvertArrayobject toList- Parameters:
array- array object- Returns:
List- Throws:
java.lang.IllegalArgumentException- if the object argument is not an array
-
readFieldsAsMap
@Nonnull public static java.util.Map<java.lang.String,java.lang.Object> readFieldsAsMap(java.lang.Object object)
Read fields value asMap- Parameters:
object- object to be read- Returns:
- fields value as
Map
-
findParameterizedTypes
public static java.util.Set<java.lang.reflect.ParameterizedType> findParameterizedTypes(java.lang.Class<?> sourceClass)
Find theSetofParameterizedType- Parameters:
sourceClass- the sourceclass- Returns:
- non-null read-only
Set
-
findHierarchicalTypes
public static <T> java.util.Set<java.lang.Class<T>> findHierarchicalTypes(java.lang.Class<?> sourceClass, java.lang.Class<T> matchType)Find the hierarchical types from the sourceclassby specifiedtype.- Type Parameters:
T- the type to match- Parameters:
sourceClass- the sourceclassmatchType- the type to match- Returns:
- non-null read-only
Set
-
getProperty
public static <T> T getProperty(java.lang.Object bean, java.lang.String methodName)Get the value from the specified bean and its getter method.- Type Parameters:
T- the type of property value- Parameters:
bean- the bean instancemethodName- the name of getter- Returns:
-
resolveTypes
public static java.lang.Class[] resolveTypes(java.lang.Object... values)
Resolve the types of the specified values- Parameters:
values- the values- Returns:
- If can't be resolved, return
empty class array
-
-