Package java.lang.reflect
Class Constructor<T>
- java.lang.Object
-
- java.lang.reflect.AccessibleObject
-
- java.lang.reflect.AbstractMethod
-
- java.lang.reflect.Constructor<T>
-
- Type Parameters:
T- the class that declares this constructor
- All Implemented Interfaces:
AnnotatedElement,GenericDeclaration,Member
public final class Constructor<T> extends AbstractMethod implements GenericDeclaration, Member
This class represents a constructor. Information about the constructor can be accessed, and the constructor can be invoked dynamically.
-
-
Field Summary
-
Fields inherited from class java.lang.reflect.AbstractMethod
accessFlags, artMethod, declaringClass, declaringClassOfOverriddenMethod, dexMethodIndex
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanequals(Object other)Returns true ifotherhas the same declaring class and parameters as this constructor.<A extends Annotation>
AgetAnnotation(Class<A> annotationType)Returns, for this element, the annotation with the specified type, ornullif no annotation with the specified type is present (including inherited annotations).Annotation[]getAnnotations()Returns, for this element, an array containing all annotations (including inherited annotations).Annotation[]getDeclaredAnnotations()Returns, for this element, all annotations that are explicitly declared (not inherited).Class<T>getDeclaringClass()Returns the class that declares this constructor.Class<?>[]getExceptionTypes()Returns the exception types as an array ofClassinstances.Type[]getGenericExceptionTypes()Returns the exception types as an array ofTypeinstances.Type[]getGenericParameterTypes()Returns the generic parameter types as an array ofTypeinstances, in declaration order.intgetModifiers()Returns the modifiers for this constructor.StringgetName()Returns the name of this constructor.Annotation[][]getParameterAnnotations()Returns an array of arrays that represent the annotations of the formal parameters of this constructor.Class<?>[]getParameterTypes()Returns an array of theClassobjects associated with the parameter types of this constructor.TypeVariable<Constructor<T>>[]getTypeParameters()Returns the declared type parameters in declaration order.inthashCode()Returns an integer hash code for this object.booleanisAnnotationPresent(Class<? extends Annotation> annotationType)Indicates whether or not this element has an annotation with the specified annotation type (including inherited annotations).booleanisSynthetic()Returns true if this constructor is synthetic (artificially introduced by the compiler).booleanisVarArgs()Returns true if this constructor takes a variable number of arguments.TnewInstance(Object... args)Returns a new instance of the declaring class, initialized by dynamically invoking the constructor represented by thisConstructorobject.voidsetAccessible(boolean flag)Attempts to set the accessible flag.StringtoGenericString()Returns the string representation of the constructor's declaration, including the type parameters.StringtoString()Returns a string containing a concise, human-readable description of this constructor.-
Methods inherited from class java.lang.reflect.AbstractMethod
compareParameters, equalMethodParameters, getAccessFlags, getDexMethodIndex
-
Methods inherited from class java.lang.reflect.AccessibleObject
isAccessible, setAccessible
-
-
-
-
Method Detail
-
getAnnotations
public Annotation[] getAnnotations()
Description copied from interface:AnnotatedElementReturns, for this element, an array containing all annotations (including inherited annotations). If there are no annotations present, this method returns a zero length array.- Specified by:
getAnnotationsin interfaceAnnotatedElement- Overrides:
getAnnotationsin classAbstractMethod- Returns:
- an array of all annotations for this element
-
getModifiers
public int getModifiers()
Returns the modifiers for this constructor. TheModifierclass should be used to decode the result.- Specified by:
getModifiersin interfaceMember- Returns:
- the modifiers for this member
- See Also:
Modifier
-
isVarArgs
public boolean isVarArgs()
Returns true if this constructor takes a variable number of arguments.
-
isSynthetic
public boolean isSynthetic()
Returns true if this constructor is synthetic (artificially introduced by the compiler).- Specified by:
isSyntheticin interfaceMember- Returns:
trueif this member is synthetic,falseotherwise
-
getName
public String getName()
Returns the name of this constructor.- Specified by:
getNamein interfaceMember- Specified by:
getNamein classAbstractMethod- Returns:
- the name of this method
-
getDeclaringClass
public Class<T> getDeclaringClass()
Returns the class that declares this constructor.- Specified by:
getDeclaringClassin interfaceMember- Returns:
- the declaring class
-
getExceptionTypes
public Class<?>[] getExceptionTypes()
Returns the exception types as an array ofClassinstances. If this constructor has no declared exceptions, an empty array will be returned.
-
getParameterTypes
public Class<?>[] getParameterTypes()
Returns an array of theClassobjects associated with the parameter types of this constructor. If the constructor was declared with no parameters, an empty array will be returned.
-
hashCode
public int hashCode()
Returns an integer hash code for this object. By contract, any two objects for whichObject.equals(java.lang.Object)returnstruemust return the same hash code value. This means that subclasses ofObjectusually override both methods or neither method.Note that hash values must not change over time unless information used in equals comparisons also changes.
See Writing a correct
hashCodemethod if you intend implementing your ownhashCodemethod.Equivalent to
getDeclaringClass().getName().hashCode().- Overrides:
hashCodein classObject- Returns:
- this object's hash code.
- See Also:
Object.equals(java.lang.Object)
-
equals
public boolean equals(Object other)
Returns true ifotherhas the same declaring class and parameters as this constructor.- Overrides:
equalsin classAbstractMethod- Parameters:
other- the object to compare this instance with.- Returns:
trueif the specified object is equal to thisObject;falseotherwise.- See Also:
Object.hashCode()
-
getTypeParameters
public TypeVariable<Constructor<T>>[] getTypeParameters()
Description copied from interface:GenericDeclarationReturns the declared type parameters in declaration order. If there are no type parameters, this method returns a zero length array.- Specified by:
getTypeParametersin interfaceGenericDeclaration- Returns:
- the declared type parameters in declaration order
-
toGenericString
public String toGenericString()
Returns the string representation of the constructor's declaration, including the type parameters.- Returns:
- the string representation of the constructor's declaration
-
getGenericParameterTypes
public Type[] getGenericParameterTypes()
Returns the generic parameter types as an array ofTypeinstances, in declaration order. If this constructor has no generic parameters, an empty array is returned.- Returns:
- the parameter types
- Throws:
GenericSignatureFormatError- if the generic constructor signature is invalidTypeNotPresentException- if any parameter type points to a missing typeMalformedParameterizedTypeException- if any parameter type points to a type that cannot be instantiated for some reason
-
getGenericExceptionTypes
public Type[] getGenericExceptionTypes()
Returns the exception types as an array ofTypeinstances. If this constructor has no declared exceptions, an empty array will be returned.- Returns:
- an array of generic exception types
- Throws:
GenericSignatureFormatError- if the generic constructor signature is invalidTypeNotPresentException- if any exception type points to a missing typeMalformedParameterizedTypeException- if any exception type points to a type that cannot be instantiated for some reason
-
getDeclaredAnnotations
public Annotation[] getDeclaredAnnotations()
Description copied from interface:AnnotatedElementReturns, for this element, all annotations that are explicitly declared (not inherited). If there are no declared annotations present, this method returns a zero length array.- Specified by:
getDeclaredAnnotationsin interfaceAnnotatedElement- Overrides:
getDeclaredAnnotationsin classAbstractMethod- Returns:
- an array of annotations declared for this element
-
isAnnotationPresent
public boolean isAnnotationPresent(Class<? extends Annotation> annotationType)
Description copied from interface:AnnotatedElementIndicates whether or not this element has an annotation with the specified annotation type (including inherited annotations).- Specified by:
isAnnotationPresentin interfaceAnnotatedElement- Overrides:
isAnnotationPresentin classAbstractMethod- Parameters:
annotationType- the type of the annotation to search for- Returns:
trueif the annotation exists,falseotherwise
-
getAnnotation
public <A extends Annotation> A getAnnotation(Class<A> annotationType)
Description copied from interface:AnnotatedElementReturns, for this element, the annotation with the specified type, ornullif no annotation with the specified type is present (including inherited annotations).- Specified by:
getAnnotationin interfaceAnnotatedElement- Overrides:
getAnnotationin classAbstractMethod- Parameters:
annotationType- the type of the annotation to search for- Returns:
- the annotation with the specified type or
null
-
getParameterAnnotations
public Annotation[][] getParameterAnnotations()
Returns an array of arrays that represent the annotations of the formal parameters of this constructor. If there are no parameters on this constructor, then an empty array is returned. If there are no annotations set, then an array of empty arrays is returned.- Specified by:
getParameterAnnotationsin classAbstractMethod- Returns:
- an array of arrays of
Annotationinstances
-
newInstance
public T newInstance(Object... args) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException
Returns a new instance of the declaring class, initialized by dynamically invoking the constructor represented by thisConstructorobject. This reproduces the effect ofnew declaringClass(arg1, arg2, ... , argN)This method performs the following:- A new instance of the declaring class is created. If the declaring class cannot be instantiated (i.e. abstract class, an interface, an array type, or a primitive type) then an InstantiationException is thrown.
- If this Constructor object is enforcing access control (see
AccessibleObject) and this constructor is not accessible from the current context, an IllegalAccessException is thrown. - If the number of arguments passed and the number of parameters do not match, an IllegalArgumentException is thrown.
- For each argument passed:
- If the corresponding parameter type is a primitive type, the argument is unboxed. If the unboxing fails, an IllegalArgumentException is thrown.
- If the resulting argument cannot be converted to the parameter type via a widening conversion, an IllegalArgumentException is thrown.
- The constructor represented by this
Constructorobject is then invoked. If an exception is thrown during the invocation, it is caught and wrapped in an InvocationTargetException. This exception is then thrown. If the invocation completes normally, the newly initialized object is returned.
- Parameters:
args- the arguments to the constructor- Returns:
- the new, initialized, object
- Throws:
InstantiationException- if the class cannot be instantiatedIllegalAccessException- if this constructor is not accessibleIllegalArgumentException- if an incorrect number of arguments are passed, or an argument could not be converted by a widening conversionInvocationTargetException- if an exception was thrown by the invoked constructor- See Also:
AccessibleObject
-
toString
public String toString()
Returns a string containing a concise, human-readable description of this constructor. The format of the string is:- modifiers (if any)
- declaring class name
- '('
- parameter types, separated by ',' (if any)
- ')'
- 'throws' plus exception types, separated by ',' (if any)
public String(byte[],String) throws UnsupportedEncodingException
-
setAccessible
public void setAccessible(boolean flag)
Attempts to set the accessible flag. Setting this to true preventsIllegalAccessExceptions.- Overrides:
setAccessiblein classAccessibleObject
-
-