Determines whether the object's property value is empty through reflection

Source: Internet
Author: User

Problem scenarios?

During project development, I submitted data in the form of a nested list inside the form, and submitted and saved the form data in the list at the same time. Sometimes I have added several new creden to the Administrator structure, but they are not actually saved to the database at this time. Instead, they are saved on the front end of the page for temporary storage and are waiting for the real submission. In this case, the deletion implementation method is as follows:

function delFundManage(obj){
$(obj).parent().parent().remove();
}

Removed, but when creating the list

 

To remove the first

 

Arr [1] is not changed to ARR [0]. After the data is submitted to the background, the following table is 1. Therefore, two data entries are displayed. This empty data cannot exist. Therefore, you need to judge the current object. whether all attribute values are empty

 

Go to the topic to determine whether the object's attribute value is null and use the reflection idea:

Private Boolean checkobjfiledsisnull (Object OBJ) {Boolean flag = false; int COUNT = 0; Class <?> Clazz = obj. getclass (); field [] fields = clazz. getdeclaredfields (); For (field: fields) {field. setaccessible (true); try {If (field. get (OBJ) = NULL) {count ++; If (COUNT = fields. length) {flag = true ;}} catch (exception e) {logger. error ("Object Property check exception:" + E) ;}return flag ;}

The difference between getfields () and getdeclaredfields () is designed here:

Getfield () obtains the public-modified attribute in the class.

All obtained by getdeclaredfields () does not include inheritance attributes.

When you are familiar with private operations, use the setaccessible (true) method to erase them.

 

Determines whether the object's property value is empty through reflection

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.