Array of reflection operations---deserialization trivia

Source: Internet
Author: User

Object[] as the data source

The model has a property of xxx[] Unknown, in Java, object[] cannot be coerced into xxx[], if the F.set (obj,fieldobj) above, the method reflects the write value, will be an error.

Specific methods:

//obj As Object//ClassKey The name of the member variable for the arrayJava.lang.reflect.Field f =Obj.getclass (). Getdeclaredfield (classkey);//Get Array typeClass Fieldclazz =F.gettype ();if(Fieldclazz.isarray ()) {object[] Arrayobjs= (object[]) value;//object[of deserialization production]//Fieldclazz.getcomponenttype (); Gets the member type of the array//array.newinstance (clazz,length) is equivalent to clazz[] Array = new Clazz[length]; but Clazz is unknown, the latter cannot be usedObject fieldobj =array.newinstance (Fieldclazz.getcomponenttype (), arrayobjs.length);  for(intj = 0; J < Arrayobjs.length; J + +) {        //methods for writing valuesArray.set (Fieldobj, J, Arrayobjs[j]); } f.set (Obj,fieldobj);}

Array of reflection operations---deserialization trivia

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.