Skip to content

Commit

Permalink
Inline class JVM support (fix #61)
Browse files Browse the repository at this point in the history
  • Loading branch information
SalomonBrys committed Jul 28, 2023
1 parent 4e01d21 commit 776f713
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
12 changes: 11 additions & 1 deletion mockmp-runtime/src/jvmMain/kotlin/org/kodein/mock/platformJvm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,24 @@ internal actual fun References.unsafeValue(cls: KClass<*>): Any? {

Modifier.isAbstract(cls.java.modifiers) -> {
val constructor = cls.java.constructors.first()
constructor.parameterTypes
val parameterValues = Array(constructor.parameterCount) { getReference(constructor.parameterTypes[it].kotlin) }
return ProxyFactory().apply {
superclass = cls.java
isUseCache = true
}.create(constructor.parameterTypes, parameterValues)
}

cls.java.isAnnotationPresent(JvmInline::class.java) -> {
val constructor = cls.java.declaredConstructors[0]
val parameter = getReference(constructor.parameterTypes[0].kotlin)
constructor.isAccessible = true
try {
return constructor.newInstance(parameter)
} finally {
constructor.isAccessible = false
}
}

else -> return ObjenesisStd(true).newInstance(cls.java)
}
}
Expand Down
6 changes: 6 additions & 0 deletions tests/tests-junit4/src/commonMain/kotlin/foo/Types.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ package foo

import data.Data
import data.Direction
import kotlin.jvm.JvmInline


@RequiresOptIn
annotation class ExperimentalTest

typealias FooMap<T> = Map<T, List<Pair<Int, Set<String>>>>

@JvmInline
value class InlineString(val value: String)

interface Foo<out T : Any> {
val roString: String
var rwString: String
Expand Down Expand Up @@ -41,6 +45,8 @@ interface Foo<out T : Any> {
@ExperimentalTest
fun experimentalMethod()

fun doSomethingInline(param: InlineString)

interface Sub {
fun doOp()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -544,4 +544,19 @@ class VerificationTests {
}
}

@Test
fun testInline() {
val foo = MockFoo<Bar>(mocker)

mocker.every {
val i = isAny<InlineString>()
foo.doSomethingInline(i)
} returns Unit

foo.doSomethingInline(InlineString("hello"))

mocker.verify {
foo.doSomethingInline(isAny())
}
}
}

0 comments on commit 776f713

Please sign in to comment.