7 Replies Latest reply: Feb 22, 2012 4:37 AM by 809019 RSS

    Generic inheritance from class string

    809019
      Hi,

      I'm looking at using generic inheritance to make working with database columns a little easier.

      I'm not sure if this is what I'm looking for:
      How To Instantiate a Generic Class Using Reflection
      I just don't understand how this ^ will solve my problem.

      I'm sitting with this class:
      public static class Column {
              
              private String name;
              private String clazz;
              private int length;
              
              /**
               * 
               * @param name
               * @param clazz
               * @param length 
               */
              public Column(String name, String clazz, int length) {
                  this.name = name;
                  this.clazz = clazz;
                  this.length = length;
              }
              /**
               * 
               * @return 
               */
              public String getName() {
                  return this.name;
              }
              /**
               * 
               * @return 
               */
              public String getColumnClassName() {
                  return this.clazz;
              }
              /**
               * 
               * @return
               * @throws ClassNotFoundException 
               */
              public Class<?> getColumnClass() throws ClassNotFoundException {
                  return Class.forName(this.clazz);
              }
              /**
               * 
               * @return 
               */
              public int getLength() {
                  return this.length;
              }
              
      }
      I would prefer to do something like this:
      public static class Column<T> {
              
              private String name;
              private int length;
              
              /**
               * 
               * @param name
               * @param clazz
               * @param length 
               */
              public Column(String name, int length) {
                  this.name = name;
                  this.length = length;
              }
              /**
               * 
               * @return 
               */
              public String getName() {
                  return this.name;
              }
              /**
               * 
               * @return 
               */
              public int getLength() {
                  return this.length;
              }
              
      }
      What I'd really like to know is, is something like this possible?
      Column column = new Column("Name", 80)<Class.forName("java.lang.String")>;

      I'm using the ResultSetMetaData, to get a columns class name.

      Regards,