public final class XReflect extends Object
| Modifier and Type | Method and Description |
|---|---|
static int |
calculatePrimitivesLength(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> |
copyFields(T source,
S target) |
static <T,S extends T> |
copyFields(T source,
S target,
Predicate<? super Field> fieldSelector) |
static <T,S extends T> |
copyFields(T source,
S target,
Predicate<? super Field> fieldSelector,
CopyPredicate copySelector) |
static <T> T |
defaultInstantiate(Class<T> type) |
static ClassLoader |
defaultTypeResolvingClassLoader()
Local alias for
ClassLoader.getSystemClassLoader(). |
static String |
deriveFieldIdentifier(Field field) |
static char |
fieldIdentifierDelimiter() |
static Field |
getAnyField(Class<?> c,
Predicate<? super Field> predicate) |
static Field |
getAnyField(Class<?> c,
String name) |
static Method |
getAnyMethod(Class<?> c,
Predicate<? super Method> predicate) |
static Method |
getAnyMethod(Class<?> c,
String name) |
static <T> Class<T> |
getClass(T object) |
static Class<?>[] |
getClassHierarchyInterfaces(Class<?> classClass) |
static <T> Constructor<T> |
getDeclaredConstructor(Class<T> c,
Class<?>... parameterTypes) |
static Class<?> |
getDeclaredEnumClass(Class<?> c) |
static Field |
getDeclaredField(Class<?> c,
String name) |
static Method |
getDeclaredMethod(Class<?> c,
String name,
Class<?>... parameterTypes) |
static int |
getField_int(Field f,
Object obj) |
static Field |
getField(Class<?> c,
String name) |
static String |
getFieldIdentifierClassName(String fieldIdentifier) |
static int |
getFieldIdentifierDelimiterIndex(String identifier) |
static String |
getFieldIdentifierFieldName(String fieldIdentifier) |
static Object |
getFieldValue(Field field,
Object obj)
Calls
Field.get(Object) and wraps the abstraction-destroying checked
IllegalAccessException with a proper IllegalAccessRuntimeException. |
static Field |
getInstanceFieldOfType(Class<?> declaringType,
Class<?> fieldType) |
static Class<?> |
getSuperClassNonNull(Class<?> c) |
static boolean |
hasEnumeratedTypeName(Class<?> type) |
static boolean |
implementsInterface(Class<?> c,
Class<?> interfaceClass) |
static <T> T |
invoke(Constructor<T> constructor,
Object... args) |
static <T> T |
invoke(Method method,
Object instance,
Object... args) |
static boolean |
isAbstract(Class<?> type) |
static boolean |
isAbstract(Method method) |
static boolean |
isActualClass(Class<?> type) |
static boolean |
isDeclaredEnum(Class<?> c) |
static boolean |
isDefaultVisible(Member field) |
static boolean |
isEnum(Class<?> c)
Utility method fixing the WRONGLY implemented
Class.isEnum(). |
static boolean |
isFinal(Member field) |
static boolean |
isInstanceField(Field field) |
static boolean |
isInterfaceOfType(Class<?> interfaceClass,
Class<?> implementedSuperInterface) |
static boolean |
isJavaUtilCollectionType(Class<?> type)
Checks if the passed type is equal to or a sub type of
Collection or Map. |
static boolean |
isNotTransient(Field field) |
static boolean |
isOfAnyType(Class<?> subject,
Class<?>... supertypes) |
static boolean |
isOfAnyType(Class<?> subject,
Iterable<Class<?>> supertypes) |
static boolean |
isOfClassType(Class<?> c,
Class<?> superclass) |
static boolean |
isPrimitive(Field field) |
static boolean |
isPrimitiveTypeName(String typeName) |
static boolean |
isPrivate(Member field) |
static boolean |
isProtected(Member field) |
static boolean |
isProxyClass(Class<?> c) |
static boolean |
isPublic(Member field) |
static boolean |
isReference(Field field) |
static boolean |
isStatic(Member field) |
static boolean |
isStaticFinal(Member field) |
static boolean |
isSubClassOf(Class<?> c,
Class<?> superclass) |
static boolean |
isSubEnum(Class<?> c) |
static boolean |
isSynthetic(Member field) |
static boolean |
isTransient(Field field) |
static boolean |
isValidProxyClass(Class<?> c) |
static <C extends Consumer<? super Method>> |
iterateAllClassMethods(Class<?> clazz,
C logic) |
static <C extends Consumer<? super Method>> |
iterateAllClassMethods(Class<?> clazz,
Class<?> bound,
C logic) |
static <L extends Consumer<Field>> |
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 passed
Consumer on it. |
static <L extends Consumer<Field>> |
iterateDeclaredFieldsUpwards(Class<?> startingClass,
L logic)
Alias for
iterateDeclaredFieldsUpwards(startingClass, Object.class, logic). |
static Class<?> |
iterativeResolveType(ClassLoader classLoader,
String... typeNames)
Alias for
tryIterativeResolveType(ClassLoader, String...) with the following difference:If none of the passed typeNames can be resolved, a ClassNotFoundException listing
all passed typeNames is thrown. |
static Class<?> |
iterativeResolveType(String... typeNames)
|
static char |
nestedClassNameSeparator() |
static Object |
resolveEnumConstantInstance(Class<?> type,
int ordinal) |
static <T> T |
resolveEnumConstantInstanceTyped(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 type
Class). |
static Class<?> |
resolveTypeForName(String typeName)
Uses
Class.forName(String) which uses the calling class's ClassLoader. |
static Field |
setAccessible(Class<?> actualClass,
Field field) |
static <T> Constructor<T> |
setAccessible(Constructor<T> constructor) |
static Field |
setAccessible(Field field) |
static Method |
setAccessible(Method method) |
static void |
setFieldValue(Field field,
Object obj,
Object value)
Calls
Field.set(Object, Object) and wraps the abstraction-destroying checked
IllegalAccessException with a proper IllegalAccessRuntimeException. |
static String |
toFieldName(Field field) |
static String |
toFullQualifiedFieldName(Class<?> actualClass,
Field field) |
static String |
toFullQualifiedFieldName(Class<?> c,
String fieldName) |
static Field |
tryGetDeclaredField(Class<?> declaringClass,
String fieldName) |
static Class<?> |
tryIterativeResolveType(ClassLoader classLoader,
String... typeNames)
This methods attempts to resolve the passed typeNames to
Class instances using
resolveType(String, ClassLoader) one by one. |
static Class<?> |
tryIterativeResolveType(String... typeNames) |
static Class<?> |
tryResolvePrimitiveType(String className) |
static Class<?> |
tryResolveType(String typeName)
|
static Class<?> |
tryResolveType(String typeName,
ClassLoader classLoader)
Calls
resolveType(String, ClassLoader), but suppresses any ClassNotFoundException and returns
null instead. |
static String |
typename_enum() |
static <A> Class<A> |
validateArrayType(Class<A> arrayType) |
static <T,S extends T> |
validateFamiliarClass(T superClassInstance,
S sameOrSubClassInstance)
Checks if
superClassInstance.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) |
public static final <T> T defaultInstantiate(Class<T> type) throws NoSuchMethodRuntimeException, InstantiationRuntimeException
public static final Field setAccessible(Field field) throws SecurityException
SecurityExceptionpublic static final Method setAccessible(Method method) throws SecurityException
SecurityExceptionpublic static final <T> Constructor<T> setAccessible(Constructor<T> constructor) throws SecurityException
SecurityExceptionpublic static final boolean isInstanceField(Field field)
public static final boolean isInterfaceOfType(Class<?> interfaceClass, Class<?> implementedSuperInterface)
public static final boolean implementsInterface(Class<?> c, Class<?> interfaceClass)
public static final Class<?>[] getClassHierarchyInterfaces(Class<?> classClass)
public static final boolean isActualClass(Class<?> type)
public static boolean isEnum(Class<?> c)
Class.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
of Enum. So the correct way of testing a class for being an enum is using
java.lang.Enum.class.isAssignableFrom(...). This method does that.
public static boolean isDeclaredEnum(Class<?> c)
public static boolean isSubEnum(Class<?> c)
public static Object resolveEnumConstantInstance(Class<?> type, int ordinal)
public static <T> T resolveEnumConstantInstanceTyped(Class<T> type, int ordinal)
public static final <L extends Consumer<Field>> L iterateDeclaredFieldsUpwards(Class<?> startingClass, L logic)
iterateDeclaredFieldsUpwards(startingClass, Object.class, logic).L - The logic's contextual type.startingClass - the class whose fields shall be iterated.logic - the Consumer to be executed on each field.public static final <L extends Consumer<Field>> L iterateDeclaredFieldsUpwards(Class<?> startingClass, Class<?> boundingClass, L logic)
Consumer on it.
The declared fields of each class are iterated in reverse order
(from index Class.getDeclaredFields().length - 1 to index 0 ).
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.
L - The logic's contextual type.startingClass - the class whose fields shall be iterated.boundingClass - the class in the hierarchy at which to stop iterating, exclusive bound.logic - the Consumer to be executed on each field.public static final Field getDeclaredField(Class<?> c, String name) throws NoSuchFieldRuntimeException
NoSuchFieldRuntimeExceptionpublic static final Method getDeclaredMethod(Class<?> c, String name, Class<?>... parameterTypes) throws NoSuchFieldRuntimeException
NoSuchFieldRuntimeExceptionpublic static final <T> Constructor<T> getDeclaredConstructor(Class<T> c, Class<?>... parameterTypes) throws NoSuchFieldRuntimeException
NoSuchFieldRuntimeExceptionpublic static final Field getField(Class<?> c, String name) throws NoSuchFieldRuntimeException
NoSuchFieldRuntimeExceptionpublic static final Field getAnyField(Class<?> c, String name) throws NoSuchFieldRuntimeException
NoSuchFieldRuntimeExceptionpublic static final Field getAnyField(Class<?> c, Predicate<? super Field> predicate) throws NoSuchFieldRuntimeException
NoSuchFieldRuntimeExceptionpublic static final Field getInstanceFieldOfType(Class<?> declaringType, Class<?> fieldType) throws NoSuchFieldRuntimeException
NoSuchFieldRuntimeExceptionpublic static final Method getAnyMethod(Class<?> c, String name) throws NoSuchMethodRuntimeException
NoSuchMethodRuntimeExceptionpublic static final Method getAnyMethod(Class<?> c, Predicate<? super Method> predicate) throws NoSuchMethodRuntimeException
NoSuchMethodRuntimeExceptionpublic static final <C extends Consumer<? super Method>> C iterateAllClassMethods(Class<?> clazz, C logic)
public static final <C extends Consumer<? super Method>> C iterateAllClassMethods(Class<?> clazz, Class<?> bound, C logic)
public static final boolean isFinal(Member field)
public static final boolean isStatic(Member field)
public static final boolean isSynthetic(Member field)
public static final boolean isStaticFinal(Member field)
public static final boolean isPrimitive(Field field)
public static final boolean isReference(Field field)
public static final boolean isTransient(Field field)
public static final boolean isNotTransient(Field field)
public static final boolean isPrivate(Member field)
public static final boolean isProtected(Member field)
public static final boolean isPublic(Member field)
public static final boolean isDefaultVisible(Member field)
public static final boolean isAbstract(Class<?> type)
public static final boolean isAbstract(Method method)
public static final Object getFieldValue(Field field, Object obj)
Field.get(Object) and wraps the abstraction-destroying checked
IllegalAccessException with a proper IllegalAccessRuntimeException.field - the field from which the value shall be extracted.obj - object from which the represented field's value is to be extractedobj;
primitive values are wrapped in an appropriate object before being returnedpublic static final <T> T invoke(Constructor<T> constructor, Object... args)
public static final void setFieldValue(Field field, Object obj, Object value)
Field.set(Object, Object) and wraps the abstraction-destroying checked
IllegalAccessException with a proper IllegalAccessRuntimeException.field - the field to be modifiedobj - the object whose field should be modifiedvalue - the new value for the field of obj being modifiedpublic static int getField_int(Field f, Object obj) throws IllegalAccessRuntimeException
IllegalAccessRuntimeExceptionpublic static final Class<?> resolveType(String typeName, ClassLoader classLoader) throws LinkageError, ExceptionInInitializerError, ClassNotFoundException
Class).
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.
typeName - the type name to be resolved, primitive name or full qualified type name.classLoader - class loader from which the class must be loadedClass)LinkageError - see Class.forName(String)ExceptionInInitializerError - see Class.forName(String)ClassNotFoundException - see Class.forName(String)public static final Class<?> resolveTypeForName(String typeName) throws LinkageError, ExceptionInInitializerError, ClassNotFoundException
Class.forName(String) which uses the calling class's ClassLoader.typeName - the type name to be resolved, primitive name or full qualified type name.Class)LinkageError - see Class.forName(String)ExceptionInInitializerError - see Class.forName(String)ClassNotFoundException - see Class.forName(String)public static final Class<?> tryResolveType(String typeName, ClassLoader classLoader)
resolveType(String, ClassLoader), but suppresses any ClassNotFoundException and returns
null instead. 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.typeName - the type name to be resolved, primitive name or full qualified type name.classLoader - class loader from which the class must be loadedClass instance representing the passed class name or null if unresolevable.public static final Class<?> iterativeResolveType(ClassLoader classLoader, String... typeNames) throws ClassNotFoundException
tryIterativeResolveType(ClassLoader, String...) with the following difference:ClassNotFoundException listing
all passed typeNames is thrown.classLoader - class loader from which the class must be loadedtypeNames - the full qualified type names to be attempted to be resolved one by one.Class instance.ClassNotFoundException - if none of the passed typeNames could have been resolved.tryIterativeResolveType(ClassLoader, String...)public static final Class<?> tryIterativeResolveType(ClassLoader classLoader, String... typeNames)
Class instances using
resolveType(String, ClassLoader) one by one.
The Class instance of the first successful attempt is returned.
If none of the passed typeNames can be resolved, null is returned.
See iterativeResolveType(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.
classLoader - class loader from which the class must be loadedtypeNames - the full qualified type names to be attempted to be resolved one by one.Class instance or nullresolveType(String, ClassLoader)public static final ClassLoader defaultTypeResolvingClassLoader()
ClassLoader.getSystemClassLoader().public static final Class<?> resolveType(String typeName) throws LinkageError, ExceptionInInitializerError, ClassNotFoundException
resolveType(String, ClassLoader) with defaultTypeResolvingClassLoader().
Make sure this is a suitable ClassLoader when using this method.typeName - the type name to be resolved, primitive name or full qualified type name.Class)LinkageError - see Class.forName(String)ExceptionInInitializerError - see Class.forName(String)ClassNotFoundException - see Class.forName(String)public static final Class<?> tryResolveType(String typeName)
tryResolveType(String, ClassLoader) with defaultTypeResolvingClassLoader().
Make sure this is a suitable ClassLoader when using this method.typeName - the type name to be resolved, primitive name or full qualified type name.Class instance representing the passed class name or null if unresolevable.public static final Class<?> iterativeResolveType(String... typeNames) throws ClassNotFoundException
iterativeResolveType(ClassLoader, String...) with defaultTypeResolvingClassLoader().
Make sure this is a suitable ClassLoader when using this method.typeNames - the full qualified type names to be attempted to be resolved one by one.Class instance.ClassNotFoundException - if none of the passed typeNames could have been resolved.public static final Field tryGetDeclaredField(Class<?> declaringClass, String fieldName)
public static boolean isPrimitiveTypeName(String typeName)
public static final boolean isOfAnyType(Class<?> subject, Iterable<Class<?>> supertypes)
public static <T> Class<T> getClass(T object)
public static char fieldIdentifierDelimiter()
public static String typename_enum()
public static char nestedClassNameSeparator()
public static String toFullQualifiedFieldName(Class<?> actualClass, Field field)
public static String toFullQualifiedFieldName(Class<?> c, String fieldName)
public static int getFieldIdentifierDelimiterIndex(String identifier)
public static String getFieldIdentifierClassName(String fieldIdentifier)
public static String getFieldIdentifierFieldName(String fieldIdentifier)
public static <A> Class<A> validateNonPrimitiveType(Class<A> primitiveType)
public static <T> Instantiator<T> WrapDefaultConstructor(Class<T> type) throws NoSuchMethodRuntimeException
NoSuchMethodRuntimeExceptionpublic static boolean isJavaUtilCollectionType(Class<?> type)
Collection or Map.
Sad that such a method is necessary in the first place, but here we are.
(See XMap for an example on how to do it correctly.)
type - the type to be checked.Collection,
Mappublic static boolean hasEnumeratedTypeName(Class<?> type)
public static boolean isProxyClass(Class<?> c)
public static boolean isValidProxyClass(Class<?> c)
public static Field[] collectPrimitiveFieldsByByteSize(Field[] fields, int byteSize)
public static final Field[] collectInstanceFields(Class<?> objectClass, Predicate<? super Field> selector)
public static int calculatePrimitivesLength(Field[] primFields)
public static <T,S extends T> S copyFields(T source,
S target)
public static <T,S extends T> S copyFields(T source,
S target,
Predicate<? super Field> fieldSelector)
public static <T,S extends T> S copyFields(T source,
S target,
Predicate<? super Field> fieldSelector,
CopyPredicate copySelector)
public static <T,S extends T> void validateFamiliarClass(T superClassInstance,
S sameOrSubClassInstance)
superClassInstance.getClass().isAssignableFrom(sameOrSubClassInstance.getClass())T - the super typeS - the same or sub typesuperClassInstance - the super instancesameOrSubClassInstance - ths sub or sub instanceIllegalArgumentException - if the check failsCopyright © 2022 MicroStream Software. All rights reserved.