-
Notifications
You must be signed in to change notification settings - Fork 182
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Annotation processors in Kotlin #775
Conversation
Codecov Report
@@ Coverage Diff @@
## master #775 +/- ##
=======================================
Coverage 96.85% 96.85%
=======================================
Files 87 87
Lines 2638 2638
Branches 296 296
=======================================
Hits 2555 2555
Misses 50 50
Partials 33 33 Continue to review full report at Codecov.
|
Just discovered that everything works from IDE on my work laptop. Just need to figure out what is the difference between setups. |
Yep, the issue was in my weird Gradle configuration for Android Studio. So everything is working without any issues. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hoho looks nice! 👍 👍
just few nits
build.gradle
Outdated
@@ -50,6 +54,7 @@ ext.libraries = [ | |||
// Core libraries | |||
supportAnnotations : 'com.android.support:support-annotations:' + supportLibsVersion, | |||
rxJava : 'io.reactivex:rxjava:1.2.1', | |||
kotlinStdLib : "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
formatting)
build.gradle
Outdated
@@ -67,6 +72,7 @@ ext.libraries = [ | |||
assertJ : 'org.assertj:assertj-core:3.6.2', | |||
assertJAndroid : 'com.squareup.assertj:assertj-android:1.1.1', | |||
mockitoCore : 'org.mockito:mockito-core:2.7.19', | |||
mockitoKotlin : 'com.nhaarman:mockito-kotlin:1.3.0', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Today was released 1.4.0) https://github.com/nhaarman/mockito-kotlin/releases/tag/1.4.0
|
||
/** | ||
* Base annotation processor for StorIO. | ||
* |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
without double empty line?
* | ||
* It will be called after Java Compiler will find lang elements annotated with annotations from [getSupportedAnnotationTypes]. | ||
* @param annotations set of annotations | ||
* |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
el here and below
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
EL
|
||
return StorIOContentResolverTypeMeta(simpleName, packageName, storIOContentResolverType) | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
EL
.addAnnotation(ANDROID_NON_NULL_ANNOTATION_CLASS_NAME) | ||
.build()) | ||
.addCode("""return DeleteQuery.builder() | ||
$INDENT.uri(${"$"}S) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
formatting query builders here and below
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, this formatting bothers me as well. The thing is that I don't know how to format it since it is a literal string which sounds like a good idea for this case, but this formatting...
I can change it to concatenation if you think that is' better.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep I agree that with concatenation it will not look pretty too =(
Up to you
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can add indentation and then apply trimIndent()
|
||
val superclass = ClassName.get("com.pushtorefresh.storio.contentresolver", SUFFIX) | ||
val superclassParametrized = ParameterizedTypeName.get(superclass, storIOSQLiteTypeClassName) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
EL
/** | ||
* Annotation processor for StorIOSQLite. | ||
* | ||
* It'll process annotations to generate StorIOSQLite |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
place Object-Mapping
on the same line
|
||
val superclass = ClassName.get("com.pushtorefresh.storio.sqlite", SUFFIX) | ||
val superclassParametrized = ParameterizedTypeName.get(superclass, typeClassName) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
EL
@artem-zinnatullin PTAL please, because you have rich experience 🐕 🍲 🍴 in Kotlin |
|
||
dependencies { | ||
compile libraries.intellijAnnotations | ||
compile libraries.autoService | ||
compile libraries.javaPoet | ||
compile libraries.kotlinStdLib |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will it increase module methods count on 6k? =(
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't remember the exact number of methods in Kotlin stdlib, but it doesn't matter anyway because this dependency isn't going to be in apk so dex limit won't be affected.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great! Few places that use forEach
could be rewritten to kotlin streams api, but I'm ok with current variant :)
import javax.lang.model.type.TypeKind | ||
import javax.lang.model.type.TypeMirror | ||
|
||
class JavaTypeTest { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Next step would be converting JUnit tests to Spek :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, I wanted to try it for quite some time, so I might take a look into it :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are few problems with it though, but I can help with that :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I know about it. Tried to setup it once - spend about an hour without success and given up until the next try.
.addAnnotation(ANDROID_NON_NULL_ANNOTATION_CLASS_NAME) | ||
.build()) | ||
.addCode("""return DeleteQuery.builder() | ||
$INDENT.uri(${"$"}S) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can add indentation and then apply trimIndent()
@artem-zinnatullin thanks for pointing out to |
Usually instead of let's call it "imperative" iterating over collections and populating some mutable data structures you can use more "functional" approach with operators like |
Ah, I thought that you were talking about something different. Of course, I know about functional operators like |
Well, they are, but that's fine :) |
Since it is possible to use Kotlin in annotation processing without adding it as a dependency to the library itself I thought that it makes sense to use the best available tool (and Kotlin is for sure better than Java). I hope you won't mind this change.
Unfortunately, not everything is smooth. I encountered into two issues so far:
AutoService
doesn't work with Kotlin classes so as a workaround I extended actual processors with dummy Java classes and annotated them.Another problem, which I haven't solved is about running tests for processors and building sample apps from IDE. It works from a console but fails with whole code full of unresolved references from IntelliJ. It would be really nice if someone could take a look at it since I haven't figured out how to fix it.
Besides these issues everything works and looks nicer than it was before in Java :D