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.

@BindsOptionalOf is not compilant with the Java 7 compiler

See original GitHub issue

Hi everyone,

I’m facing to a compiler bug with @BindsOptionalOf when compiling with the Java 7 source compatibility. I’ve created a module RootModule with this binding :

@Module
interface RootModule {
    @BindsOptionalOf
    fun string(): String
}

and the RootComponent :

@Component(modules = [RootModule::class])
interface RootComponent {
    @Component.Factory
    interface Factory {
        fun build(): RootComponent
    }
    fun inject(application: Application)
}

Inside my class Application, I create an injectable field:

class Application : Application() {

    @Inject
    lateinit var string: Optional<String>

    override fun onCreate() {
        super.onCreate()
        DaggerRootComponent.factory().build().also { it.inject(this) }
    }
}

With this previous setup, I have a compiler exception into the generated DaggerRootComponent at the line 28:

private Application injectApplication(Application instance) {
    Application_MembersInjector.injectString(instance, Optional.empty());
    return instance;
  }

It fails because because the generic type of Optional.empty() should be explicitly specified in Java 7 (not required in Java8), so, when using a Java 7 compiler, this code should be replaced by Optional.<String>empty().

You can clone a working example in my repository HERE. Warning ! It’s an Android app using Kotlin, not a plain Java app, I’m less familiar with pure java developement than Android 😃. On this repository, you can enable java 8 compilation at this location to see the difference between and face the bug.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:3
  • Comments:8

github_iconTop GitHub Comments

1reaction
JakeWhartoncommented, Dec 6, 2020

I wish Android actually cared about not fragmenting the Java ecosystem. There is no reason to embed your own optional anymore. You’re just doing what core library desugar is doing, but worse, since your version is inoperable with the standard versions.

1reaction
Plastixcommented, Dec 5, 2020

I wish Dagger would provide its own Optional<T> type just like its Lazy<T>. This way Android wouldn’t have to rely on desugaring.

Read more comments on GitHub >

github_iconTop Results From Across the Web

BindsOptionalOf - Dagger
Annotates methods that declare bindings for Optional containers of values from bindings that may or may not be present in the component.
Read more >
Can't Compile Java 11 Code With Source Options
The main reason is that you cannot use a source value that is lower than the target value. If you want the code...
Read more >
Diff - 325b516^! - platform/external/dagger2 - Git at Google
Any binding graph errors, such as duplicate bindngs (but not including missing bindings), ... diff --git a/java/dagger/internal/codegen/CompilerOptions.java ...
Read more >
https://www.vmware.com/content/dam/aw-microsites/o...
The VMware Product that includes this file does not necessarily use all the open source ... proxy-vole-20131209 >>> stacktrace-js-1.0.4 >>> dnsjava-2.1.7 ...
Read more >
Dagger2 – How to conditionally choose modules at runtime
Dagger generates code at compile-time, so you are not going to have as much ... but abstract methods usually power * @Binds, @BindsOptionalOf,...
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