0

尝试在改造服务中使用数据类作为结果类型时收到以下错误。

11-15 11:35:38.345 14693-14693/com.example.app E/AndroidRuntime: FATAL EXCEPTION: main
        Process: com.example.app, PID: 14693
        kotlin.reflect.jvm.internal.KotlinReflectionInternalError: Incorrect resolution sequence for Java constructor public constructor AuthenticationResponse() defined in com.example.app.data.auth.AuthenticationResponse[JavaClassConstructorDescriptor@6d57dc3] (kotlin.reflect.jvm.internal.impl.load.java.structure.reflect.ReflectJavaClass: class com.example.app.data.auth.AuthenticationResponse)
            at kotlin.reflect.jvm.internal.RuntimeTypeMapper.mapSignature(RuntimeTypeMapper.kt:202)
            at kotlin.reflect.jvm.internal.KFunctionImpl.<init>(KFunctionImpl.kt:46)
            at kotlin.reflect.jvm.internal.KClassImpl$Data$constructors$2.invoke(KClassImpl.kt:87)
            at kotlin.reflect.jvm.internal.KClassImpl$Data$constructors$2.invoke(KClassImpl.kt:39)
            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.getConstructors(Unknown Source:7)
            at kotlin.reflect.jvm.internal.KClassImpl.getConstructors(KClassImpl.kt:192)
            at kotlin.reflect.full.KClasses.getPrimaryConstructor(KClasses.kt:40)
            at com.squareup.moshi.KotlinJsonAdapterFactory.create(KotlinJsonAdapter.kt:160)
            at com.squareup.moshi.Moshi.adapter(Moshi.java:100)
            at retrofit2.converter.moshi.MoshiConverterFactory.responseBodyConverter(MoshiConverterFactory.java:90)
            at retrofit2.Retrofit.nextResponseBodyConverter(Retrofit.java:328)
            at retrofit2.Retrofit.responseBodyConverter(Retrofit.java:311)
            at retrofit2.ServiceMethod$Builder.createResponseConverter(ServiceMethod.java:735)
            at retrofit2.ServiceMethod$Builder.build(ServiceMethod.java:168)
            at retrofit2.Retrofit.loadServiceMethod(Retrofit.java:169)
            at retrofit2.Retrofit$1.invoke(Retrofit.java:146)
            at java.lang.reflect.Proxy.invoke(Proxy.java:913)
            at $Proxy4.credentialsLogin(Unknown Source)
            at com.example.app.data.rest.service.AuthenticationService$DefaultImpls.credentialsLogin$default(AuthenticationService.kt:37)
            at com.example.app.ui.module.auth.AuthenticatorActivity.login(AuthenticatorActivity.kt:204)
            at com.example.app.ui.module.auth.AuthenticatorActivity.access$login(AuthenticatorActivity.kt:37)
            at com.example.app.ui.module.auth.AuthenticatorActivity$onCreate$4.onEditorAction(AuthenticatorActivity.kt:107)
            at android.widget.TextView.onEditorAction(TextView.java:5909)
            at com.android.internal.widget.EditableInputConnection.performEditorAction(EditableInputConnection.java:138)
            at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:360)
            at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:85)
            at android.os.Handler.dispatchMessage(Handler.java:105)
            at android.os.Looper.loop(Looper.java:164)
            at android.app.ActivityThread.main(ActivityThread.java:6541)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

老实说,我什至不确定这个错误是什么意思,也没有找到很多关于它的资源。类描述相当模糊:

Kotlin 反射已达到无法恢复的不一致状态的信号。

唯一使用反射的地方是在 Moshi 内,但我不知道是什么导致了这个问题。


代码

这是数据类:

data class AuthenticationResponse(
    @Json(name = "access_token) val accessToken: String,
    @Json(name = "refresh_token) val refreshToken: String,
    @Json(name = "user_id") val userId: String,
    val scope: String
)

AuthenticationService:_

interface AuthenticationService {

    @FormUrlEncoded
    @POST("oauth/token")
    fun credentials(
        @Field("username") username: String,
        @Field("password") password: String,
        @Field("scope") scope: String,
        @Field("grant_type") grantType: String = "password"
    ): Flowable<AuthenticationResponse>

    @FormUrlEncoded
    @POST("oauth/token")
    fun refreshToken(
        @Field("refresh_token") refreshToken: String,
        @Field("scope") scope: String,
        @Field("grant_type") grantType: String = "refresh_token"
    ): Flowable<AuthenticationResponse>

}

我使用 Moshi 作为 JSON 转换器,我已经KotlinJsonAdapterFactoryMoshi.Builder.


更新

通过尝试AuthenticationResponse直接使用 Moshi 创建适配器,我可以可靠地重现错误。

val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory()).build()
val json = "{\"user_id\": 000000," +
        "\"access_token\": \"testAccessToken\"," +
        "\"expires_in\": 3600," +
        "\"token_type\": \"Bearer\"," +
        "\"refresh_token\": \"testRefreshToken\"," +
        "\"scope\": \"test\"}"

val adapter = moshi.adapter<AuthenticationResponse>(AuthenticationResponse::class.java)

val response = adapter.fromJson(json)

Timber.d("response=$response")

KotlinReflectionInternalError上面的代码在尝试创建adapter值时同样失败。

4

0 回答 0