title: Should I avoid using annotations?
body: <p>In my project I’m loading configuration from a json file (using gson) and as you probably know, missing fields will be populate with an empty string.</p>
  
  <p>Some of the fields are mandatory and other must be greater then X, and I want to validate it.</p>
  
  <p>The simple (and ugly) way is to use if condition for each property, like:</p>
  
  <pre><code>if (StringUtils.isEmpty(sftpConfiguration.getHostName)) {
     logger.error (“hostName property is mandatory”);
    // etc.
  }
  </code></pre>
  
  <p>but, I have more than one field and more and more properties will added in the future, therefore I built two annotations, called <code>NorNullEmpty</code> and <code>GreaterThen</code> (with value property) and I run throw the fields like this:</p>
  
  <pre><code>public static boolean validateWithAnnotation(Object object) throws IllegalAccessException {
      boolean result = true;
      Field[] classFields = object.getClass().getDeclaredFields();
  
      for (Field field : classFields) {
          if (field.getAnnotation(NorNullEmpty.class) != null) {
              if (field.getType() == String.class) {
                  field.setAccessible(true);
                  String value = (String) field.get(object);
                  if (StringUtils.isEmpty(value)) {
                      result = false;
                      logger.error("Property {} is mandatory but null or an empty.", field.getName());
                  }
                  field.setAccessible(false);
              } else {
                  logger.warn("@NorNullEmpty annotation is working on String type only.");
              }
          } else if (field.getAnnotation(GreaterThan.class) != null) {
              Class&lt;?&gt; fieldType = field.getType();
              if (fieldType == long.class || fieldType == Long.class) {
                  field.setAccessible(true);
                  Long val = field.getLong(object);
                  if (val &lt;= field.getAnnotation(GreaterThan.class).value()) {
                      result = false;
                      logger.error("Property {} value is {} and it must be greater than {}.", field.getName(), val, field.getAnnotation(GreaterThan.class).value());
                  }
                  field.setAccessible(false);
              }
          }
      }
  
      return result;
  }
  </code></pre>
  
  <p>When I did a code review with a collage he very afraid from the annotations use, “it’s very risky and very expensive cost”..</p>
  
  <p>I’ll be glad to know what you think, should I use a simple-stupid if for each field? continue with the reflection? or I should validate the fields using other way?</p>
  
  <p>Note: NOT using Spring / Hibernate.</p>