1 package jfun.yan.util; 2 3 4 import java.beans.IntrospectionException ; 5 import java.io.FileNotFoundException ; 6 import java.io.IOException ; 7 import java.io.InputStream ; 8 import java.lang.reflect.AccessibleObject ; 9 import java.lang.reflect.InvocationTargetException ; 10 import java.lang.reflect.Method ; 11 import java.util.ArrayList ; 12 import java.util.Arrays ; 13 import java.util.Collection ; 14 import java.util.HashMap ; 15 import java.util.HashSet ; 16 import java.util.Hashtable ; 17 import java.util.Iterator ; 18 import java.util.LinkedList ; 19 import java.util.List ; 20 import java.util.Map ; 21 import java.util.Properties ; 22 import java.util.Set ; 23 import java.util.Vector ; 24 25 import jfun.util.ArrayAsList; 26 import jfun.util.Misc; 27 import jfun.util.beans.BeanType; 28 import jfun.yan.Binder; 29 import jfun.yan.Component; 30 import jfun.yan.ComponentInstantiationException; 31 import jfun.yan.Components; 32 import jfun.yan.DefaultingException; 33 import jfun.yan.Dependency; 34 import jfun.yan.Functions; 35 import jfun.yan.Monad; 36 import jfun.yan.PropertyEntry; 37 import jfun.yan.YanException; 38 import jfun.yan.util.resource.ResourceLoader; 39 40 41 47 public class Utils { 48 54 public static String toString(Class [] param_types){ 55 if(param_types==null) return "()"; 56 StringBuffer buf = new StringBuffer (); 57 buf.append('('); 58 for(int i=0; i<param_types.length; i++){ 59 buf.append(Misc.getTypeName(param_types[i])); 60 if(i<param_types.length-1){ 61 buf.append(", "); 62 } 63 } 64 buf.append(')'); 65 return buf.toString(); 66 } 67 72 static void forceAccess(AccessibleObject acc){ 73 try{ 74 acc.setAccessible(true); 75 } 76 catch(SecurityException e){} 77 } 78 85 public static boolean isCompatible(Class type, jfun.yan.Component c){ 86 final Class ctype = c.getType(); 87 if(ctype==null) 88 return true; 89 if(c.isConcrete()){ 90 return ReflectionUtil.isAssignableFrom(type,ctype); 91 } 92 else{ 93 return ReflectionUtil.isCompatible(type, ctype); 94 } 95 } 96 105 public static Set createSet(Class impltype, int capacity) 106 throws IllegalAccessException , InstantiationException { 107 if(impltype==null || HashSet .class.equals(impltype) 108 || Set .class.equals(impltype)){ 109 return new HashSet (capacity); 110 } 111 else{ 112 return (Set )createPreallocatedCollection(impltype); 113 } 114 } 115 124 public static Map createMap(Class impltype, int capacity) 125 throws IllegalAccessException , InstantiationException { 126 if(impltype==null || HashMap .class.equals(impltype) 127 || Map .class.equals(impltype)){ 128 return new HashMap (capacity); 129 } 130 else if(Hashtable .class.equals(impltype)){ 131 return new Hashtable (capacity); 132 } 133 else{ 134 return (Map )createPreallocatedCollection(impltype); 135 } 136 } 137 146 public static List createList(Class impltype, int capacity) 147 throws IllegalAccessException , InstantiationException { 148 if(impltype==null || ArrayList .class.equals(impltype) 149 || List .class.equals(impltype) || Collection .class.equals(impltype)){ 150 return new ArrayList (capacity); 151 } 152 else if(LinkedList .class.equals(impltype)){ 153 return new LinkedList (); 154 } 155 else if(Vector .class.equals(impltype)){ 156 return new Vector (capacity); 157 } 158 return (List )Utils.createPreallocatedCollection(impltype); 159 } 160 170 public static Object createPreallocatedCollection(Class type) 171 throws IllegalAccessException , InstantiationException { 172 return type.newInstance(); 173 } 174 175 private static final class HelperMethods{ 176 static final Method binder_bind = 177 ReflectionUtil.getMethod(Binder.class, "bind", new Class []{Object .class}, 178 false); 179 } 180 187 public static Component asComponent(Binder binder){ 188 return Components.fun(Functions.method(binder, HelperMethods.binder_bind)) 189 .bind(Monad.instantiator()); 190 } 191 192 197 public static List asList(Object arr){ 198 if(arr instanceof Object []){ 199 return Arrays.asList((Object [])arr); 200 } 201 else{ 202 return new ArrayAsList(arr); 203 } 204 } 205 211 public static YanException wrapInstantiationException(Throwable e){ 212 if(e instanceof Error ) 213 throw (Error )e; 214 if(e instanceof YanException){ 215 final YanException ye = (YanException)e; 216 return ye; 217 } 218 if(e instanceof InvocationTargetException ){ 219 final Throwable cause = 220 ((InvocationTargetException )e).getTargetException(); 221 if(cause != e){ 222 return wrapInstantiationException(cause); 223 } 224 } 225 final YanException ye = new ComponentInstantiationException(e); 226 return ye; 227 } 228 235 public static void injectProperty(BeanType btype, Object obj, String name, 236 Dependency dep){ 237 final Class objtype = obj.getClass(); 238 final Class atype = btype.getPropertySetterType(name); 239 if(atype!=null){ 240 final Object v = dep.getProperty(objtype, name, atype); 241 try{ 242 btype.setProperty(obj, name, v); 243 } 244 catch(Throwable e){ 245 throw wrapInstantiationException(e); 246 } 247 } 248 } 249 254 public static BeanType toBeanType(Class type){ 255 if(type==null) return null; 256 try{ 257 return BeanType.instance(type); 258 } 259 catch(IntrospectionException e){ 260 throw new ComponentInstantiationException(e); 261 } 262 } 263 270 public static void injectProperties(BeanType btype, Object obj, 271 Set props, Dependency dep){ 272 for(Iterator it=props.iterator(); it.hasNext();){ 273 final String name = it.next().toString(); 274 try{ 275 jfun.yan.util.Utils.injectProperty(btype, obj, name, dep); 276 } 277 catch(DefaultingException e){ 278 continue; 280 } 281 catch(YanException e){ 282 e.push(new PropertyEntry(btype.getType(), name)); 283 throw e; 284 } 285 } 286 } 287 private static Class getArgumentType(BeanType btype, String name){ 288 return btype.getPropertySetterType(name); 289 } 290 296 public static void verifyProperties(BeanType btype, Set props, Dependency dep){ 297 for(Iterator it=props.iterator(); it.hasNext();){ 298 final String name = it.next().toString(); 299 final Class atype = getArgumentType(btype, name); 300 if(atype!=null){ 301 try{ 302 dep.verifyProperty(btype.getType(), name, atype); 303 } 304 catch(DefaultingException e){ 305 continue; 306 } 307 catch(YanException e){ 308 e.push(new PropertyEntry(btype.getType(), name)); 309 throw e; 310 } 311 } 312 } 313 314 } 315 321 public static java.util.HashSet toSet(Object [] keys, String item_name){ 322 final HashSet hset = new HashSet (); 323 for(int i=0; i<keys.length; i++){ 324 final Object name = keys[i]; 325 if(hset.contains(name)){ 326 throw new IllegalArgumentException ("duplicate " + item_name 327 +": " + name); 328 } 329 hset.add(name); 330 } 331 return hset; 332 } 333 341 public static Properties loadResourceProperties(ResourceLoader loader, String resource) 342 throws IOException { 343 final Properties props = new Properties (); 344 final InputStream in = loader.getResourceAsStream(resource); 345 if(in==null){ 346 throw new FileNotFoundException (resource); 347 } 348 try{ 349 props.load(in); 350 } 351 finally{ 352 try{ 353 in.close(); 354 } 355 catch(Exception e){} 356 } 357 return props; 358 } 359 365 public static String getObjTypeName(Object arg, String nullname){ 366 return (arg==null)?nullname:Misc.getTypeName(arg.getClass()); 367 } 368 374 public static Class getObjType(Object arg, Class nulltype){ 375 if(arg==null) return nulltype; 376 else return arg.getClass(); 377 } 378 } 379 | Popular Tags |