The Thinking of programmers is very active. When I first read the Pom. xml file, I suddenly forgot what Maven meant. I found out that the word Maven came from the yidixu language, meaning the accumulation of knowledge ......
Knowledge is constantly accumulated. Sometimes, you must repeat it to know whether it has accumulated into your knowledge base. Here, the Java Foundation and reflection mechanism are taken into account.
First look at the principle:
Definition: the Java reflection mechanism is in the running state. For any class, all attributes and methods of the class can be known. For any object, can call any of its methods and attributes. This kind of information obtained dynamically and the function of dynamically calling methods of objects is called the reflection mechanism of Java language.
Similar to most definitions, Java reflection mechanisms are used to obtain objects (classes, attributes, and methods) by name.
Provides the following functions:
Determine the class to which any object belongs at runtime.
Construct any class object at runtime.
Judge the member variables and methods of any class at runtime.
Call methods of any object at runtime
At this time, some people may have an epiphany that they have been using this technology.
By convention, paste the source code to make it clearer:
Package Nov; import Java. lang. reflect. field; import Java. lang. reflect. method;/***** <p> * Title: reflection mechanism test code/P> ** <p> * description: example business class * </P> ** <p> * copyright: Copyright (c) 2012 * </P> *** @ author DML @ 2012-11-29 * @ version 1.0 */public class reflecttester {public object copy (Object object) throws exception {// obtain the class of the object type <?> Classtype = object. getclass (); system. out. println ("class:" + classtype. getname (); // use the default constructor to create a new object objectcopy = classtype. getconstructor (new class [] {}). newinstance (new object [] {}); // obtain all attributes of the object field fields [] = classtype. getdeclaredfields (); For (INT I = 0; I <fields. length; I ++) {field = Fields [I]; // attribute name string fieldname = field. getname (); // obtain the first letter of the attribute name and convert it to an uppercase string firstletter = fieldname. substring (0, 1 ). touppercase (); // get the name of the getxxx () method corresponding to the property: the first letter of the get + property name and convert it to uppercase + the attribute name to remove the first letter, // If the attribute name is name, get + N + amestring getmethodname = "get" + firstletter + fieldname. substring (1); // obtain the name of the setxxx () method corresponding to the property string setmethodname = "set" + firstletter + fieldname. substring (1); // obtain the getxxx () method that corresponds to the property. Method getmethod = classtype. getmethod (getmethodname, new class [] {}); // obtains the setxxx () method corresponding to the property. The input parameter is of the parameter type: Method setmethod = classtype. getmethod (setmethodname, new class [] {field. getType ()}); // call the getxxx () method of the original object. Object value = getmethod. invoke (object, new object [] {}); system. out. println (fieldname + ":" + value); // call the setxxx () method of the copy object setmethod. invoke (objectcopy, new object [] {value});} return objectcopy;} public static void main (string [] ARGs) throws exception {customer = new customer ("Tom", 21); customer. setid (New Long (1); // object instance generated during runtime: the constructor without parameters in the class directly uses the newinstance () method customer customercopy = (customer) new reflecttester (). copy (customer); system. out. println ("copy information:" + customercopy. GETID () + "" + customercopy. getname () + "" + customercopy. getage () ;}} Class Customer {private long ID; private string name; private int age; public customer () {} public customer (string name, int age) {This. name = Name; this. age = age;} public long GETID () {return ID;} public void setid (long ID) {This. id = ID;} Public String getname () {return name;} public void setname (string name) {This. name = Name;} public int getage () {return age;} public void setage (INT age) {This. age = age ;}}
Note: The above code is taken from: [video Note] Java reflection mechanism note
Input result:
Class:nov.Customerid:1name:Tomage:21Copy information:1 Tom 21
Take a closer look at this piece of code, then look at the theory, and finally look at it again, and finally write another program. In the future, there will be another good tool for project development.
P.s. Is coming to work. Recently, my life has been very depressing. My longing for life has instantly become a bubble. Ah, I think about heaven and hell. Who knows what it will look like in the future?
Dml@2012.11.29