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 }

 

java反射机制,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。