1 package jfun.yan; 2 3 import java.beans.IntrospectionException ; 4 import java.lang.reflect.Method ; 5 import java.util.HashSet ; 6 import java.util.Iterator ; 7 import java.util.Set ; 8 9 import jfun.util.beans.BeanType; 10 import jfun.yan.util.MemberPredicate; 11 import jfun.yan.util.Utils; 12 13 20 public class FilteredPropertiesInjector implements PropertiesInjector { 21 22 private final BeanType btype; 23 private final MemberPredicate filter; 24 private final Set propnames; 25 26 34 public static PropertiesInjector instance(Class type, 35 MemberPredicate pred) 36 throws IntrospectionException { 37 final BeanType bt = Utils.toBeanType(type); 38 return new FilteredPropertiesInjector(bt, pred); 39 } 40 45 public FilteredPropertiesInjector(final BeanType btype, 46 final MemberPredicate pred){ 47 this.btype = btype; 48 this.filter = pred; 49 this.propnames = btype==null?null:getPropertyNames(btype, pred); 50 } 51 private BeanType obtainBeanType(Class type){ 52 if(btype!=null) return btype; 56 else{ 57 return jfun.yan.util.Utils.toBeanType(type); 58 } 59 } 60 public void injectProperties(Object obj, Dependency dep){ 61 if(obj==null){ 62 throw new NullBeanObjectException("null component"); 63 } 64 final Class objtype = obj.getClass(); 66 final BeanType btype = obtainBeanType(objtype); 67 final Set props = getPropertyNames(btype); 68 Utils.injectProperties(btype, obj, props, dep); 69 } 70 public void verifyProperties(Class type, Dependency dep){ 71 final BeanType btype = obtainBeanType(type); 74 final Set props = getPropertyNames(btype); 75 Utils.verifyProperties(btype, props, dep); 76 } 77 public String toString(){ 78 return "bean"; 79 } 80 private Set getPropertyNames(BeanType bt){ 81 return propnames==null?getPropertyNames(bt, this.filter):propnames; 82 } 83 private static Set getPropertyNames(BeanType bt, MemberPredicate pred){ 84 final HashSet props = new HashSet (); 85 for(Iterator it = bt.getPropertyNames().iterator(); it.hasNext();){ 86 final String name = (String )it.next(); 87 Method mtd = bt.getWriter(name); 88 if(mtd==null) 89 mtd = bt.getIndexedWriter(name); 90 if(mtd!=null){ 91 if(pred.isMember(name, mtd)){ 92 props.add(name); 93 } 94 } 95 } 96 return props; 97 } 98 } 99 | Popular Tags |