/** * 为对象的一个字段设值。 优先调用对象的 setter,如果没有,直接设置字段的值 * * @param obj 对象 * @param field 字段 * @param value 值。如果为 null,字符和数字字段,都会设成 0 * @throws FailToSetValueException */ public void setValue(Object obj, Field field, Object value) throws FailToSetValueException { if (!field.isAccessible()) field.setAccessible(true); Class<?> ft = field.getType(); // 非 null 值,进行转换 if (null != value) { try { value = Castors.me().castTo(value, field.getType()); } catch (FailToCastObjectException e) { throw makeSetValueException(obj.getClass(), field.getName(), value, e); } } // 如果是原生类型,转换成默认值 else if (ft.isPrimitive()) { if (boolean.class == ft) { value = false; } else if (char.class == ft) { value = (char) 0; } else { value = (byte) 0; } } try { this.getSetter(field).invoke(obj, value); } catch (Exception e1) { try { field.set(obj, value); } catch (Exception e) { throw makeSetValueException(obj.getClass(), field.getName(), value, e); } } }
@Override protected String _val(Object val) { Integer n = null == val ? dft : Castors.me().castTo(val, Integer.class); if (null != n) { return String.format(fmt, n); } return null; }
/** * 将数组转换成Object[] 数组。将会采用 Castor 来深层转换数组元素 * * @param args 原始数组 * @param pts 新数组的元素类型 * @return 新数组 * @throws FailToCastObjectException * @see org.nutz.castor.Castors */ public static <T> Object[] array2ObjectArray(T[] args, Class<?>[] pts) throws FailToCastObjectException { if (null == args) return null; Object[] newArgs = new Object[args.length]; for (int i = 0; i < args.length; i++) { newArgs[i] = Castors.me().castTo(args[i], pts[i]); } return newArgs; }
/** * 将数组转换成另外一种类型的数组。将会采用 Castor 来深层转换数组元素 * * @param array 原始数组 * @param eleType 新数组的元素类型 * @return 新数组 * @throws FailToCastObjectException * @see org.nutz.castor.Castors */ public static Object array2array(Object array, Class<?> eleType) throws FailToCastObjectException { if (null == array) return null; int len = Array.getLength(array); Object re = Array.newInstance(eleType, len); for (int i = 0; i < len; i++) { Array.set(re, i, Castors.me().castTo(Array.get(array, i), eleType)); } return re; }
/** * 将数组转换成一个列表。将会采用 Castor 来深层转换数组元素 * * @param array 原始数组 * @param eleType 新列表的元素类型 * @return 新列表 * @see org.nutz.castor.Castors */ public static <T, E> List<E> array2list(Object array, Class<E> eleType) { if (null == array) return null; int len = Array.getLength(array); List<E> re = new ArrayList<E>(len); for (int i = 0; i < len; i++) { Object obj = Array.get(array, i); re.add(Castors.me().castTo(obj, eleType)); } return re; }
@Override protected Object cast(Collection src, Class<?> toType, String... args) throws FailToCastObjectException { Class<?> compType = toType.getComponentType(); Object ary = Array.newInstance(compType, src.size()); int index = 0; for (Iterator it = src.iterator(); it.hasNext(); ) { Array.set(ary, index++, Castors.me().castTo(it.next(), compType)); } return ary; }
/** * 将集合变成指定类型的数组 * * @param col 集合对象 * @param eleType 数组元素类型 * @return 数组 */ @SuppressWarnings("unchecked") public static <E> E[] collection2array(Collection<?> col, Class<E> eleType) { if (null == col) return null; Object re = Array.newInstance(eleType, col.size()); int i = 0; for (Iterator<?> it = col.iterator(); it.hasNext(); ) { Object obj = it.next(); if (null == obj) Array.set(re, i++, null); else Array.set(re, i++, Castors.me().castTo(obj, eleType)); } return (E[]) re; }
/** * 查找一个方法。匹配的很宽泛 * * @param name 方法名 * @param paramTypes 参数类型列表 * @return 方法 * @throws NoSuchMethodException */ public Method findMethod(String name, Class<?>... paramTypes) throws NoSuchMethodException { try { return klass.getMethod(name, paramTypes); } catch (NoSuchMethodException e) { for (Method m : klass.getMethods()) { if (m.getName().equals(name)) if (doMatchMethodParamsType(paramTypes, m.getParameterTypes())) return m; } } throw new NoSuchMethodException( String.format( "Fail to find Method %s->%s with params:\n%s", klass.getName(), name, Castors.me().castToString(paramTypes))); }
public AnnotationIocLoader(String... packages) { for (String packageZ : packages) for (Class<?> classZ : Scans.me().scanPackage(packageZ)) addClass(classZ); if (map.size() > 0) { if (log.isInfoEnabled()) log.infof( "Scan complete ! Found %s classes in %s base-packages!\nbeans = %s", map.size(), packages.length, Castors.me().castToString(map.keySet())); } else { log.warn( "NONE Annotation-Class found!! Check your configure or report a bug!! packages=" + Arrays.toString(packages)); } }
/** * 根据返回值类型,以及参数类型,查找第一个匹配的方法 * * @param returnType 返回值类型 * @param paramTypes 参数个数 * @return 方法 * @throws NoSuchMethodException */ public Method findMethod(Class<?> returnType, Class<?>... paramTypes) throws NoSuchMethodException { for (Method m : klass.getMethods()) { if (returnType == m.getReturnType()) if (paramTypes.length == m.getParameterTypes().length) { boolean noThisOne = false; for (int i = 0; i < paramTypes.length; i++) { if (paramTypes[i] != m.getParameterTypes()[i]) { noThisOne = true; break; } } if (!noThisOne) return m; } } throw new NoSuchMethodException( String.format( "Can not find method in [%s] with return type '%s' and arguemtns \n'%s'!", klass.getName(), returnType.getName(), Castors.me().castToString(paramTypes))); }
/** * 根据一个 Map,和给定的对象类型,创建一个新的 JAVA 对象 * * @param src Map 对象 * @param toType JAVA 对象类型 * @return JAVA 对象 * @throws FailToCastObjectException */ @SuppressWarnings({"unchecked", "rawtypes"}) public static <T> T map2Object(Map<?, ?> src, Class<T> toType) throws FailToCastObjectException { if (null == toType) throw new FailToCastObjectException("target type is Null"); // 类型相同 if (toType == Map.class) return (T) src; // 也是一种 Map if (Map.class.isAssignableFrom(toType)) { Map map; try { map = (Map) toType.newInstance(); map.putAll(src); return (T) map; } catch (Exception e) { throw new FailToCastObjectException("target type fail to born!", e); } } // 数组 if (toType.isArray()) return (T) Lang.collection2array(src.values(), toType.getComponentType()); // List if (List.class == toType) { return (T) Lang.collection2list(src.values()); } // POJO Mirror<T> mirror = Mirror.me(toType); T obj = mirror.born(); for (Field field : mirror.getFields()) { if (src.containsKey(field.getName())) { Object v = src.get(field.getName()); if (null == v) continue; Class<?> ft = field.getType(); Object vv = null; // 集合 if (v instanceof Collection) { Collection c = (Collection) v; // 集合到数组 if (ft.isArray()) { vv = Lang.collection2array(c, ft.getComponentType()); } // 集合到集合 else { // 创建 Collection newCol; Class eleType = Mirror.getGenericTypes(field, 0); if (ft == List.class) { newCol = new ArrayList(c.size()); } else if (ft == Set.class) { newCol = new LinkedHashSet(); } else { try { newCol = (Collection) ft.newInstance(); } catch (Exception e) { throw Lang.wrapThrow(e); } } // 赋值 for (Object ele : c) { newCol.add(Castors.me().castTo(ele, eleType)); } vv = newCol; } } // Map else if (v instanceof Map && Map.class.isAssignableFrom(ft)) { // 创建 final Map map; // Map 接口 if (ft == Map.class) { map = new HashMap(); } // 自己特殊的 Map else { try { map = (Map) ft.newInstance(); } catch (Exception e) { throw new FailToCastObjectException("target type fail to born!", e); } } // 赋值 final Class<?> valType = Mirror.getGenericTypes(field, 1); each( v, new Each<Entry>() { public void invoke(int i, Entry en, int length) { map.put(en.getKey(), Castors.me().castTo(en.getValue(), valType)); } }); vv = map; } // 强制转换 else { vv = Castors.me().castTo(v, ft); } mirror.setValue(obj, field, vv); } } return obj; }
/** * 将集合编程变成指定类型的列表 * * @param col 集合对象 * @param eleType 列表类型 * @return 列表对象 */ public static <E> List<E> collection2list(Collection<?> col, Class<E> eleType) { if (null == col) return null; List<E> list = new ArrayList<E>(col.size()); for (Object obj : col) list.add(Castors.me().castTo(obj, eleType)); return list; }
private static SqlExpression _evalRange(Mirror<?> mirror, String name, Object value) { if (mirror.isInt()) return inInt(name, Castors.me().castTo(value, int[].class)); else if (mirror.isLong()) return inLong(name, Castors.me().castTo(value, long[].class)); return inStr(name, Castors.me().castTo(value, String[].class)); }