Annyce Davis

ones and zeros

  • Home
  • Public Speaking
  • About Me
  • Courses
  • Life

@JvmSuppressWildcards My Biggest Annoyance with Kotlin

August 29, 2017 by Annyce Davis 7 Comments

Over the past few months my team has been steadily moving our code over to Kotlin. It’s been a joy to get to work with such a succinct, yet expressive language. However, I have spent needless hours attempting to debug issues in my application that were all solved with the addition of this one thing: @JvmSuppressWildcards.

Me debugging these issues


The annotation @JvmSuppressWildcards, is used to instruct the compiler to generate or omit wildcards for type arguments. Why is this the key to stopping endless debugging?

It’s related to how Kotlin generates code in order to work with Java. The technical term is declaration site-variance.  In the simplest terms Kotlin will take your code that looks like this:

fun recordActionsWithGps(@Body gpsRequests: List<GpsRequest>)

And turn it into this:

fun recordActionsWithGps(@Body gpsRequests: List<? extends GpsRequest>)

However, this may not always be what you want. For instance, if you have a Java class that implements the Interface containing this method, the compiler will complain. Typically you may see something like 'Method does not override method from its superclass'.

Compiler complaining

You have to take some type of corrective action before being able to move forward. The best way to figure out what’s happening in your case, is to view the decompiled code. Here’s what it will look like:

Kotlin Bytecode

By checking the declaration you can see if the ? extends is being tacked on. If so, then you can simply change your parameter declaration to the following:

fun recordActionsWithGps(@Body gpsRequests: List<@JvmSuppressWildcards GpsRequest>)

And the ? extends will not be added. It’s your way of telling the compiler you know what you’re doing. Here are a few other cases where our favorite annotation has come in handy: with Multi-Binding Maps in Dagger 2, and then when working with custom GSON Adapters.

Have you had to use @JvmSuppressWildcards? If so, what’s the use-case? Comment below.

Special thanks to: @donnfelker and @trionkidnapper for reviewing this post!

Share this:

  • Click to share on Twitter (Opens in new window)
  • Click to share on Facebook (Opens in new window)
  • Click to share on LinkedIn (Opens in new window)

Related

Filed Under: Android, Kotlin Tagged With: Bytecode, Kotlin

Follow Me

What engineering leaders need to know from this year’s Google I/O

I didn't intentionally aim to create a rhyming title, but there you have it. Each year, I look forward to Google I/O like a kid going back to school. This year … [Read More...]

Talk: The Real MVP

You have an idea for a new app. You've secured buy-in from the business. What's next? The MVP. But what does it take to ship a new app these days? What are the … [Read More...]

Categories

  • Android (55)
  • Career (2)
  • Communication (4)
  • Flutter (1)
  • Git (4)
  • Gradle (4)
  • Grails (23)
  • Java (8)
  • JavaScript (6)
  • Kotlin (17)
  • Life (4)
  • Public Speaking (23)
  • RxJava (1)
  • Software Development (7)
  • Twitter (3)
  • Uncategorized (11)
  • Video Course (5)

Copyright © 2023 · Beautiful Pro Theme on Genesis Framework · WordPress · Log in

 

Loading Comments...