Java reflection Gets the property values of the object and the child property values in the object properties

Source: Internet
Author: User

In recent times, I've been working on a Web project, with jquery Easyui on the front.

For convenience, ready to do a front-end universal Datagird export Excel function, Bo Master also considered the idea and the final function, 1, the front-end selected row export, 2, the current page export, 3, the current filtering conditions export.

Want to lazy on the Internet to find the existing code changes, found that most can only meet individual needs, using JS Export can only meet the front end, using code to achieve 3 of functions.

。。。。。。


Well, say a bunch of crap, back to the point, this article is to do the general custom field export needs, according to the property name to find objects and sub-objects, find the corresponding property values, crawl back into Excel.


Directly on the code, has been encapsulated, direct call can:

import java.lang.reflect.field;/** *  Property (object) value reflection Get tool classes  *  @author   around  * @ date 2016-8-14 */public class paramsreflect {public static final  string file_field =  "Nameb,";/** *  Gets the properties (objects)  *  declarations of the corresponding fields of the current object, It is important to be aware that capturing the attribute fields that you need in the nosuchfieldexception exception capture intercepts, telling you when querying these property names, specifying which objects to look for, and if you don't tell it, it's not known  *  @param   Obj Current object  *  @param  field The name of the property you want to get, either the property name in the current object,  or the property name of the object in the current object  *  @returnObject    the current object specifies the property value  */public static object getfieldvalue (object obj, string  field)  {class<?> claz = obj.getclass (); field f = null;object fieldvalue = null;try {f =  Claz.getdeclaredfield (field); F.setaccessible (true); Fieldvalue = f.get (obj);}  catch  (securityexception e)  {e.printstacktrace ();}  catch  (nosuchfieldexception e) &NBSp {//Here the exception is caught as: The property name exception is not found. Note here that we are going to manually help it find the value of which FIELD the object is supposed to be, because we don't know the relationship between them, so we need to specify the relationship manually and which object to associate if (file_field.indexof field)  !=  -1)  fieldvalue = getcustomchildobj (Obj, claz, b.class, field);else  Fieldvalue = null;}  catch  (illegalargumentexception e)  {e.printstacktrace ();}  catch  (illegalaccessexception e)  {e.printstacktrace ();} Return fieldvalue;} /** *  Gets the custom sub-Property object, passes in the specified object name, finds the child object in the current object, and finds the Child property by field  *  @param  obj The parent object name  * @ Param claz Parent Object class reflection  *  @param  customclass Custom-judged sub-object type  *  @param  field property name  *  @return  object */public static object getcustomchildobj (Object obj,  class<?> claz, class<?> customclass, string field)  {Field[]  fs = claz.getdeclaredfields (); field f = null;for  (int i = 0; i < fs.length; i++)  {f = fs[i];if (F.gettype (). Equals (Customclass))  {return  Getchildobjectparam (Obj, f, field);//return claz.getdeclaredfield (F.getName ());}} Return null;} /** *  by finding the child object, getting to the current property, passing in the desired property name, getting the property value  *  @param  o Parent object  *  @param   The Field object for the child object under the F Parent Object  *  @param the property name that you want to get  field  *  @returnObject  */public static  object getchildobjectparam (Object o, field f, string field)   { F.setaccessible (True);object obj = null; class<?> childclass = null; Field childf = null;object fieldvalue = null;try {obj = f.get (o); Childclass = obj.getclass (); Childf = childclass.getdeclaredfield (field); Childf.setaccessible (True); Fieldvalue = childf.get (obj);}  catch  (illegalargumentexception e)  {e.printstacktrace ();}  catch  (securityexception e)  {e.printstacktrace ();}  catch  (illegalaccessexception e)  {e.printstacktrace ();}  catch  (nosuchfieldexception e)  {e.printstacktrace ();} Return fieldvalue;}}

Simulation test:

Object A has the attribute name, child object B, and object B with the property ID,NAMEB.

b b = new B ();

B.setid ("B");

B.setnameb ("B-name");


A = new A ();

A.setname ("A-name");

A.setb (b);


Object value1 = Paramsreflect.getfieldvalue (A, "name"); Get the properties in a

Object value2 = Paramsreflect.getfieldvalue (A, "nameb"); Get the properties in B



function is relatively simple, not flexible, in order to cope with the function, first made a version of this, and then continue to improve, but also leave a few questions.

1. The current version only controls the need to pass in the corresponding sub-object. Class, bloggers have not yet thought about how to set the corresponding property values automatically. (also want to directly to the object and all the sub-objects inside the scan, take the need to get the worthwhile field in, but found a problem, the properties in different objects, there may be the same property name, so the scan can not control uniqueness)


2, the above version is used for Easyui in the DataGrid, the control of the field property can not be named "B.nameb", can not directly use sub-object to get sub-attributes, Bo Master also tried to mend Easyui JS Source, Let IT support child objects. The way the property is, but there are other problems incompatible with this way, so the implementation of this scheme, there are other ways of the great God?


3, the final version, the author is ready to perfect upgrade it in the idle time, not limited to the DataGrid, all support the export of Excel features. What we're going to do here is to get the object into the background and go into the database query, which gets all the table, field name, and field type of the corresponding entity. It is then processed into a corresponding format, which is a sub-object, Use the child object property name. Sub-object Properties stitching good, back to the foreground, let the user choose the column to be exported, after the selection to return to the background, splicing these fields, generate SQL statements, incoming background self-query, as for the filter that part, should be no way to extract, need to be dealt with in their respective control layer.


Keep track of every technical experience.

This article is from the "Around" blog, make sure to keep this source http://around.blog.51cto.com/10926575/1837847

Java reflection Gets the property values of the object and the child property values in the object properties

Related Article

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.