title: Dynamic cast to enum using SimpleFramework
body: <p>I have an XML String with an attribute that contains integer value:</p>
  
  <pre><code>&lt;item status="2" /&gt;
  </code></pre>
  
  <p>There is also <code>Simple Framework</code> class that describes this <code>item</code>:</p>
  
  <pre><code>@Root
  public static class Item {
  
      @Attribute(name="status")
      private int status;
  
      public int getStatus() {
          return status;
      }
  
  }
  </code></pre>
  
  <p>Unserialization works well, however I want to be able to change <code>int</code> type to the defined <code>enum</code> type.</p>
  
  <pre><code>public enum Status {
      OK(0), PENDING(1), ERROR(2);
  
      BetStatus(int status) {
          this.status = status;
      }
  
      public int getStatus() {
          return status;
      }
  }
  </code></pre>
  
  <p>So with a quick modification:</p>
  
  <pre><code>@Root
  public static class Item {
  
      @Attribute(name="status")
      private Status status;
  
      public Status getStatus() {
          return status;
      }
  
  }
  </code></pre>
  
  <p>However now I get an error:</p>
  
  <pre><code>java.lang.IllegalArgumentException: 2 is not a constant in com.my.package.Status
  </code></pre>
  
  <p>Is it possible to cast <code>int</code> this way during unserialization ?</p>
  
  <p>I bet I have to add some magic method to my <code>Status</code> class.</p>
  
  <p><strong>Solution:</strong></p>
  
  <p>According to <code>Reimeus</code> answer I leaved <code>int</code> type for <code>status</code> attribute and I modified just <code>Item</code> class with:</p>
  
  <pre><code>@Root
  public static class Item {
  
      @Attribute(name="status")
      private int status;
  
      public Status getStatus() {
          return Status.getByOrdinal(status);
      }
  
  }
  </code></pre>