jeudi 15 juin 2017

How to properly query for a Constructor with an argument that is generic

I need to acquire the Constructor of a class at runtime using reflection instead of direct access. For simple types, this is trivial:

public class MyType {
    public MyType(String a, Integer b, Long c) {}
}

Constructor constructor = MyType.class.getConstructor(String.class, Integer.class, Long.class);

But if the class uses generics as part of its arguments, it's not clear what I should put:

public class MyType {
    public MyType(Set<String> a, Integer b, List<Long> c) {}
}

Constructor constructor = MyType.class.getConstructor(Set<String>.class /*Doesn't compile*/, Integer.class, List<Long>.class /*doesn't compile*/);

I can write something like MyType.class.getConstructor(Set.class, Integer.class, List.class); but it's not clear that that will achieve the behavior I want. What's the correct way to write this kind of code?





Aucun commentaire:

Enregistrer un commentaire