Skip to content

Commit

Permalink
Properly handle sealed classes with Kotlin 1.7 and JDK 17
Browse files Browse the repository at this point in the history
Attempt to fix mockk#832 by making `ObjenesisInstantiator` work with a `KClass`.
  • Loading branch information
stuebingerb committed Sep 6, 2022
1 parent 6c571a6 commit 3c0aeec
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 11 deletions.
Expand Up @@ -14,7 +14,7 @@ internal class OnjenesisInstantiator(val log: MockKAgentLogger) : MockKInstantia
private val objenesis = ObjenesisStd(false)
private val instantiators = Collections.synchronizedMap(WeakHashMap<Class<*>, ObjectInstantiator<*>>())

override fun <T> instance(cls: Class<T>): T {
override fun <T : Any> instance(cls: Class<T>): T {
val clazz = proxyInterface(cls)

log.trace("Creating new empty instance of $clazz")
Expand Down
@@ -1,5 +1,5 @@
package io.mockk.proxy

interface MockKInstantiatior {
fun <T> instance(cls: Class<T>): T
fun <T : Any> instance(cls: Class<T>): T
}
Expand Up @@ -20,11 +20,16 @@ class ObjenesisInstantiator(

private val instantiators = Collections.synchronizedMap(WeakHashMap<Class<*>, ObjectInstantiator<*>>())

override fun <T> instance(cls: Class<T>): T {
if (cls == Any::class.java) {
override fun <T : Any> instance(cls: Class<T>): T {
val kcls = cls.kotlin
if (kcls == Any::class) {
@Suppress("UNCHECKED_CAST")
return Any() as T
} else if (!Modifier.isFinal(cls.modifiers)) {
} else if (kcls.isSealed) {
kcls.sealedSubclasses.firstNotNullOfOrNull { subCls ->
runCatching { instance(subCls.java) }.getOrNull()
} ?: error("could not find subclass for sealed class $cls")
} else if (kcls.isFinal) {
try {
val instance = instantiateViaProxy(cls)
if (instance != null) {
Expand Down
Expand Up @@ -2,15 +2,13 @@ package io.mockk.it

import io.mockk.every
import io.mockk.mockk
import kotlin.test.Ignore
import kotlin.test.Test
import kotlin.test.assertEquals


class SealedClassTest {

@Test
@Ignore("Fails on JDK17+ https://github.com/mockk/mockk/issues/832")
fun serviceReturnsSealedClassImpl() {
val factory = mockk<Factory> {
every { create() } returns Leaf(1)
Expand All @@ -22,7 +20,6 @@ class SealedClassTest {
}

@Test
@Ignore("Fails on JDK17+ https://github.com/mockk/mockk/issues/832")
fun serviceAnswersSealedClassImpl() {
val factory = mockk<Factory> {
every { create() } answers { Leaf(1) }
Expand Down
Expand Up @@ -3,14 +3,12 @@ package io.mockk.it
import io.mockk.every
import io.mockk.mockk
import kotlin.test.Test
import kotlin.test.Ignore
import kotlin.test.assertEquals


class SealedInterfaceTest {

@Test
@Ignore("Fails on JDK17+ https://github.com/mockk/mockk/issues/832")
fun serviceReturnsSealedClassImpl() {
val factory = mockk<Factory> {
every { create() } returns Leaf(1)
Expand All @@ -22,7 +20,6 @@ class SealedInterfaceTest {
}

@Test
@Ignore("Fails on JDK17+ https://github.com/mockk/mockk/issues/832")
fun serviceAnswersSealedClassImpl() {
val factory = mockk<Factory> {
every { create() } answers { Leaf(1) }
Expand Down

0 comments on commit 3c0aeec

Please sign in to comment.