zoukankan      html  css  js  c++  java
  • 内省Introspector(反射操作javaBean)

    一:内省是一种特殊的反射,来更方便的操作javaBean对象,通过内省可以获取到类字节码的描述器,

    然后解剖每一个字段,获取每个字段的读写方法,即get/set方法的反射,然后获取或者是封装bean的value

    下面是通过内省向Bean中set值得示例:

    public static <T> T toBean(T t){
    		Class<?> clazz = t.getClass();
    		try {
    			//根据Class对象获取该类的BeanInfo信息
    			BeanInfo beanInfo = Introspector.getBeanInfo(clazz);
    			//根据BeanInfo获取该类中每一个字段的描述器
    			PropertyDescriptor[] props = beanInfo.getPropertyDescriptors();
    			//遍历描述器,获取每个字段的名称以及写方法(set方法),然后将value值set入bean中
    			for(PropertyDescriptor property:props){
    				String field = property.getName();
    				if(EXCLUDE_FIELD.equals(field)){
    					continue;
    				}
    				Method method = property.getWriteMethod();
    				method.invoke(t,getCode(field));
    			}
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    		return t;
    	}
    

    整个测试案例如下:

     1 /**
     2  * 
     3  */
     4 package com.hlcui.test;
     5 
     6 import java.beans.BeanInfo;
     7 import java.beans.Introspector;
     8 import java.beans.PropertyDescriptor;
     9 import java.lang.reflect.Method;
    10 import java.util.HashMap;
    11 import java.util.Map;
    12 
    13 import com.hlcui.entity.Person;
    14 
    15 /**
    16  * @author Administrator
    17  *
    18  */
    19 public class TestCase {
    20     
    21     public static Map<String,Object> codeMap = new HashMap<String,Object>();
    22     
    23     private static final String EXCLUDE_FIELD = "class";
    24     
    25     //静态代码块加载静态资源
    26     static{
    27         codeMap.put("id",1);
    28         codeMap.put("name", "jack");
    29         codeMap.put("salary",14000);
    30     }
    31     
    32     public static void main(String[] args){
    33         Person person = toBean(new Person());
    34         System.out.println(person);
    35     }
    36     
    37     public static <T> T toBean(T t){
    38         Class<?> clazz = t.getClass();
    39         try {
    40             //根据Class对象获取该类的BeanInfo信息
    41             BeanInfo beanInfo = Introspector.getBeanInfo(clazz);
    42             //根据BeanInfo获取该类中每一个字段的描述器
    43             PropertyDescriptor[] props = beanInfo.getPropertyDescriptors();
    44             //遍历描述器,获取每个字段的名称以及写方法(set方法),然后将value值set入bean中
    45             for(PropertyDescriptor property:props){
    46                 String field = property.getName();
    47                 if(EXCLUDE_FIELD.equals(field)){
    48                     continue;
    49                 }
    50                 Method method = property.getWriteMethod();
    51                 method.invoke(t,getCode(field));
    52             }
    53         } catch (Exception e) {
    54             e.printStackTrace();
    55         }
    56         return t;
    57     }
    58     
    59     public static Object getCode(String field){
    60         return codeMap.get(field);
    61     }
    62 }
  • 相关阅读:
    (参考)爬虫5-爬取中国大学排名情况
    005_软件安装之_常用办公软件
    004_软件安装之_Altium Designer
    001_基础硬件电路_二极管
    添加QQ群
    024_STM32程序移植之_ESP8266_TCP
    020_C语言常用函数
    004——转载C#禁止改变窗体大小
    003转载----C#打开网页
    002_Python基础学习网站
  • 原文地址:https://www.cnblogs.com/warrior4236/p/6947357.html
Copyright © 2011-2022 走看看