Skip to content

io microsphere reflect ConstructorUtils

github-actions[bot] edited this page Jul 27, 2026 · 15 revisions

ConstructorUtils

Type: Class | Module: microsphere-java-core | Package: io.microsphere.reflect | Since: 1.0.0

Source: microsphere-java-core/src/main/java/io/microsphere/reflect/ConstructorUtils.java

Overview

The utilities class of Constructor

Declaration

public abstract class ConstructorUtils implements Utils

Author: Mercy

Version Information

  • Introduced in: 1.0.0
  • Current Project Version: 0.3.16-SNAPSHOT

Version Compatibility

This component is tested and compatible with the following Java versions:

Java Version Status
Java 8 ✅ Compatible
Java 11 ✅ Compatible
Java 17 ✅ Compatible
Java 21 ✅ Compatible
Java 25 ✅ Compatible

Examples

Method Examples

isNonPrivateConstructorWithoutParameters

Constructor<?> constructor = MyClass.class.getConstructor();
boolean isValid = isNonPrivateConstructorWithoutParameters(constructor);

hasNonPrivateConstructorWithoutParameters

boolean result = hasNonPrivateConstructorWithoutParameters(MyClass.class);

findConstructors

List<Constructor<?>> constructors = findConstructors(MyClass.class);
List<Constructor<?>> nonPrivateConstructors = findConstructors(MyClass.class,
    constructor -> !MemberUtils.isPrivate(constructor));
List<Constructor<?>> noArgConstructors = findConstructors(MyClass.class,
    constructor -> constructor.getParameterCount() == 0);

findDeclaredConstructors

List<Constructor<?>> constructors = findDeclaredConstructors(MyClass.class);
List<Constructor<?>> nonPrivateConstructors = findDeclaredConstructors(MyClass.class,
    constructor -> !MemberUtils.isPrivate(constructor));
List<Constructor<?>> noArgConstructors = findDeclaredConstructors(MyClass.class,
    constructor -> constructor.getParameterCount() == 0);

getConstructor

Constructor<MyClass> constructor = getConstructor(MyClass.class, String.class, int.class);

getDeclaredConstructor

Constructor<MyClass> constructor = getDeclaredConstructor(MyClass.class, String.class, int.class);

findConstructor

Constructor<MyClass> constructor = findConstructor(MyClass.class, String.class, int.class);
if (constructor != null) {
    MyClass instance = newInstance(constructor, "example", 42);
}
Constructor<MyClass> constructor = findConstructor(MyClass.class, double.class, boolean.class);
if (constructor == null) {
    System.out.println("No matching constructor found.");
}

newInstance

Person person = newInstance(Person.class, "Mercy", 18);

newInstance

User user = newInstance(false, User.class, "Alice");
Singleton singleton = newInstance(true, Singleton.class);

newInstance

Constructor<MyClass> constructor = MyClass.class.getConstructor(String.class, int.class);
MyClass instance = newInstance(constructor, "hello", 42);

newInstance

Constructor<MyClass> constructor = MyClass.class.getConstructor(String.class);
MyClass instance = newInstance(false, constructor, "hello");
Constructor<SingletonClass> constructor = SingletonClass.class.getDeclaredConstructor();
SingletonClass instance = newInstance(true, constructor);

Usage

Maven Dependency

Add the following dependency to your pom.xml:

<dependency>
    <groupId>io.github.microsphere-projects</groupId>
    <artifactId>microsphere-java-core</artifactId>
    <version>${microsphere-java.version}</version>
</dependency>

Tip: Use the BOM (microsphere-java-dependencies) for consistent version management. See the Getting Started guide.

Import

import io.microsphere.reflect.ConstructorUtils;

API Reference

Public Methods

Method Description
isNonPrivateConstructorWithoutParameters Checks whether the given constructor is a non-private constructor without parameters.
hasNonPrivateConstructorWithoutParameters Checks whether the specified class has at least one non-private constructor without parameters.
findConstructors Find public constructors from the specified class that match the given filter conditions.
findDeclaredConstructors Find declared constructors from the specified class that match the given filter conditions.
getConstructor Retrieves the public constructor of the specified class that matches the given parameter types.
getDeclaredConstructor Retrieves the declared constructor (including private, protected, and package-private)
findConstructor Finds a constructor in the specified class that matches the given parameter types.
newInstance Creates a new instance of the specified class by inferring constructor parameter types
newInstance Creates a new instance of the specified class by locating a declared constructor
newInstance Creates a new instance by invoking the specified Constructor with the provided arguments.
newInstance Creates a new instance by invoking the specified Constructor with optional forced access.

Method Details

isNonPrivateConstructorWithoutParameters

public static boolean isNonPrivateConstructorWithoutParameters(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);
`

hasNonPrivateConstructorWithoutParameters

public static boolean hasNonPrivateConstructorWithoutParameters(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);
`

findConstructors

public static List<Constructor<?>> findConstructors(Class<?> type,
                                                        Predicate<? super 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> constructors = findConstructors(MyClass.class);
`

Filter by Non-Private Constructors

`List> nonPrivateConstructors = findConstructors(MyClass.class,
    constructor -> !MemberUtils.isPrivate(constructor));
`

Filter by Constructor Parameter Count

`List> noArgConstructors = findConstructors(MyClass.class,
    constructor -> constructor.getParameterCount() == 0);
`

findDeclaredConstructors

public static List<Constructor<?>> findDeclaredConstructors(Class<?> type,
                                                                Predicate<? super 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> constructors = findDeclaredConstructors(MyClass.class);
`

Filter by Non-Private Constructors

`List> nonPrivateConstructors = findDeclaredConstructors(MyClass.class,
    constructor -> !MemberUtils.isPrivate(constructor));
`

Filter by Constructor Parameter Count

`List> noArgConstructors = findDeclaredConstructors(MyClass.class,
    constructor -> constructor.getParameterCount() == 0);
`

getConstructor

public static <T> Constructor<T> getConstructor(Class<T> type, 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 constructor = getConstructor(MyClass.class, String.class, int.class);
`

getDeclaredConstructor

public static <T> Constructor<T> getDeclaredConstructor(Class<T> type, 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 constructor = getDeclaredConstructor(MyClass.class, String.class, int.class);
`

findConstructor

public static <T> Constructor<T> findConstructor(Class<T> type, 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 constructor = findConstructor(MyClass.class, String.class, int.class);
if (constructor != null) {
    MyClass instance = newInstance(constructor, "example", 42);
`
}

Handling Missing Constructors

`Constructor constructor = findConstructor(MyClass.class, double.class, boolean.class);
if (constructor == null) {
    System.out.println("No matching constructor found.");
`
}

newInstance

public static <T> T newInstance(Class<T> type, Object... args)

Creates a new instance of the specified class by inferring constructor parameter types from the runtime types of args.

This method delegates to #newInstance(boolean, Class, Object...) with forceAccess = false, so normal Java access checks apply.

Example Usage

`Person person = newInstance(Person.class, "Mercy", 18);
`

newInstance

public static <T> T newInstance(boolean forceAccess, Class<T> type, Object... args)

Creates a new instance of the specified class by locating a declared constructor that matches the runtime argument types and invoking it.

When forceAccess is true, this method attempts to make the resolved constructor accessible before invocation, which allows use of non-public constructors.

Example Usage

Invoke Public Constructor

`User user = newInstance(false, User.class, "Alice");
`

Invoke Private Constructor

`Singleton singleton = newInstance(true, Singleton.class);
`

newInstance

public static <T> T newInstance(Constructor<T> constructor, Object... args)

Creates a new instance by invoking the specified Constructor with the provided arguments.

This method uses normal Java access checks and delegates to #newInstance(boolean, Constructor, Object...) with forceAccess = false.

Example Usage

`Constructor constructor = MyClass.class.getConstructor(String.class, int.class);
MyClass instance = newInstance(constructor, "hello", 42);
`

newInstance

public static <T> T newInstance(boolean forceAccess, Constructor<T> constructor, Object... args)

Creates a new instance by invoking the specified Constructor with optional forced access.

When forceAccess is true, this method attempts to make the constructor accessible first, allowing invocation of non-public constructors. When forceAccess is false, normal Java access checks apply.

Example Usage

Invoke a Public Constructor Without Forcing Access

`Constructor constructor = MyClass.class.getConstructor(String.class);
MyClass instance = newInstance(false, constructor, "hello");
`

Invoke a Private Constructor by Forcing Access

`Constructor constructor = SingletonClass.class.getDeclaredConstructor();
SingletonClass instance = newInstance(true, constructor);
`

This documentation was auto-generated from the source code of microsphere-java.

Home

annotation-processor

annotation-test

java-annotations

java-core

java-test

jdk-tools

lang-model

Clone this wiki locally