1 8 14 package jfun.yan; 15 16 import java.util.HashMap ; 17 import java.util.LinkedList ; 18 19 import jfun.util.Misc; 20 import jfun.yan.function.Signature; 21 import jfun.yan.util.ReflectionUtil; 22 23 24 30 final class Utils { 31 static void checkType(Signature src, Object ckey, int k, Class param_type, Class arg_type){ 32 if(arg_type==null) return; 33 if(!ReflectionUtil.isAssignableFrom(param_type, arg_type)){ 34 throw new ParameterTypeMismatchException(ckey, src, k, param_type, arg_type); 35 } 36 } 37 static void checkType(Class src, Object ckey, Object k, Class param_type, Class arg_type){ 38 if(arg_type==null) return; 39 if(!ReflectionUtil.isAssignableFrom(param_type, arg_type)){ 40 throw new PropertyTypeMismatchException(ckey, src, k, param_type, arg_type); 41 } 42 } 43 static void checkArg(Signature src, Object ckey, int k, Class param_type, Object arg){ 44 if(!ReflectionUtil.isInstance(param_type, arg)){ 45 throw new ParameterTypeMismatchException(ckey, src, k, param_type, 46 getObjType(arg)); 47 } 48 } 49 static void checkArg(Class src, Object ckey, Object k, Class param_type, Object arg){ 50 if(!ReflectionUtil.isInstance(param_type, arg)){ 51 throw new PropertyTypeMismatchException(ckey, src, k, param_type, 52 getObjType(arg)); 53 } 54 } 55 62 static String getObjTypeName(Object arg){ 63 return jfun.yan.util.Utils.getObjTypeName(arg, null); 64 } 65 static Class getObjType(Object arg){ 66 return jfun.yan.util.Utils.getObjType(arg, Object .class); 67 } 68 static boolean eq(Object o1, Object o2){ 69 if(o1==null) return o2==null; 70 if(o2==null) return false; 71 return o1.equals(o2); 72 } 73 static java.util.Map hashmap(Object [] keys, Object [] vals){ 74 final HashMap ret = new HashMap (keys.length); 75 populateMap(ret, keys, vals); 76 return ret; 77 } 78 private static void checkParent(final LinkedList buf, final Class t){ 79 for(java.util.Iterator it=buf.iterator(); it.hasNext();){ 81 final Class t0 = (Class )it.next(); 82 if(ReflectionUtil.isAssignableFrom(t0, t)){ 83 return; 84 } 85 else if(ReflectionUtil.isAssignableFrom(t, t0)){ 86 it.remove(); 87 } 88 } 89 buf.add(t); 90 } 91 static Class getCommonRootType(Typeful[] arr){ 92 final LinkedList buf = new LinkedList (); 93 for(int i=0; i<arr.length; i++){ 94 final Class t = arr[i].getType(); 95 if(t == null) return Object .class; 96 checkParent(buf, t); 97 } 98 if(buf.isEmpty() || buf.size() > 1){ 99 return Object .class; 100 } 101 else return (Class )buf.get(0); 102 124 } 125 static RuntimeException wrapInvocationException(java.lang.reflect.InvocationTargetException e){ 126 final Throwable th = e.getTargetException(); 127 if(th instanceof Error ) throw (Error )th; 128 else return new ComponentInstantiationException(th); 129 } 130 private static void populateMap(java.util.Map m, Object [] keys, Object [] vals){ 131 if(keys.length != vals.length){ 132 throw new IllegalArgumentException ("keys.length=="+keys.length 133 + ", vals.length=="+vals.length); 134 } 135 for(int i=0; i<keys.length; i++){ 136 final Object key = keys[i]; 137 if(m.containsKey(key)){ 138 throw new IllegalArgumentException ("duplicate key: "+key); 139 } 140 m.put(key, vals[i]); 141 } 142 } 143 static final Class [] no_params = {}; 144 145 static YanException getThreadLocalException(ThreadLocal tl){ 146 final YanException ex = (YanException)tl.get(); 147 ex.clearResolutionTrace(); 148 return ex; 149 } 150 } 151 | Popular Tags |