Kotlin confuses which Spring method to call: ``` &...
# announcements
s
Kotlin confuses which Spring method to call:
Copy code
<T> T getBean(String name, Class<T> requiredType) throws BeansException;
	Object getBean(String name, Object... args) throws BeansException;
I want to call the former, but Kotlin calls the later and then flags my implementation as not guaranteeing the specified type
Copy code
fun worker(worker: String): PipelineElementProcessor = try {
    context.getBean(worker, PipelineElementProcessor::javaClass)
}
Is there as solution apart from casting the result to PipelineElementProcessor?
r
Maybe
context.getBean<PipelineElementProcessor>(worker, PipelineElementProcessor::javaClass)
l
What if you use named argument for requiredType?
Nevermind, that's Java code and wouldn't work.
s
@ribesg that doesn’t even compile (“no type argument expected”). to be fair, that was my first instinct as well 😆. I’ve currently resigned to using
context.getBean(worker) as PipelineElementProcessor
but am still open to suggestions.
l
Is it possible to create extension functions for context which call the proper getBean functions?
o
since the 1st
getBean
has a parameterized type, would calling it as
context.getBean<PipelineElementProcessor>(...)
work?
s
nope -> compile msg: “no type argument expected”
o
weird. message makes me wonder if it is even seeing the method you want?
s
it seems to be shadowed by the generic one.
o
try
PipelineElementProcessor::class.java
instead of
::javaClass
I just tried it in a mock here, it worked, but with
::javaClass
I had the same problem you did.
seems like
javaClass
shouldn't be invoked by reference, but instead on an instance of a class, since it uses the receiver to determine the type parameter.
s
yep,
context.getBean(worker, PipelineElementProcessor::class.java)
works 😃