There are more than one javabean, but there are different properties in each bean, and both the get and set methods are used to modify and retrieve the values. If the debugging step by step to guess the internal structure, want to use a method to get different image of the values of each attribute, how to do? You can take advantage of the Java reflection mechanism.
1. This is Keng
Using reflection mechanism to traverse each attribute value in a single pair of image
public static void Reflect_object (Object o,string classPath) {
try {
Class UserClass = Class.forName (ClassPath);//Load Class
Method[] methods = Userclass.getdeclaredmethods ()//collection of methods for the class
Traversal Method Collection
for (int i =0; i<methods.length;i++) {
Gets the return value of all GETXX ()
Methods[i].getname () method returns the method name
if (Methods[i].getname (). StartsWith ("get")) {
Object = Methods[i].invoke (o, null);
System.out.println ("" "+methods[i].getname () +" = "+object);
}
}
System.out.println ("= = =");
catch (Exception e) {
E.printstacktrace ();
}
}
2. If the traversal of the bean has a parent class, then method 1 will not get the values of the properties of the parent class, so this class is expanded, not only can take the value of each property in the bean, but also the property values of the parent class can be obtained.
@SuppressWarnings ("Unchecked")
public static void Reflect_object (Object o,string classPath) {
try {
Class _class = Class.forName (ClassPath);//Load Class
Method[] methods = _class.getdeclaredmethods ()//collection of methods for the class
Recursive (O,_class);
Traversal Method Collection
for (int i = 0; i < methods.length; i++) {
Gets the return value of all GETXX ()
if (Methods[i].getname (). StartsWith ("get")) {//Method return method name
Methods[i].setaccessible (TRUE);//Allow private access (to avoid private getxx ())
Object = Methods[i].invoke (o, null);
System.out.println ("" + methods[i].getname () + "=" + object);
// }
// }
Traversing the parent class method collection
if (_class.getgenericsuperclass ()!= null) {
Class superclass = _class.getsuperclass ();//Parent class
method[] Supermethods = Superclass.getdeclaredmethods ();//Parent class method
for (int j = 0; J < Supermethods.length; J + +) {
Gets the return value of all Getxx () method:
if (Supermethods[j].getname (). StartsWith ("get")) {//Method return method name
Methods[j].setaccessible (TRUE);//Allow private access
Object = Supermethods[j].invoke (o, null);
System.out.println ("" + supermethods[j].getname () + "=" + object);
// }
// }
field[] fields = Superclass.getdeclaredfields ();//Parent class variable
for (Field field:fields) {
System.out.println (field);
// }
// }
System.out.println ("= = =");
catch (Exception e) {
E.printstacktrace ();
}
}
Recursive traversal of the property values of the class and the parent class
@SuppressWarnings ("Unchecked")
public static Class recursive (Object o,class _class) {
if (_class==null)
return null;
else{
Method[] methods = _class.getdeclaredmethods ()//collection of methods for the class
Traversal Method Collection
for (int i = 0; i < methods.length; i++) {
Gets the return value of all GETXX ()
if (Methods[i].getname (). StartsWith ("get")) {//Method return method name
Methods[i].setaccessible (TRUE);//Allow private access (to avoid private getxx ())
Object object;
try {
Object = Methods[i].invoke (o, null);
System.out.println ("" + methods[i].getname () + "=" + object);
catch (IllegalArgumentException e) {
TODO auto-generated Catch block
E.printstacktrace ();
catch (Illegalaccessexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
catch (InvocationTargetException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}
System.out.println ("= =" +_class.getname ());
Return recursive (O,_class.getsuperclass ());
}
}