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 )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.inthashCode()Returns a hash code value for this ConstructorInfo.<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, getFullName, getParameter, getParameterCount, getParameters, getParameterTypes, getShortName, getSimpleName, getTypeParameters, hasAnnotation, hasAnyName, hasAnyName, hasMatchingParameters, hasName, hasNumParameters, hasParameters, hasParameterTypeParents, hasParameterTypeParents, hasParameterTypes, hasParameterTypes, hasParameterTypesLenient, hasParameterTypesLenient, is, isConstructor, 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.inner- The constructor being wrapped.
-
-
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.- Type Parameters:
T- The constructor class type.- Parameters:
args- the arguments used for the method call.- 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.
-