Class XReflect
public final class XReflect extends Object
-
Method Summary
Modifier and Type Method Description static intcalculatePrimitivesLength(Field[] primFields)static Field[]collectInstanceFields(Class<?> objectClass)static Field[]collectInstanceFields(Class<?> objectClass, Predicate<? super Field> selector)static Field[]collectPrimitiveFieldsByByteSize(Field[] fields, int byteSize)static <T, S extends T>
ScopyFields(T source, S target)static <T, S extends T>
ScopyFields(T source, S target, Predicate<? super Field> fieldSelector)static <T, S extends T>
ScopyFields(T source, S target, Predicate<? super Field> fieldSelector, CopyPredicate copySelector)static <T> TdefaultInstantiate(Class<T> type)static ClassLoaderdefaultTypeResolvingClassLoader()Local alias forClassLoader.getSystemClassLoader().static StringderiveFieldIdentifier(Field field)static charfieldIdentifierDelimiter()static FieldgetAnyField(Class<?> c, String name)static FieldgetAnyField(Class<?> c, Predicate<? super Field> predicate)static MethodgetAnyMethod(Class<?> c, String name)static MethodgetAnyMethod(Class<?> c, Predicate<? super Method> predicate)static <T> Class<T>getClass(T object)*sigh*static Class<?>[]getClassHierarchyInterfaces(Class<?> classClass)static <T> Constructor<T>getDeclaredConstructor(Class<T> c, Class<?>... parameterTypes)static Class<?>getDeclaredEnumClass(Class<?> c)static FieldgetDeclaredField(Class<?> c, String name)static MethodgetDeclaredMethod(Class<?> c, String name, Class<?>... parameterTypes)static FieldgetField(Class<?> c, String name)static intgetField_int(Field f, Object obj)static StringgetFieldIdentifierClassName(String fieldIdentifier)static intgetFieldIdentifierDelimiterIndex(String identifier)static StringgetFieldIdentifierFieldName(String fieldIdentifier)static ObjectgetFieldValue(Field field, Object obj)CallsField.get(Object)and wraps the abstraction-destroying checkedIllegalAccessExceptionwith a properIllegalAccessRuntimeException.static FieldgetInstanceFieldOfType(Class<?> declaringType, Class<?> fieldType)static Class<?>getSuperClassNonNull(Class<?> c)static booleanhasEnumeratedTypeName(Class<?> type)static booleanimplementsInterface(Class<?> c, Class<?> interfaceClass)static <T> Tinvoke(Constructor<T> constructor, Object... args)static <T> Tinvoke(Method method, Object instance, Object... args)static booleanisAbstract(Class<?> type)static booleanisAbstract(Method method)static booleanisActualClass(Class<?> type)static booleanisDeclaredEnum(Class<?> c)static booleanisDefaultVisible(Member field)static booleanisEnum(Class<?> c)Utility method fixing the WRONGLY implementedClass.isEnum().static booleanisFinal(Member field)static booleanisInstanceField(Field field)static booleanisInterfaceOfType(Class<?> interfaceClass, Class<?> implementedSuperInterface)static booleanisJavaUtilCollectionType(Class<?> type)Checks if the passed type is equal to or a sub type ofCollectionorMap.static booleanisNotTransient(Field field)static booleanisOfAnyType(Class<?> subject, Class<?>... supertypes)static booleanisOfAnyType(Class<?> subject, Iterable<Class<?>> supertypes)static booleanisOfClassType(Class<?> c, Class<?> superclass)static booleanisPrimitive(Field field)static booleanisPrimitiveTypeName(String typeName)static booleanisPrivate(Member field)static booleanisProtected(Member field)static booleanisProxyClass(Class<?> c)static booleanisPublic(Member field)static booleanisReference(Field field)static booleanisStatic(Member field)static booleanisStaticFinal(Member field)static booleanisSubClassOf(Class<?> c, Class<?> superclass)static booleanisSubEnum(Class<?> c)static booleanisSynthetic(Member field)static booleanisTransient(Field field)static booleanisValidProxyClass(Class<?> c)static <C extends Consumer<? super Method>>
CiterateAllClassMethods(Class<?> clazz, C logic)static <C extends Consumer<? super Method>>
CiterateAllClassMethods(Class<?> clazz, Class<?> bound, C logic)static <L extends Consumer<Field>>
LiterateDeclaredFieldsUpwards(Class<?> startingClass, Class<?> boundingClass, L logic)Iterates over every declared field of all classes upwards starting at startingClass until class boundingClass is reached and executes the passedConsumeron it.static <L extends Consumer<Field>>
LiterateDeclaredFieldsUpwards(Class<?> startingClass, L logic)Alias foriterateDeclaredFieldsUpwards(startingClass, Object.class, logic).static Class<?>iterativeResolveType(ClassLoader classLoader, String... typeNames)Alias fortryIterativeResolveType(ClassLoader, String...)with the following difference:
If none of the passed typeNames can be resolved, aClassNotFoundExceptionlisting all passed typeNames is thrown.static Class<?>iterativeResolveType(String... typeNames)static charnestedClassNameSeparator()static ObjectresolveEnumConstantInstance(Class<?> type, int ordinal)static <T> TresolveEnumConstantInstanceTyped(Class<T> type, int ordinal)static Class<?>resolveType(String typeName)static Class<?>resolveType(String typeName, ClassLoader classLoader)Resolves the passed type name to a runtime type (instance of typeClass).static Class<?>resolveTypeForName(String typeName)UsesClass.forName(String)which uses the calling class'sClassLoader.static FieldsetAccessible(Class<?> actualClass, Field field)static <T> Constructor<T>setAccessible(Constructor<T> constructor)static FieldsetAccessible(Field field)static MethodsetAccessible(Method method)static voidsetFieldValue(Field field, Object obj, Object value)CallsField.set(Object, Object)and wraps the abstraction-destroying checkedIllegalAccessExceptionwith a properIllegalAccessRuntimeException.static StringtoFieldName(Field field)static StringtoFullQualifiedFieldName(Class<?> actualClass, Field field)static StringtoFullQualifiedFieldName(Class<?> c, String fieldName)static FieldtryGetDeclaredField(Class<?> declaringClass, String fieldName)static Class<?>tryIterativeResolveType(ClassLoader classLoader, String... typeNames)This methods attempts to resolve the passed typeNames toClassinstances usingresolveType(String, ClassLoader)one by one.static Class<?>tryIterativeResolveType(String... typeNames)static Class<?>tryResolvePrimitiveType(String className)static Class<?>tryResolveType(String className)static Class<?>tryResolveType(String className, ClassLoader classLoader)CallsresolveType(String, ClassLoader), but suppresses anyClassNotFoundExceptionand returnsnullinstead.static Stringtypename_enum()static <A> Class<A>validateArrayType(Class<A> arrayType)static <T, S extends T>
voidvalidateFamiliarClass(T superClassInstance, S sameOrSubClassInstance)Checks ifsuperClassInstance.getClass().isAssignableFrom(sameOrSubClassInstance.getClass())static <A> Class<A>validateInterfaceType(Class<A> type)static <T> Class<T>validateIsEnum(Class<T> type)static <A> Class<A>validateNonArrayType(Class<A> type)static <A> Class<A>validateNonInterfaceType(Class<A> type)static <A> Class<A>validateNonPrimitiveType(Class<A> primitiveType)static <A> Class<A>validatePrimitiveType(Class<A> primitiveType)static <T> Instantiator<T>WrapDefaultConstructor(Class<T> type)
-
Method Details
-
defaultInstantiate
public static final <T> T defaultInstantiate(Class<T> type) throws NoSuchMethodRuntimeException, InstantiationRuntimeException -
setAccessible
-
setAccessible
- Throws:
SecurityException
-
setAccessible
- Throws:
SecurityException
-
setAccessible
public static final <T> Constructor<T> setAccessible(Constructor<T> constructor) throws SecurityException- Throws:
SecurityException
-
isInstanceField
-
toFieldName
-
isInterfaceOfType
-
implementsInterface
-
getClassHierarchyInterfaces
-
isOfClassType
-
isSubClassOf
-
isActualClass
-
isEnum
Utility method fixing the WRONGLY implementedClass.isEnum().Their description is weird ("if this class was declared as an enum in the source code") and the implemented behavior is dangerous and useless to identify all classes of instances that are enums.
For enum anonymous inner class instances (writing { ... } behind an enum constant),
Class.isEnum()returns false on the generated type. That is a bug since the type is still an enum, a sub class ofEnum. So the correct way of testing a class for being an enum is usingjava.lang.Enum.class.isAssignableFrom(...). This method does that. -
isDeclaredEnum
-
isSubEnum
-
getDeclaredEnumClass
-
resolveEnumConstantInstance
-
resolveEnumConstantInstanceTyped
-
validateIsEnum
-
iterateDeclaredFieldsUpwards
public static final <L extends Consumer<Field>> L iterateDeclaredFieldsUpwards(Class<?> startingClass, L logic)Alias foriterateDeclaredFieldsUpwards(startingClass, Object.class, logic).- Type Parameters:
L- The logic's contextual type.- Parameters:
startingClass- the class whose fields shall be iterated.logic- theConsumerto be executed on each field.- Returns:
- the passed logic.
-
iterateDeclaredFieldsUpwards
public static final <L extends Consumer<Field>> L iterateDeclaredFieldsUpwards(Class<?> startingClass, Class<?> boundingClass, L logic)Iterates over every declared field of all classes upwards starting at startingClass until class boundingClass is reached and executes the passedConsumeron it.The declared fields of each class are iterated in reverse order (from index
Class.getDeclaredFields().length - 1to index0).This method is useful to maintain the natural declaration order of the fields, iterating from the last declared field of the lowest class (the passed class itself) to the first declared field of the highest class declaring a field.
- Type Parameters:
L- The logic's contextual type.- Parameters:
startingClass- the class whose fields shall be iterated.boundingClass- the class in the hierarchy at which to stop iterating, exclusive bound.logic- theConsumerto be executed on each field.- Returns:
- the passed logic.
-
getDeclaredField
public static final Field getDeclaredField(Class<?> c, String name) throws NoSuchFieldRuntimeException- Throws:
NoSuchFieldRuntimeException
-
getDeclaredMethod
public static final Method getDeclaredMethod(Class<?> c, String name, Class<?>... parameterTypes) throws NoSuchFieldRuntimeException- Throws:
NoSuchFieldRuntimeException
-
getDeclaredConstructor
public static final <T> Constructor<T> getDeclaredConstructor(Class<T> c, Class<?>... parameterTypes) throws NoSuchFieldRuntimeException- Throws:
NoSuchFieldRuntimeException
-
getField
- Throws:
NoSuchFieldRuntimeException
-
getAnyField
- Throws:
NoSuchFieldRuntimeException
-
getAnyField
public static final Field getAnyField(Class<?> c, Predicate<? super Field> predicate) throws NoSuchFieldRuntimeException- Throws:
NoSuchFieldRuntimeException
-
getInstanceFieldOfType
public static final Field getInstanceFieldOfType(Class<?> declaringType, Class<?> fieldType) throws NoSuchFieldRuntimeException- Throws:
NoSuchFieldRuntimeException
-
getAnyMethod
public static final Method getAnyMethod(Class<?> c, String name) throws NoSuchMethodRuntimeException- Throws:
NoSuchMethodRuntimeException
-
getAnyMethod
public static final Method getAnyMethod(Class<?> c, Predicate<? super Method> predicate) throws NoSuchMethodRuntimeException- Throws:
NoSuchMethodRuntimeException
-
iterateAllClassMethods
-
iterateAllClassMethods
-
isFinal
-
isStatic
-
isSynthetic
-
isStaticFinal
-
isPrimitive
-
isReference
-
isTransient
-
isNotTransient
-
isPrivate
-
isProtected
-
isPublic
-
isDefaultVisible
-
isAbstract
-
isAbstract
-
getFieldValue
CallsField.get(Object)and wraps the abstraction-destroying checkedIllegalAccessExceptionwith a properIllegalAccessRuntimeException.- Parameters:
field- the field from which the value shall be extracted.obj- object from which the represented field's value is to be extracted- Returns:
- the value of the represented field in object
obj; primitive values are wrapped in an appropriate object before being returned
-
invoke
-
invoke
-
setFieldValue
CallsField.set(Object, Object)and wraps the abstraction-destroying checkedIllegalAccessExceptionwith a properIllegalAccessRuntimeException.- Parameters:
field- the field to be modifiedobj- the object whose field should be modifiedvalue- the new value for the field ofobjbeing modified
-
getField_int
- Throws:
IllegalAccessRuntimeException
-
resolveType
public static final Class<?> resolveType(String typeName, ClassLoader classLoader) throws LinkageError, ExceptionInInitializerError, ClassNotFoundExceptionResolves the passed type name to a runtime type (instance of typeClass). In contrary to JDK's type resolving mechanisms, this method resolves primitive type names, as well.Note on naming:
1.) Looking up a runtime type instance for a type name string is best described as "resolving" the type.
2.) The things that are resolved are TYPES (classes, interfaces, arrays and in later Java versions enums and annotations), not just classes. That the java inventors seemingly didn't understand their own type system and just called everything "Class" on the API-level,* even interfaces, is just an error that should be repeated as less as possible.- Parameters:
typeName- the type name to be resolved, primitive name or full qualified type name.- Returns:
- the resolved type instance (of type
Class) - Throws:
LinkageError- seeClass.forName(String)ExceptionInInitializerError- seeClass.forName(String)ClassNotFoundException- seeClass.forName(String)
-
resolveTypeForName
public static final Class<?> resolveTypeForName(String typeName) throws LinkageError, ExceptionInInitializerError, ClassNotFoundExceptionUsesClass.forName(String)which uses the calling class'sClassLoader.- Parameters:
typeName-- Throws:
LinkageErrorExceptionInInitializerErrorClassNotFoundException
-
tryResolveType
CallsresolveType(String, ClassLoader), but suppresses anyClassNotFoundExceptionand returnsnullinstead. This is useful if the passed class name is only potentially resolvable at runtime and is still valid if not. Example: resolving a old type dictionary as far as possible and marking the not resolvable types as unresolvable.- Parameters:
className-- Returns:
- the
Classinstance representing the passed class name ornullif unresolevable.
-
iterativeResolveType
public static final Class<?> iterativeResolveType(ClassLoader classLoader, String... typeNames) throws ClassNotFoundExceptionAlias fortryIterativeResolveType(ClassLoader, String...)with the following difference:
If none of the passed typeNames can be resolved, aClassNotFoundExceptionlisting all passed typeNames is thrown.- Parameters:
typeNames- the full qualified type names to be attempted to be resolved one by one.- Returns:
- the first successfully resolved
Classinstance. - Throws:
ClassNotFoundException- if none of the passed typeNames could have been resolved.- See Also:
tryIterativeResolveType(ClassLoader, String...)
-
tryIterativeResolveType
This methods attempts to resolve the passed typeNames toClassinstances usingresolveType(String, ClassLoader)one by one. TheClassinstance of the first successful attempt is returned. If none of the passed typeNames can be resolved, null is returned. SeeiterativeResolveType(ClassLoader, String...)for an exception-throwing version.Note:
While it is generally a bad idea to just use a trial and error approach until something works, a logic like this is required to resolve types whose packages changes accross different versions of a library. If the different full qualified class names are known, they can be used in an iterative attempt to resolve the class, hence avoiding hard dependencies to certain library versions in the using code by moving type names from imports at compile time to dynamic class resolving at runtime.
However, this approach has its limits, of course. If too much changes (field names, method names, parameters, behavior) the dynamic strategy results in chaos as the compiler gets more and more circumvented and more and more source code is transformed into contextless plain strings.
Therefore, when in doubt, it is preferable to stick to the general notion of this method being a "bad idea" and finding a more reliable solution.- Parameters:
typeNames- the full qualified type names to be attempted to be resolved one by one.- Returns:
- the first successfully resolved
Classinstance or null - See Also:
resolveType(String, ClassLoader)
-
defaultTypeResolvingClassLoader
Local alias forClassLoader.getSystemClassLoader().- Returns:
- the system class loader.
-
resolveType
public static final Class<?> resolveType(String typeName) throws LinkageError, ExceptionInInitializerError, ClassNotFoundExceptionCallsresolveType(String, ClassLoader)withdefaultTypeResolvingClassLoader(). Make sure this is a suitableClassLoaderwhen using this method.- Parameters:
typeName-- Throws:
LinkageErrorExceptionInInitializerErrorClassNotFoundException
-
tryResolveType
CallstryResolveType(String, ClassLoader)withdefaultTypeResolvingClassLoader(). Make sure this is a suitableClassLoaderwhen using this method.- Parameters:
className-
-
iterativeResolveType
public static final Class<?> iterativeResolveType(String... typeNames) throws ClassNotFoundExceptionCallsiterativeResolveType(ClassLoader, String...)withdefaultTypeResolvingClassLoader(). Make sure this is a suitableClassLoaderwhen using this method.- Parameters:
typeNames-- Throws:
ClassNotFoundException
-
tryIterativeResolveType
-
tryGetDeclaredField
-
tryResolvePrimitiveType
-
isPrimitiveTypeName
-
isOfAnyType
-
isOfAnyType
-
getClass
*sigh*- Parameters:
object-
-
fieldIdentifierDelimiter
public static char fieldIdentifierDelimiter() -
typename_enum
-
nestedClassNameSeparator
public static char nestedClassNameSeparator() -
toFullQualifiedFieldName
-
deriveFieldIdentifier
-
toFullQualifiedFieldName
-
getFieldIdentifierDelimiterIndex
-
getFieldIdentifierClassName
-
getFieldIdentifierFieldName
-
validateInterfaceType
-
validateNonInterfaceType
-
validateNonArrayType
-
validateArrayType
-
validatePrimitiveType
-
validateNonPrimitiveType
-
WrapDefaultConstructor
public static <T> Instantiator<T> WrapDefaultConstructor(Class<T> type) throws NoSuchMethodRuntimeException- Throws:
NoSuchMethodRuntimeException
-
isJavaUtilCollectionType
Checks if the passed type is equal to or a sub type ofCollectionorMap.Sad that such a method is necessary in the first place, but here we are.
(SeeXMapfor an example on how to do it correctly.)- Parameters:
type- the type to be checked.- Returns:
- whether or not the passed type is a java.util collection "in the broader sense".
- See Also:
Collection,Map
-
hasEnumeratedTypeName
-
isProxyClass
-
isValidProxyClass
-
collectPrimitiveFieldsByByteSize
-
collectInstanceFields
-
collectInstanceFields
-
calculatePrimitivesLength
-
copyFields
public static <T, S extends T> S copyFields(T source, S target) -
copyFields
-
copyFields
public static <T, S extends T> S copyFields(T source, S target, Predicate<? super Field> fieldSelector, CopyPredicate copySelector) -
validateFamiliarClass
public static <T, S extends T> void validateFamiliarClass(T superClassInstance, S sameOrSubClassInstance)Checks ifsuperClassInstance.getClass().isAssignableFrom(sameOrSubClassInstance.getClass())- Type Parameters:
T-S-- Parameters:
superClassInstance-sameOrSubClassInstance-
-
getSuperClassNonNull
-