0

I want to find the class name of the argument sent to the generic class as follows.

public abstract class RootClass<T extends Iface> {
    protected ApplicationContext applicationContext;
    public T getIfaceBean() {
        return applicationContext.getBean(T.class);
    }
}

But it looks like I can't do T.class (due to Type Erasure?).

So, Is such an action possible with Java Generics ?

How can I achieve this type of functionality with Java Generics?

TheKojuEffect
  • 18,635
  • 17
  • 83
  • 116

1 Answers1

7

Because of Type Erasure, you can't say T.class because T doesn't exist at runtime.

The best you can do is to take a parameter of type Class<T> to get the Class object:

public T getIfaceBean(Class<T> clazz) {
rgettman
  • 172,063
  • 28
  • 262
  • 343