Is it possible in Java to find out the generic type of a collection getter via reflection?

Bastian Voigt

I have a class with a collection getter that I'm inspecting via reflection. Something like this:

class Bar {
  String baz;
  int bazooka;
}
class Foo {
  List<Bar> bars = new ArrayList<Bar>();
  List<Bar> getBarsList() { return bars; }
}

What I need to find out at runtime is the class object Class<Bar>. I know it's possible if you have a reference to the Field object. But is it also possible from the getter Method?

sksamuel

In your case it is possible using reflection. What you can't do is get runtime information about types. Given an instance of a parametrized type, you can't at runtime find out what that was parametrized with. This is what the other answers are telling you, but that's not what you're asking. You're asking about compile time type constraints.

public class Test {

  public static void main(String args[]) throws NoSuchMethodException {
    Method method = Foo.class.getDeclaredMethod("getBars", new Class[]{});
    Type grt = method.getGenericReturnType();
    if (grt instanceof ParameterizedType) {
      Type Bar = ((ParameterizedType) grt).getActualTypeArguments()[0];
      System.out.println(Bar);
    }
  }
}

class Foo {
  List<Bar> getBars() {
    return Collections.emptyList();
  }
}

class Bar {
}

Prints class com.package.Bar

If on the other hand you had a class like this:

class Foo<T> {
  List<T> getBars() {
    return Collections.emptyList();
  }
}

Then you could not get T for instances of Foo at runtime. There exists workarounds, where people create anonymous subclasses with the type parameter fixed.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Is it possible in Java to find out the generic type of a collection getter via reflection?

From Dev

Java Reflection: Invoking Setter and Getter method for collection type Object

From Dev

How can a generic base type be cast via reflection in Java?

From Dev

How can a generic base type be cast via reflection in Java?

From Dev

Getting generic type of super interface via reflection

From Dev

find type implementing generic interface through reflection

From Dev

Reflection to find out if property is of option type

From Dev

Reflection to find out if property is of option type

From Dev

Java get generic type of collection

From Dev

Java Reflection, extract generic type from method

From Dev

Java Reflection, extract generic type from method

From Dev

Multiple methods via reflection when implementing a method with generic return type

From Dev

What is a reliable way to find a Scala type's companion object via Java reflection?

From Dev

How to find out the generic type passed to the parent

From Dev

Find specific property with reflection by examining its generic type

From Dev

Reflection subclass of generic type

From Dev

Java Collection<Generic Type> Sorting without Collections

From Dev

Understanding Java bound generic type within collection

From Dev

Java Collection<Generic Type> Sorting without Collections

From Dev

Understanding Java bound generic type within collection

From Dev

How to determine the class type of generic property in java without reflection?

From Dev

Passing Subclass Type to Java Generic Method with reflection throws no Exception

From Dev

Instantiate a Generic class via Reflection

From Dev

Handling generic properties via reflection

From Dev

Reflection.Emit with generic type = type is not generic

From Dev

Is it possible to extend the generic type to multiple classes in Java?

From Dev

List of a list of generic type in Java possible?

From Dev

Is it possible to refactor generic parameter constraints out of the type/function definition?

From Dev

Is it possible to refactor generic parameter constraints out of the type/function definition?

Related Related

  1. 1

    Is it possible in Java to find out the generic type of a collection getter via reflection?

  2. 2

    Java Reflection: Invoking Setter and Getter method for collection type Object

  3. 3

    How can a generic base type be cast via reflection in Java?

  4. 4

    How can a generic base type be cast via reflection in Java?

  5. 5

    Getting generic type of super interface via reflection

  6. 6

    find type implementing generic interface through reflection

  7. 7

    Reflection to find out if property is of option type

  8. 8

    Reflection to find out if property is of option type

  9. 9

    Java get generic type of collection

  10. 10

    Java Reflection, extract generic type from method

  11. 11

    Java Reflection, extract generic type from method

  12. 12

    Multiple methods via reflection when implementing a method with generic return type

  13. 13

    What is a reliable way to find a Scala type's companion object via Java reflection?

  14. 14

    How to find out the generic type passed to the parent

  15. 15

    Find specific property with reflection by examining its generic type

  16. 16

    Reflection subclass of generic type

  17. 17

    Java Collection<Generic Type> Sorting without Collections

  18. 18

    Understanding Java bound generic type within collection

  19. 19

    Java Collection<Generic Type> Sorting without Collections

  20. 20

    Understanding Java bound generic type within collection

  21. 21

    How to determine the class type of generic property in java without reflection?

  22. 22

    Passing Subclass Type to Java Generic Method with reflection throws no Exception

  23. 23

    Instantiate a Generic class via Reflection

  24. 24

    Handling generic properties via reflection

  25. 25

    Reflection.Emit with generic type = type is not generic

  26. 26

    Is it possible to extend the generic type to multiple classes in Java?

  27. 27

    List of a list of generic type in Java possible?

  28. 28

    Is it possible to refactor generic parameter constraints out of the type/function definition?

  29. 29

    Is it possible to refactor generic parameter constraints out of the type/function definition?

HotTag

Archive