Methods for traversing classes and parent classes with Java reflection

Source: Internet
Author: User

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 ());
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.