1 package jfun.yan.etc; 2 3 import java.beans.IntrospectionException ; 4 import java.lang.reflect.Method ; 5 import java.util.ArrayList ; 6 import java.util.HashMap ; 7 import java.util.Iterator ; 8 import java.util.Set ; 9 10 import jfun.util.beans.BeanType; 11 12 22 public class TypeFilteredPropertyPredicate implements PropertyPredicate { 23 private final HashMap filter_types = new HashMap (); 24 public boolean isProperty(Class type, Object key, Class property_type) { 25 final Object t = filter_types.get(key); 26 if(t==null) return false; 27 if(t instanceof Class ){ 28 return ((Class )t).isAssignableFrom(type); 29 } 30 else{ 31 final ArrayList types = (ArrayList )t; 32 final int sz = types.size(); 33 for(int i=0; i<sz; i++){ 34 final Class filter_type = (Class )types.get(i); 35 if(filter_type.isAssignableFrom(type)){ 36 return true; 37 } 38 } 39 return false; 40 } 41 } 42 47 public TypeFilteredPropertyPredicate addType(Class type){ 48 try{ 49 final BeanType btype = BeanType.instance(type); 50 final Set names = btype.getPropertyNames(); 51 for(Iterator it=names.iterator();it.hasNext();){ 52 final String key = (String )it.next(); 53 final Method writer = btype.getWriter(key); 54 if(writer!=null){ 55 addProperty(writer.getDeclaringClass(), key); 56 } 57 } 58 } 59 catch(IntrospectionException e){} 60 return this; 61 } 62 68 public TypeFilteredPropertyPredicate addProperty(Class type, String key){ 69 final Object slot = filter_types.get(key); 70 if(slot==null){ 71 filter_types.put(key, type); 72 } 73 else{ 74 final ArrayList list = toArrayList(slot); 75 list.add(type); 76 filter_types.put(key, list); 77 } 78 return this; 79 } 80 private static ArrayList toArrayList(Object slot){ 81 if(slot instanceof Class ){ 82 final ArrayList list = new ArrayList (4); 83 list.add(slot); 84 return list; 85 } 86 else{ 87 return (ArrayList )slot; 88 } 89 } 90 } 91 | Popular Tags |