3 Replies Latest reply: Dec 26, 2010 10:48 PM by jtahlborn RSS

    References to generic type BeanListHandler<T> should be parameterized

    826582
      Hi,

      I am trying to write a java class that will execute any sql select/update/delete statement and return the result set object transforming to list of type <T> objects.
      For this i am using DBUtils from apache.

      But i am geting a waring at statement "*h = new BeanListHandler(type.newInstance().getClass());* that
      *BeanListHandler is a raw type. References to generic type BeanListHandler<T> should be parameterized*


      public class StatementHandler<T>{

           protected static DataSource ds = null;
           protected final Class<T> type;
           
           static{
                try {
                     
                     ds = (DataSource) new InitialContext().lookup(ApplicationProperties.getInstance().getProperty("DATA_SOURCE"));
                } catch (NamingException e) {
                     logger.error("Data source lookup failed" , e);
                }
           }
           
           public StatementHandler(Class<T> type){
                this.type = type;
           }
           
           @Override
           public List<T> execute(String query, Object... args) {

                List<T> list = null;
                QueryRunner run = new QueryRunner(ds);
                ResultSetHandler<List<T>> h=null;
                try {
                     *h = new BeanListHandler(type.newInstance().getClass());*
                } catch (InstantiationException e1) {
                     e1.printStackTrace();
                } catch (IllegalAccessException e1) {
                     e1.printStackTrace();
                }

                try {
                     list= run.query(query, h);
                } catch (SQLException e) {
                     e.printStackTrace();
                }

                return list;
           }

      }


      the constructor of BeanListHandler from DBUtils is

      public BeanListHandler(Class<T> type) {
      this(type, ArrayHandler.ROW_PROCESSOR);
      }

      Please some one help me on how to solve this and get rid of warning.
      If my design in not correct, then also please suggest me for improvement.

      Thanks,
      Sridhar
        • 1. Re: References to generic type BeanListHandler<T> should be parameterized
          jtahlborn
          a raw type is something like "List". a parameterized type is something like "List<String>". when you instantiate a parameterized type, you need to provide the generic type, e.g. "new ArrayList<String>()".
          • 2. Re: References to generic type BeanListHandler<T> should be parameterized
            826582
            I have tried the following options and got the errors

            h = new BeanListHandler<List<T>>(type.newInstance().getClass());
            Description     Resource     Path     Location     Type
            The constructor BeanListHandler<List<T>>(Class<capture#1-of ? extends Object>) is undefined     StatementHandler.java     /Hotels/src/com/abhi/hotel/db/handler     line 54     Java Problem
            Type mismatch: cannot convert from BeanListHandler<List<T>> to ResultSetHandler<List<T>>     StatementHandler.java     /Hotels/src/com/abhi/hotel/db/handler     line 54     Java Problem


            h = new BeanListHandler<T>(type.newInstance().getClass());

            The constructor BeanListHandler<T>(Class<capture#1-of ? extends Object>) is undefined     StatementHandler.java     /Hotels/src/com/abhi/hotel/db/handler     line 54     Java Problem



            public class BeanListHandler<T> implements ResultSetHandler<List<T>> {


            private final Class<T> type;

            /**
            * Creates a new instance of BeanListHandler.
            *
            * @param type The Class that objects returned from <code>handle()</code>
            * are created from.
            */
            public BeanListHandler(Class<T> type) {
            this(type, ArrayHandler.ROW_PROCESSOR);
            }
            .....
            }
            • 3. Re: References to generic type BeanListHandler<T> should be parameterized
              jtahlborn
              did you try:

              h = new BeanListHandler<T>(type);