@SuppressWarnings({"unchecked", "rawtypes"}) public static boolean filterFields( Object obj, FieldMatcher matcher, Dao dao, Callback2<MappingField, Object> callback) { if (obj == null) return false; obj = Lang.first(obj); if (obj == null) { return false; } if (obj.getClass() == Class.class) { throw Lang.impossible(); } if (obj instanceof String || obj instanceof Number || obj instanceof Boolean) { throw Lang.impossible(); } Entity en = dao.getEntity(obj.getClass()); if (en == null) { throw Lang.impossible(); } List<MappingField> mfs = en.getMappingFields(); if (matcher != null) { Iterator<MappingField> it = mfs.iterator(); while (it.hasNext()) { MappingField mf = it.next(); if (!matcher.match(mf.getName())) it.remove(); } } boolean flag = false; for (MappingField mf : mfs) { if (matcher.isIgnoreId() && mf.isId()) continue; if (matcher.isIgnoreName() && mf.isName()) continue; if (matcher.isIgnorePk() && mf.isCompositePk()) continue; Object val = mf.getValue(obj); if (val == null) { if (matcher.isIgnoreNull()) continue; } if (val instanceof Number && ((Number) val).doubleValue() == 0.0) { if (matcher.isIgnoreZero()) continue; } if (val instanceof Date) { if (matcher.isIgnoreDate()) continue; } callback.invoke(mf, val); flag = true; } return flag; }
/** * 转换一个 POJO 从一个指定的类型到另外的类型 * * @param src 源对象 * @param fromType 源对象类型 * @param toType 目标类型 * @param args 转换时参数。有些 Castor 可能需要这个参数,比如 Array2Map * @return 目标对象 * @throws FailToCastObjectException 如果没有找到转换器,或者转换失败 */ @SuppressWarnings({"unchecked", "rawtypes"}) public <F, T> T cast(Object src, Class<F> fromType, Class<T> toType, String... args) throws FailToCastObjectException { if (null == src) { // 原生数据的默认值 if (toType.isPrimitive()) { if (toType == int.class) return (T) Integer.valueOf(0); else if (toType == long.class) return (T) Long.valueOf(0L); else if (toType == byte.class) return (T) Byte.valueOf((byte) 0); else if (toType == short.class) return (T) Short.valueOf((short) 0); else if (toType == float.class) return (T) Float.valueOf(.0f); else if (toType == double.class) return (T) Double.valueOf(.0); else if (toType == boolean.class) return (T) Boolean.FALSE; else if (toType == char.class) return (T) Character.valueOf(' '); throw Lang.impossible(); } // 是对象,直接返回 null return null; } if (fromType == toType || toType == null || fromType == null) return (T) src; if (fromType.getName().equals(toType.getName())) return (T) src; if (toType.isAssignableFrom(fromType)) return (T) src; Mirror<?> from = Mirror.me(fromType, extractor); if (from.canCastToDirectly(toType)) // Use language built-in cases return (T) src; Castor c = find(from, toType); if (null == c) throw new FailToCastObjectException( String.format( "Can not find castor for '%s'=>'%s' in (%d) because:\n%s", fromType.getName(), toType.getName(), map.size(), "Fail to find matched castor")); try { return (T) c.cast(src, toType, args); } catch (FailToCastObjectException e) { throw e; } catch (Exception e) { throw new FailToCastObjectException( String.format( "Fail to cast from <%s> to <%s> for {%s} because:\n%s:%s", fromType.getName(), toType.getName(), src, e.getClass().getSimpleName(), e.getMessage()), Lang.unwrapThrow(e)); } }
private Tmpl(String tmpl, Pattern ptn, int groupIndex) { this(ptn, groupIndex); // 开始解析 Matcher m = _P.matcher(tmpl); int lastIndex = 0; while (m.find()) { int pos = m.start(); // 看看是否要生成静态对象 if (pos > lastIndex) { list.add(new TmplStaticEle(tmpl.substring(lastIndex, pos))); } // 分析键 Matcher m2 = _P2.matcher(m.group(this.groupIndex)); if (!m2.find()) throw Lang.makeThrow("Fail to parse tmpl key '%s'", m.group(1)); String key = m2.group(1); String type = Strings.sNull(m2.group(3), "string"); String fmt = m2.group(5); String dft = m2.group(7); // 记录键 keys.add(key); // 创建元素 if ("string".equals(type)) { list.add(new TmplStringEle(key, dft)); } // int else if ("int".equals(type)) { list.add(new TmplIntEle(key, fmt, dft)); } // long else if ("long".equals(type)) { list.add(new TmplLongEle(key, fmt, dft)); } // boolean else if ("boolean".equals(type)) { list.add(new TmplBooleanEle(key, fmt, dft)); } // float else if ("float".equals(type)) { list.add(new TmplFloatEle(key, fmt, dft)); } // double else if ("double".equals(type)) { list.add(new TmplDoubleEle(key, fmt, dft)); } // date else if ("date".equals(type)) { list.add(new TmplDateEle(key, fmt, dft)); } // 靠不可能 else { throw Lang.impossible(); } // 记录 lastIndex = m.end(); } // 最后结尾是否要加入一个对象 if (!(lastIndex >= tmpl.length())) { list.add(new TmplStaticEle(tmpl.substring(lastIndex))); } }