The Java reflection mechanism is in the running state, for any class, can know all the properties and methods of this class, for any one object, can call any of its methods and properties; This dynamically acquired information and the ability to dynamically invoke the object's methods are called the reflection mechanisms of the Java language.
Java Reflection (emission) mechanism: "When a program is run, it is allowed to change the program structure or variable type, which is called Dynamic language." From this point of view, Perl,python,ruby is a dynamic language, and c++,java,c# is not a dynamic language. But Java has a very prominent dynamic-related mechanism: Reflection, in Java, refers to the classes that we can load, detect, and use completely unknown during compilation at runtime. In other words, a Java program can load a runtime to know the name of a class, learn its full construct (but not including the methods definition), and generate its object entities, or set values on its fields, or evoke its methods.
The Java Reflection mechanism provides the following functions: To determine the class to which any object belongs at run time, to construct an object of any class at run time, to determine the member variables and methods that any class has at run time, to invoke a method of any object at run time, and to generate a dynamic proxy.
Here we take a practical example to illustrate the use of Java's reflection mechanism to set up and get the property values of an entity class method.
package com.ultrapower.ultracmdb.inventory.webapp.component.special.modelmanager.util;import java.lang.reflect.field;public class testinvoke{ @SuppressWarnings ("Rawtypes" ) public static void main (string args[]) throws illegalargumentexception, illegalaccessexception { perpole bean = new perpole (); // Get Class Objects Class userCla = (Class) bean.getclass (); /* * get all the property collections in the class */ field[] fs = usercla.getdeclaredfields (); for (int i = 0;i < fs.length;i++) { Field f = fs[i]; f.setaccessible (true); // setting some properties is accessible object val = f.get (bean);// Gets the value of this property system.out.println ("Name: " + f.getname () + " \t value = " + val); string type = f.gettype (). toString ();// Gets the type of this attribute if (Type.endswith ("String")) { &Nbsp; system.out.println (F.gettype () + "\ T is string"); f.set (Bean, "Caocao"); // Set values for attributes } else if (Type.endswith ("int") | | type.endswith ("Integer")) { system.out.println ( F.gettype () + "\ t is int"); f.set (bean,12); // attribute set value } else { system.out.println (F.gettype () + "\ T"); } } System.out.println ("======================================"); System.out.println ("The value of the set Bean is: \ n" +bean.getname () + "\ T" +bean.getage ()); }}class perpole{ private string name; private int Age; 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; } }
Using the reflection mechanism in Java to get and set property values for an entity class