1

I'm kind of lame with generics, but I wonder, for the following class:

static class SomeClass<T> { private T value; public SomeClass(T value) { T.class? this.value = value; } public T getValue() { return value; } } 

If called for example: SomeClass<String> stringer = new SomeClass<String>("Hello"); Is it possible to get String.class (or whatever T would be) in the constructor?

Ok, wait a sec, I'm going to explain what I'm trying to solve

The actual problem is that I'm using OrmLite and I have a lot of DAO objects, like this one:

public class PostDAO extends BaseJdbcDao<Post, String> { public PostDAO(DatabaseType databaseType) { super(databaseType, Post.class); } } 

For Domain it is:

public class DomainDAO extends BaseJdbcDao<Domain, String> { public DomainDAO(DatabaseType databaseType) { super(databaseType, Domain.class); } } 

and so on. I wanted to parametrize these, so that I can only have one:

public class DAO<K, V> extends BaseJdbcDao<K, V> { public DAO(DatabaseType databaseType) { super(databaseType, (WHAT HERE?)); } } 

but I'm stuck on the what here part)

5
  • Why do you want to get the class? Commented Jul 1, 2010 at 9:21
  • you can't do new SomeClass<String>("Hello"); on a static class btw. Commented Jul 1, 2010 at 9:22
  • @pakore: Maybe you can't but I can. ;) Seriously: It works fine for me and why shouldn't it? Commented Jul 1, 2010 at 9:39
  • possible duplicate of Deriving Class from Generic T Commented Jul 1, 2010 at 11:16
  • I've added a createDao method to the BaseJdbcDao class in ORMLite version 2.7. It will allow you to create DOAs without needing a DAO class. Commented Jul 19, 2010 at 19:48

3 Answers 3

1

What about:

public class DAO<K, V> extends BaseJdbcDao<K, V> { public DAO(DatabaseType databaseType, Class databaseClass) { super(databaseType, databaseClass); } } 
Sign up to request clarification or add additional context in comments.

Comments

1

@pakore's answer is a good one but I wanted to add that you don't have to define the per-class DAO object. I recommended it in the ORMLite documentation but it's supposed to be a convenience, not a pain.

You can always do something like the following using BaseJdbcDao as an anonymous class:

BaseJdbcDao<Post, String> postDao = new BaseJdbcDao<Post, String>(databaseType, Post.class) { }; postDao.setDataSource(dataSource); postDao.initialize(); 

I do that a lot in the ORMLite junit tests. Might be better to have a utility method like the following. I've just added it to the BaseJdbcDao class which will be in the 2.7 release.

public static <T, ID> Dao<T, ID> createDao(DatabaseType databaseType, DataSource dataSource, Class<T> clazz) throws SQLException { BaseJdbcDao<T, ID> dao = new BaseJdbcDao<T, ID>(databaseType, clazz) { }; dao.setDataSource(dataSource); dao.initialize(); return dao; } 

Comments

-1

value.getClass() should do the trick (assuming value is never null!)

3 Comments

Then you can't. This information is only available at compile time. At runtime there is just Object.
I think that BaseJdbcDao has the class argument as parameter because you can not get the class from the type parameter. As Dao is not a concrete class itself, you can add the class parameter there too.
even if value is not null, it may still not give the answer you wanted, as value could be of any subclass of T

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.