Skip to content

Commit

Permalink
Merge pull request #922 from aSemy/patch-1
Browse files Browse the repository at this point in the history
value class check - catch KotlinReflectionInternalError
  • Loading branch information
Raibaz committed Sep 18, 2022
2 parents 1f2d038 + 8bacef4 commit 0e8be81
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 1 deletion.
Expand Up @@ -6,6 +6,7 @@ import kotlin.reflect.full.declaredMemberProperties
import kotlin.reflect.full.primaryConstructor
import kotlin.reflect.jvm.isAccessible
import kotlin.reflect.jvm.javaField
import kotlin.reflect.jvm.internal.KotlinReflectionInternalError


actual object ValueClassSupport {
Expand Down Expand Up @@ -61,6 +62,8 @@ actual object ValueClassSupport {
private val <T : Any> KClass<T>.isValue_safe: Boolean
get() = try {
this.isValue
} catch (_: KotlinReflectionInternalError) {
false
} catch (_: UnsupportedOperationException) {
false
}
Expand Down
4 changes: 3 additions & 1 deletion test-modules/client-tests/build.gradle.kts
Expand Up @@ -3,7 +3,9 @@ plugins {
}

kotlin {
jvm()
jvm {
withJava()
}

sourceSets {
val commonMain by getting {
Expand Down
@@ -0,0 +1,10 @@
package io.mockk.core;

/** @see ValueClassSupport */
public enum JavaEnum {
A {
public String toString() {
return "aaa";
}
}
}
@@ -0,0 +1,27 @@
package io.mockk.core

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


class ValueClassSupportTest {

/** https://github.com/mockk/mockk/issues/868 */
@Test
fun `verify Java class does not cause KotlinReflectionInternalError`() {
val mock = mockk<MockTarget> {
every { func() } returns JavaEnum.A
}

val result = mock.func() // check this doesn't throw KotlinReflectionInternalError

assertEquals(JavaEnum.A, result)
}

}

private class MockTarget {
fun func(): JavaEnum = JavaEnum.A
}

0 comments on commit 0e8be81

Please sign in to comment.