diff options
author | George Mount <mount@google.com> | 2015-03-30 07:05:26 -0700 |
---|---|---|
committer | George Mount <mount@google.com> | 2015-03-31 08:23:39 -0700 |
commit | dea555cf42dc3583604699c8c018d22681f56166 (patch) | |
tree | 6b984c768017068a1e4d974687aa5a7111f5496b | |
parent | 61630faa88ee4817834d47294a0e17f19d8e1c51 (diff) | |
download | data-binding-dea555cf42dc3583604699c8c018d22681f56166.tar.gz |
Merge implementation into base class for single implementations.
Bug 19933646
Change-Id: Ifc947d929b5275e32598e8d40f0122504a99d7f8
6 files changed, 123 insertions, 75 deletions
diff --git a/annotationprocessor/src/main/java/android/databinding/annotationprocessor/ProcessExpressions.java b/annotationprocessor/src/main/java/android/databinding/annotationprocessor/ProcessExpressions.java index 8f891430..0d2239a5 100644 --- a/annotationprocessor/src/main/java/android/databinding/annotationprocessor/ProcessExpressions.java +++ b/annotationprocessor/src/main/java/android/databinding/annotationprocessor/ProcessExpressions.java @@ -124,7 +124,7 @@ public class ProcessExpressions extends ProcessDataBinding.ProcessingStep { CompilerChef compilerChef = CompilerChef.createChef(resourceBundle, getWriter()); if (compilerChef.hasAnythingToGenerate()) { compilerChef.addBRVariables(mProcessBindable); - compilerChef.writeViewBinderInterfaces(); + compilerChef.writeViewBinderInterfaces(forLibraryModule); if (!forLibraryModule) { compilerChef.writeDbrFile(); compilerChef.writeViewBinders(); diff --git a/compiler/src/main/java/android/databinding/tool/CompilerChef.java b/compiler/src/main/java/android/databinding/tool/CompilerChef.java index 200c33bf..624119f9 100644 --- a/compiler/src/main/java/android/databinding/tool/CompilerChef.java +++ b/compiler/src/main/java/android/databinding/tool/CompilerChef.java @@ -74,14 +74,14 @@ public class CompilerChef { ensureDataBinder(); for (LayoutBinder layoutBinder : mDataBinder.mLayoutBinders) { for (String variableName : layoutBinder.getUserDefinedVariables().keySet()) { - bindables.addVariable(variableName, layoutBinder.getInterfaceName()); + bindables.addVariable(variableName, layoutBinder.getClassName()); } } } - public void writeViewBinderInterfaces() { + public void writeViewBinderInterfaces(boolean isLibrary) { ensureDataBinder(); - mDataBinder.writerBinderInterfaces(); + mDataBinder.writerBaseClasses(isLibrary); } public void writeViewBinders() { diff --git a/compiler/src/main/java/android/databinding/tool/DataBinder.java b/compiler/src/main/java/android/databinding/tool/DataBinder.java index 1fc9f638..70f8bcb4 100644 --- a/compiler/src/main/java/android/databinding/tool/DataBinder.java +++ b/compiler/src/main/java/android/databinding/tool/DataBinder.java @@ -47,23 +47,26 @@ public class DataBinder { return mLayoutBinders; } - public void writerBinderInterfaces() { + public void writerBaseClasses(boolean isLibrary) { Set<String> writtenFiles = new HashSet<String>(); for (LayoutBinder layoutBinder : mLayoutBinders) { - String interfaceName = layoutBinder.getInterfaceName(); - if (writtenFiles.contains(interfaceName)) { - continue; + if (isLibrary || layoutBinder.hasVariations()) { + String className = layoutBinder.getClassName(); + if (writtenFiles.contains(className)) { + continue; + } + mFileWriter.writeToFile(layoutBinder.getPackage() + "." + className, + layoutBinder.writeViewBinderBaseClass()); + writtenFiles.add(className); } - mFileWriter.writeToFile(layoutBinder.getPackage() + "." + interfaceName, - layoutBinder.writeViewBinderInterface()); - writtenFiles.add(interfaceName); } } public void writeBinders() { for (LayoutBinder layoutBinder : mLayoutBinders) { - L.d("writing data binder %s", layoutBinder.getClassName()); - mFileWriter.writeToFile(layoutBinder.getPackage() + "." + layoutBinder.getClassName(), + String className = layoutBinder.getImplementationName(); + L.d("writing data binder %s", className); + mFileWriter.writeToFile(layoutBinder.getPackage() + "." + className, layoutBinder.writeViewBinder()); } } diff --git a/compiler/src/main/java/android/databinding/tool/LayoutBinder.java b/compiler/src/main/java/android/databinding/tool/LayoutBinder.java index 9d275757..03f7e302 100644 --- a/compiler/src/main/java/android/databinding/tool/LayoutBinder.java +++ b/compiler/src/main/java/android/databinding/tool/LayoutBinder.java @@ -155,7 +155,7 @@ public class LayoutBinder { } } - public String writeViewBinderInterface() { + public String writeViewBinderBaseClass() { ensureWriter(); return mWriter.writeBaseClass(); } @@ -190,18 +190,21 @@ public class LayoutBinder { return mBundle.getFileName(); } - public String getClassName() { - final String suffix; + public String getImplementationName() { if (hasVariations()) { - suffix = mBundle.getConfigName(); + final String suffix; + if (hasVariations()) { + suffix = mBundle.getConfigName(); + } else { + suffix = ""; + } + return mBaseClassName + suffix + "Impl"; } else { - suffix = ""; + return mBaseClassName; } - return mBaseClassName + suffix + "Impl"; - } - public String getInterfaceName() { + public String getClassName() { return mBaseClassName; } diff --git a/compiler/src/main/kotlin/android/databinding/tool/writer/DataBinderWriter.kt b/compiler/src/main/kotlin/android/databinding/tool/writer/DataBinderWriter.kt index 6f3cdc50..171ee521 100644 --- a/compiler/src/main/kotlin/android/databinding/tool/writer/DataBinderWriter.kt +++ b/compiler/src/main/kotlin/android/databinding/tool/writer/DataBinderWriter.kt @@ -27,7 +27,7 @@ class DataBinderWriter(val pkg: String, val projectPackage: String, val classNam layoutBinders.groupBy{it.getLayoutname()}.forEach { tab("case ${it.value.first!!.getModulePackage()}.R.layout.${it.value.first!!.getLayoutname()}:") { if (it.value.size() == 1) { - tab("return new ${it.value.first!!.getPackage()}.${it.value.first!!.getClassName()}(view);") + tab("return ${it.value.first!!.getPackage()}.${it.value.first!!.getImplementationName()}.bind(view);") } else { // we should check the tag to decide which layout we need to inflate tab("{") { @@ -36,7 +36,7 @@ class DataBinderWriter(val pkg: String, val projectPackage: String, val classNam it.value.forEach { // TODO don't use strings. not necessary tab("if (tag.equals(String.valueOf(${it.getId()}))) {") { - tab("return new ${it.getPackage()}.${it.getClassName()}(view);") + tab("return new ${it.getPackage()}.${it.getImplementationName()}(view);") } tab("}") } }tab("}") diff --git a/compiler/src/main/kotlin/android/databinding/tool/writer/LayoutBinderWriter.kt b/compiler/src/main/kotlin/android/databinding/tool/writer/LayoutBinderWriter.kt index ef57f5b4..56d43949 100644 --- a/compiler/src/main/kotlin/android/databinding/tool/writer/LayoutBinderWriter.kt +++ b/compiler/src/main/kotlin/android/databinding/tool/writer/LayoutBinderWriter.kt @@ -337,13 +337,13 @@ class LayoutBinderWriter(val layoutBinder : LayoutBinder) { } val dynamics by Delegates.lazy { model.getExprMap().values().filter { it.isDynamic() } } - val className = layoutBinder.getClassName() + val className = layoutBinder.getImplementationName() val identifiers by Delegates.lazy { dynamics.filter { it is IdentifierExpr } } - val baseClassName = "${layoutBinder.getInterfaceName()}" + val baseClassName = "${layoutBinder.getClassName()}" val includedBinders by Delegates.lazy { layoutBinder.getBindingTargets().filter { it.isBinder() } @@ -364,7 +364,13 @@ class LayoutBinderWriter(val layoutBinder : LayoutBinder) { nl("import ${layoutBinder.getModulePackage()}.R;") nl("import ${layoutBinder.getModulePackage()}.BR;") nl("import android.view.View;") - nl("public class ${className} extends ${baseClassName} {") { + val classDeclaration : String + if (layoutBinder.hasVariations()) { + classDeclaration = "${className} extends ${baseClassName}" + } else { + classDeclaration = "${className} extends android.databinding.ViewDataBinding" + } + nl("public class ${classDeclaration} {") { tab(declareIncludeViews()) tab(declareViews()) tab(declareVariables()) @@ -378,6 +384,9 @@ class LayoutBinderWriter(val layoutBinder : LayoutBinder) { tab(executePendingBindings()) tab(declareDirtyFlags()) + if (!layoutBinder.hasVariations()) { + tab(declareFactories()) + } } nl("}") tab(flagMapping()) @@ -421,67 +430,74 @@ class LayoutBinderWriter(val layoutBinder : LayoutBinder) { nl("}") } fun declareConstructor() = kcode("") { - nl("private ${className}(View root, View[] views) {") { - tab("super(root, ${model.getObservables().size()}") { - layoutBinder.getBindingTargets().filter{it.getId() != null}.forEach { - val index = indices.get(it) - if (!it.isUsed()) { - tab(", null") - } else{ - val variableName : String - if (index == null) { - variableName = "root"; - } else { - variableName = "views[${index}]" - } - tab(", ${it.superConversion(variableName)}") + val viewCount = layoutBinder.getBindingTargets().filter{it.isUsed()}.count() + if (layoutBinder.hasVariations()) { + nl("") + nl("public ${className}(View root) {") { + tab("this(root, mapChildViews(root, ${viewCount}, sIncludes, sViewsWithIds));") + } + nl("}") + nl("private ${className}(View root, View[] views) {") { + tab("super(root, ${model.getObservables().size()}") { + layoutBinder.getBindingTargets().filter { it.getId() != null }.forEach { + tab(", ${fieldConversion(it)}") } + tab(");") } - tab(");") } - val taggedViews = layoutBinder.getBindingTargets().filter{it.isUsed() && !it.isBinder()} - taggedViews.forEach { + } else { + nl("${baseClassName}(View root) {") { + tab("super(root, ${model.getObservables().size()});") + tab("final View[] views = mapChildViews(root, ${viewCount}, sIncludes, sViewsWithIds);") + } + } + val taggedViews = layoutBinder.getBindingTargets().filter{it.isUsed()} + taggedViews.forEach { + if (!layoutBinder.hasVariations() || it.getId() == null) { + tab("this.${it.fieldName} = ${fieldConversion(it)};") + } + if (!it.isBinder()) { if (it.getResolvedType() != null && it.getResolvedType().extendsViewStub()) { tab("this.${it.fieldName}.setContainingBinding(this);") } - if (it.getTag() == null) { - if (it.getId() == null) { - tab("this.${it.fieldName} = (${it.getViewClass()}) root;") - } - } else { - if (it.getId() == null) { - tab("this.${it.fieldName} = (${it.getViewClass()}) views[${it.getTag()}];") - } - if (it.supportsTag()) { - val originalTag = it.getOriginalTag(); - var tagValue = "null" - if (originalTag != null) { - tagValue = "\"${originalTag}\"" - if (originalTag.startsWith("@")) { - var packageName = layoutBinder.getModulePackage() - if (originalTag.startsWith("@android:")) { - packageName = "android" - } - val slashIndex = originalTag.indexOf('/') - val resourceId = originalTag.substring(slashIndex + 1) - tagValue = "root.getResources().getString(${packageName}.R.string.${resourceId})" + if (it.supportsTag() && it.getTag() != null) { + val originalTag = it.getOriginalTag(); + var tagValue = "null" + if (originalTag != null) { + tagValue = "\"${originalTag}\"" + if (originalTag.startsWith("@")) { + var packageName = layoutBinder.getModulePackage() + if (originalTag.startsWith("@android:")) { + packageName = "android" } + val slashIndex = originalTag.indexOf('/') + val resourceId = originalTag.substring(slashIndex + 1) + tagValue = "root.getResources().getString(${packageName}.R.string.${resourceId})" } - tab("this.${it.fieldName}.setTag(${tagValue});") } + tab("this.${it.fieldName}.setTag(${tagValue});") } } - tab("invalidateAll();"); - nl("}") - } - nl("") - nl("public ${className}(View root) {") { - val viewCount = layoutBinder.getBindingTargets().filter{it.isUsed()}.count() - tab("this(root, mapChildViews(root, ${viewCount}, sIncludes, sViewsWithIds));") } + tab("invalidateAll();"); nl("}") } + fun fieldConversion(target : BindingTarget) : String { + val index = indices.get(target) + if (!target.isUsed()) { + return "null" + } else { + val variableName: String + if (index == null) { + variableName = "root"; + } else { + variableName = "views[${index}]" + } + return target.superConversion(variableName) + } + } + fun declareInvalidateAll() = kcode("") { nl("@Override") nl("public void invalidateAll() {") { @@ -607,8 +623,15 @@ class LayoutBinderWriter(val layoutBinder : LayoutBinder) { } fun declareViews() = kcode("// views") { - layoutBinder.getBindingTargets().filter {it.isUsed() && (it.getId() == null)}.forEach { - nl("private final ${it.interfaceType} ${it.fieldName};") + val oneLayout = !layoutBinder.hasVariations(); + layoutBinder.getBindingTargets().filter {it.isUsed() && (oneLayout || it.getId() == null)}.forEach { + val access : String + if (oneLayout && it.getId() != null) { + access = "public" + } else { + access = "private" + } + nl("${access} final ${it.interfaceType} ${it.fieldName};") } } @@ -785,6 +808,25 @@ class LayoutBinderWriter(val layoutBinder : LayoutBinder) { tab("}") } + fun declareFactories() = kcode("") { + nl("public static ${baseClassName} inflate(android.view.ViewGroup root) {") { + tab("return bind(android.view.LayoutInflater.from(root.getContext()).inflate(${layoutBinder.getModulePackage()}.R.layout.${layoutBinder.getLayoutname()}, root, true));") + } + nl("}") + nl("public static ${baseClassName} inflate(android.content.Context context) {") { + tab("return bind(android.view.LayoutInflater.from(context).inflate(${layoutBinder.getModulePackage()}.R.layout.${layoutBinder.getLayoutname()}, null, false));") + } + nl("}") + nl("public static ${baseClassName} bind(android.view.View view) {") { + tab("if (!\"${layoutBinder.getId()}\".equals(view.getTag())) {") { + tab("throw new RuntimeException(\"view tag doesn't isn't correct on view\");") + } + tab("}") + tab("return new ${baseClassName}(view);") + } + nl("}") + } + public fun writeBaseClass() : String = kcode("package ${layoutBinder.getPackage()};") { nl("import android.databinding.Bindable;") @@ -829,4 +871,4 @@ class LayoutBinderWriter(val layoutBinder : LayoutBinder) { tab("}") nl("}") }.generate() -}
\ No newline at end of file +} |