# JvmSuppressWildcards: The secret sauce to your sandwich-style generics

DevFeed: [JvmSuppressWildcards: The secret sauce to your sandwich-style generics](<https://devfeed.tech/articles/jvmsuppresswildcards-the-secret-sauce-to-your-sandwich-style-generics-25906.md>)

Original publisher: [Read original article](<https://chao2zhang.medium.com/jvmsuppresswildcards-the-secret-sauce-to-your-sandwich-style-generics-b0093aa5979d?source=rss-d19045640fe------2>)

Author: Chao Zhang

Published: 2021-02-23T16:01:15Z

Content type: article

Language: en

Sources: [Chao Zhang](<https://devfeed.tech/sources/stories-by-chao-zhang-on-medium.md>)

Topics: [generic programming](<https://devfeed.tech/topics/generic-programming.md>), [Dagger](<https://devfeed.tech/topics/dagger.md>), [Use-Site Variance](<https://devfeed.tech/topics/use-site-variance.md>), [annotation-processor](<https://devfeed.tech/topics/annotation-processor.md>)

Tags: [android](<https://devfeed.tech/tags/android.md>), [android-app-development](<https://devfeed.tech/tags/android-app-development.md>), [compilation-error](<https://devfeed.tech/tags/compilation-error.md>), [dagger](<https://devfeed.tech/tags/dagger.md>), [declaration-site-variance](<https://devfeed.tech/tags/declaration-site-variance.md>), [generics](<https://devfeed.tech/tags/generics.md>), [java](<https://devfeed.tech/tags/java.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [processing](<https://devfeed.tech/tags/processing.md>), [recyclerview](<https://devfeed.tech/tags/recyclerview.md>)

## AI overview

In mixed Java/Kotlin dependency chains, Kotlin's generic variance can produce Java wildcards that break overrides or Dagger binding resolution. In the RecyclerView example, changing nullability or the Java parameter to `List<? extends Object>` or `List<?>` does not fix the clash; annotating the Kotlin element type with `@JvmSuppressWildcards` makes `List<Any>` translate to Java `List<Object>`. For Dagger multibinding, annotating the Kotlin map type similarly corrects the generated Java type and resolves the reported missing binding. The excerpt presents `@JvmSuppressWildcards` as a way to suppress generated wildcards for Java compatibility.

## Source excerpt

Photo: amirali mirhashemian from Unsplash If generic types are exposed in Kotlin API, consider @JvmSuppressWildcards so that your Java consumer can compile successfully. Kotlin generics are different from (in my opinion, smarter than) Java generics. Kotlin generics has declaration-site variance and type projections, which are officially documented here. Generics is a gigantic topic itself. My usual practice is to leverage IDE warnings and compiler error messages to fix my code since they usually contain the necessary information to show you where you did wrong. However, as we are Kotlinizing our codebase from Java to Kotlin, it is possible to have a sandwich-style code dependency that mixes Java code and Kotlin code. In such cases, when dealing with generics, your compiler and IDE may not be smart enough to help you fix those issues. What is sandwich-style code dependency?🥪 Sandwich-style Kotlin conversion At a certain time during your Java to Kotlin conversion, there could be some Java code depending on Kotlin code, which in turn depends on Java code. A typical example on Android is that your app code in Java depending on your library in Kotlin, which depends on Android SDK in Java. Even if your app code is written purely in Kotlin, the sandwich might still exist: The Java-based annotation processing Dagger, depends on your app code in Kotlin, which depends on Android SDK in Java. Now let's look at how the secret sauce @JvmSuppressWildcards can help us by making our generic sandwich tasty. Sandwich: List<Object> in RecyclerView methodshttps://medium.com/media/87fd0c63db2b9c82af2b241bf61cef31/href With the code above, the following compilation error is observed: MyChildAdapter.java:13: error: name clash: onBindViewHolder(MyViewHolder,int,List<Object>) in MyChildAdapter overrides a method whose erasure is the same as another method, yet neither overrides the other The error message leads us to think that we need to change our type here: Change the Kotlin generic para