/** * Boring code to get constraints from the field annotations * * @param field * @return */ protected String getFieldAnnotationConstraints(Field field) { List<String> constraints = new ArrayList<String>(); if (field.getAnnotation(Email.class) != null) { constraints.add("Email"); } if (field.getAnnotation(Future.class) != null) { constraints.add("Future date"); } if (field.getAnnotation(NotNull.class) != null) { constraints.add("Not null"); } if (field.getAnnotation(Past.class) != null) { constraints.add("Past date"); } if (field.getAnnotation(NotEmpty.class) != null) { constraints.add("Not empty"); } if (field.getAnnotation(Pattern.class) != null) { constraints.add( String.format("Pattern(regexp = '%s')", field.getAnnotation(Pattern.class).regexp())); } if (field.getAnnotation(Length.class) != null) { constraints.add( String.format( "Length(min = %d, max = %d)", field.getAnnotation(Length.class).min(), field.getAnnotation(Length.class).max())); } if (field.getAnnotation(Size.class) != null) { constraints.add( String.format( "Size(min = %d, max = %d)", field.getAnnotation(Size.class).min(), field.getAnnotation(Size.class).max())); } if (field.getAnnotation(Range.class) != null) { constraints.add( String.format( "Range(min = %d, max = %d)", field.getAnnotation(Range.class).min(), field.getAnnotation(Range.class).max())); } if (constraints.size() == 0) { return null; } StringBuffer buff = new StringBuffer(); Iterator<String> constraintsIt = constraints.iterator(); while (constraintsIt.hasNext()) { String value = constraintsIt.next(); buff.append(value); if (constraintsIt.hasNext()) { buff.append(", "); } } return buff.toString(); }
/** * Extract the enumeration constants * * @param enumeration is the enumeration * @return is the string with constants */ protected String getEnumerationConstants(Class<?> enumeration) { Object[] constants = enumeration.getEnumConstants(); int idx = 0; StringBuffer strBuff = new StringBuffer(); for (Object konst : constants) { strBuff.append(konst.toString()); if (idx < constants.length - 1) { strBuff.append(", "); } ++idx; } return strBuff.toString(); }