public static void checkObject(Object data, Class<?> outType, Checkable checkable) throws CheckableDataException { checkIsNull(data, checkable); if (outType.isArray()) { checkArray((Object[]) data, outType, checkable); } else if (outType == Date.class || StringUtils.isNotEmpty(checkable.dateFormat())) { checkDate(ConvertUtil.toString(data), checkable); } else if (outType == String.class) { checkString(ConvertUtil.toString(data), checkable); } else if (outType == Double.class || outType == double.class) { checkDouble(ConvertUtil.toString(data), checkable); } else if (outType == Float.class || outType == float.class) { checkFloat(ConvertUtil.toString(data), checkable); } else if (outType == Integer.class || outType == int.class) { checkInteger(ConvertUtil.toString(data), checkable); } else if (!ClassUtil.isJavaType(outType)) { Field[] fields = outType.getDeclaredFields(); for (int i = 0; i < fields.length; i++) { if (fields[i].isAnnotationPresent(Checkable.class)) { Checkable tmpCheckable = fields[i].getAnnotation(Checkable.class); try { fields[i].setAccessible(true); Object fieldValue = fields[i].get(data); fields[i].setAccessible(false); checkObject(fieldValue, fields[i].getType(), tmpCheckable); } catch (Exception e) { throw new CheckableDataException(e.getMessage(), e); } } } } }
public static void checkArray(Object[] data, Class<?> outType, Checkable checkable) throws CheckableDataException { checkIsNull(data, checkable); int length = data.length; if (checkable.maxLength() != -1 && length > checkable.maxLength()) { String message = StringUtils.unite( "[", data, "] length is ", length, " claim array max length ", checkable.maxLength(), ". data check failed!"); throw new CheckableDataException(message); } if (checkable.minLength() != -1 && length < checkable.minLength()) { String message = StringUtils.unite( "[", data, "] length is ", length, " claim array min length ", checkable.minLength(), ". data check failed!"); throw new CheckableDataException(message); } for (int i = 0; i < length; i++) { String tmp = ConvertUtil.toString(data[i]); if (outType == String[].class) { checkString(tmp, checkable); } else if (outType == Integer[].class || outType == int[].class) { checkInteger(tmp, checkable); } else if (outType == Double[].class || outType == double[].class) { checkDouble(tmp, checkable); } else if (outType == Float[].class || outType == float[].class) { checkFloat(tmp, checkable); } else if (outType == Date[].class) { checkDate(tmp, checkable); } else { checkObject(data[i], outType.getComponentType(), checkable); } } }
@SuppressWarnings("unchecked") private static <T> T initValue(Object value, T defaultValue) { if (value == null) { return defaultValue; } else { Object tmp = defaultValue; if (defaultValue instanceof Boolean) { if (value.toString().trim().equalsIgnoreCase("true")) { tmp = true; } else { tmp = false; } } else if (defaultValue instanceof Integer) { tmp = ConvertUtil.toIt(value, Integer.class); } return (T) tmp; } }