Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring inject list of generic interface implementations in kotlin

Disclaimer: New to Kotlin, may be a easy to solve issue or misunderstood basics.

I am trying to inject a List of a specific interface's Spring implementations, in a regular java class this have been easy like this:

@Autowired
List<IMyClass> myClassList;

But in Kotlin doing the following gives me a error

@Autowired
lateinit private var myClassList: List<IMyClass<Any>>

// No beans of '? extends IMyClass<Object>' or 'List<? extends IMyClass<Object>>' types found

Doing it like this:

@Autowired
lateinit private var myClassList: List<IMyClass<*>>

Makes the injection work but doesn't allow me to use a function defined in the interface that takes a generic object as input

// Out-projected type 'IMyClass<*>' prohibits the use of 'public abstract fun myFunction(obj: T!): T! defined in com.path.IMyClass'

So how am I supposed to solve this? Is it easier to rewrite the interface in Kotlin with some specific syntac?

like image 650
Jeppz Avatar asked Jan 18 '18 11:01

Jeppz


2 Answers

Thing you're doing in Java is just using implicit wildcard. So you have 2 ways here:

  1. Try to refactor API and inject list of List<IMyClass<? extends SomeInterface>>
  2. Use injected List<IMyClass<*>> but cast it explicitly to thing you need, i.e. myClassList as List<IMyClass<Any>>

Thing here is kotlin's erasure is more explicit. If you don't know what type is there — we can't guarantee your code will work, because there is such type in kotlin as Nothing, instance of which can't exist.

like image 98
asm0dey Avatar answered Nov 03 '22 06:11

asm0dey


I faced a similar situation and as answered by asm0dey, casting was solution which I felt was better for me, but doing it in a cleaner way was a concern.

Just answering below how exactly I did it in the best possible way that I could think of:

@Service
class MyService(_myClassList: List<IMyClass<out Any>>) {

    @Suppress("UNCHECKED_CAST")
    val myClassList: List<IMyClass<Any>> = _myClassList.map { it as IMyClass<Any> }

    // ...
}
like image 38
Sahil Chhabra Avatar answered Nov 03 '22 07:11

Sahil Chhabra