1 8 9 15 package jfun.yan.etc; 16 17 import java.beans.IntrospectionException ; 18 import java.beans.Introspector ; 19 import java.beans.PropertyDescriptor ; 20 import java.util.HashMap ; 21 import jfun.yan.Component; 22 import jfun.yan.Components; 23 import jfun.yan.Creator; 24 import jfun.yan.PropertyBinder; 25 26 39 public class Beans { 40 private static PropertyDescriptor [] getBeanProperties(Class c){ 41 try{ 42 return Introspector.getBeanInfo(c).getPropertyDescriptors(); 43 } 44 catch(IntrospectionException e){ 45 throw new IllegalArgumentException (e.getMessage()); 46 } 47 } 48 57 public static Component resolveBy(final Component cc, final Class type, 58 final FromProperty pn){ 59 final PropertyDescriptor [] props = getBeanProperties(type); 60 final java.util.HashMap hmap = new java.util.HashMap (); 62 for(int i=0; i<props.length; i++){ 63 final PropertyDescriptor prop = props[i]; 64 if(prop.getWriteMethod() == null) continue; 65 final String name = prop.getName(); 66 hmap.put(name, Components.useKey(pn.fromProperty(type, prop))); 67 69 } 71 return cc.withProperties(hmap); 72 } 74 82 public static Component resolveBy(final Component cc, final FromProperty pn){ 83 final Class type = cc.getType(); 84 if(type==null || !cc.isConcrete()){ 85 return cc.bind(new jfun.yan.Binder(){ 86 public Creator bind(Object obj) 87 throws Exception { 88 if(obj==null) return Components.value(null); 89 else return resolveBy(Components.value(obj), obj.getClass(), pn); 90 } 91 }); 92 } 93 else{ 94 return resolveBy(cc, type, pn); 95 } 96 } 97 104 public static Component byName(final Component cc){ 105 110 return cc.bindProperties(new jfun.yan.PropertyBinder(){ 113 public Creator bind(Class component_type, Object key, Class type){ 114 return Components.useKey(key); 115 } 116 }); 117 } 118 124 public static Component byType(final Component cc){ 125 return cc.bindProperties(new jfun.yan.PropertyBinder(){ 126 public Creator bind(Class component_type, Object key, Class type){ 127 return Components.useType(type); 128 } 129 }); 130 } 131 138 public static Component byDisplayName(final Component cc){ 139 return resolveBy(cc, new FromProperty(){ 140 public Object fromProperty(Class c, PropertyDescriptor prop){ 141 return prop.getDisplayName(); 142 } 143 }); 144 } 145 153 public static Component byQualifiedName(final Component cc){ 154 return resolveBy(cc, new FromProperty(){ 155 public Object fromProperty(Class c, PropertyDescriptor prop){ 156 return jfun.util.Misc.getTypeName(c)+'.'+prop.getName(); 157 } 158 }); 159 } 160 161 181 public static Component beanComponent(final Component cc, 182 final String [] mandatory_params) 183 throws IntrospectionException { 184 if(mandatory_params==null || mandatory_params.length==0) 185 return cc.bean().optionalProperties(); 186 final HashMap params = new HashMap (); 187 for(int i=0;i<mandatory_params.length;i++){ 188 final String name = mandatory_params[i]; 189 params.put(name, name); 190 } 191 return cc.fromProperties(mandatory_params).bean() 192 .bindProperties(new PropertyBinder(){ 193 public Creator bind(Class component_type, Object key, Class type){ 194 final Component p = Components.useProperty(component_type, key, type); 195 if(params.containsKey(key)){ 196 return p; 197 } 198 else return p.optional(); 199 } 200 }); 201 } 202 203 204 } 205 | Popular Tags |