zoukankan      html  css  js  c++  java
  • Introspector(内省)简单演示样例 与 简单应用


    简单演示样例:

    package com.asdfLeftHand.test;
    
    import java.beans.BeanDescriptor;
    import java.beans.BeanInfo;
    import java.beans.IntrospectionException;
    import java.beans.Introspector;
    import java.beans.MethodDescriptor;
    import java.beans.PropertyDescriptor;
    import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;
    
    class Person {
    
    	private String name;
    	private int age;
    
    	public int getAge() {
    		return age;
    	}
    
    	public void setAge(int age) {
    		this.age = age;
    	}
    
    	public String getName() {
    		return name;
    	}
    
    	public void setName(String name) {
    		this.name = name;
    	}
    
    }
    
    public class IntrospectorTest {
    
    	/**
    	 * @param args
    	 * @throws IntrospectionException
    	 * @throws InvocationTargetException 
    	 * @throws IllegalArgumentException 
    	 * @throws IllegalAccessException 
    	 */
    	public static void main(String[] args) throws IntrospectionException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
    		// 
    		Person person = new Person();
    		person.setAge(22);
    		person.setName("小强");
    		
    		BeanInfo beanInfo = Introspector.getBeanInfo(person.getClass());
    		//BeanInfo beanInfo = Introspector.getBeanInfo(Person.class);
    
    		System.out.println("--------BeanDescriptor--------");
    		BeanDescriptor beanDesc = beanInfo.getBeanDescriptor();
    		Class cls = beanDesc.getBeanClass();
    		System.out.println(cls.getName());
    
    		System.out.println("--------MethodDescriptor-------");
    		MethodDescriptor[] methodDescs = beanInfo.getMethodDescriptors();
    		for (int i = 0; i < methodDescs.length; i++) {
    			Method method = methodDescs[i].getMethod();
    			System.out.println(method.getName());
    		}
    		
    		System.out.println("--------PropertyDescriptor------");
    		
    		PropertyDescriptor[] propDescs = beanInfo.getPropertyDescriptors();
    		for(int i = 0; i < propDescs.length; i++) {
    			Method methodR = propDescs[i].getReadMethod();
    			if (methodR != null) {
    				System.out.println("读方法:"+methodR.getName());
    				Object o= methodR.invoke(person);
    				System.out.println(methodR.getName()+":"+o);
    			}
    			Method methodW = propDescs[i].getWriteMethod();
    			if (methodW != null) {
    				System.out.println("写方法:"+methodW.getName());
    				if(methodW.getName().equals("setName")){
    					methodW.invoke(person,"小王");
    					System.out.println("调用"+methodW.getName()+"方法后的值为:"+person.getName());//此处为了方便就直接用person.getName()方法了
    				}
    			}
    		}
    	}
    
    }
    
    

    执行结果:

    --------BeanDescriptor--------
    com.asdfLeftHand.test.Person
    --------MethodDescriptor-------
    hashCode
    wait
    setAge
    notifyAll
    equals
    wait
    wait
    toString
    setName
    getAge
    notify
    getClass
    getName
    --------PropertyDescriptor------
    读方法:getAge
    getAge:22
    写方法:setAge
    读方法:getClass
    getClass:class com.asdfLeftHand.test.Person
    读方法:getName
    getName:小强
    写方法:setName
    调用setName方法后的值为:小王


    一个简单应用:利用内省简化一系列类似的方法为一个通用的方法。

    部分代码:

    用到common BeanUtils包。

    /**
    	 * 有一张图片Image表,存有何种对象相应的图像(如 用户头像),用hql语句查处相应的图片集合,
    	 * 各种对象字段有差别可是查询方法相似,就写一个通用的方法(相对通用)
    	 * 得到某个对象集合的图片。
    	 * key:guid+id,value:address+fileName
    	 * @param list
    	 * @param type 
    	 * @return
    	 */
    	public  Map<String,String> getImagesMap(List<?> list,int imageType) {
    		Map<String, String> imagesMap = new HashMap<String, String>();
    		List<?> entityList = list;
    		for(int i=0;i<entityList.size();i++){ 
    			Object entity = entityList.get(i);
    			String id = "";
    			String guid = "";
    			try {
    				BeanInfo bi = Introspector.getBeanInfo(entity.getClass(), Object.class);
    				PropertyDescriptor[] props = bi.getPropertyDescriptors();
    		        for (int i2 = 0; i2 < props.length; i2++) {
    		        	String str = props[i2].getName();
    		        	if(str.equals("guid")){
    		        		guid = BeanUtils.getProperty(entity, str);
    		        	}else if(str.endsWith("ID")){
    		        		id = BeanUtils.getProperty(entity, str);
    		        	}
    		        }
    			} catch (Exception e) {
    				e.printStackTrace();
    			}
    	        String hql2 = "from Image where guid='"+guid+"' and FKID='"+id+"' and type="+imageType";
    			List<Image> list2 = imageDao.query(hql2);
    			//,,,
    		}
    		return imagesMap;
    		
    	}

    这样全部须要图片的地方就仅仅须要调用这一个方法了。

  • 相关阅读:
    【API知识】类型转换工具ConvertUtils引发的思考
    【API知识】MongoTemplate非映射方式操作Document
    python+scrapy分析博客园首页4000篇优质博客(图解)
    px和dp(内含大量的像素单位详解)
    【惊!】代码中出现“//保重,兄弟!”
    用故事解析setTimeout和setInterval(内含js单线程和任务队列)
    css_transition_animation(内含贝赛尔曲线详解)
    你绝对不知道的head标签
    css3的@media
    安利一个免费下载VIP文档神器
  • 原文地址:https://www.cnblogs.com/bhlsheji/p/4044293.html
Copyright © 2011-2022 走看看