Java. Lang. Reflect. Constructor

Source: Internet
Author: User

Package java. Lang. Reflect;

Import sun. Reflect. constructoraccessor;
Import sun. Reflect. reflection;

/**
* Indicates the object of the constructor.
*
* Comment by liqiang
*
* @ Author kenth Russell
* @ Author nakul saraiya
*/
Public final
Class constructor extends accessibleobject implements member {

// Declare the class object of this Constructor
Private class clazz;
Private int slot;
// Parameter list Array
Private class [] parametertypes;
// Exception Array
Private class [] exceptiontypes;
// Modifier
Private int modifiers;
// The class that actually implements the function. The current class is only its proxy
Private volatile constructoraccessor;
// The coyp object retains the reference of the original object
Private constructor root;

// Constructor in the package
Constructor (class declaringclass,
Class [] parametertypes,
Class [] checkedexceptions,
Int modifiers,
Int slot)
{
This. clazz = declaringclass;
This. parametertypes = parametertypes;
This. exceptiontypes = checkedexceptions;
This. modifiers = modifiers;
This. Slot = slot;
}

/**
* Returns a copy of the current constructor object.
*/
Constructor copy (){
// Use the data of this object to generate a new constructor object
Constructor res = new Constructor (clazz, parametertypes,
Predictiontypes, modifiers, slot );
// The root of the new constructor points to itself
Res. Root = this;
// Share the constructoracoraccessor with the new constructor object
Res. constructoraccessor = constructoraccessor;
Return res;
}

// Return the class that defines this Constructor
Public class getdeclaringclass (){
Return clazz;
}

// Obtain the constructor name, which is consistent with the class name
Public String getname (){
Return getdeclaringclass (). getname ();
}

// Get Modifier
Public int getmodifiers (){
Return modifiers;
}

// Obtain the parameter list of this object
Public class [] getparametertypes (){
Return method. Copy (parametertypes );
}

// Returns the list of exceptions defined by this constructor. If no exception is defined, an array of 0 lengths is returned.
Public class [] getexceptiontypes (){
Return method. Copy (exceptiontypes );
}

/**
* Determine whether the specified object obj is equal to the current constructor object
*/
Public Boolean equals (Object OBJ ){
If (OBJ! = NULL & OBJ instanceof constructor) {// the object is not null and is a constructor object.
// Transformation
Constructor Other = (constructor) OBJ;
If (getdeclaringclass () = Other. getdeclaringclass ()){
// The definition classes of the two objects are equal, so their names must be equal.

Class [] params1 = parametertypes;
Class [] params2 = Other. parametertypes;

// Compare the parameter list
If (params1.length = params2.length ){
For (INT I = 0; I <params1.length; I ++ ){
If (params1 [I]! = Params2 [I])
Return false;
}
Return true;
}

}
}
 
// Two objects are not equal
Return false;
}

// Return hashcode
Public int hashcode (){
Return getdeclaringclass (). getname (). hashcode ();
}

/**
* Returns the string description of the constructor.
*/
Public String tostring (){
Try {
Stringbuffer sb = new stringbuffer ();
Int mod = getmodifiers ();

If (mod! = 0 ){
// Identifier
SB. append (modifier. tostring (MOD) + "");
}

// Note that the class name is displayed in the field method. The array type is different from that of class. getname ().
// Class Name
SB. append (field. gettypename (getdeclaringclass ()));
SB. append ("(");

// Separate the parameter list ","
Class [] Params = parametertypes; // avoid clone
For (Int J = 0; j <Params. length; j ++ ){
SB. append (field. gettypename (Params [J]);
If (j <(Params. Length-1 ))
SB. append (",");
}
SB. append (")");

// Use "," to separate the exception list
Class [] Exceptions = exceptiontypes; // avoid clone
If (exceptions. length> 0 ){
SB. append ("throws ");
For (int K = 0; k <exceptions. length; k ++ ){
SB. append (exceptions [K]. getname ());
If (k <(exceptions. Length-1 ))
SB. append (",");
}

}
Return sb. tostring ();
} Catch (exception e ){
Return "<" + E + "> ";
}
}

/**
* The Most Important Method
* Construct an instance of a class. If the constructor has no parameters, the input array is null or the length is 0,
* Parameters of the original type need to be encapsulated into their packaging classes.
*
*/
Public object newinstance (object [] initargs)
Throws instantiationexception, illegalaccessexception,
Illegalargumentexception, invocationtargetexception
{
If (! Override) {// check
If (! Reflection. quickcheckmemberaccess (clazz, modifiers )){
Class caller = reflection. getcallerclass (2 );
If (securitycheckcache! = Caller ){
// The Class Object of the previous caller passed is cached. If it is different from the previous one, the security check is performed.
Reflection. ensurememberaccess (caller, clazz, null, modifiers );
Securitycheckcache = caller;
}
}
}

// Check acquireconstructoraccessor
If (constructoraccessor = NULL) acquireconstructoraccessor ();

// Constructor is only a proxy class. The operation to create an object is performed in constructoraccescessor.
Return constructoraccessor. newinstance (initargs );
}

Private void acquireconstructoraccessor (){
Constructoraccessor TMP = NULL;
// Obtain the upper-layer constructoraccessor
If (root! = NULL) TMP = root. getconstructoraccessor ();
If (TMP! = NULL ){
// If the upper-layer constructoraccessor is not empty
// Constructoraccessor pays the current constructoraccessor and returns
Constructoraccessor = TMP;
Return;
}

// Create
TMP = reflectionfactory. newconstructoraccessor (this );
Setconstructoraccessor (TMP );
}

// Directly return onstructoraccessor
Constructoraccessor getconstructoraccessor (){
Return constructoraccessor;
}

// Set the current constructoraccessor object to change the previous constructoraccescessor object
Void setconstructoraccessor (constructoraccessor accessor ){
// Set the current constructoraccessor object to a new constructoraccescessor object.
Constructoraccessor = accessor;

// Recursively call the upper-layer constructoraccessor object setting method
If (root! = NULL ){
Root. setconstructoraccessor (accessor );
}
}

Int getslot (){
Return slot;
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.