@BindsOptionalOf is not compilant with the Java 7 compiler
See original GitHub issueHi 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:
- Created 4 years ago
- Reactions:3
- Comments:8
Top GitHub Comments
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.
I wish Dagger would provide its own
Optional<T>
type just like itsLazy<T>
. This way Android wouldn’t have to rely on desugaring.