lundi 19 octobre 2020

Java get the second generic types

I'm struggling to get the type of second generic from an object.

The abstract class takes two Generic types T and S

abstract class Concept<T, S> {
    
    public Concept() { 
     //do nothing
    }

    public final Class<?> getTypeParam() {
    
        ParameterizedType parameterizedType = (ParameterizedType) getClass().getGenericSuperclass();

        Class<?> result = (Class<?>) parameterizedType.getActualTypeArguments()[0];

        return result;
    }               
}

In this derivative class one (in this case T) generic is defined:

public class Decision<S> extends Concept<String, S>{
    
    public Decision () {
        super();
        System.out.println(getTypeParam()); //returns the first parameterized type. How do I get the second one?
    }       
}

When I now run it I get the first parmerized generic back. Great. But how do I get out the second one?

public class Main {

    public static void main(String[] args){

        Decision<Boolean> myBooleanDecision = new Decision<>();
    }
}




Aucun commentaire:

Enregistrer un commentaire