0

Suppose to have a type Pair<K,V> declared in your package. Given an object instance of:

 Pair<Integer, Integer> obj = new Pair<>(1,23);

I want to retrieve the type arguments Integer and Integer to which K and V are respectively associated. By the way, it seems to be that using Java standard reflection I cannot access to the actual class with the instances within Java.

 TypeVariable<? extends Class<?>>[] parameters = obj.getClass().getTypeParameters();

I did not manage to extract such desired information using such parameters. I start to wonder, maybe the type information is kept only at compile time, while at run time the actual type parameter information is removed.

6
  • So, you just want the type of the type argument in the of the Pair object which in your example Integer ? Commented Apr 29, 2017 at 21:56
  • Exactely. I just manage to retrieve K and V from the type parameters, but I do not get the actual classes which are associated to such paramteres. Commented Apr 29, 2017 at 21:57
  • Is is always a primitive-like type (i.e. Integer, String ..etc) or it could be of an Object Type (e.g. Class Person )? Commented Apr 29, 2017 at 22:02
  • It could be even an Object Type. Commented Apr 29, 2017 at 22:04
  • 1
    Possible duplicate of stackoverflow.com/questions/3403909/… Commented Apr 29, 2017 at 22:14

2 Answers 2

1

If you just have an instance, as you exemplified: Pair<Integer, Integer> pair = new Pair<>(1,23), there's no way to get the static generic types as that information is lost in runtime.

If, on the other hand, Pair<Integer, String> was the type of a field, method return type, the type of a method parameter, or if the generic type arguments came from the superclass, it would be possible to extract the static generic types.

Check my answer here for examples on extracting generic types in these cases.

Short of that, you can only check the dynamic types of the values in runtime, as explained by Yahya.

Sign up to request clarification or add additional context in comments.

Comments

0

Please consider this answer:

public static String[] getArgumentType(Object obj){
    String[] type = new String[2];
    type[0]=((Pair<?, ?>) obj).getKey().getClass().getName().replace("java.lang.", "");
    type[1]=((Pair<?, ?>) obj).getValue().getClass().getName().replace("java.lang.", "");
    return type;
}

 public static void main(String[] args) {
    Pair<String,Integer> obj = new Pair<String,Integer>("", 2);
    String[] type = getArgumentType(obj);
    System.out.println(type[0] + ", " + type[1]);
    Pair<Integer,Integer> obj1 = new Pair<Integer,Integer>(1, 2);
    type = getArgumentType(obj1);
    System.out.println(type[0] + ", " + type[1]);
    Pair<Double,Float> obj2 = new Pair<Double,Float>(1.1, 2.2f);
    type = getArgumentType(obj2);
    System.out.println(type[0] + ", " + type[1]);
    Pair<Person,Object> obj3 = new Pair<Person,Object>(new Person(), new Object());
    type = getArgumentType(obj3);
    System.out.println(type[0] + ", " + type[1]);
}

The Output:

String, Integer
Integer, Integer
Double, Float
Person, Object

2 Comments

Since in my context-application I do not need to have the actual class is ok… but, is there no way to get the same result by using the reflection, that is more clean than parsing a string?
Plus, Pair is just an example, and I could even have different and other types of classes to which I do not know the association between the type parameter and the getter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.