Currently, there are few places in the project to use the Java reflection mechanism (except the spring framework), but for subsequent preparation, it is simple to summarize the recent reflections as follows:
1. By reflection in optics, the reflection in Java can be interpreted as "mirroring". Has the following uses:
The Java reflection mechanism mainly provides the following purposes:
- Determine the class to which any object belongs at run time
- To construct an object of any class at run time
- Determine the member variables and methods that any one class has at run time
- Methods to invoke any object at run time
2. Using the reflection mechanism of Java, it is generally necessary to follow three steps:
- Getting class objects for classes
- Gets the method or property name of the action class
- The method or property obtained in the second step of the operation
3. There are 3 ways to obtain class objects for classes:
1. Call the static method of class Forname
2. Use the class's. class syntax, such as:class<?> CLS = String.class
3. GetClass method for calling objects
4. In the reflection mechanism of Java, classes correspond to class objects; class methods correspond to method; class attributes correspond to field.
5. When you get the methods, properties, and constructors of a class, there are two methods that correspond to GetXXX and getdeclaredxxx. The difference is that the former returns methods and properties that have access to public, including those in the parent class, and that returns all methods and properties of the access permission, but does not include the parent class.
6. Example: Copy (shallow copy) of object using reflection mechanism
Public Static voidMain (string[] args) {Student Student=NewStudent (); Student.setstuage (29); Student.setstuname ("Chris"); Persons Persons=NewPersons (); Persons.setfriend ("Chen"); Persons.setteacher ("All People"); Student.setpersons (persons); Student Copystu= copyObject(student); System.out.println ("--------" + copystu.getstuname () + ":" + copystu.getstuage () + "-" +copystu.getpersons (). Getfriend () + "-" +copystu.getpersons (). Getteacher ()); Student.getpersons (). Setteacher ("Hehe"); System.out.println ("--------" + copystu.getstuname () + ":" + copystu.getstuage () + "-" +copystu.getpersons (). Getfriend () + "-" +copystu.getpersons (). Getteacher ()); }
Persons class:
Public classPersons {PrivateString friend; PrivateString teacher; PublicString getfriend () {returnfriend; } Public voidsetfriend (String friend) { This. Friend =friend; } PublicString Getteacher () {returnteacher; } Public voidSetteacher (String teacher) { This. Teacher =teacher; }}
Student class:
Public classStudent {/**name*/ PrivateString Stuname; /**Age*/ Private intStuage; /**Persons*/ PrivatePersons Persons; PublicPersons getpersons () {returnpersons; } Public voidsetpersons (Persons Persons) { This. Persons =persons; } PublicString Getstuname () {returnStuname; } Public voidsetstuname (String stuname) { This. Stuname =Stuname; } Public intGetstuage () {returnStuage; } Public voidSetstuage (intstuage) { This. Stuage =Stuage; }}
CopyObject Method:
Private Static<T>T copyObject(T t) {Class<?> CLS =T.getclass (); Field[] Fields=Cls.getdeclaredfields (); T o=NULL; Try{o=(T) cls.newinstance (); for(Field field:fields) {field.setaccessible (true); Object O1=Field.get (t); Field.set (O,O1); } } Catch(instantiationexception e) {e.printstacktrace (); } Catch(illegalaccessexception e) {e.printstacktrace (); } returno; }
When writing the CopyObject method, the following generic method is attempted. The flag <T> in the original generic method is required.
The above example shows that using the reflection mechanism to realize the function of copy object can only be shallow copy.
This example extends to the implementation of the Copy object, as follows:
Http://blog.sina.com.cn/s/blog_70e6618b0100om09.html
(1) object copy shallow copy and deep copy. The difference is in the replication of non-basic data types. The former copies the reference, which copies the child objects.
(2) Shallow copy object:
1. Implementation method:
The first step: Implement java.lang.Cloneable interface;
Step two: Override the Java.lang.Object.clone () method and set the method to public
2. The Clone () method in the object class is native and performs more efficiently. But the execution is still shallow copy!!! Therefore, the overridden clone () method performs a shallow copy.
(3) Deep copy objects:
There are several ways to achieve this:
1. New object and sub-object, all reset properties;
2. For all non-basic data type properties, the corresponding class implements the Cloneable interface and overrides the Clone () method;
3. use object serialization and deserialization .
Analysis of Java Reflection mechanism