Java-reflection Reflection-gets all fields, including the parent class

Source: Internet
Author: User

Preface

Today, Android Mobile has to add a new feature, so return to the identity of Android programmers. In the process of development, found that the previous code wrote a lot of problems, really should take time out to refactor.

Where there is a crater of reflection, a tool class A method of reflection gets the property value of the incoming model. But when I pulled out the public attribute to do the base class, I found that I couldn't get the property value of the base class. The reason is that getdeclaredfields () is used.

Analysis
Method function
GetFields () Get all public fields, including the parent class field
Getdeclaredfields () Get all fields, public and protected and private, but do not include the parent class field
write a small way to verify the next ~

Write two classes, which define three fields, respectively, with public,protected,private decoration,
One called Parentmodel, as the parent class.
One called model, inherits Parentmodel

/** * 用作父类 */public class ParentModel { private String p_privateField; public String p_publicField; protected String p_protectedField;}

/** * 子类,继承上面定义的用作父类的ParentModel */public class Model extends ParentModel{ private String privateField; public String publicField; protected String protectedField;}

OK, use GetFields () and Getdeclaredfields () to get the model field, and cycle through the print.

 Field[] fs = Model.class.getFields(); Field[] fs1 = Model.class.getDeclaredFields(); for (Field f:fs) { Log.d("getFields","getFields---"+f.getName()); } for (Field f:fs1) { Log.d("getDeclaredFields","getDeclaredFields---"+f.getName()); }

It's time to witness the answer
GetFields () Print output:

Getdeclaredfields () Print output:

The test confirms that our conclusion is right.

I want to get all the field of the subclass and the parent class

What if you want to use reflection to get all the fields of Parentmodel and model with the model? It is obvious that the above two methods are not enough. What should I do?

Do not be afraid, we recursive model of the parent class to Getdeclaredfields (), the code is as follows:

new ArrayList<>() ;Class tempClass = Model.class;while (tempClass != null) {//当父类为null的时候说明到达了最上层的父类(Object类). fieldList.addAll(Arrays.asList(tempClass .getDeclaredFields())); tempClass = tempClass.getSuperclass(); //得到父类,然后赋给自己}for (Field f : fieldList) { Log.d("getAllFields","getFields---"+f.getName());}

You can see that we have all the fields of model and Parentmodel, not only that, but also two more fields shadow$_klass_ and shadow_monitor_ This is the field in object.

shadow$_monitor_And the shadow$_klass_ two fields that are added after the Android Sdk21 object.

If you want to block the effect of the object class, you can add another condition for the while loop:

while (tmpClass !=null && !tmpClass.getName().toLowerCase().equals("java.lang.object") ){ ....}
Update Instructions

2017.6.27 Update:

Before by the Netizen Lucky_god88 points out that the value of the blog reflect the real situation does not match, after verification, has been corrected as the correct answer, here thanks to the lovely lucky_god88 found and pointed out to me the problem, solve the problem at the same time I also grow. At the same time also reflect on their own, later to code more verification, careful and meticulous, serious and responsible.

Problem:
1.getFields () Gets the value of the protected Type field

This reason has not yet reappeared again, very strange, very puzzling

2.getFields () and Getdeclaredfields () method reflection gets one more field$change

This is related to the configuration of the development tool, as if the instant run was turned on, and the Android Studio 2.2.3 has been fixed, and the link is here

Java-reflection Reflection-gets all fields, including the parent 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.