0

I am currently working on some reflexion stuff and I'm just a beginner at it.

I got this :

List<X> list;
Set<Class<? extends X>> set;

And I want to do something like this :

for (Class<? extends X> classx : set) list.add(classx);

But i couldn't find anything on how to achieve it. Can someone help me?

  • 3
    `X` and `Class extends X>` are very different types. As written, you need at least to create a new instance of `Class extends X>` using reflection. Is your question about how to create an instance when you have a `Class>`? – Oleg Estekhin Aug 14 '17 at 15:11
  • Possible duplicate of [Creating an instance using the class name and calling constructor](https://stackoverflow.com/questions/6094575/creating-an-instance-using-the-class-name-and-calling-constructor) – Oleg Estekhin Aug 14 '17 at 15:14

1 Answers1

0

You can't achieve such behavior with that input data. List<X> list expected the instance of class X, but you're trying to add the object of Type Class<? extends X>. Let's imagine that type X is Number(for simplicity), we have Double, Integer, Character and etc, children of Number. So, you're doing the next thing: You want to add the object of type Class<Integer> to list which can hold only Numbers(not a Class<Integer>, Class<Double> and so on). The only thing you can do, it's create the instance of Class<Integer>, Class<Double> this way, using reflection:

List<X> list = //some values;
Set<Class<? extends X>> set = //some values;
for (Class<? extends X> classx : set) list.add(classx.newInstance());

But that's so bad idea to use reflection in such way. Maybe you meant something this:

List<Class<X>> list = //some values;
Set<Class<? extends X>> set = //some values;
for (Class<? extends X> classx : set) list.add(classx);

Because still the Class<X> is not the same as Class<? extends X>. But maybe you meant something like this:

List<X> list = //some values;
Set<? extends X> set = = //some values;
for (X elem : set) list.add(elem);

Last one looks nice and it should work.

  • But I do need to have a List because I need to invoke some methods on these objects. Having a Set extends X> is exactly what I'm trying to get. – Manon Grivot Aug 14 '17 at 15:53
  • @MaëlGrivot so, let me ask some questions. First of all: do you really have Set> set, maybe you have Set extends X> set ? If 1 variant - first of all you need to instantiate you Class extends X> because in Java variable Class extends X> someVar is the object of type Class extends X>(let's call it Class Object), not the same as object of type ? extends X. The Class Objects have methods for reflection(example newInstance() - which creates from this Class Object - the Object of type which specified in brackets of it.) – Aleksander Melnichnikov Aug 14 '17 at 16:07
  • Yes I do have a Set> which contains a list of classes that extends an abstract class. – Manon Grivot Aug 14 '17 at 16:15
  • @MaëlGrivot so, the only option it's to iterate from this set of classes and call method newInstance()(to instantiate that class) https://docs.oracle.com/javase/tutorial/reflect/member/ctorInstance.html - look here. And add them to your result list - if it's your task. Maybe you can share the idea: what are trying to implement?(I mean the main task) – Aleksander Melnichnikov Aug 14 '17 at 16:20