Beanmap这个Map类用于把一个javaBean转换为Map,在其中存储了javaBean的各个属性的setXXX方法和getXXX方法,属性的类型。
public class Beanmap extends AbstractMap implements Cloneable{ private transient Object bean;//javaBean对象 private transient HashMap readMethods = new HashMap();//getXXX方法集 private transient HashMap writeMethods = new HashMap();//setXXX方法集 private transient HashMap types = new HashMap();//成员变量类型集 public static final Object[] NULL_ARGUMENTS = {};//空参数集,用于通过reflection调用getXXX方法 public static HashMap defaultTransformers = new HashMap();//把基本类型映射为transformer类型,后者用于将字符串转换为合 适的基本型的包装类 //默认transformer static { defaultTransformers.put( Boolean.TYPE, new Transformer() { public Object transform( Object input ) { return Boolean.valueOf( input.toString() ); } } ); defaultTransformers.put( Character.TYPE, new Transformer() { public Object transform( Object input ) { return new Character( input.toString().charAt( 0 ) ); } } ); defaultTransformers.put( Byte.TYPE, new Transformer() { public Object transform( Object input ) { return Byte.valueOf( input.toString() ); } } ); defaultTransformers.put( Short.TYPE, new Transformer() { public Object transform( Object input ) { return Short.valueOf( input.toString() ); } } ); defaultTransformers.put( Integer.TYPE, new Transformer() { public Object transform( Object input ) { return Integer.valueOf( input.toString() ); } } ); defaultTransformers.put( Long.TYPE, new Transformer() { public Object transform( Object input ) { return Long.valueOf( input.toString() ); } } ); defaultTransformers.put( Float.TYPE, new Transformer() { public Object transform( Object input ) { return Float.valueOf( input.toString() ); } } ); defaultTransformers.put( Double.TYPE, new Transformer() { public Object transform( Object input ) { return Double.valueOf( input.toString() ); } } ); } public Beanmap(Object bean) { this.bean = bean; initialise(); } public Object clone() throws CloneNotSupportedException { Beanmap newMap = (Beanmap)super.clone(); if(bean == null) {//若底层bean不存在,则返回一个复制的空Beanmap, return newMap; } Object newBean = null; Class beanClass = null; try { beanClass = bean.getClass();//底层bean的Class newBean = beanClass.newInstance();//实例化一个新的bean } catch (Exception e) { // unable to instantiate throw new CloneNotSupportedException ("Unable to instantiate the underlying bean /"" + beanClass.getName() + "/": " + e); } try { newMap.setBean(newBean); } catch (Exception exception) { throw new CloneNotSupportedException ("Unable to set bean in the cloned bean map: " + exception); } try { //复制所有可读写的属性 IteraTor readableKeys = readMethods.keySet().iteraTor(); while(readableKeys.hasNext()) { Object key = readableKeys.next();//属性名称 if(getWriteMethod(key) != null) { newMap.put(key, get(key));//放入到新Beanmap中 } } } catch (Exception exception) { throw new CloneNotSupportedException ("Unable to copy bean values to cloned bean map: " + exception); } return newMap; } public void clear() { if(bean == null) return; Class beanClass = null; try { beanClass = bean.getClass(); bean = beanClass.newInstance();//重新实例化,一切都回到默认状态 } catch (Exception e) { throw new UnsupportedOperationException( "Could not create new instance of class: " + beanClass ); } } public Object get(Object name) {//获取指定名称属性的值 if ( bean != null ) { Method method = getReadMethod( name ); if ( method != null ) { try { return method.invoke( bean, NULL_ARGUMENTS ); } catch ( IllegalAccessException e ) { logWarn( e ); } catch ( IllegalArgumentException e ) { logWarn( e ); } catch ( InvocationTargetException e ) { logWarn( e ); } catch ( NullPointerException e ) { logWarn( e ); } } } return null; } public Object put(Object name, Object value) throws IllegalArgumentException, ClassCastException {//设置指定名称的属性的值 if ( bean != null ) { Object ldValue = get( name );//原来的值 Method method = getWriteMethod( name ); if ( method == null ) { throw new IllegalArgumentException( "The bean of type: "+ bean.getClass().getName() + " has no property called: " + name ); } try { Object[] arguments = createWriteMethodArguments( method, value );//转换参数 method.invoke( bean, arguments );//设置新值 Object newValue = get( name );//获取新设置的值 firePropertyChange( name, oldValue, newValue );//fire属性值改变事件 } catch ( InvocationTargetException e ) { logInfo( e ); throw new IllegalArgumentException( e.getMessage() ); } catch ( IllegalAccessException e ) { logInfo( e ); throw new IllegalArgumentException( e.getMessage() ); } return oldValue; } return null; } public Method getReadMethod(String name) {//获取指定名称属性的getXXX方法 return (Method) readMethods.get(name); } public Method getWriteMethod(String name) {//获取指定名称属性的setXXX方法 return (Method) writeMethods.get(name); } private void initialise() { if(getBean() == null) return; Class beanClass = getBean().getClass();//bean的Class try { //BeanInfo beanInfo = IntrospecTor.getBeanInfo( bean, null ); BeanInfo beanInfo = IntrospecTor.getBeanInfo( beanClass );//bean的信息 PropertyDescripTor[] propertyDescripTors = beanInfo.getPropertyDescripTors(); if ( propertyDescripTors != null ) { for ( int i = 0; i 0 ) { Class paramType = types[0]; if ( ! paramType.isAssignableFrom( value.getClass() ) ) { value = convertType( paramType, value );//把新参数转换为setXXX方法的参数类型 } } } Object[] answer = { value }; return answer; } catch ( InvocationTargetException e ) { logInfo( e ); throw new IllegalArgumentException( e.getMessage() ); } catch ( InstantiationException e ) { logInfo( e ); throw new IllegalArgumentException( e.getMessage() ); } } protected Object convertType( Class newType, Object value ) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { // try call construcTor Class[] types = { value.getClass() }; try {//尝试用带一个参数的构造函数进行转换 ConstrucTor construcTor = newType.getConstrucTor( types ); Object[] arguments = { value }; return construcTor.newInstance( arguments ); } catch ( NoSuchMethodException e ) { // try using the transformers Transformer transformer = getTypeTransformer( newType );//获取可用的transformer if ( transformer != null ) { return transformer.transform( value );//转换类型 } return value; } } protected Transformer getTypeTransformer( Class aType ) { return (Transformer) defaultTransformers.get( aType ); }}
不论你在什么时候结束,重要的是结束之后就不要悔恨