First, let's get to know several classes.
Class (Java.lang.Class)
A class object is a special object, and each class has a class object that is used to create the general object of the class. Can be obtained through the object's GetClass () method.
For example, we run this line of code:
Copy Code code as follows:
SYSTEM.OUT.PRINTLN ("Test". GetClass (). toString ());
The result is:
Copy Code code as follows:
Class Java.lang.String
Field (Java.lang.reflect.Field)
This class represents a field that you can use to access the class
Next, we'll create the test class:
Copy Code code as follows:
Class Book {
Public String title;
public int length = 0;
public arraylist<page> pages = null;
@Override
public String toString () {
String sb = "book:\n";
sb = "title=" +title+ "\ n";
SB + + "Length=" + length + "\ n";
sb = "pages=" + pages + "\ n";
if (pages!= null) {
for (Page page:pages) {
SB + page.tostring ();
}
}
return sb.tostring ();
}
}
Class Page {
@Override
Public String toString () {
return "page\n";
}
}
Call the following methods to test the above class:
Copy Code code as follows:
Book book = new book ();
SYSTEM.OUT.PRINTLN (book);
The result of it is this:
Copy Code code as follows:
Book:
Title=null
Length=0
Pages=null
This is the initial state of the book object
We then get the Length field in the book object and modify it to see the result, through the reflection mechanism:
Copy Code code as follows:
Book book = new book ();
class<?> AClass = Book.getclass ();
Field field = Aclass.getfield ("Length");
Field.setint (book, 9);
SYSTEM.OUT.PRINTLN (book);
You can see the results of the run:
Copy Code code as follows:
Book:
Title=null
Length=9
Pages=null
As you can see, the value of the length field has been modified.
The above is a simple int type of field, in fact, like the title field, such as Object fields can be modified, the following is an example:
Copy Code code as follows:
Book book = new book ();
class<?> AClass = Book.getclass ();
field[] fields = Aclass.getfields ();
for (Field field:fields) {
Field.setaccessible (TRUE);
if (Field.gettype (). Equals (String.class)) {
Field.set (book, "Grimm's Fairy Tale");
}else if (Field.gettype (). Equals (Int.class)) {
Field.set (book, 199);
}
}
SYSTEM.OUT.PRINTLN (book);
The above code output results are:
Copy Code code as follows:
Book:
Title= Grimm's Fairy Tales
length=199
Pages=null
In fact, all of the string fields are modified to "Grimm's Fairy Tales," and all int type fields are modified to 199. We don't even know what this field means.
Next, we modify the pages for this field. This field is a ArrayList, we will create a ArrayList object and insert an object into it.
Copy Code code as follows:
Book book = new book ();
class<?> AClass = Book.getclass ();
field[] fields = Aclass.getfields ();
for (Field field:fields) {
Field.setaccessible (TRUE);
if (Field.gettype (). Equals (Arraylist.class)) {
String genric = Field.getgenerictype (). toString ();
String Genricclass = genric.substring (
Genric.indexof (' < ') + 1,
Genric.indexof (' > '));
class<?> Entityclass = Class.forName (Genricclass);
Object obj = entityclass.newinstance ();
ArrayList list = new ArrayList ();
List.add (obj);
Field.set (book, list);
}
}
SYSTEM.OUT.PRINTLN (book);
The output results are as follows:
Copy Code code as follows:
Book:
Title=null
Length=0
Pages=[page
]
Page
As you can see, even though we didn't use the page class directly, we created a Page object.
The creation statement for the Page object is in: Entityclass.newinstance (). The newinstance statement is an important method of the class object to create the corresponding object for this class. Of course, the construction method support of the class is required. In addition, through the Gengenerictype method, we can get the type modifiers of the field. Put it here, get the arraylist<page>. With this string, we can load the page class and create a Page object through the class loader class.forname.