Generics and reflection in Java
- by Ragesh
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.