Skip to content
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

Reflect the annotations declared in constructor params #519

Merged

Conversation

ShaishavGandhi
Copy link
Contributor

If I declare a model like:

abstract class AnnotationModel(@StringRes val resId: Int): EpoxyModelWithHolder<AnnotationHolder>() {}

the Kotlin extension generated doesn't add the annotation to the extension function. However, it is added to the AnnotationModel_ class.

This PR fixes that.

Copy link
Contributor

@elihart elihart left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shaishavgandhi05 thanks for this! Just one request - looks good besides that

@@ -102,6 +103,11 @@ private fun JavaClassName.getSimpleNamesInKotlin(): List<String> {
return originalNames
}

fun JavaAnnotationSpec.toKPoet(): KotlinAnnotationSpec {
val annotationClass = KotlinClassName.bestGuess(type.toString())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't quite right because it won't capture parameter values in the annotation - so it will only work for basic annotations.

If you're interested it would be great to add that full functionality, otherwise can you add a note warning of this partial implementation?

Copy link
Contributor Author

@ShaishavGandhi ShaishavGandhi Sep 4, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Will update the extension to include the parameter values also.

I'm interested in also adding tests for the PoetExtensions file. Would it be best for adding tests in the epoxy-processor module itself?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shaishavgandhi05 great! yes, I think adding tests to epoxy-processor makes sense

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After some investigation, this seems like something really hard to do. Parameters are added as CodeBlock's to the AnnotationSpec. There's no way to convert a JavaCodeBlock to KotlinCodeBlock since CodeBlock's have no getters to know what values they're going to emit.

Signed-off-by: shaishavgandhi05 <[email protected]>
@ShaishavGandhi
Copy link
Contributor Author

Have updated the PR with a bunch of tests for PoetExtensions. Also, as noted in the comment before, some investigation revealed that it was hard to transfer parameters from JavaAnnotationSpec to KotlinAnnotationSpec since JavaCodeBlock has no accessors that will let me move it to KotlinCodeBlock.

Copy link
Contributor

@elihart elihart left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shaishavgandhi05 hm, that param limitation is understandable so it seems fine to not support for now. Thank you for adding the comment and tests though.

I just have one last concern, I don't want code breaking if people do use annotations with values. For example, annotations like FloatRange are not that rare in android, and if generated code loses the parameters it won't compile.

Could we check which annotations have parameters and just omit those for now?

I can ship a new release as soon as this is merged :)

import org.junit.Test
import javax.lang.model.element.Modifier

class PoetExtensionsTest {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for the tests!

@@ -102,6 +104,13 @@ private fun JavaClassName.getSimpleNamesInKotlin(): List<String> {
return originalNames
}

// Does not support transferring complex annotations which
// have parameters and values associated with them.
fun JavaAnnotationSpec.toKPoet(): KotlinAnnotationSpec {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe this can be nullable, and we would return null if the JavaAnnotationSpec has a code block?

* Add test asserting null behavior

Signed-off-by: shaishavgandhi05 <[email protected]>
@ShaishavGandhi
Copy link
Contributor Author

Yup you're right. Don't want to break existing functionality. I've added a test for the null case as well as added it to the sample model so that we are sure it doesn't cause any compile errors.

@elihart
Copy link
Contributor

elihart commented Sep 5, 2018

Excellent, thanks again! I'll try to push a new release asap

@elihart elihart merged commit 2712b45 into airbnb:master Sep 5, 2018
@ShaishavGandhi ShaishavGandhi deleted the sg/reflect-constructor-annotations branch September 5, 2018 21:57
@ShaishavGandhi
Copy link
Contributor Author

No hurries. It's a nice to have, not a deal breaker 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants