Java Reflection (i)-Starting from viewing members

Source: Internet
Author: User

There was a problem in the work. There are many data classes in the project that are named after Xxxbean, such as Novelbean,topicbean. There is one of the biggest hometopbean. This data class is very large and includes a variety of member variables. And seniors tend to use it when they just throw a jsonobject in. In short jsonobject inside some, this inside must have. There's nothing in the Jsonobject, it's inside. Then, this hometopbean is transmitted, I do not know which Members have values, which members are NULL, which member in the middle of the update data. Anyway, a mess.

Then I thought, I had to find a simple way to easily see the values of all the members in the data class anywhere in the code.

First I found the first point of knowledge. Gets the name of the member.

class<?> ClassType ==for(Field field:fields) {                             Stringbuilder.append (Field.getname ()). Append (":"). Append ("\ n");}

By the method above, we can get the names of all members of a class through Getdeclaredfields.

The moment feels half done (in fact, it's far away

The next step is to get the values for all the members.

After searching, I found a black technology called reflection. I do not understand the specific, and later study slowly. However, I now know that it has a feature that can be used to spell out some methods in code, like a spelling string, and call directly.

God, that is to say, I know the member name of the class, such as title, I can directly splicing out the GetTitle method, and then call. That means I just have to write the setter and getter in the Hometopbean, and I can use a foreach to get all of its member names and member values.

Here's how:

 PackageCom.dream.fishbonelsy.opentheclass;ImportAndroid.util.Log;ImportJava.lang.reflect.Field;Importjava.lang.reflect.InvocationTargetException;ImportJava.lang.reflect.Method;/*** Created by Fishbonelsy on 2015/6/15.*/ Public classOpenthestringclass { Public Staticstring Getclassstring (Object object) {string str=NULL; Class<?> ClassType =Object.getclass (); Field fields[]=Classtype.getdeclaredfields (); StringBuilder StringBuilder=NewStringBuilder ();  for(Field field:fields) {Try {                //get the GetXXX () method corresponding to the attributeString fieldName = Field.getname (). substring (0,1). toUpperCase () +field.getname (). SUBSTRING (1); //get the name of the GetXXX () method corresponding to the attributeString getmethodname = "get" + fieldName;//+ fieldname.substring (1);LOG.D ("Tag", "fieldname===" +fieldName); LOG.D ("Tag", "method name is = = = =:" +getmethodname); //get the GetXXX () method corresponding to the attributeMethod GetMethod = Classtype.getmethod (Getmethodname,Newclass[]{}); //Call the GetXXX () method of the original objectObject value = Getmethod.invoke (object,Newobject[]{}); if(value!=NULL) {stringbuilder.append (Field.getname ()). Append (":"). Append (Value.tostring ()). Append ("\ n"); }            } Catch(nosuchmethodexception e) {e.printstacktrace (); } Catch(InvocationTargetException e) {e.printstacktrace (); } Catch(illegalaccessexception e) {e.printstacktrace (); }} str=stringbuilder.tostring (); returnstr; }}

This gives you the value of all the members and is here for the time being. Done.

Java Reflection (i)-Starting from viewing members

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.