KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jfun > yan > FilteredPropertiesInjector


1 package jfun.yan;
2
3 import java.beans.IntrospectionException JavaDoc;
4 import java.lang.reflect.Method JavaDoc;
5 import java.util.HashSet JavaDoc;
6 import java.util.Iterator JavaDoc;
7 import java.util.Set JavaDoc;
8
9 import jfun.util.beans.BeanType;
10 import jfun.yan.util.MemberPredicate;
11 import jfun.yan.util.Utils;
12
13 /**
14  * This implementation only attempts to inject a property when this property
15  * satisfies a MemberPredicate object.
16  * <p>
17  * @author Ben Yu
18  * Dec 31, 2005 12:51:53 PM
19  */

20 public class FilteredPropertiesInjector implements PropertiesInjector {
21   /*The meta information of the bean. null if unknown*/
22   private final BeanType btype;
23   private final MemberPredicate filter;
24   private final Set JavaDoc propnames;
25   
26   /**
27    * Create an instance of FilteredPropertiesInjector.
28    * If type is null, dynamic binding will be used.
29    * @param type the bean class.
30    * @param pred the MemberPredicate object.
31    * @return the instance.
32    * @throws IntrospectionException
33    */

34   public static PropertiesInjector instance(Class JavaDoc type,
35       MemberPredicate pred)
36   throws IntrospectionException JavaDoc{
37     final BeanType bt = Utils.toBeanType(type);
38     return new FilteredPropertiesInjector(bt, pred);
39   }
40   /**
41    * Create an instance of FilteredPropertiesInjector.
42    * @param btype the bean type. If null, dynamic binding is used.
43    * @param pred the MemberPredicate object to filter the properties.
44    */

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 JavaDoc type){
52     //dynamically get the bean type.
53
//if the bean type is not early bound,
54
//use the object type to dynamically create it.
55
if(btype!=null) return btype;
56     else{
57       return jfun.yan.util.Utils.toBeanType(type);
58     }
59   }
60   public void injectProperties(Object JavaDoc obj, Dependency dep){
61     if(obj==null){
62       throw new NullBeanObjectException("null component");
63     }
64     //dynamically resolve bean type and properties if necessary.
65
final Class JavaDoc objtype = obj.getClass();
66     final BeanType btype = obtainBeanType(objtype);
67     final Set JavaDoc props = getPropertyNames(btype);
68     Utils.injectProperties(btype, obj, props, dep);
69    }
70   public void verifyProperties(Class JavaDoc type, Dependency dep){
71     //dynamically resolve bean type and properties if necessary.
72
//the type cannot be null now.
73
final BeanType btype = obtainBeanType(type);
74     final Set JavaDoc props = getPropertyNames(btype);
75     Utils.verifyProperties(btype, props, dep);
76   }
77   public String JavaDoc toString(){
78     return "bean";
79   }
80   private Set JavaDoc getPropertyNames(BeanType bt){
81     return propnames==null?getPropertyNames(bt, this.filter):propnames;
82   }
83   private static Set JavaDoc getPropertyNames(BeanType bt, MemberPredicate pred){
84     final HashSet JavaDoc props = new HashSet JavaDoc();
85     for(Iterator JavaDoc it = bt.getPropertyNames().iterator(); it.hasNext();){
86       final String JavaDoc name = (String JavaDoc)it.next();
87       Method JavaDoc 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