How to call java method taking parameter as List<Class<?>> from Scala

sooper :

I am trying to call a java method which takes List<Class<?>> from scala.

The compilation fails with

type mismatch;
 found   : java.util.List[Class[T]] where type T <: Person.type
 required: java.util.List[Class[_]]

I tried using JavaConverters but get the same error.

Java method:

void registerClasses(List<Class<?>> var1);

Calling from Scala:

def registerEntities() = registry.registerClasses(List(Person.getClass).asJava)

Alexey Romanov :

The problem is that the inferred type for Person.getClass is not the one you want. You need to specify the expected type, and this can be done by an explicit type parameter

registry.registerClasses(List[Class[_]](Person.getClass).asJava)

(I don't understand why type ascription doesn't work here:

registerClasses(List(Person.getClass: Class[_]).asJava)

type mismatch;
 found   : java.util.List[Class[_$2]] where type _$2
 required: java.util.List[Class[_]]
Note: Class[_$2] <: Class[_], but Java-defined trait List is invariant in type E.
You may wish to investigate a wildcard type such as `_ <: Class[_]`. (SLS 3.2.10)

So far as I can tell it should.)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=69692&siteId=1