Java Bean Conversion Map

Source: Internet
Author: User

/**

*

* @Title: Convertmap

* @Description: Using generic Map-to-bean

* First, the type of the incoming entity bean is instantiated by reflection to get all the methods of the bean.

* Traverse all key of map, capitalize key first letter, prefix add set, get method name

* Then traverse all methods of the bean, if there is a method name and map can be spliced method name consistent,

* Insert the value of map into the bean (provided the type must be consistent, otherwise an error)

* @param @param <T>

* @param @param map

* @param @param TC

* @param @return

* @param @throws IllegalArgumentException

* @param @throws Illegalaccessexception

* @param @throws InvocationTargetException

* @param @throws SecurityException

* @param @throws Nosuchmethodexception

* @param @throws instantiationexception Setting parameters

* @return T return type

* @since 1.0.0

* @author Huyidong

* @throws

*/

public static <T> T convertmap (map map, class<t> tc) throws IllegalArgumentException, Illegalaccessexception , InvocationTargetException, SecurityException, Nosuchmethodexception, instantiationexception{

T t = tc.newinstance ();

Iterator It=map.entryset (). Iterator ();

Method [] methods=tc.getdeclaredmethods ();

while (It.hasnext ()) {

Map.entry entry= (Map.entry) it.next ();

String methodname= "Set" +beantomap.fristtouppercase (Entry.getkey (). toString ());

for (Method method:methods) {

if (Method.getname (). Equals (MethodName)) {

Object Value=entry.getvalue ();

Method.invoke (t, value);

Break

}

}

}

return t;

}

/**

*

* @Title: Fristtouppercase

* @Description: Capitalize the first letter

* @param @param str

* @param @return Set parameters

* @return String return type

* @since 1.0.0

* @author Huyidong

* @throws

*/

public static string Fristtouppercase (String str) {

Str=str.substring (0,1). toUpperCase () +str.substring (1,str.length ());

return str;

}

/**

*

* @Title: Fristtouppercase

* @Description: First Letter lowercase

* @param @param str

* @param @return Set parameters

* @return String return type

* @since 1.0.0

* @author Huyidong

* @throws

*/

public static string Fristtolowercase (String str) {

Str=str.substring (0,1). toLowerCase () +str.substring (1,str.length ());

return str;

}


/**

*

* @Title: Convertbean

* @Description: Entity Bean turns into map

* Traverse all methods of an object

* Gets the method of get in the entity bean, returns the value using the reflection Get method, if the value is not empty, is the value of map,

* Then take the Get method to remove the get part, the first letter to lowercase, the key for the map

* @param @param obj

* @param @return

* @param @throws IllegalArgumentException

* @param @throws Illegalaccessexception

* @param @throws invocationtargetexception Setting parameters

* @return Map return type

* @since 1.0.0

* @author Huyidong

* @throws

*/

public static Map Convertbean (Object obj) throws IllegalArgumentException, Illegalaccessexception, invocationtargetexception{

Class Cla=obj.getclass ();

Method [] methods=cla.getdeclaredmethods ();

Map map =new HashMap ();

for (Method method:methods) {

if (Method.getname (). StartsWith ("get")) {

String name=method.getname (). substring (3,method.getname (). Length ());

String key =beantomap.fristtolowercase (name);

Object value = Method.invoke (obj);

if (null!=value) Map.put (key, value);

}

}

return map;

}


Java Bean Conversion Map

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.