question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't create Set<Interceptor> using Multibindings

See original GitHub issue

Hi!

I trying to use multibinding to provide OkHttp Interceptors for the client.

    @Provides
    @Singleton
    fun provideOkHttpClient(interceptors: Set<Interceptor>): OkHttpClient {
        val builder = OkHttpClient.Builder()
        for (interceptor in interceptors) {
            builder.addNetworkInterceptor(interceptor)
        }
        return builder.build()
    }

    @Provides @IntoSet
    @Singleton
    fun provideLoggingInterceptor(): Interceptor =
            HttpLoggingInterceptor({ message -> Timber.tag("OkHttp").d(message) })

but can’t compile. There is an error:

error: java.util.Set<? extends okhttp3.Interceptor> cannot be provided without an @Provides-annotated method.

When I provide strings in the same way all works. But not with the Interceptor.

What I use: Kotlin 1.1.1 (kapt{generateStubs = true}) , Dagger 2.10

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:12

github_iconTop GitHub Comments

106reactions
tbroyercommented, Mar 28, 2017

Dagger expects a java.util.Set<okhttp3.Interceptor>, not ? extends okhttp3.Interceptor. The problem is that you’re using Kotlin and Kotlin will by default translate a Set<X> into a Set<? extends X>: http://kotlinlang.org/docs/reference/java-to-kotlin-interop.html#variant-generics

Apparently (according to the docs), you should be able to make it work with Set<@JvmSuppressWildcards okhttp3.Interceptor>.

Wrt Set<String>, it works because String is final, so Kotlin emits a Set<String> as Set<? extends String> would have no sense. See this note in the docs:

NOTE: when the argument type is final, there’s usually no point in generating the wildcard, so Box<String> is always Box<String>, no matter what position it takes.

7reactions
ronshapirocommented, Mar 31, 2017

This keeps coming up. I wonder if we should try to inspect the classpath for kotlin classes (if that’s possible?) and present an error when we see this

Read more comments on GitHub >

github_iconTop Results From Across the Web

Multibindings - Dagger
Dagger allows you to bind several objects into a collection even when the objects are bound in different modules using multibindings. Dagger assembles...
Read more >
Dagger2 is hard, but it can be easy, part 7 - FunkyMuse
In this post we'll learn about multi bindings and their power. ... The ViewModel can't be created because Dagger doesn't know how to...
Read more >
Guice Multibindings: Manually obtain a Set<T> from Injector
I use this snippet to create a TypeLiteral<Set<T>> : import com.google.inject.util.Types; @SuppressWarnings("unchecked") public static <T> ...
Read more >
OkHttp Interceptor Multibindings with Dagger - velog
OkHttp Interceptor Multibindings with Dagger ... create(BaseProvidesModule module, Provider<Set<Interceptor>> interceptorsProvider) { return ...
Read more >
javax.inject.Singleton Scala Example - ProgramCreek.com
InternalServerError("Can't show Trace.")) } } Example 11 ... FORBIDDEN).build) } } def setInterceptor(in: TokenAuthorizingInterceptor) = interceptor = in }.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found