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
  • Loading branch information
stuebingerb committed Sep 7, 2022
1 parent 6c571a6 commit 6c2b269
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
Expand Up @@ -105,6 +105,12 @@ internal class ProxyMaker(
} else if (interfaces.isEmpty() && !Modifier.isAbstract(clazz.modifiers) && inliner != null) {
log.trace("Taking instance of $clazz itself because it is not abstract and no additional interfaces specified.")
clazz
} else if (clazz.kotlin.isSealed) {
log.trace("Taking instance of subclass of $clazz because it is sealed.")
clazz.kotlin.sealedSubclasses.firstNotNullOfOrNull {
@Suppress("UNCHECKED_CAST")
subclass(it.java, interfaces) as Class<T>
} ?: error("Unable to create proxy for sealed class $clazz, available subclasses: ${clazz.kotlin.sealedSubclasses}")
} else {
log.trace(
"Building subclass proxy for $clazz with " +
Expand Down Expand Up @@ -143,7 +149,7 @@ internal class ProxyMaker(
val defaultConstructor = cls.getDeclaredConstructor()
try {
defaultConstructor.isAccessible = true
} catch (ex: Exception) {
} catch (ignored: Exception) {
// skip
}

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 6c2b269

Please sign in to comment.