/**
* Construct a copy of a new object based on the parameters of the specified method and return it
* @ Param OBJ original object
* @ Return new object
* @ Throws nosuchmethodexception
* @ Throws invocationtargetexception
* @ Throws illegalaccessexception
* @ Throws instantiationexception
* @ Throws securityexception
* @ Throws illegalargumentexception
*/
@ Suppresswarnings ("unchecked ")
Public static object copy (Object OBJ) throws illegalargumentexception, securityexception, instantiationexception, illegalaccessexception,
Invocationtargetexception, nosuchmethodexception
{
// Obtain the object type
Class classtype = obj. getclass ();
// Create a new object using the default constructor. The getconstructor determines which constructor is called based on its parameters.
Object objectcopy = classtype. getconstructor (new class [] {}). newinstance (new object [] {});
// Obtain all attributes of an object
Field [] fields = classtype. getdeclaredfields ();
For (INT I = 0; I <fields. length; I ++)
{
// Obtain the corresponding attribute in the array
Field field = Fields [I];
String fieldname = field. getname ();
String stringletter = fieldname. substring (0, 1). touppercase ();
// Obtain the getxxx and setxxx method names of the corresponding attributes.
String getname = "get" + stringletter + fieldname. substring (1 );
String setname = "set" + stringletter + fieldname. substring (1 );
// Obtain the corresponding method
Method getmethod = classtype. getmethod (getname, new class [] {});
Method setmethod = classtype. getmethod (setname, new class [] {field. GetType ()});
// Call the getxxx () method of the source object
Object value = getmethod. Invoke (OBJ, new object [] {});
// Call the setxxx () method of the copy object
Setmethod. Invoke (objectcopy, new object [] {value });
}
Return objectcopy;
}