Class ConstructorUtils
- java.lang.Object
-
- io.microsphere.reflect.ConstructorUtils
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.reflect.Constructor
NOT_FOUND_CONSTRUCTOR
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> java.lang.reflect.Constructor<T>
findConstructor(java.lang.Class<T> type, java.lang.Class<?>... parameterTypes)
Finds a constructor in the specified class that matches the given parameter types.static java.util.List<java.lang.reflect.Constructor<?>>
findConstructors(java.lang.Class<?> type, java.util.function.Predicate<? super java.lang.reflect.Constructor<?>>... constructorFilters)
Find public constructors from the specified class that match the given filter conditions.static java.util.List<java.lang.reflect.Constructor<?>>
findDeclaredConstructors(java.lang.Class<?> type, java.util.function.Predicate<? super java.lang.reflect.Constructor<?>>... constructorFilters)
Find declared constructors from the specified class that match the given filter conditions.static <T> java.lang.reflect.Constructor<T>
getConstructor(java.lang.Class<T> type, java.lang.Class<?>... parameterTypes)
Retrieves the public constructor of the specified class that matches the given parameter types.static <T> java.lang.reflect.Constructor<T>
getDeclaredConstructor(java.lang.Class<T> type, java.lang.Class<?>... parameterTypes)
Retrieves the declared constructor (including private, protected, and package-private) of the specified class that matches the given parameter types.static boolean
hasNonPrivateConstructorWithoutParameters(java.lang.Class<?> type)
Checks whether the specified class has at least one non-private constructor without parameters.static boolean
isNonPrivateConstructorWithoutParameters(java.lang.reflect.Constructor<?> constructor)
Checks whether the given constructor is a non-private constructor without parameters.static <T> T
newInstance(java.lang.reflect.Constructor<T> constructor, java.lang.Object... args)
Creates a new instance by invoking the specifiedConstructor
with the provided arguments.
-
-
-
Method Detail
-
isNonPrivateConstructorWithoutParameters
public static boolean isNonPrivateConstructorWithoutParameters(java.lang.reflect.Constructor<?> constructor)
Checks whether the given constructor is a non-private constructor without parameters.This method verifies that the constructor:
- Is not null
- Is not private (
MemberUtils.isPrivate(Member)
) - Has no parameters (i.e., parameter count is less than 1)
Example Usage
Constructor<?> constructor = MyClass.class.getConstructor(); boolean isValid = isNonPrivateConstructorWithoutParameters(constructor);
- Parameters:
constructor
- the constructor to check- Returns:
true
if the constructor is non-private and has no parameters;false
otherwise
-
hasNonPrivateConstructorWithoutParameters
public static boolean hasNonPrivateConstructorWithoutParameters(java.lang.Class<?> type)
Checks whether the specified class has at least one non-private constructor without parameters.This method examines all declared constructors of the given class and returns
true
if any of them is non-private and has no parameters.Example Usage
boolean result = hasNonPrivateConstructorWithoutParameters(MyClass.class);
- Parameters:
type
- the class to check for a non-private parameterless constructor- Returns:
true
if such a constructor exists; otherwise,false
-
findConstructors
@Nonnull @Immutable public static java.util.List<java.lang.reflect.Constructor<?>> findConstructors(java.lang.Class<?> type, java.util.function.Predicate<? super java.lang.reflect.Constructor<?>>... constructorFilters)
Find public constructors from the specified class that match the given filter conditions.This method retrieves all public constructors of the provided class and filters them based on the specified predicates. It is useful for selecting constructors with specific characteristics, such as visibility, parameter types, or other custom criteria.
Example Usage
Basic Filtering
List<Constructor<?>> constructors = findConstructors(MyClass.class);
Filter by Non-Private Constructors
List<Constructor<?>> nonPrivateConstructors = findConstructors(MyClass.class, constructor -> !MemberUtils.isPrivate(constructor));
Filter by Constructor Parameter Count
List<Constructor<?>> noArgConstructors = findConstructors(MyClass.class, constructor -> constructor.getParameterCount() == 0);
- Parameters:
type
- the class to find constructors fromconstructorFilters
- one or more predicates used to filter constructors- Returns:
- an immutable list of constructors that match the filter conditions
-
findDeclaredConstructors
@Nonnull @Immutable public static java.util.List<java.lang.reflect.Constructor<?>> findDeclaredConstructors(java.lang.Class<?> type, java.util.function.Predicate<? super java.lang.reflect.Constructor<?>>... constructorFilters)
Find declared constructors from the specified class that match the given filter conditions.This method retrieves all declared constructors (including private, protected, and package-private) of the provided class and filters them based on the specified predicates. It is useful for selecting constructors with specific characteristics such as visibility, parameter types, or other custom criteria.
Example Usage
Basic Filtering
List<Constructor<?>> constructors = findDeclaredConstructors(MyClass.class);
Filter by Non-Private Constructors
List<Constructor<?>> nonPrivateConstructors = findDeclaredConstructors(MyClass.class, constructor -> !MemberUtils.isPrivate(constructor));
Filter by Constructor Parameter Count
List<Constructor<?>> noArgConstructors = findDeclaredConstructors(MyClass.class, constructor -> constructor.getParameterCount() == 0);
- Parameters:
type
- the class to find declared constructors fromconstructorFilters
- one or more predicates used to filter constructors- Returns:
- an immutable list of declared constructors that match the filter conditions
-
getConstructor
@Nullable public static <T> java.lang.reflect.Constructor<T> getConstructor(java.lang.Class<T> type, java.lang.Class<?>... parameterTypes)
Retrieves the public constructor of the specified class that matches the given parameter types.This method attempts to find a public constructor in the provided class that accepts the specified parameter types. It wraps the underlying reflective operation and handles exceptions via the
ThrowableSupplier
execution.Example Usage
Constructor<MyClass> constructor = getConstructor(MyClass.class, String.class, int.class);
- Type Parameters:
T
- the type of the object constructed by the retrieved constructor- Parameters:
type
- the class to retrieve the constructor fromparameterTypes
- the types of parameters expected by the constructor- Returns:
- the matching public constructor, or throws an exception if not found
-
getDeclaredConstructor
@Nullable public static <T> java.lang.reflect.Constructor<T> getDeclaredConstructor(java.lang.Class<T> type, java.lang.Class<?>... parameterTypes)
Retrieves the declared constructor (including private, protected, and package-private) of the specified class that matches the given parameter types.This method attempts to find a constructor with the specified parameter types in the provided class. It wraps the underlying reflective operation and handles exceptions via the
ThrowableSupplier
execution.Example Usage
Constructor<MyClass> constructor = getDeclaredConstructor(MyClass.class, String.class, int.class);
- Type Parameters:
T
- the type of the object constructed by the retrieved constructor- Parameters:
type
- the class to retrieve the declared constructor fromparameterTypes
- the types of parameters expected by the constructor- Returns:
- the matching declared constructor, or throws an exception if not found
-
findConstructor
@Nullable public static <T> java.lang.reflect.Constructor<T> findConstructor(java.lang.Class<T> type, java.lang.Class<?>... parameterTypes)
Finds a constructor in the specified class that matches the given parameter types.This method attempts to locate a constructor within the provided class that accepts the specified parameter types. If no such constructor is found, it returns
null
and logs a trace message indicating the failure.Example Usage
Finding a Constructor
Constructor<MyClass> constructor = findConstructor(MyClass.class, String.class, int.class); if (constructor != null) { MyClass instance = newInstance(constructor, "example", 42); }
Handling Missing Constructors
Constructor<MyClass> constructor = findConstructor(MyClass.class, double.class, boolean.class); if (constructor == null) { System.out.println("No matching constructor found."); }
- Type Parameters:
T
- the type of the object constructed by the retrieved constructor- Parameters:
type
- the class to search for the constructorparameterTypes
- the types of parameters expected by the constructor- Returns:
- the matching constructor, or
null
if none is found
-
newInstance
@Nonnull public static <T> T newInstance(java.lang.reflect.Constructor<T> constructor, java.lang.Object... args)
Creates a new instance by invoking the specifiedConstructor
with the provided arguments.This method makes the constructor accessible (if it is not already) using
AccessibleObjectUtils.trySetAccessible(AccessibleObject)
and then invokes it using the utility method fromExecutableUtils.execute(Executable, ThrowableSupplier)
to handle exceptions uniformly.Example Usage
Basic Instantiation
Constructor<MyClass> constructor = MyClass.class.getConstructor(String.class, int.class); MyClass instance = newInstance(constructor, "hello", 42);
Using a Private Constructor
Constructor<SingletonClass> privateConstructor = SingletonClass.class.getDeclaredConstructor(); SingletonClass instance = newInstance(privateConstructor); // Accesses private constructor
- Type Parameters:
T
- the type of the object created by the constructor- Parameters:
constructor
- the constructor to invokeargs
- the arguments to pass to the constructor- Returns:
- a new instance of the object constructed by the given constructor
- Throws:
java.lang.NullPointerException
- if the constructor isnull
java.lang.IllegalStateException
- if the constructor cannot be accessed or invokedjava.lang.IllegalArgumentException
- if the arguments do not match the expected parameter typesjava.lang.RuntimeException
- if the constructor throws an exception during invocation
-
-