Map objects and transforming gongju

 1 package czc.superzig.modular.utils;
 2 
 3 import java.lang.reflect.Field;
 4 import java.util.HashMap;
 5 import java.util.Map;
 6 import java.util.regex.Matcher;
 7 import java.util.regex.Pattern;
 8 
 9 /** 对象转Map工具类
10  * <b>
11  * </b><br><br><i>Description</i> :
12  * <br><br>Date: 2019/6/18 ${time}    <br>Author : dxl
13  */
14 public class ObjToMap {
15     private static= LinePattern of Pattern.compile the Pattern ( "_ (\\ W)" );
 16      Private  static the Pattern of Pattern.compile humpPattern = ( "[AZ]" );
 . 17  
18 is      / ** 
. 19       * <B> object-Map
 20 is       * @ date 2019 Nian 7 afternoon of May 1 <br> 2:53:33 <i> author </ i>: DXL
 21       * / 
22      public  static the Map <String, Object> modelToMap (Object obj) {
 23  
24-          the Map <String, Object> = Map new new the HashMap <String, Object> ();
 25          // all domain Gets f corresponding to an object class 
26 is          field, [] = fields obj.getClass ().
getDeclaredFields();
27          for ( int I = 0, len = fields.length; I <len; I ++ ) {
 28              String varName = Fields [I] .getName ();
 29              IF ( "serialVersionUID" .equals (varName)) {
 30                  Continue ;
 31              }
 32              // the key set lower case, the default property of the object 
33 is               varName = humpToLine (varName) .toString ();
 34 is              the try {
 35                  // Get original access control privileges 
36                  Boolean accessFlag = Fields [I] .isAccessible ();
 37                  // modify the access control privileges 
38                 fields [i] .setAccessible ( to true );
 39                  // Get the object f in the attribute fields [i] corresponding to the variable object 
40                  Object O = fields [i] .get (obj);
 41 is                  IF (= O! null ) {
 42 is                      map.put (varName, O);}
 43 is                  // System.out.println ( "incoming object contains a variable as follows:" + varName + "=" + O);
 44 is                  // restore access control permissions 
45                  Fields [I] .setAccessible (accessFlag);
 46 is              } the catch (an IllegalArgumentException EX) {
 47                  ex.printStackTrace ();
 48              }catch (IllegalAccessException ex) {
49                 ex.printStackTrace();
50             }
51         }
52         return map;
53     }
54     /** 下划线转驼峰 */
55     public static String lineToHump(String str) {
56         str = str.toLowerCase();
57         Matcher matcher = linePattern.matcher(str);
58         StringBuffer sb = new StringBuffer();
59         while (matcher.find()) {
60             matcher.appendReplacement (SB, Matcher.group (. 1 ) .toUpperCase ());
 61 is          }
 62 is          matcher.appendTail (SB);
 63 is          return sb.toString ();
 64      }
 65      / ** hump underlined turn, higher efficiency than the above * / 
66      public  static String humpToLine (String STR) {
 67          Matcher Matcher = humpPattern.matcher (STR);
 68          the StringBuffer SB = new new the StringBuffer ();
 69          the while (matcher.find ()) {
 70              matcher.appendReplacement (SB, " _ "+ Matcher.group (0 ) .toLowerCase ());
71         }
72         matcher.appendTail(sb);
73         return sb.toString();
74     }
75 }
View Code

 

Guess you like

Origin www.cnblogs.com/xiatc/p/12362855.html