Java and generics

I am new to java, so I apologize if I have a completely wrong stick end.

I am trying to write a general (in the English sense of the word!) Data access class. For example, I have:

public class DA<T> { public static Dao getAccountDao() throws NamingException, SQLException { Context ctx = new InitialContext(); DataSource dataSource = (DataSource)ctx.lookup("java:comp/env/jdbc/test"); ConnectionSource connectionSource = new DataSourceConnectionSource(dataSource, new MysqlDatabaseType()); Dao<Account, Integer> accountDao = DaoManager.createDao(connectionSource, Account.class); return accountDao; } } 

And I can call it with:

 Dao<Account, Integer> accountDao = DA.getAccountDao(); 

But I will need a version of this for each Dao / model. Therefore, I am trying to do something that can be called the following:

 Dao<SomeClass, Integer> someClassDao = DA.getDao(SomeClass); 

Is it possible?

I tried things like:

 public class DA { public static Dao getDao(<T>) throws NamingException, SQLException { Context ctx = new InitialContext(); DataSource dataSource = (DataSource)ctx.lookup("java:comp/env/jdbc/test"); ConnectionSource connectionSource = new DataSourceConnectionSource(dataSource, new MysqlDatabaseType()); Dao<T, Integer> accountDao = DaoManager.createDao(connectionSource, T.class); return accountDao; } 

}

but Netbeans gives an error: illegal start of type

My brain is struggling with generics, is that what they can do ?!

EDIT: using the messages below I should:

 public class DA<T> { public static Dao<T, Integer> getDao(T daoType) throws NamingException, SQLException { Dao<T, Integer> accountDao = DaoManager.createDao(T.class); return accountDao; } 

}

Which generates two errors: non-static type variable T cannot be referenced from a static context and if I remove the static I get: cannot select from a type variable I need to read about how common and static works together, but the second one looks like a result of erasing (http://www.coderanch.com/t/386358/java/java/Converting-type-parameters-class), so I'm not sure if this will be possible.

Should be mentioned earlier, Dao stuff uses an ORM library called ORMLite, so createDao, etc. is not my code.

+4
source share
4 answers

To access what you mean by T.class , you need to pass a class object to a method:

 class Account {} class Dao<TEntity, TId> {} class DA { // your DaoManager.createDao() will also need a similar signature public static <TEntity> Dao<TEntity, Integer> getDao(Class<TEntity> daoType) { /// yadda blah, create DAO as appropriate return new Dao<TEntity, Integer>(); } } public class Test { public static void main(String[] args) { // Pass the class literal for what you want TEntity to be as a parameter Dao<Account, Integer> dao = DA.getDao(Account.class); } } 
+2
source

You want your DA class to have two types (for example, <Account, Integer> ), but in the class declaration you specify only one type <T> . Take a look at some documents and examples, for example:

http://en.wikipedia.org/wiki/Generics_in_Java#Generic_class_definitions
http://docs.oracle.com/javase/tutorial/java/generics/index.html

+2
source

You will need to do this as follows:

 public class DA { public static <T> Dao<T,Integer> getDao(Class<T> clazz) throws NamingException, SQLException { Context ctx = new InitialContext(); DataSource dataSource = (DataSource)ctx.lookup("java:comp/env/jdbc/test"); ConnectionSource connectionSource = new DataSourceConnectionSource(dataSource, new MysqlDatabaseType()); Dao<T, Integer> accountDao = DaoManager.createDao(connectionSource, clazz); return accountDao; } } 

Also, the createDao method will have this signature:

 public static <T> Dao<T,Integer> createDao(ConnectionSource source,Class<T> clazz) { ... } 
+1
source

If you need to specify a method that takes the type of the template parameter, the template comes before the return type:

 <T> void foo(T param) { ... } 
-one
source

Source: https://habr.com/ru/post/1394076/


All Articles