Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

value class check - catch KotlinReflectionInternalError #922

Merged
merged 3 commits into from Sep 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
}