1 package jfun.yan; 2 3 import java.beans.IndexedPropertyDescriptor ; 4 import java.beans.IntrospectionException ; 5 import java.util.HashSet ; 6 import java.util.Iterator ; 7 import java.util.Set ; 8 9 import jfun.util.beans.BeanType; 10 11 18 public class DefaultPropertiesInjector implements PropertiesInjector { 19 20 private final BeanType btype; 21 22 23 private final java.util.Set props; 24 25 32 public static PropertiesInjector instance(Class type) 33 throws IntrospectionException { 34 final BeanType bt =jfun.yan.util.Utils.toBeanType(type); 35 return new DefaultPropertiesInjector(bt, null); 36 } 37 38 45 public static PropertiesInjector instance(final Class type, final java.util.Set props) 46 throws IntrospectionException { 47 final BeanType bt = jfun.yan.util.Utils.toBeanType(type); 48 checkIncludes(bt, props); 49 return new DefaultPropertiesInjector(bt, props); 50 } 51 56 public DefaultPropertiesInjector(final BeanType btype, 57 final Set props){ 58 this.btype = btype; 59 this.props = props; 60 } 61 private static void checkIncludes(BeanType btype, java.util.Set s2){ 63 if(btype==null) return; 65 for(Iterator it=s2.iterator(); it.hasNext();){ 66 final Object name = it.next(); 67 final java.beans.PropertyDescriptor desc = 68 btype.getPropertyDescriptor(""+name); 69 if(desc==null){ 70 throw new IllegalPropertyNameException(btype.getType(), ""+name); 71 } 72 else{ 73 if(desc.getWriteMethod()==null){ 74 if(desc instanceof IndexedPropertyDescriptor ){ 75 final IndexedPropertyDescriptor idesc = (IndexedPropertyDescriptor )desc; 76 if(idesc.getIndexedWriteMethod()!=null){ 77 return; 78 } 79 } 80 throw new NonWritablePropertyException(btype.getType(), ""+name); 81 } 82 } 83 } 84 } 85 private BeanType obtainBeanType(Class type){ 86 if(btype!=null) return btype; 90 else{ 91 return jfun.yan.util.Utils.toBeanType(type); 92 } 93 } 94 private Set getProperties(BeanType bt){ 95 if(props==null){ 99 final HashSet props = new HashSet (); 100 for(Iterator it=bt.getPropertyNames().iterator();it.hasNext();){ 101 final String name = (String )it.next(); 102 if(bt.getWriter(name)!=null||bt.getIndexedWriter(name)!=null){ 103 props.add(name); 104 } 105 } 106 return props; 107 } 108 else{ 109 if(this.btype==null){ 110 checkIncludes(bt, props); 112 } 113 return props; 114 } 115 } 116 public void injectProperties(Object obj, Dependency dep){ 117 if(props!=null && props.isEmpty()) return; 119 if(obj==null){ 120 throw new NullBeanObjectException("null component"); 121 } 122 final Class objtype = obj.getClass(); 124 final BeanType btype = obtainBeanType(objtype); 125 final Set props = getProperties(btype); 126 jfun.yan.util.Utils.injectProperties(btype, obj, props, dep); 127 } 128 129 public void verifyProperties(Class type, Dependency dep){ 130 final BeanType btype = obtainBeanType(type); 133 final Set props = getProperties(btype); 134 jfun.yan.util.Utils.verifyProperties(btype, props, dep); 135 } 136 public String toString(){ 137 return "bean"; 138 } 139 } 140 | Popular Tags |