title: Remove whitespace from custom annotated fields
body: <p>In my application I have lots of fields where user can insert numeric values. Requirement would be that any leading, leading or inline whitespace must be removed after submission. I have total about 10 fields that need that kind of formation.</p>
  
  <p><strong>Edit</strong>: It's Spring MVC application. Data is inserted through the web. Number (telephone) fields are String type and following is valid if inserted, but should be stripped before making it to the converters and other validators: " +44 123456 55 ", "+4 4 1 2 3 4 " or anything that would contain whitespace.</p>
  
  <p>I have looked into Aspects, but haven't had any luck modifying the values via reflection.</p>
  
  <p>So far I've came up with custom annotation <code>@NumericValue</code> and have it on all the fields that need to be formatted. My current solution would be to pass in an object which contains these fields and remove them by looping through using the following method:</p>
  
  <pre><code>public static void formatNumericFields(Object object) {
          for (Field field : object.getClass().getDeclaredFields())
              for (Annotation annotation : field.getDeclaredAnnotations())
                  if (annotation.annotationType().equals(NumericValue.class)) {
                      field.setAccessible(true);
                      if (getField(field, object) != null)
                          setField(field, object, getField(field, object).toString().replace(" ", ""));
                  }
      }
  </code></pre>
  
  <p>I would ask if there is any more practical way of doing what I am trying to achieve. I wouldn't want to do .replace(" ", "") on all fields separately. For now I would call that method in 3 different controllers passing in the form data that needs to be formatted. Thanks.</p>