How to use generic in Class.class
Question: I want to avoid the warning:

"type safety the expression of type needs unchecked conversion to conform to
Class"

From this sentence:

    
    
    Class<MyInterface> cc = interpreter.get("Myclass", Class.class );
    

I have tried:

    
    
    Class<MyInterface> cc = interpreter.get("Myclass", Class<MyInterface>.class );
    

But is invalid.

How can I do that without @SuppressWarnings("unchecked")

The signature of interpreter.get:

    
    
    T interpreter.get(String name, Class<T> javaClass)
    

* * *

The context: I use the library Jython and I define a class in Python who
implement MyInterface, then I capture this class in Java then I create
instances of them. That is why I need the class itself, not a instance of the
class.

The code is something like:

    
    
    PythonInterpreter interpreter = new PythonInterpreter();
    interpreter.exec("from cl.doman.python import MyInterface");
    ....
    interpreter.exec(pythonCode);
    Class<MyInterface> cc = interpreter.get("Myclass", Class.class);
    MyInterface a = (MyInterface) cc.newInstance();
    

My code work fine But I can't suppress the warning.


Answer: Probably

    
    
    Class<?> clazz = interpreter.get("Myclass", Class.class); 
    Class<? extends MyInterface> cc = clazz.asSubclass(MyInterface.class);
    // look, Ma, no typecast!
    MyInterface a = cc.newInstance();
    

