Class ConstructorInfo
- All Implemented Interfaces:
Comparable<ConstructorInfo>,Annotatable
This class provides a convenient wrapper around Constructor that extends the standard Java reflection
API with additional functionality for constructor introspection, annotation handling, and instance creation.
It extends ExecutableInfo to provide common functionality shared with methods.
Features:
- Constructor introspection - access constructor metadata, parameters, exceptions
- Annotation support - get annotations declared on the constructor
- Instance creation - create new instances with type safety
- Accessibility control - make private constructors accessible
- Thread-safe - instances are immutable and safe for concurrent access
Use Cases:
- Introspecting constructor metadata for code generation or analysis
- Creating instances of classes dynamically
- Finding annotations on constructors
- Working with constructor parameters and exceptions
- Building frameworks that need to instantiate objects
Usage:
See Also:
ClassInfo- Class introspectionMethodInfo- Method introspectionFieldInfo- Field introspection- Reflection Package
-
Field Summary
Fields inherited from class org.apache.juneau.commons.reflect.ExecutableInfo
declaringClass -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedConstructorInfo(ClassInfo declaringClass, Constructor<?> inner) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionAttempts to callx.setAccessible(and quietly ignores security exceptions.true )booleancanResolveAllParameters(BeanStore beanStore, Object enclosingInstance, Object... otherBeans) Checks if all constructor parameters can be resolved.intbooleanCompares this ConstructorInfo with the specified object for equality.Returns the type of this annotatable object.getLabel()Returns a human-readable label for this annotatable element.getMissingParameterTypes(BeanStore beanStore, Object enclosingInstance, Object... otherBeans) Returns comma-delimited list of missing parameter types.inthashCode()Returns a hash code value for this ConstructorInfo.<T> TResolves parameters from the bean store and invokes the constructor.<T> Constructor<T>inner()Returns the wrapped constructor.<T> TnewInstance(Object... args) Shortcut for calling the new-instance method on the underlying constructor.<T> TnewInstanceLenient(Object... args) Shortcut for calling the new-instance method on the underlying constructor using lenient argument matching.static ConstructorInfoof(Constructor<?> inner) Creates a ConstructorInfo wrapper for the specified constructor.static ConstructorInfoof(ClassInfo declaringClass, Constructor<?> inner) Creates a ConstructorInfo wrapper for the specified constructor.Methods inherited from class org.apache.juneau.commons.reflect.ExecutableInfo
canAccept, getAnnotatedExceptionTypes, getAnnotatedParameterTypes, getAnnotatedReceiverType, getDeclaredAnnotations, getDeclaredAnnotations, getDeclaringClass, getExceptionTypes, getNameFull, getNameShort, getNameSimple, getParameter, getParameterCount, getParameters, getParameterTypes, getTypeParameters, hasAnnotation, hasAnyName, hasAnyName, hasMatchingParameters, hasName, hasNumParameters, hasParameters, hasParameterType, hasParameterTypeParent, hasParameterTypeParents, hasParameterTypeParents, hasParameterTypes, hasParameterTypes, hasParameterTypesLenient, hasParameterTypesLenient, is, isConstructor, isDeclaringClass, isDeprecated, isNotDeprecated, isSynthetic, isVarArgs, isVisible, parameterMatchesLenientCount, parameterMatchesLenientCount, parameterMatchesLenientCount, setAccessible, toGenericString, toStringMethods inherited from class org.apache.juneau.commons.reflect.AccessibleInfo
isAccessibleMethods inherited from class org.apache.juneau.commons.reflect.ElementInfo
ai, getModifiers, isAbstract, isAll, isAny, isFinal, isInterface, isNative, isNotAbstract, isNotFinal, isNotInterface, isNotNative, isNotPrivate, isNotProtected, isNotPublic, isNotStatic, isNotSynchronized, isNotTransient, isNotVolatile, isPrivate, isProtected, isPublic, isStatic, isSynchronized, isTransient, isVolatile
-
Constructor Details
-
ConstructorInfo
Constructor.Creates a new ConstructorInfo wrapper for the specified constructor. This constructor is protected and should not be called directly. Use the static factory methods
of(Constructor)or obtain ConstructorInfo instances fromClassInfo.getConstructor(Constructor).- Parameters:
declaringClass- The ClassInfo for the class that declares this constructor. Must not benull .inner- The constructor being wrapped. Must not benull .
-
-
Method Details
-
of
Creates a ConstructorInfo wrapper for the specified constructor.Example:
ClassInfo
ci = ClassInfo.of (MyClass.class ); Constructor<?>c = MyClass.class .getConstructor(String.class ); ConstructorInfoci2 = ConstructorInfo.of (ci ,c );- Parameters:
declaringClass- The ClassInfo for the class that declares this constructor. Must not benull .inner- The constructor being wrapped. Must not benull .- Returns:
- A new ConstructorInfo object wrapping the constructor.
-
of
Creates a ConstructorInfo wrapper for the specified constructor.This convenience method automatically determines the declaring class from the constructor.
Example:
Constructor<?>
c = MyClass.class .getConstructor(String.class ); ConstructorInfoci = ConstructorInfo.of (c );- Parameters:
inner- The constructor being wrapped. Must not benull .- Returns:
- A new ConstructorInfo object wrapping the constructor.
-
accessible
Description copied from class:ExecutableInfoAttempts to callx.setAccessible(and quietly ignores security exceptions.true )- Overrides:
accessiblein classExecutableInfo- Returns:
- This object.
-
compareTo
- Specified by:
compareToin interfaceComparable<ConstructorInfo>
-
getAnnotatableType
Description copied from interface:AnnotatableReturns the type of this annotatable object.- Specified by:
getAnnotatableTypein interfaceAnnotatable- Returns:
- The type of annotatable object this represents.
-
getLabel
Description copied from interface:AnnotatableReturns a human-readable label for this annotatable element.The label format depends on the type of annotatable:
CLASS_TYPE- Simple class name (e.g.,"MyClass" )METHOD_TYPE- Class and method with parameter types (e.g.,"MyClass.myMethod(String,int)" )FIELD_TYPE- Class and field name (e.g.,"MyClass.myField" )CONSTRUCTOR_TYPE- Class and constructor with parameter types (e.g.,"MyClass.MyClass(String)" )PARAMETER_TYPE- Class, method/constructor, and parameter index (e.g.,"MyClass.myMethod[0]" )PACKAGE_TYPE- Package name (e.g.,"com.example.package" )
- Specified by:
getLabelin interfaceAnnotatable- Returns:
- The human-readable label for this annotatable element.
-
inner
Returns the wrapped constructor.Example:
ConstructorInfo
ci = ...; Constructor<MyClass>ctor =ci .inner();- Type Parameters:
T- The class type of the constructor.- Returns:
- The wrapped constructor.
-
equals
Compares this ConstructorInfo with the specified object for equality.Two ConstructorInfo objects are considered equal if they wrap the same underlying
Constructorobject. This delegates to the underlyingConstructor.equals(Object)method.This method makes ConstructorInfo suitable for use as keys in hash-based collections such as
HashMapandHashSet. -
hashCode
Returns a hash code value for this ConstructorInfo.This delegates to the underlying
Constructor.hashCode()method.This method makes ConstructorInfo suitable for use as keys in hash-based collections such as
HashMapandHashSet. -
newInstance
Shortcut for calling the new-instance method on the underlying constructor.If the number of formal parameters required by the underlying constructor is 0, the supplied
args array may be of length 0 ornull .- Type Parameters:
T- The constructor class type.- Parameters:
args- The arguments used for the constructor call. Can benull or empty for constructors with no parameters.- Returns:
- The object returned from the constructor.
- Throws:
ExecutableException- Exception occurred on invoked constructor/method/field.
-
newInstanceLenient
Shortcut for calling the new-instance method on the underlying constructor using lenient argument matching.Lenient matching allows arguments to be matched to parameters based on parameter types.
Arguments can be in any order.
Extra arguments are ignored.
Missing arguments are set tonull .- Type Parameters:
T- The constructor class type.- Parameters:
args- The arguments used for the constructor call.- Returns:
- The object returned from the constructor.
- Throws:
ExecutableException- Exception occurred on invoked constructor/method/field.
-
getMissingParameterTypes
public String getMissingParameterTypes(BeanStore beanStore, Object enclosingInstance, Object... otherBeans) Returns comma-delimited list of missing parameter types.Analyzes the parameters of this constructor and checks if all required beans are available in the bean store or in the
otherBeans parameter.The following parameter types are considered optional and are not checked:
- Parameters of type
Optional<T> - Parameters of type
T[] ,List<T> ,Set<T> , orMap<String,T> - The first parameter if it matches the
enclosingInstance object type (for non-static inner classes)
If a parameter has a
@Named or@Qualifierannotation, the method checks for a bean with that specific name in the bean store. Otherwise, it checks for an unnamed bean of the parameter type in the bean store, and if not found, checks theotherBeans parameter.- Parameters:
beanStore- The bean store to check for beans.enclosingInstance- The outer class instance for non-static inner class constructors. If the first parameter type matches this object's type, it is used as the first parameter and not checked in the bean store. Can benull for regular classes or static inner classes.otherBeans- Optional additional bean instances to check if not found in the bean store. These are checked after the bean store but before marking the parameter as missing.- Returns:
- A comma-delimited, sorted list of missing parameter types (e.g.,
"String,Integer" ), ornull if all required parameters are available.
- Parameters of type
-
canResolveAllParameters
public boolean canResolveAllParameters(BeanStore beanStore, Object enclosingInstance, Object... otherBeans) Checks if all constructor parameters can be resolved.This method performs the same checks as
getMissingParameterTypes(BeanStore, Object, Object...)but returns a boolean instead of a list of missing types.The following parameter types are considered optional and are not checked:
- Parameters of type
Optional<T> - Parameters of type
T[] ,List<T> ,Set<T> , orMap<String,T> - The first parameter if it matches the
enclosingInstance object type (for non-static inner classes)
If a parameter has a
@Named or@Qualifierannotation, the method checks for a bean with that specific name in the bean store. Otherwise, it checks for an unnamed bean of the parameter type in the bean store, and if not found, checks theotherBeans parameter.- Parameters:
beanStore- The bean store to check for beans.enclosingInstance- The outer class instance for non-static inner class constructors. If the first parameter type matches this object's type, it is used as the first parameter and not checked in the bean store. Can benull for regular classes or static inner classes.otherBeans- Optional additional bean instances to check if not found in the bean store. These are checked after the bean store but before marking the parameter as missing.- Returns:
true if all required parameters are available in the bean store orotherBeans ,false otherwise.
- Parameters of type
-
inject
Resolves parameters from the bean store and invokes the constructor.For each parameter in the constructor, this method:
- If the first parameter type matches the
enclosingInstance object type, uses theenclosingInstance object (for non-static inner class constructors). - If the parameter is a collection/array/map type, collects all beans of the element type.
- Otherwise, looks up a single bean by type and optional qualifier name in the bean store.
- If not found in the bean store, checks the
otherBeans parameter.
Parameter Resolution:
- Single beans - Resolved using
BeanStore.getBean(Class)orBeanStore.getBean(Class, String). If not found, checksotherBeans for a compatible instance. ThrowsExecutableExceptionif not found in either location. - Optional beans - Wrapped in
Optional , orOptional.empty() if not found. Never throws an exception. - Arrays - All beans of the element type are collected into an array (may be empty). Never throws an exception.
- Lists - All beans of the element type are collected into a
List (may be empty). Never throws an exception. - Sets - All beans of the element type are collected into a
LinkedHashSet (may be empty). Never throws an exception. - Maps - All beans of the value type are collected into a
LinkedHashMap keyed by bean name (may be empty). Unnamed beans use an empty string as the key. Never throws an exception.
The
enclosingInstance parameter is used for non-static inner class constructors. If the first parameter type matches this object's type, it is used as the first parameter value. For regular constructors, this parameter is ignored if it doesn't match the first parameter type.- Type Parameters:
T- The return type of the constructor.- Parameters:
beanStore- The bean store to resolve parameters from.enclosingInstance- The outer class instance for non-static inner classes (can benull ).otherBeans- Optional additional bean instances to use if not found in the bean store.- Returns:
- The result of invoking the constructor.
- Throws:
ExecutableException- If the constructor cannot be invoked or parameter resolution fails.
- If the first parameter type matches the
-