Read the 83rd example in the Java FAQ-dyslexia, which requires non-reflection to implement a copy of the Singleton object. Part of the data is reviewed to realize the copying of objects by reflection.
1. Write the object that needs to be copied person
PackageCom.scl.j2se.reflectjavabean; Public classperson{ PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } PublicString GetPassword () {returnpassword; } Public voidSetPassword (String password) { This. Password =password; } PublicString Getemail () {returnemail; } Public voidsetemail (String email) { This. email =email; } PublicString name; PublicString password; PublicString Email; PublicPerson () {} PublicPerson (string name, string password, string email) { This. Name =name; This. Password =password; This. email =email; } //The copy is a field, there is no copy of the object's method Public voidShowmymail () {System.out.println ("My Email address is" + This. email); }}View Code
2. Copy the fields of the person object using reflection
PackageCom.scl.j2se.reflectjavabean;ImportJava.lang.reflect.Field;ImportJava.lang.reflect.Method;/************************************************************************ * Version number: v1.0.0.0 * Author: SUNCL * Created: September 3, 2015 * Updated: September 3, 2015 * Project name: Reflectperson.java * Description: Learning content * Summary: * 1. Gets the method of the Type Object * 1) using the Class property of the classes. Class * Class<?> ClassType = Person.class; * 2) Known class object, using the object's instance method GetClass (), returns the object type at runtime * Person P = new person (); * P.getclass (); * 3) Use the class static method Forname () * class<?> ClassType = class.forname ("Com.scl.j2se.reflectjavabean.Person"); * 2. Getdeclaredfields (), the Getdeclaredmethods () method returns all declared Property objects and method objects defined by the class that contain "private". To manipulate private methods and fields, you need to use Setaccessible (true ); * ************************************************************************ * Copyright @ SUNCL 2013. All rights reserved. ************************************************************************/ Public classreflectperson{ Public Static voidMain (string[] args)throwsException {person MyPerson=NewPerson (); Myperson.setemail ("[Email protected]"); Myperson.setname ("Lee Ming"); Myperson.setpassword ("214"); Person Copyperson=(person) copymyobject (MyPerson); System.out.println ("Copyperson Name:" + copyperson.getname () + ", Password:" + copyperson.getpassword () + ", Email:" +Copyperson.getemail ()); } //copying objects using reflection Public StaticObject Copymyobject (Object obj)throwsException {//Get Type ObjectClass<?> ClassType =Obj.getclass (); //System.out.println (Classtype.getname ()); //creating a reflected type by constructing a methodObject object =classtype.getconstructor (). newinstance (); Field[] Fields=Classtype.getdeclaredfields (); for(Field field:fields) {//get access to all propertiesField.setaccessible (true); //Gets the field name, generating the corresponding method name based on the field name, for reflectionString FieldName =Field.getname (); String MethodName= Character.touppercase (Fieldname.charat (0)) + fieldname.substring (1); String Getstr= "Get" +MethodName; String Setstr= "Set" +MethodName; //call the Get method of the original object to assign a value to the set method of the copied objectMethod GetMethod =Classtype.getmethod (GETSTR); Method Setmethod=Classtype.getmethod (Setstr, Field.gettype ()); Object Getmethodresult=Getmethod.invoke (obj); //call the Set method to copy the value of the original object to the Copy objectSetmethod.invoke (object, Getmethodresult); } returnobject; }}View Code
Finally, the use of reflection is initially understood through this code writing. At the same time, after learning to produce some questions:
1) The object copy in strict sense should contain a copy of the referenced object, and the above content only copies the value type field
2) The method also belongs to the object, but is it useful to copy the object method? Similarly, the program does not copy the property's Get method, just set the value of the object to another object
3) Can I see the object address in myeclipse like Visual Studio ...
Java uses reflection to copy objects General field values