FileDocCategorySizeDatePackage
Constructor.javaAPI DocAndroid 1.5 API17176Wed May 06 22:41:04 BST 2009java.lang.reflect

Constructor

public final class Constructor extends AccessibleObject implements GenericDeclaration, Member
This class represents a constructor. Information about the constructor can be accessed, and the constructor can be invoked dynamically.
param
the class that declares this constructor
since
Android 1.0

Fields Summary
Class
declaringClass
Class[]
parameterTypes
Class[]
exceptionTypes
org.apache.harmony.luni.lang.reflect.ListOfTypes
genericExceptionTypes
org.apache.harmony.luni.lang.reflect.ListOfTypes
genericParameterTypes
TypeVariable[]
formalTypeParameters
private volatile boolean
genericTypesAreInitialized
int
slot
Constructors Summary
private Constructor()
Prevent this class from being instantiated.

        //do nothing
    
private Constructor(Class declaringClass, Class[] ptypes, Class[] extypes, int slot)
Creates an instance of the class. Only called from native code, thus private.

param
declaringClass the class this constructor object belongs to
param
ptypes the parameter types of the constructor
param
extypes the exception types of the constructor
param
slot the slot of the constructor inside the VM class structure

        this.declaringClass = declaringClass;
        this.parameterTypes = ptypes;
        this.exceptionTypes = extypes;          // may be null
        this.slot = slot;
    
Methods Summary
private native TconstructNative(java.lang.Object[] args, java.lang.Class declaringClass, java.lang.Class[] parameterTypes, int slot, boolean noAccessCheck)

public booleanequals(java.lang.Object object)
Indicates whether or not the specified {@code object} is equal to this constructor. To be equal, the specified object must be an instance of {@code Constructor} with the same declaring class and parameter types as this constructor.

param
object the object to compare
return
{@code true} if the specified object is equal to this constructor, {@code false} otherwise
see
#hashCode
since
Android 1.0

        return object instanceof Constructor && toString().equals(object.toString());
    
private native intgetConstructorModifiers(java.lang.Class declaringClass, int slot)

public java.lang.annotation.Annotation[]getDeclaredAnnotations()

        return getDeclaredAnnotations(declaringClass, slot);
    
private native java.lang.annotation.Annotation[]getDeclaredAnnotations(java.lang.Class declaringClass, int slot)

public java.lang.ClassgetDeclaringClass()
Returns the class that declares this constructor.

return
the declaring class
since
Android 1.0

        return declaringClass;
    
public java.lang.Class[]getExceptionTypes()
Returns the exception types as an array of {@code Class} instances. If this constructor has no declared exceptions, an empty array will be returned.

return
the declared exception classes
since
Android 1.0

        if (exceptionTypes == null)
            return new Class[0];
        return exceptionTypes;
    
public java.lang.reflect.Type[]getGenericExceptionTypes()
Returns the exception types as an array of {@code Type} instances. If this constructor has no declared exceptions, an empty array will be returned.

return
an array of generic exception types
throws
GenericSignatureFormatError if the generic constructor signature is invalid
throws
TypeNotPresentException if any exception type points to a missing type
throws
MalformedParameterizedTypeException if any exception type points to a type that cannot be instantiated for some reason
since
Android 1.0

        initGenericTypes();
        return Types.getClonedTypeArray(genericExceptionTypes);
    
public java.lang.reflect.Type[]getGenericParameterTypes()
Returns the generic parameter types as an array of {@code Type} instances, in declaration order. If this constructor has no generic parameters, an empty array is returned.

return
the parameter types
throws
GenericSignatureFormatError if the generic constructor signature is invalid
throws
TypeNotPresentException if any parameter type points to a missing type
throws
MalformedParameterizedTypeException if any parameter type points to a type that cannot be instantiated for some reason
since
Android 1.0

        initGenericTypes();
        return Types.getClonedTypeArray(genericParameterTypes);
    
public intgetModifiers()
Returns the modifiers for this constructor. The {@link Modifier} class should be used to decode the result.

return
the modifiers for this constructor
see
Modifier
since
Android 1.0

        return getConstructorModifiers(declaringClass, slot);
    
public java.lang.StringgetName()
Returns the name of this constructor.

return
the name of this constructor
since
Android 1.0

        return declaringClass.getName();
    
public java.lang.annotation.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.

return
an array of arrays of {@code Annotation} instances
since
Android 1.0

        Annotation[][] parameterAnnotations
                = getParameterAnnotations(declaringClass, slot);
        if (parameterAnnotations.length == 0) {
            return Method.noAnnotations(parameterTypes.length);
        }
        return parameterAnnotations;
    
private native java.lang.annotation.Annotation[][]getParameterAnnotations(java.lang.Class declaringClass, int slot)

public java.lang.Class[]getParameterTypes()
Returns an array of the {@code Class} objects associated with the parameter types of this constructor. If the constructor was declared with no parameters, an empty array will be returned.

return
the parameter types
since
Android 1.0

        return parameterTypes;
    
private java.lang.StringgetSignature()
Returns the constructor's signature in non-printable form. This is called (only) from IO native code and needed for deriving the serialVersionUID of the class

return
the constructor's signature

        StringBuilder result = new StringBuilder();
        
        result.append('(");
        for(int i = 0; i < parameterTypes.length; i++) {            
            result.append(getSignature(parameterTypes[i]));
        }
        result.append(")V");
        
        return result.toString();
    
private native java.lang.Object[]getSignatureAnnotation(java.lang.Class declaringClass, int slot)
Get the Signature annotation for this constructor. Returns null if not found.

java.lang.StringgetSignatureAttribute()

        Object[] annotation = getSignatureAnnotation(declaringClass, slot);

        if (annotation == null) {
            return null;
        }

        return StringUtils.combineStrings(annotation);
    
public java.lang.reflect.TypeVariable[]getTypeParameters()

        initGenericTypes();
        return formalTypeParameters.clone();
    
public inthashCode()
Returns an integer hash code for this constructor. Constructors which are equal return the same value for this method. The hash code for a Constructor is the hash code of the name of the declaring class.

return
the hash code
see
#equals
since
Android 1.0

        return declaringClass.getName().hashCode();
    
private synchronized voidinitGenericTypes()


        
        if (!genericTypesAreInitialized) {
            String signatureAttribute = getSignatureAttribute();
            GenericSignatureParser parser = new GenericSignatureParser(
                    VMStack.getCallingClassLoader2());
            parser.parseForConstructor(this, signatureAttribute);
            formalTypeParameters = parser.formalTypeParameters;
            genericParameterTypes = parser.parameterTypes;
            genericExceptionTypes = parser.exceptionTypes;
            genericTypesAreInitialized = true;
        }
    
public booleanisSynthetic()
Indicates whether or not this constructor is synthetic (artificially introduced by the compiler).

return
{@code true} if this constructor is synthetic, {@code false} otherwise
since
Android 1.0

        int mods = getConstructorModifiers(declaringClass, slot);
        return (mods & Modifier.SYNTHETIC) != 0;
    
public booleanisVarArgs()
Indicates whether or not this constructor takes a variable number of arguments.

return
{@code true} if a vararg is declare, otherwise {@code false}
since
Android 1.0

        int mods = getConstructorModifiers(declaringClass, slot);
        return (mods & Modifier.VARARGS) != 0;
    
public TnewInstance(java.lang.Object args)
Returns a new instance of the declaring class, initialized by dynamically invoking the constructor represented by this {@code Constructor} object. This reproduces the effect of {@code new 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 {@link 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 unwrapped. If the unwrapping 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 {@code Constructor} object 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.

param
args the arguments to the constructor
return
the new, initialized, object
exception
InstantiationException if the class cannot be instantiated
exception
IllegalAccessException if this constructor is not accessible
exception
IllegalArgumentException if an incorrect number of arguments are passed, or an argument could not be converted by a widening conversion
exception
InvocationTargetException if an exception was thrown by the invoked constructor
see
AccessibleObject
since
Android 1.0

        return constructNative (args, declaringClass, parameterTypes, slot, flag);
    
public java.lang.StringtoGenericString()
Returns the string representation of the constructor's declaration, including the type parameters.

return
the string representation of the constructor's declaration
since
Android 1.0

        StringBuilder sb = new StringBuilder(80);
        initGenericTypes();
        // append modifiers if any
        int modifier = getModifiers();
        if (modifier != 0) {
            sb.append(Modifier.toString(modifier & ~Modifier.VARARGS)).append(' ");
        }
        // append type parameters
        if (formalTypeParameters != null && formalTypeParameters.length > 0) {
            sb.append('<");
            for (int i = 0; i < formalTypeParameters.length; i++) {
                appendGenericType(sb, formalTypeParameters[i]);
                if (i < formalTypeParameters.length - 1) {
                    sb.append(", ");
                }
            }
            sb.append("> ");
        }
        // append constructor name
        appendArrayType(sb, getDeclaringClass());
        // append parameters
        sb.append('(");
        appendArrayGenericType(sb, 
                Types.getClonedTypeArray(genericParameterTypes));
        sb.append(')");
        // append exeptions if any
        Type[] genericEceptionTypeArray = 
                Types.getClonedTypeArray(genericExceptionTypes);
        if (genericEceptionTypeArray.length > 0) {
            sb.append(" throws ");
            appendArrayGenericType(sb, genericEceptionTypeArray);
        }
        return sb.toString();
    
public java.lang.StringtoString()
Returns a string containing a concise, human-readable description of this constructor. The format of the string is:
  1. modifiers (if any)
  2. declaring class name
  3. '('
  4. parameter types, separated by ',' (if any)
  5. ')'
  6. 'throws' plus exception types, separated by ',' (if any)
For example: {@code public String(byte[],String) throws UnsupportedEncodingException}

return
a printable representation for this constructor
since
Android 1.0

        StringBuilder result = new StringBuilder(Modifier.toString(getModifiers()));

        if (result.length() != 0)
            result.append(' ");
        result.append(declaringClass.getName());
        result.append("(");
        result.append(toString(parameterTypes));
        result.append(")");
        if (exceptionTypes != null && exceptionTypes.length != 0) {
            result.append(" throws ");
            result.append(toString(exceptionTypes));
        }
        
        return result.toString();