Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin annotation processing ignores items with similar names

I recently converted the majority of my project to kotlin. Now I encounter several unusual errors that all seem to relate to annotation libraries. Needless to say, it didn't happen in Java.

I'll describe the cases - one in Dagger and one in Butterknife.

1. When having 2 @Provides methods in different models with the same name. For example in file "FooProvider.kt" having a "provideFooOrBar" method

@Module
class FooProvider(private val view: FooActivity) {
    ... 
    @Provides @FooScope fun provideView() = view
    @Provides @FooScope fun provideFooOrBar() = Foo()
}

And having another file "BarProvider.kt" with the same method name

@Module
class BarProvider(private val view: BarActivity) {
    ...
    @Provides @BarScope fun provideView() = view
    @Provides @BarScope fun provideFooOrBar() = Bar()
}

In this case, Dagger fails to generate some factory libraries and I get the following compilation error: Error:(27, 32) error: cannot find symbol class FooProvider_ProvideFooOrBarFactory

A sample project reproducing the issue can be found at https://github.com/maxandron/DaggerIssue325

2. This is an issue when using Butterknife. When having two @Bind annotated variables in two different classes - One of them just fails to initialize at runtime without any compilation error!

For example if I have:

class FooActivity {
    @Bind(R.id.foo) lateinit var mFoo: View
}
class NotFooActivity {
    @Bind(R.id.not_foo) lateinit var mFoo: View
}

Then one of them (or both?) will just fail to initialize without any error. Causing a kotlin.UninitializedPropertyAccessException: lateinit property mFoo has not been initialized exception to be thrown when the field is accessed.


Is it something I'm doing wrong in configuring Kotlin or is it a kotlin bug?

Thank you in advance! Ron

like image 438
maxandron Avatar asked Feb 25 '16 17:02

maxandron


2 Answers

I was having this issue, so I started to investigate and it's caused because Kapt is only checking the method name when comparing them, and they are added in a set, thus duplicates are not allowed. The same happens for annotated fields, so currently you can have one method/field name per annotation.

I added the class name to the equals method and the annotations were properly handled now, but the tests broke and I don't know how they work, so I hope someone knows how to fix this.

like image 90
inorichi Avatar answered Sep 20 '22 05:09

inorichi


It turned out to be a bug with kapt. I posted an issue on Kotlin's bug tracker and the issue is now marked as fixed.

This solution was merged

Should be resolved in Kotlin version 1.0.2

like image 22
maxandron Avatar answered Sep 20 '22 05:09

maxandron