Use reflection in Java to get all field values for a Pojo (entity) class

Source: Internet
Author: User

Speak of reflection. I have to say that it is too powerful to be able to easily get a variety of things through reflection, assuming that you want to release a dependency on a class in your project and consider using reflection.


Today's share is to get all the field values of the Pojo class through reflection in Java.


Why do you do this? The main purpose is to override the ToString method of the entity class. Someone would say. Override the ToString method directly. Returns the string for the field. ToString () is not allowed.

It's true that you can do this. But suppose your Pojo class has hundreds, thousands of, you also want to change it? So we need to deal with it from the new direction.


Because of all our Pojo class. Inherit a Pojo class of identity.

It only has an id attribute, so if I rewrite the ToString method in the identity, then all other Pojo classes that inherit and identity do not have to write anymore. And also guarantees the unification of the format. This is to reflect all the field values of the Pojo class, assuming that the field is an object of other Pojo classes, but also take it to list all the properties.

Package Entity.qx;import java.lang.reflect.field;/** * @author: Trichengrong * @group: Tgb8 * @Date: 2014-4-9 pm 8:37:22 * @Comme NTS: ToString method for overriding entity classes * @Version: 1.0.0 */public class entitytostring{/** * @MethodName: GetString * @Description: Received Take all attributes and attribute values in the class * @param o * operand * @param c * Operation class. Used to get the method in the class * @return */public static string getString (Object o, class<? > C) {string result = C.getsimplename () + " : ";//Gets the parent class. Infer whether the entity class if (C.getsuperclass (). GetName (). IndexOf ("entity") >= 0) {result + = "\n<" +getstring (O, C.getsuperclass ()) + ">,\n";} Gets all the defined fields in the class field[] field = C.getdeclaredfields ();//Loop through the field to get the corresponding property value for the field for (field Field:fields) {//assumed not to be empty. Set the visibility and return field.setaccessible (true); try{//the Set field is visible, you can get the property value using the Get method.

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

Then in identity, overriding the ToString method allows you to:

@Overridepublic String toString () {return entitytostring.getstring (This,this.getclass ());}

For example, the following:


This format is unified, and at the same time avoids the increase in error rates due to too much code modification. The most important thing is that you can save a lot of time. It is very necessary to modify the function at the minimum cost.

Use reflection in Java to get all field values for a Pojo (entity) class

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.