zoukankan      html  css  js  c++  java
  • java反射机制

    1.获取java entity里的属性有俩个方法:getDeclaredField()和getField();

      getField() 只能获取public类型的属性;

      getDeclaredField() 能获取类或接口声明的所有属性;

     如:

    import java.lang.reflect.*;  
    
    public class HellWorld{ 
       private double a; 
       public static final int b = 16; 
       String str = "helloword";  
    
       public static void main(String args[]) { 
          try { 
               Class cls = Class.forName("HellWorld"); 
               Field fieldlist[] = cls.getDeclaredFields(); 
               for (int i = 0; i < fieldlist.length; i++) { 
                  Field fld = fieldlist[i]; 
                  System.out.println("name = " + fld.getName());  //获取属性名
                  System.out.println("decl class = " + fld.getDeclaringClass()); //所属的声明类
                  System.out.println("type = " + fld.getType());  //获取属性类型
                  int mod = fld.getModifiers();  
                  System.out.println("modifiers = " + Modifier.toString(mod));  //属性成员的修饰语
                  System.out.println("///////////////"); 
               } 
          } 
          catch (Throwable e) { 
               System.err.println(e); 
          } 
       } 
    }
    

     输出:

     1     name = a
     2     decl class = class HellWorld
     3     type = double
     4     modifiers = private
     5     /////////////////
     6     name = b
     7     decl class = class HellWorld
     8     type = int
     9     modifiers = public static final
    10     /////////////////
    11     name = str
    12     decl class = class HellWorld
    13     type = class java.lang.String
    14     modifiers = 
    15     /////////////////

    使用场景:可以根据页面post/get过来的参数值,初始化java entity里的对应属性的值。

    如:POST  a=2&str=test

     1 public void init(HttpServletRequest request){
     2         try {
     3             String queryBody = IOUtils.toString(request.getInputStream());
     4             
     5             Class cls = Class.forName("HelloWorld");
     6             
     7             if(StringUtils.isNotBlank(queryBody)){
     8                 StringTokenizer st = new StringTokenizer(queryBody, "&");
     9                 
    10                 while (st.hasMoreTokens()) {
    11                     String pairs = st.nextToken();
    12                     String key = pairs.substring(0, pairs.indexOf('='));
    13                     String value = pairs.substring(pairs.indexOf('=') + 1);
    14         
    15                     if(StringUtils.isBlank(value))
    16                         continue;
    17                     
    18                     value = URLDecoder.decode(value, "UTF-8");
    19                     
    20                     Field fld = cls.getDeclaredField(key);
    21                     Class type = fld.getType();
    22                     if(type.toString().equalsIgnoreCase("double")){
    23                         fld.setDouble(this, Double.parseDouble(value));
    24                     }else{
    25                         fld.set(this, value);
    26                     }
    27                 }
    28             }
    29             
    30         } catch(UnsupportedEncodingException e){
    31 } catch (IOException e) {
    32 } catch (SecurityException e) {
    33 } catch (NoSuchFieldException e) {
    34 } catch (ClassNotFoundException e) {
    35 } catch (IllegalArgumentException e) {
    36 } catch (IllegalAccessException e) {
    37 } 38 }
  • 相关阅读:
    「CF722E Research Rover」
    「Luogu P4062 [Code+#1]Yazid 的新生舞会」
    【cf比赛记录】Codeforces Round #613 (Div. 2)
    【cf比赛记录】Educational Codeforces Round 78 (Rated for Div. 2)
    【cf补题记录】Codeforces Round #608 (Div. 2)
    【cf补题记录】Codeforces Round #607 (Div. 2)
    【cf比赛记录】Codeforces Round #606 (Div. 2, based on Technocup 2020 Elimination Round 4)
    【cf比赛记录】Codeforces Round #605 (Div. 3)
    【Codeforces】B. Div Times Mod
    【牛客练习赛53】A-超越学姐爱字符串
  • 原文地址:https://www.cnblogs.com/mihu/p/3799500.html
Copyright © 2011-2022 走看看