Generics and reflection in Java

Posted by Ragesh on Stack Overflow See other posts from Stack Overflow or by Ragesh
Published on 2010-02-21T10:55:30Z Indexed on 2010/06/09 21:02 UTC
Read the original article Hit count: 217

Filed under:
|
|

This is probably a very basic question, but I'm really new to generics in Java and I'm having a hard time altering my thought process from the way things are done in C#, so bear with me.

I'm trying to build a generic repository in Java. I've created an IRepository interface that looks like this:

public interface IRepository<T extends IEntity>

And a Repository class that looks like this:

public class Repository<T extends IEntity> implements IRepository<T>

Now, from within the constructor of my Repository class, I'd like to be able to "divine" the exact type of T. For example, if I instantiated a repository like this:

IRepository<MyClass> repo = new Repository<MyClass>();

I'd like to know that T is actually MyClass. This is trivial in C#, but obviously generics are a totally different beast in Java and I can't seem to find anything that would help me do this.

© Stack Overflow or respective owner

Related posts about java

Related posts about generics