ansman/kotshi

Factory issues with kotshi 2.0-rc1

mjmacleod opened this issue · 9 comments

Using kotshi:1.0.6 the following works

@KotshiJsonAdapterFactory
abstract class ApplicationJsonAdapterFactory : JsonAdapter.Factory {
    companion object {
        val INSTANCE: ApplicationJsonAdapterFactory = KotshiApplicationJsonAdapterFactory()
    }
}
...
moshi = Moshi.Builder().add(ApplicationJsonAdapterFactory.INSTANCE).build()

If I upgrade to kotshi:2.0-rc1 it no longer works Expression 'KotshiApplicationJsonAdapterFactory' of type 'KotshiApplicationJsonAdapterFactory' cannot be invoked as a function. The function 'invoke()' is not found

If I follow the latest in the README:

@KotshiJsonAdapterFactory
object ApplicationJsonAdapterFactory : JsonAdapter.Factory by KotshiApplicationJsonAdapterFactory
 ...
 moshi = Moshi.Builder().add(ApplicationJsonAdapterFactory).build()

This also doesn't work Unresolved reference: KotshiApplicationJsonAdapterFactory

Is there something I'm missing here?

Kotlin version is 1.3.21
Moshi version is 1.8.0

Man, I had hoped kapt would be better with this now. Anyway, the generated factory is an object in 2.0 so just remove your parentheses in the first example and that will still work

Oh weird, you are right that works (removing parenthesis) even though the IDE flags it as an error and displays it red as a result.

The weird part is that if you switch to the delegate approach now it will probably work

I do seem to get the same error still now if I switch to the delegate approach after successfully building first with the original approach, anyway the first approach is fine so I'll just stick to that - thanks.

Ok, glad you got it to work. Let me know if you find any other issues.

@JakeWharton Do you have any idea of why this keeps happening? Even with delegates Kapt seems really flaky.

I'm considering going for a reflective factory. That way you could easily use reflection for debug.

Is there a kapt bug for it? I suspect that the stub generator is just failing on the missing reference instead of ignoring it.

I haven't found one and I'm not sure what to write if I were to file one since I can't reproduce it consistently.