zoukankan      html  css  js  c++  java
  • java的内省(introspector)

    package com.wzh.test.introspector;
    
    import java.beans.BeanInfo;
    import java.beans.IntrospectionException;
    import java.beans.Introspector;
    import java.beans.PropertyDescriptor;
    import java.lang.reflect.Method;
    
    import org.junit.Test;
    
    //使用内省API操作Bean的属性
    public class Demo1 {
    
    	@Test
    	public void test() throws Exception{
    //		BeanInfo info=Introspector.getBeanInfo(Person.class);
    //		BeanInfo info=Introspector.getBeanInfo(Class.forName("com.wzh.test.introspector.Person"));
    		BeanInfo info=Introspector.getBeanInfo(Person.class,Object.class);
    		PropertyDescriptor[] pds=info.getPropertyDescriptors();
    		for(PropertyDescriptor pd : pds){
    			System.out.println(pd.getName());
    		}
    	}
    	
    	@Test
    	public void test2() throws Exception{
    		Person p=new Person();
    		PropertyDescriptor pd=new PropertyDescriptor("age", Person.class);
    		Method m=pd.getWriteMethod();
    		m.invoke(p,45);
    		
    		System.out.println("getAge:"+p.getAge());
    	}
    	
    	//获取当前操作的属性的类型
    	@Test
    	public void test3() throws IntrospectionException{
    		Person p=new Person();
    		PropertyDescriptor pd=new PropertyDescriptor("age", Person.class);
    		System.out.println(pd.getPropertyType());
    	}
    }
    

      

  • 相关阅读:
    hdu 4334 Trouble
    hdu 4324 Triangle LOVE
    hdu 4255 A Famous Grid
    hdu 3549 Flow Problem
    hdu 3371 Connect the Cities
    hdu 2846 Repository
    hdu 2120 Ice_cream's world I
    P3304 [SDOI2013]直径(【模板】树直径的必经边)
    P5490 【模板】扫描线
    P1364 医院设置(【模板】树的重心)
  • 原文地址:https://www.cnblogs.com/zhuawang/p/3378056.html
Copyright © 2011-2022 走看看