0

How do you get the class name of the generic type of Collection, using reflection, in this situation?

public class MyClass<T> implements TheirClass<Collection<T>> {
    ...

    public Class<?> getCollectionType() {
        //Need to use reflection on `this` in order to return the proper class
        //of the type of Collection 
    }
}

When we assign MyClass to be of type Collection, below:

MyClass<Collection<AnotherClass>> me = new MyClass<>();

The result would look like this below:

MyClass<Collection<AnotherClass>> meAnotherClass = new MyClass<>();
MyClass<Collection<String>> meString = new MyClass<>();

System.out.println(""+meString.getCollectionType().getClass() == String.class); //true
System.out.println(""+meAnotherClass.getCollectionType().getClass() == AnotherClass.class); //true
Tyler
  • 19,113
  • 19
  • 94
  • 151

0 Answers0