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

Conversation

aSemy
Copy link
Contributor

@aSemy aSemy commented Sep 16, 2022

might fix #868 caused by #913? not caused by #913, it seems KotlinReflectionInternalError was never caught...

This is a draft because I want to make a test for this to make sure it doesn't happen again. See https://youtrack.jetbrains.com/issue/KT-41373 for reproductions. Test added

@aSemy aSemy marked this pull request as draft September 16, 2022 10:27
@krissrex
Copy link

krissrex commented Sep 16, 2022

Some help for creating a test case:

What triggered the issue for me was using spyk(myInstance), and somewhere inside myInstance.aFunction
a function returned a functional interface instance:

  private fun handleFailure(request: Supplier<MyResponse>):
Supplier<Either<SomeDeviation, MyResponse>>
return Supplier {
      val response = request.get()
      checkThatResponseIsNotFailure(response)
    }

where it is defined as

@FunctionalInterface
public interface Supplier<T> {

    /**
     * Gets a result.
     *
     * @return a result
     */
    T get();
}

The equivalent in kotlin to @FunctionalInterface is fun interface.

It crashed on the return line with Supplier { }.

kotlin.reflect.jvm.internal.KotlinReflectionInternalError: Unresolved class: class com.mypackage.aproject.MyClass$$Lambda$607/0x000000080103d4c8

	at kotlin.reflect.jvm.internal.KClassImpl.reportUnresolvedClass(KClassImpl.kt:328)
	at kotlin.reflect.jvm.internal.KClassImpl.access$reportUnresolvedClass(KClassImpl.kt:44)
	at kotlin.reflect.jvm.internal.KClassImpl$Data$descriptor$2.invoke(KClassImpl.kt:56)
	at kotlin.reflect.jvm.internal.KClassImpl$Data$descriptor$2.invoke(KClassImpl.kt:48)
	at kotlin.reflect.jvm.internal.ReflectProperties$LazySoftVal.invoke(ReflectProperties.java:93)
	at kotlin.reflect.jvm.internal.ReflectProperties$Val.getValue(ReflectProperties.java:32)
	at kotlin.reflect.jvm.internal.KClassImpl$Data.getDescriptor(KClassImpl.kt:48)
	at kotlin.reflect.jvm.internal.KClassImpl.getDescriptor(KClassImpl.kt:182)
	at kotlin.reflect.jvm.internal.KClassImpl.isValue(KClassImpl.kt:290)
	at io.mockk.core.ValueClassSupport.isValue_safe(ValueClassSupport.kt:63)
	at io.mockk.core.ValueClassSupport.getBoxedValue(ValueClassSupport.kt:20)
	at io.mockk.proxy.jvm.advice.Interceptor.call(Interceptor.kt:22)

I used mockk-jvm version 1.12.8.

And my code works if I downgrade to mockk version 1.12.4.

@aSemy
Copy link
Contributor Author

aSemy commented Sep 16, 2022

Amazing, thanks @krissrex - I will give it a go

@epli2
Copy link

epli2 commented Sep 16, 2022

@aSemy
Hello. I have created another case that reproduces the problem.

public enum JavaEnum {
    A {
        public String toString() {
            return "aaa";
        }
    }
}
class MockTarget {
    fun func(): JavaEnum = JavaEnum.A
}

fun main() {
    val mock = mockk<MockTarget>()
    every { mock.func() } returns JavaEnum.A

    mock.func() // throws KotlinReflectionInternalError
}
Exception in thread "main" kotlin.reflect.jvm.internal.KotlinReflectionInternalError: Unresolved class: class example.JavaEnum$1
	at kotlin.reflect.jvm.internal.KClassImpl.reportUnresolvedClass(KClassImpl.kt:328)
	at kotlin.reflect.jvm.internal.KClassImpl.access$reportUnresolvedClass(KClassImpl.kt:44)
	at kotlin.reflect.jvm.internal.KClassImpl$Data$descriptor$2.invoke(KClassImpl.kt:56)
	at kotlin.reflect.jvm.internal.KClassImpl$Data$descriptor$2.invoke(KClassImpl.kt:48)
	at kotlin.reflect.jvm.internal.ReflectProperties$LazySoftVal.invoke(ReflectProperties.java:93)
	at kotlin.reflect.jvm.internal.ReflectProperties$Val.getValue(ReflectProperties.java:32)
	at kotlin.reflect.jvm.internal.KClassImpl$Data.getDescriptor(KClassImpl.kt:48)
	at kotlin.reflect.jvm.internal.KClassImpl.getDescriptor(KClassImpl.kt:182)
	at kotlin.reflect.jvm.internal.KClassImpl.isValue(KClassImpl.kt:290)
	at io.mockk.core.ValueClassSupport.isValue_safe(ValueClassSupport.kt:63)
	at io.mockk.core.ValueClassSupport.getBoxedValue(ValueClassSupport.kt:20)
	at io.mockk.proxy.jvm.advice.Interceptor.call(Interceptor.kt:22)
	at example.MockTarget.func(Main.kt:7)
	at example.MainKt.main(Main.kt:14)
	at example.MainKt.main(Main.kt)

https://github.com/epli2/mockk868

@aSemy
Copy link
Contributor Author

aSemy commented Sep 16, 2022

That's really helpful, thanks @epli2

Ready for review and merging @Raibaz!

@aSemy aSemy marked this pull request as ready for review September 16, 2022 14:49
@Raibaz Raibaz merged commit 0e8be81 into mockk:master Sep 18, 2022
@Raibaz
Copy link
Collaborator

Raibaz commented Sep 18, 2022

Thanks a lot @aSemy and @epli2 for looking into this!

@vojkny
Copy link

vojkny commented Sep 28, 2022

can we release this? 🙏

@guuilp
Copy link

guuilp commented Sep 28, 2022

can we release this? 🙏

It was released 5 days ago on 1.13.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1.12.5 fails to every certain java classes
6 participants