-
Method Summary
Modifier and TypeMethodDescriptiongetAllFields(Class<?> type, boolean includeStatic, MethodHandles.Lookup lookup) Retrieves all fields from a given class, including fields declared in its superclasses (except for fields of the `Object` class) and (optionally) static fields.getAllFields(Class<?> type, MethodHandles.Lookup lookup) Retrieves all non-static fields from a given class, including fields declared in its superclasses (except for fields the `Object` class).static <DTO> Constructor<DTO>[]getConstructors(Class<DTO> dtoClass) static FieldRetrieves a specific field from the specified class or its superclasses.static Class<?> getGenericType(Field field) Retrieves the first generic type argument of a givenField, if the field is parameterized.static Class<?>[]getGenericTypes(Field field) Retrieves the generic type arguments of a givenField, if the field is parameterized.static Class<?>[]getGenericTypes(Type genericType) Retrieves the generic type arguments of a givenType, if it is parameterized.static booleanisBasicType(Class<?> type) Determines if the provided class type represents a basic type.static <DTO> DTOnewInstance(Class<DTO> dtoClass) Creates a new instance of the specified class type using its no-argument constructor.static <DTO> DTOnewInstance(Class<DTO> dtoClass, Constructor<DTO> constructor, Object... args)
-
Method Details
-
getAllFields
Retrieves all non-static fields from a given class, including fields declared in its superclasses (except for fields the `Object` class). Equivalent togetAllFields(type, false).- Parameters:
type- the class from which to retrieve all declared fields- Returns:
- a list of all fields declared in the given class and its superclasses
-
getAllFields
public static List<Field> getAllFields(Class<?> type, boolean includeStatic, MethodHandles.Lookup lookup) Retrieves all fields from a given class, including fields declared in its superclasses (except for fields of the `Object` class) and (optionally) static fields.- Parameters:
type- the class from which to retrieve all declared fields- Returns:
- a list of all fields declared in the given class and its superclasses
-
getField
Retrieves a specific field from the specified class or its superclasses.This method attempts to find a field in the given class by its name. If the field is not found in the current class, it recursively checks the superclasses up to (but not including) the `Object` class.
- Parameters:
cls- the class to search for the fieldfieldName- the name of the field to retrieve- Returns:
- the
Fieldobject representing the specified field if found - Throws:
IllegalArgumentException- if the field cannot be found in the specified class or its superclasses
-
isBasicType
Determines if the provided class type represents a basic type.A basic type is defined as one of the following: - A primitive type (e.g., int, double). - An enum. - A type that is a subclass or implementation of
CharSequence. - A type that is a subclass ofNumber. - A type that is a subclass ofBoolean. - Abyte[]type.- Parameters:
type- theClassobject to check for being a basic type- Returns:
trueif the provided class type is considered a basic type,falseotherwise
-
getGenericType
Retrieves the first generic type argument of a givenField, if the field is parameterized.For example, if the field is of type
List<String>, this method will returnString.class. However, if the field is of typeMap<Long, String>, it will returnLong.class.- Parameters:
field- the field from which to retrieve the first generic type argument- Returns:
- the
Class<?>object representing the first generic type argument of the field
-
getGenericTypes
Retrieves the generic type arguments of a givenField, if the field is parameterized.For example, if the field is of type
List<String>, this method will return an array containingString.class.- Parameters:
field- the field from which to retrieve the generic type arguments- Returns:
- an array of
Class<?>objects representing the generic type arguments of the field
-
getGenericTypes
Retrieves the generic type arguments of a givenType, if it is parameterized.For example, if the generic type is
List<String>, this method will return an array containingString.class.- Parameters:
genericType- the type for which to retrieve the generic type arguments- Returns:
- an array of
Class<?>objects representing the generic type arguments - Throws:
IllegalArgumentException- if the generic type is not parameterized or if a concrete class cannot be determined for one of the arguments
-
newInstance
Creates a new instance of the specified class type using its no-argument constructor.This method allows instantiating classes even if their constructors are not publicly accessible.
- Type Parameters:
DTO- the type of the object to be created- Parameters:
dtoClass- the class object representing the type to instantiate- Returns:
- a new instance of the specified class type
- Throws:
IllegalStateException- if the instantiation fails due to an exception (e.g., no accessible constructor, security restriction)
-
newInstance
public static <DTO> DTO newInstance(Class<DTO> dtoClass, Constructor<DTO> constructor, Object... args) -
getConstructors
-