Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

140
Views
Retrieve the classes used within an instance of a Generic Type using Reflection

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.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

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.

over 4 years ago · Santiago Trujillo Report

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
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!