title: Java enums in generic type
body: <p>I'd like to create a generic enum-based mapper for IBatis. I'm doing this with the below code. This does have compile time errors, which I don't know how to fix. Maybe my solution is just plain wrong (keep in mind the use of IBatis), in such case please suggest something better.</p>
  
  <p>Any help appreciated.</p>
  
  <p>What I want to achieve is to define subsequent mappers as:</p>
  
  <pre><code>public class XEnumTypeHandler extends CommonEnumTypeHandler&lt;X&gt; {
  }
  </code></pre>
  
  <p>The current code:</p>
  
  <pre><code>public class CommonEnumTypeHandler&lt;T extends Enum&gt; implements TypeHandlerCallback {
  
   public void setParameter(ParameterSetter ps, Object o) throws SQLException {
    if (o.getClass().isAssignableFrom(**T**)) { 
     ps.setString(((**T**) o).value().toUpperCase());
    } else
     throw new SQLException("Excpected ParameterType object than: " + o);
   }
  
   public Object getResult(ResultGetter rs) throws SQLException {
    Object o = valueOf(rs.getString());
    if (o == null)
     throw new SQLException("Unknown parameter type: " + rs.getString());
    return o;
   }
  
   public Object valueOf(String s) {
    for (T pt : T.**values()**) {
     if (pt.**value()**.equalsIgnoreCase(s))
      return pt;
    }
    return null;
   }
  }
  </code></pre>
  
  <p>I've added error markings to the above code, the error messages are in order:</p>
  
  <ul>
  <li>T cannot be resolved</li>
  <li>The method value() is undefined for
  the type T</li>
  <li>The method values() is undefined for
  the type T</li>
  <li>The method values() is undefined for
  the type T</li>
  </ul>
  
  <p>I've solved this issue with the following code:</p>
  
  <pre><code>public class CommonEnumTypeHandler&lt;T extends Enum&gt; implements TypeHandlerCallback {
  
      Class&lt;T&gt; clazz;
  
      public CommonEnumTypeHandler(Class&lt;T&gt; clazz) {
          this.clazz = clazz;
      }
  
      public void setParameter(ParameterSetter ps, Object o) throws SQLException {
          if (o.getClass().isAssignableFrom(clazz)) {
              ps.setString(((T) o).name().toUpperCase());
          } else
              throw new SQLException("Excpected " + clazz + " object than: " + o);
      }
  
      public Object getResult(ResultGetter rs) throws SQLException {
          Object o = valueOf(rs.getString());
          if (o == null)
              throw new SQLException("Unknown parameter type: " + rs.getString());
          return o;
      }
  
      public Object valueOf(String s) {
          return Enum.valueOf(clazz, s);
      }
  }
  </code></pre>
  
  <p>Inheriting from this class I do:</p>
  
  <pre><code>public class SalesChannelTypeHandler extends CommonEnumTypeHandler&lt;SalesChannel&gt; {
  
      public SalesChannelTypeHandler() {
          super(SalesChannel.class);
      }
  
      public SalesChannelTypeHandler(Class&lt;SalesChannel&gt; clazz) {
          super(clazz);
      }
  
  }
  </code></pre>