Use reflection in java to obtain all the field values of the pojo (entity) Class

Source: Internet
Author: User

Use reflection in java to obtain all the field values of the pojo (entity) Class

When talking about reflection, you have to say that it is too powerful. through reflection, you can easily get a variety of things. If you want to remove the dependency on a class in the project, you can consider using reflection.

Today, we will share with you how to get all the field values of the pojo class through reflection in java.

Why is this operation required? It is mainly used to override the toString method of the object class. Some people will say that the toString method is directly rewritten, and the string of the returned field. toString () is not enough. This can be done, but if you have hundreds or thousands of pojo classes, do you need to change them one by one? So we need to solve it in a new direction.

Because all our pojo classes inherit an IdEntity pojo class. It only has one id attribute. Therefore, if I override the toString method in IdEntity, all other pojo classes that inherit and IdEntity do not need to be written, and the format is also consistent. All the Field Values of the pojo class should be reflected here. If the field is an object of other pojo classes, all attributes should be included.

 

Package entity. qx; import java. lang. reflect. field;/*** @ author tq * @ version Creation Time: 5:03:08, January 1, December 16, 2015 */public class EntityToString {/*** @ MethodName: getString * @ Description: obtains all attributes and attribute values of a class * @ param o * operation Object * @ param c * operation class, which is used to obtain the Method * @ return */public static String getString (Object o, class <?> C) {String result = c. getSimpleName () + ":"; // obtain the parent class and determine whether it is an object class if (c. getSuperclass (). getName (). indexOf ("entity")> = 0) {result + = "\ n <" + getString (o, c. getSuperclass () + ">, \ n" ;}// obtain all the defined fields in the class Field [] fields = c. getDeclaredFields (); // cyclically traverses the Field to obtain the attribute value of the field for (field Field field: fields) {// if it is not empty, set the visibility and return the field. setAccessible (true); try {// you can use the get method to obtain the property value. Result + = field. getName () + "=" + field. get (o) + ", \ n";} catch (Exception e) {// System. out. println ("error --------" + methodName + ". reason is: "+ e. getMessage () ;}} if (result. indexOf (",")> = 0) result = result. substring (0, result. length ()-2); return result ;}}

In IdEntity, rewrite the toString method:

 

 

@Override  public String toString(){      return EntityToString.getString(this,this.getClass( ));  }  

 

 

As follows:

 

 

In this way, the format is unified, and the error rate increases due to too many code changes. The most important thing is to save a lot of time. It is necessary to complete the modification function at minimal cost.

 

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.