-
Notifications
You must be signed in to change notification settings - Fork 729
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
Enable Enable @ModelProp @TextProp and @CallbackProp with all options on field attribute #343
Conversation
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.
@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) { |
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.
return when (prop) {
is ExecutableElement -> validateExecutableElement
is VariableElement -> validateFieldElement
else -> {
log...
false
}
"\$L.\$L(\$L);\n") | ||
} | ||
|
||
private fun buildCodeBlockToSetAttribute( |
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.
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 |
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.
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 |
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.
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 { |
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.
can you make this a sealed class instead?
element: Element, | ||
errorLogger: ErrorLogger | ||
) { | ||
when { |
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.
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] |
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.
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) { |
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.
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) { |
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.
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() |
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 know if the name viewSetterPropName
makes sense for fields
8703058
to
107eff6
Compare
I updated the code and addressed all comments. This makes all props, options, default value works. It is quite complete and fully working. |
107eff6
to
893e28a
Compare
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.
@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 |
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.
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, |
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.
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" |
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 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" |
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.
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 |
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.
don't use triple equals here
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.
can you do when(element.kind) is FIELD ... is METHOD...
?
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.
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)) |
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.
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))) { |
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.
can do if (annotationClass in ModelViewProcessor.modelPropAnnotationsNames) continue
893e28a
to
77e8427
Compare
@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. |
574a4f2
to
b6ff338
Compare
b6ff338
to
28cd1f9
Compare
@elihart Tests added. PTAL. Thank you |
Great! tests look pretty exhaustive - I'll try to give this one final lookover soon and then merge 👍 |
Thank you! |
Enable Enable @ModelProp @TextProp and @CallbackProp with all options on field attribute