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

Enable Enable @ModelProp @TextProp and @CallbackProp with all options on field attribute #343

Merged

Conversation

juliafu1
Copy link
Contributor

@juliafu1 juliafu1 commented Nov 6, 2017

Enable Enable @ModelProp @TextProp and @CallbackProp with all options on field attribute

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.

@snowywinter2013 Cool, looks like the right idea!

We will want to make sure that @NullOnRecycle works.

Once this is cleaned up more I can help you write tests

): Boolean =
validateExecutableElement(methodElement, propAnnotation, 1)
): Boolean {
if (prop !is ExecutableElement && prop.kind != ElementKind.FIELD) {
Copy link
Contributor

Choose a reason for hiding this comment

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

return when (prop) {
is ExecutableElement -> validateExecutableElement
is VariableElement -> validateFieldElement
else -> {

log...
false
}

"\$L.\$L(\$L);\n")
}

private fun buildCodeBlockToSetAttribute(
Copy link
Contributor

Choose a reason for hiding this comment

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

if buildCodeBlockToSetAttribute isn't used elsewhere I don't think we need to make it a separate method. it's a bit weird to pull just this out and not the other code block

@@ -9,31 +9,46 @@ import java.util.*
import javax.lang.model.element.*
import javax.lang.model.type.*
import javax.lang.model.util.*
import com.sun.corba.se.impl.activation.ServerMain.logError
Copy link
Contributor

Choose a reason for hiding this comment

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

this seems like the wrong import

@@ -9,31 +9,46 @@ import java.util.*
import javax.lang.model.element.*
import javax.lang.model.type.*
import javax.lang.model.util.*
import com.sun.corba.se.impl.activation.ServerMain.logError
import javax.lang.model.element.ElementKind
import com.sun.tools.javac.util.BaseFileManager.getKind
Copy link
Contributor

Choose a reason for hiding this comment

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

this seems like the wrong import too


private val NOT_NULL_ANNOTATION_SPEC = AnnotationSpec.builder(NotNull::class.java).build()
private val NON_NULL_ANNOTATION_SPEC = AnnotationSpec.builder(NonNull::class.java).build()

internal enum class ViewAttributeType {
Copy link
Contributor

Choose a reason for hiding this comment

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

can you make this a sealed class instead?

element: Element,
errorLogger: ErrorLogger
) {
when {
Copy link
Contributor

Choose a reason for hiding this comment

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

it's cleaner to return a value than to set a field. the field should be a val to be safer


init {
val propAnnotation = setterElementOnView.getAnnotation(ModelProp::class.java)
val textAnnotation = setterElementOnView.getAnnotation(TextProp::class.java)
val callbackAnnotation = setterElementOnView.getAnnotation(CallbackProp::class.java)

val options = HashSet<Option>()
val param = setterElementOnView.parameters[0]
val param = if (setterElementOnView is ExecutableElement) setterElementOnView.parameters[0]
Copy link
Contributor

Choose a reason for hiding this comment

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

setterElementOnView should be renamed, it doesn't make sense for fields

@@ -43,6 +58,11 @@ internal class ViewAttributeInfo(
options.addAll(Arrays.asList(*propAnnotation.options))
options.addAll(Arrays.asList(*propAnnotation.value))
} else if (textAnnotation != null) {
//TODO juliafu support textProp for field
if (viewAttributeTypeName == ViewAttributeType.Field) {
Copy link
Contributor

Choose a reason for hiding this comment

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

we shouldn't have to do anything special here to support it

@@ -52,6 +72,10 @@ internal class ViewAttributeInfo(
}
options.add(Option.GenerateStringOverloads)
} else if (callbackAnnotation != null) {
if (viewAttributeTypeName == ViewAttributeType.Field) {
Copy link
Contributor

Choose a reason for hiding this comment

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

same here

@@ -75,8 +99,8 @@ internal class ViewAttributeInfo(
modelName = modelInfo.generatedName.simpleName()
modelPackageName = modelInfo.generatedClassName.packageName()

this.viewSetterMethodName = setterElementOnView.simpleName.toString()
propName = removeSetPrefix(viewSetterMethodName)
this.viewSetterPropName = setterElementOnView.simpleName.toString()
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't know if the name viewSetterPropName makes sense for fields

@juliafu1 juliafu1 force-pushed the juliafu/add_model_prop_epoxy_attribute branch 2 times, most recently from 8703058 to 107eff6 Compare November 20, 2017 07:32
@juliafu1 juliafu1 changed the title Enable @ModelProp on field attribute Enable Enable @ModelProp @TextProp and @CallbackProp with all options on field attribute Nov 20, 2017
@juliafu1
Copy link
Contributor Author

juliafu1 commented Nov 20, 2017

I updated the code and addressed all comments. This makes all props, options, default value works. It is quite complete and fully working.
The testing is here: https://docs.google.com/document/d/1sT-AUi-Iiv3qI6dTfzbW3szRteq9qwaY-m1L7vnSY58/edit#bookmark=id.t7rle9y2y6xm
@elihart PTAL when you have time. No hurry. I look forward to writing tests

@juliafu1 juliafu1 force-pushed the juliafu/add_model_prop_epoxy_attribute branch from 107eff6 to 893e28a Compare November 22, 2017 17:25
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.

@juliafu1 looks pretty great! just a bunch of small things, once it is cleaned up lets write some tests and then merge this :)

resourceProcessor))
}

//TODO juliafu handle the super class case
Copy link
Contributor

Choose a reason for hiding this comment

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

can you add more details to this comment? not sure what needs to be done exactly

element.enclosingElement?.let { modelClassMap[it] }

companion object {
val modelPropAnnotations = listOf(ModelProp::class, TextProp::class,
CallbackProp::class).map { it.java }
val modelPropAnnotationsNames = listOf(ModelProp::class, TextProp::class,
Copy link
Contributor

Choose a reason for hiding this comment

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

it would be nice not to duplicate the list of prop classes

viewAttribute.viewSetterMethodName,
getValueToSetOnView(viewAttribute, boundObjectParam))
val expression = if (viewAttribute.viewAttributeTypeName == ViewAttributeType.Field)
"\$L.\$L=\$L;\n" else "\$L.\$L(\$L);\n"
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we can do CodeBlock.builder().addStatement()... to avoid having to do ;\n manually.

viewAttribute.viewSetterMethodName,
getValueToSetOnView(viewAttribute, boundObjectParam))
val expression = if (viewAttribute.viewAttributeTypeName == ViewAttributeType.Field)
"\$L.\$L=\$L;\n" else "\$L.\$L(\$L);\n"
Copy link
Contributor

Choose a reason for hiding this comment

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

this is mostly duplicated with the unbind code, the only difference is that the unbind code sets null. can you think of a way to DRY it out?

): ViewAttributeType? {
return when {
element is ExecutableElement -> ViewAttributeType.Method
element.kind === ElementKind.FIELD -> ViewAttributeType.Field
Copy link
Contributor

Choose a reason for hiding this comment

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

don't use triple equals here

Copy link
Contributor

Choose a reason for hiding this comment

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

can you do when(element.kind) is FIELD ... is METHOD...?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It couldn't use 'is'. The compiler tells: "'is' over enum entry is not allowed, use comparison instead". I can use '==' instead of '===' but it actually doesn't make a difference for enum

if (ModelViewProcessor.modelPropAnnotationsNames.contains(ClassName.get(annotationElement as TypeElement))) {
continue
}
val builder = AnnotationSpec.builder(ClassName.get(annotationElement))
Copy link
Contributor

Choose a reason for hiding this comment

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

save ClassName.get(annotationElement) to a val since it is done twice?

@@ -274,7 +298,10 @@ internal class ViewAttributeInfo(
) {
for (annotationMirror in paramElement.annotationMirrors) {
val annotationElement = types.asElement(annotationMirror.annotationType)
val builder = AnnotationSpec.builder(ClassName.get(annotationElement as TypeElement))
if (ModelViewProcessor.modelPropAnnotationsNames.contains(ClassName.get(annotationElement as TypeElement))) {
Copy link
Contributor

Choose a reason for hiding this comment

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

can do if (annotationClass in ModelViewProcessor.modelPropAnnotationsNames) continue

@juliafu1 juliafu1 force-pushed the juliafu/add_model_prop_epoxy_attribute branch from 893e28a to 77e8427 Compare December 11, 2017 17:01
@juliafu1
Copy link
Contributor Author

@elihart all comments addressed. I also tested the parent view case (if the parent view has an attribute, the generated child view model shall have that attribute as well). The test is added in https://docs.google.com/document/d/1sT-AUi-Iiv3qI6dTfzbW3szRteq9qwaY-m1L7vnSY58/edit#bookmark=id.t7rle9y2y6xm. Thank you.

@juliafu1 juliafu1 force-pushed the juliafu/add_model_prop_epoxy_attribute branch 4 times, most recently from 574a4f2 to b6ff338 Compare December 14, 2017 22:08
@juliafu1 juliafu1 force-pushed the juliafu/add_model_prop_epoxy_attribute branch from b6ff338 to 28cd1f9 Compare December 14, 2017 22:13
@juliafu1
Copy link
Contributor Author

@elihart Tests added. PTAL. Thank you

@elihart
Copy link
Contributor

elihart commented Dec 16, 2017

Great! tests look pretty exhaustive - I'll try to give this one final lookover soon and then merge 👍

@elihart elihart merged commit 17a9be1 into airbnb:master Dec 20, 2017
@juliafu1
Copy link
Contributor Author

Thank you!

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