/**
*
* @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