0

I've seen these two answers:

What I'm actually trying to do is:

    final Class<? extends Xyz<?>> clazz = Class.forName(name).asSubclass(Xyz<?>.class);

but Xyz<?>.class is not valid Java. Is there a magic incantation I should be using?

Community
  • 1
  • 1
Alun
  • 541
  • 6
  • 16

2 Answers2

0

I am not quite sure if I understand your problem.

If you mean, that your code-line is not compiling, you can fix it by doing a cast as you can see in the following example:

package test;

public class Test<T> {

    public static void main(String[] args) throws ClassNotFoundException {
        @SuppressWarnings("unchecked")
        final Class<? extends Test<?>> clazz = (Class<Test<?>>) Class.forName("test.Test").asSubclass(Test.class);
        //... do something with the class
    }

}

How to avoid the warning for an "unchecked"-cast you can read for example here: How do I address unchecked cast warnings?

Community
  • 1
  • 1
Denis Lukenich
  • 3,084
  • 1
  • 20
  • 38
0

Class<? extends Xyz<?>> is meaningful only if Xyz is itself a generic type.

atao
  • 835
  • 6
  • 13