static void check(Person person) throws IllegalArgumentException, ReflectiveOperationException { for (Field field : person.getClass().getFields()) { Range range = field.getAnnotation(Range.class); if (range != null) { Object value = field.get(person); if( value instanceof String){ if( value.toString().length() > range.max() || value.toString().length() < range.min()){ throw new IllegalArgumentException("String is too long/short"); } } else if(value instanceof Integer){ if((Integer)value > range.max() || (Integer)value < range.min() ){ throw new IllegalArgumentException("Int is too big/small"); } } } } }
Sign in to make a reply
Lumen.