How to import type extension function in Kotlin - kotlin

I wrote an extension function for Any type, that will retrieve object property value by its name. I want to be able to use it from everywhere in my project. Here is my extension function:
package com.example.core.utils.validation
import java.util.NoSuchElementException
import kotlin.reflect.full.memberProperties
fun Any.getFieldValue(fieldName: String): Any? {
try {
return this.javaClass.kotlin.memberProperties.first { it.name == fieldName }.get(this)
} catch (e: NoSuchElementException) {
throw NoSuchFieldException(fieldName)
}
}
Now I want to use it like this
package com.example.core
import com.example.core.utils.validation.*
class App {
val obj = object {
val field = "value"
}
val fieldValue = obj.getFieldValue("field")
}
But there is Unresolved reference error
How should I make my extension function global and import it anywhere?

You should use import statement instead of second package declaration in your second code snippet.
See documentation: https://kotlinlang.org/docs/reference/extensions.html#scope-of-extensions
And actually I am not sure that it is valid to make extension for Any type. I think in that case you need to call it on object of type Any.

Related

Kotlin: invoke function in companion object (via reflection)

I am trying to notify a central class on startup about the existence of some classes that will be used during runtime. My idea is to use reflection: scan for annotation (or interface implementation) and call a method from the companion's object of those classes.
As suggested in [1] I am using classgraph but I'm totally open to alternatives.
package com.test
import io.github.classgraph.ClassGraph
import io.github.classgraph.ClassInfo
import io.github.classgraph.ScanResult
import kotlin.reflect.KFunction
import kotlin.reflect.jvm.kotlinFunction
#Target(AnnotationTarget.FUNCTION)
#Retention(AnnotationRetention.RUNTIME)
annotation class OnStartup
// a class that will be instantiated a lot of times during runtime
data class SomeClass(val name: String) {
companion object {
#OnStartup
fun onStartup() {
// notify someone at startup about our existence
}
}
}
fun main() {
val scanResult: ScanResult = ClassGraph().enableAllInfo().acceptPackages("com.test").scan()
scanResult
.getClassesWithMethodAnnotation(OnStartup::class.java.name)
.filter { it.isStatic }
.flatMap { findStartupMethods(it) }
.forEach { it.call() }
}
private fun findStartupMethods(classInfo: ClassInfo): List<KFunction<*>> {
return classInfo.methodInfo.filter { function ->
function.hasAnnotation(OnStartup::class.java)
}.mapNotNull { method ->
method.loadClassAndGetMethod().kotlinFunction
}
}
The problem is, that the code exits with
Exception in thread "main" java.lang.IllegalArgumentException: Callable expects 1 arguments, but 0 were provided.
From reading the Kotlin Docs and [2] my guess is that I should hand over the companionObjectInstance as a parameter. But I have absolutely no idea how to get it...
Any help is really appreciated.
[1] Getting a list of annotated functions in Kotlin using reflection
[2] Kotlin invoke companion function with reflection
Maybe it looks ugly, but it works...
it.call((it.parameters[0].type.classifier as KClass<*>).objectInstance)

How to write parametric/generic functions in kotlin

I'm trying to find a workaround to make a Spring Reactive Webclient work with JDBC.
Here is were I got the idea from: https://gitorko.github.io/2019/04/02/Spring-Webflux-Reactive-JDBC/.
I'm writing a service that calls the jdbc repository interface and instead of returning the type of my domain object MyClass returns a Mono<MyClass> like this:
//other specific imports here
import org.springframework.stereotype.Service
import reactor.core.publisher.Mono
import reactor.core.scheduler.Scheduler
import reactor.core.scheduler.Schedulers
import java.util.concurrent.Callable
#Service
class MyClassService(val repo: MyClassRepository, val jdbcScheduler: Scheduler){
fun save(obj: MyClass?): Mono<MyClass?>? {
return asyncCallable { repo.save(obj) }
}
protected fun <S> asyncCallable(callable: Callable<S>?): Mono<S>? {
return Mono.fromCallable(callable).subscribeOn(Schedulers.parallel()).publishOn(jdbcScheduler)
}
}
//this is a jdbc repository
interface MyClassRepository : CrudRepository<MyClass, UUID> {}
Now the problem is that calling asyncCallable { repo.save(obj) } returns the compile error inferred type is MyClass? but TypeVariable(S) was expected and Mono.fromCallable(callable).subscribeOn(Schedulers.parallel()).publishOn(jdbcScheduler) returns the compile error inferred type is Callable<S>? but Callable<out TypeVariable(T)!> was expected.
I understand by reading about kotlin generics that this has to do with the variance. If I'm not wrong the function asyncCallableis invariant on the generic type Sand in this case covariance is required?
I think the syntax you need is asyncCallable(Callable { repo.save(obj) }).
Complete example:
#Service
class MyClassService(val repo: MyClassRepository, val jdbcScheduler: Scheduler){
fun save(obj: MyClass): Mono<MyClass?>? {
return asyncCallable(Callable { repo.save(obj) })
}
protected fun <S> asyncCallable(callable: Callable<S>): Mono<S>? {
return Mono.fromCallable(callable).subscribeOn(Schedulers.parallel()).publishOn(jdbcScheduler)
}
}
I'd also remove the ?s, but I left them to keep it as close to your code as possible.

How do you Mockk a Kotlin top level function?

Mockk allows mocking static functions, but how does one mock a Kotlin top level function?
For example, if I have a Kotlin file called HelloWorld.kt, how do I mock the sayHello() function?
HelloWorld.kt
fun sayHello() = "Hello Kotlin!"
The following syntax has worked to me.
mockkStatic(::sayHello.javaMethod!!.declaringClass.kotlin)
I'm surprised there is nothing on the jvm-stdlib yet for this.
Edit:
This overload has now been introduced officially:
https://github.com/mockk/mockk/pull/518
mockkStatic(::sayHello)
There is way to mockk a top level function:
mockkStatic("pkg.FileKt")
every { fun() } returns 5
You just need to know which file this function goes. Check in JAR or stack trace.
To add on previous answers this is working:
mockkStatic("pkg.FileKt")
every { fun() } returns 5
Where mockStatic takes as an argument "package_name:class_file_name"
But to simplify the mockStatick call you can give your file a name for the compiler with #file:JvmName directly in the file.
HelloWorld.kt
#file:JvmName("hello")
fun sayHello() = "Hello Kotlin!"
HelloWorldTest.kt
mockkStatic("pkg.hello")
every { fun() } returns 5
More detailed explication on why this is necessary and other examples here:https://blog.kotlin-academy.com/mocking-is-not-rocket-science-mockk-advanced-features-42277e5983b5
Building on #Sergey's answer:
You could have the actual implementation of the sayHello() function in a variable that's then the default value of a function parameter to sayHello().
This example works:
package tests
import io.mockk.every
import io.mockk.mockk
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
val sayHelloKotlin = { "Hello Kotlin!" }
fun sayHello(producer: () -> String = sayHelloKotlin): String = producer()
class Tests {
interface Producer {
fun produce(): String
}
#Test
fun `Top level mocking`() {
val mock = mockk<Producer>()
every { mock.produce() } returns "Hello Mockk"
val actual = sayHello(mock::produce)
Assertions.assertEquals(actual, "Hello Mockk")
}
}
The problem with this is that you're changing production code just to cater for testing, and it feels contrived.
This code doesn't work for me with mockk version 1.10.0 but works well in 1.11.0 (of course need to change mockkStatic(::bar) )
Utils.kt
#file:JvmName("UtilsKt")
package com.example.myapplication
fun foo(): Boolean {
return bar()
}
fun bar():Boolean {
return false
}
Test
#RunWith(RobolectricTestRunner::class)
#Config(sdk = [Build.VERSION_CODES.O_MR1])
class ExampleUnitTest {
#Test
fun addition_isCorrect() {
mockkStatic("com.example.myapplication.UtilsKt")
every { bar() } returns true
assertTrue(foo())
}
}

How mock Kotlin extension function in interface?

I have an extension function for interface like the following:
import javax.jms.ConnectionFactory
fun ConnectionFactory.foo() {
println("do some stuff")
}
How can I mock the function foo?
Please note, I have seen approaches for classes and objects in http://mockk.io/#extension-functions, but it does not work. I have tried this one:
import io.mockk.classMockk
import io.mockk.every
import org.junit.Test
import javax.jms.ConnectionFactory
class ExtensionFunctionTest {
#Test
fun mockExtensionFunction() {
val connectionFactory = classMockk(ConnectionFactory::class)
every { connectionFactory.foo() } returns println("do other stuff")
connectionFactory.foo()
}
}
It throws exception:
io.mockk.MockKException: Missing calls inside every { ... } block.
According to the documentation in case of module wide extension functions you need to staticMock "hidden" class created for an extension function.
Here is an example (assuming the file name is com/sample/extmockingtest/SampleTest.kt):
fun <T> Iterable<T>.foo(): String = "do some stuff"
class ExtensionFunctionTest {
#Test
fun mockExtensionFunction() {
val itMock = classMockk(Iterable::class);
staticMockk("com.sample.extmockingtest.SampleTestKt").use {
every {
itMock.foo()
} returns "do other stuff"
assertEquals("do other stuff", itMock.foo())
verify {
itMock.foo()
}
}
}
}

Why doesn't Kotlin let me use writeText extension?

I have a Kotlin class with a method, which creates some text and then I want to write it to a file:
import java.io.File
import java.util.*
import kotlin.io.*
class MyClass {
fun run() {
val result = html {
head {
title { +"Entry page" }
}
body {
h1 {
+"Map"
}
+"Some HTML code"
}
}
File("target/wdef/index.html").writeText(result)
}
}
I get an error - the writeText(result) is highlighted red and I get the error message Error:(26, 40) Kotlin: Unresolved reference: writeText.
How can I fix it?
A problem might be that you pass a wrong type to writeText. It requires String, but you pass an html building object HTML. Try to convert it with toString:
File("target/wdef/index.html").writeText(result.toString())