The method of transforming objects and maps using reflection mechanism in Java

Source: Internet
Author: User

The main function of the reflection mechanism of Java is to access the object's properties, methods and so on. Therefore, the translation of objects and maps in Java can be implemented using the reflection mechanism of java. Examples are as follows:

First, the method of object transfer map

1      Public StaticMap<string, object> javabean2map (Object javaBean)throwsException {2map<string, object> map =NewHashmap<>();3Method[] methods = Javabean.getclass (). GetMethods ();//Get all methods4          for(Method method:methods) {5             if(Method.getname (). StartsWith ("Get")) {6String field = Method.getname ();//Stitching Property name7field = Field.substring (Field.indexof ("get") + 3);8field = Field.tolowercase (). CharAt (0) + field.substring (1);9Object value = Method.invoke (JavaBean, (object[])NULL);//Execution MethodTen map.put (field, value); One             } A         } -         returnmap; -}

In this example, the GetMethods () method is used to support the acquisition of private properties of the object's parent class. You can also use the Getdeclaredmethods () method instead of the GetMethods () method. There are certain differences between the two, depending on the circumstances of the object to be converted:

GetMethods (): Gets the current class and all inherited, public-decorated methods of the parent class, including public only.

Getdeclaredmethods (): Gets all the methods of the current class, including the Public/private/protected/default-modified method.

Similarly, this distinction applies to obtaining the GetFields () and Getdeclaredfields () of class properties, GetConstructors () and Getdeclaredconstructors (), which get the class construction method.

Method of Map to Object

1      Public StaticObject Map2javabean (class<?> clazz, map<string, object> Map)throwsException {2Object JavaBean = Clazz.newinstance ();//Building Objects3Method[] methods = Clazz.getmethods ();//Get all methods4          for(Method method:methods) {5             if(Method.getname (). StartsWith ("Set")) {6String field = Method.getname ();//intercepting attribute names7field = Field.substring (Field.indexof ("set") + 3);8field = Field.tolowercase (). CharAt (0) + field.substring (1);9                 if(Map.containskey (field)) {Ten Method.invoke (JavaBean, Map.get (field)); One                 } A             } -         } -         returnJavaBean; the}

In this case, the same is done by getting all the methods of the object to loop, and then executing the set method of the Class property to store the value in the object's properties. As mentioned above, the method of reflection has a keyword range, and usually the class property of Get (), set () is public, the class property itself may be private, so it is more general, if there is no inheritance relationship or the class property is public, It is easier to use the Field.set (obj, value) method to extract the values by iterating through the map.

The method of transforming objects and maps using reflection mechanism in Java

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.