Don't use reflection for DataBinderMapper.
By having a concrete implementation of DataBinderMapper in the
library and then stripping it out, the generated DataBinderMapper
may be instantiated without reflection.
diff --git a/tools/data-binding/compiler/src/main/java/android/databinding/tool/CompilerChef.java b/tools/data-binding/compiler/src/main/java/android/databinding/tool/CompilerChef.java
index 624119f9..7e2449b 100644
--- a/tools/data-binding/compiler/src/main/java/android/databinding/tool/CompilerChef.java
+++ b/tools/data-binding/compiler/src/main/java/android/databinding/tool/CompilerChef.java
@@ -61,7 +61,7 @@
ensureDataBinder();
final String pkg = "android.databinding";
DataBinderWriter dbr = new DataBinderWriter(pkg, mResourceBundle.getAppPackage(),
- "GeneratedDataBinderRenderer", mDataBinder.getLayoutBinders());
+ "DataBinderMapper", mDataBinder.getLayoutBinders());
if (dbr.getLayoutBinders().size() > 0) {
mFileWriter.writeToFile(pkg + "." + dbr.getClassName(), dbr.write());
}
diff --git a/tools/data-binding/compiler/src/main/kotlin/android/databinding/tool/writer/DataBinderWriter.kt b/tools/data-binding/compiler/src/main/kotlin/android/databinding/tool/writer/DataBinderWriter.kt
index 171ee52..22524fef 100644
--- a/tools/data-binding/compiler/src/main/kotlin/android/databinding/tool/writer/DataBinderWriter.kt
+++ b/tools/data-binding/compiler/src/main/kotlin/android/databinding/tool/writer/DataBinderWriter.kt
@@ -20,8 +20,7 @@
kcode("") {
nl("package $pkg;")
nl("import $projectPackage.BR;")
- nl("class $className implements android.databinding.DataBinderMapper {") {
- tab("@Override")
+ nl("class $className {") {
tab("public android.databinding.ViewDataBinding getDataBinder(android.view.View view, int layoutId) {") {
tab("switch(layoutId) {") {
layoutBinders.groupBy{it.getLayoutname()}.forEach {
diff --git a/tools/data-binding/library/build.gradle b/tools/data-binding/library/build.gradle
index bb078f7..4679d3f 100644
--- a/tools/data-binding/library/build.gradle
+++ b/tools/data-binding/library/build.gradle
@@ -53,6 +53,7 @@
exclude 'META-INF/services/javax.annotation.processing.Processor'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE.txt'
+ exclude 'android/databinding/DataBinderMapper.class'
}
}
dependencies {
@@ -87,6 +88,11 @@
}
}
+afterEvaluate {
+ tasks['packageReleaseJar'].exclude('android/databinding/DataBinderMapper.*')
+ tasks['packageDebugJar'].exclude('android/databinding/DataBinderMapper.*')
+}
+
uploadJarArchives {
repositories {
mavenDeployer {
diff --git a/tools/data-binding/library/src/main/java/android/databinding/DataBinderMapper.java b/tools/data-binding/library/src/main/java/android/databinding/DataBinderMapper.java
index ff6eec4..0376c95 100644
--- a/tools/data-binding/library/src/main/java/android/databinding/DataBinderMapper.java
+++ b/tools/data-binding/library/src/main/java/android/databinding/DataBinderMapper.java
@@ -18,7 +18,16 @@
import android.view.View;
-public interface DataBinderMapper {
- public ViewDataBinding getDataBinder(View view, int layoutId);
- public int getId(String key);
+/**
+ * This class will be stripped from the jar and then replaced by the annotation processor
+ * as part of the code generation step. This class's existence is just to ensure that
+ * compile works and no reflection is needed to access the generated class.
+ */
+public class DataBinderMapper {
+ public ViewDataBinding getDataBinder(View view, int layoutId) {
+ return null;
+ }
+ public int getId(String key) {
+ return 0;
+ }
}
diff --git a/tools/data-binding/library/src/main/java/android/databinding/DataBindingUtil.java b/tools/data-binding/library/src/main/java/android/databinding/DataBindingUtil.java
index 7f30097..cb5d209 100644
--- a/tools/data-binding/library/src/main/java/android/databinding/DataBindingUtil.java
+++ b/tools/data-binding/library/src/main/java/android/databinding/DataBindingUtil.java
@@ -25,22 +25,7 @@
* Utility class to create {@link ViewDataBinding} from layouts.
*/
public class DataBindingUtil {
- private static DataBinderMapper sMapper;
-
- private static DataBinderMapper getMapper() {
- if (sMapper != null) {
- return sMapper;
- }
- try {
- sMapper = (DataBinderMapper) ViewDataBinding.class.getClassLoader()
- .loadClass("android.databinding.GeneratedDataBinderRenderer")
- .newInstance();
- } catch (Throwable t) {
- throw new RuntimeException(t);
- }
- return sMapper;
- }
-
+ private static DataBinderMapper sMapper = new DataBinderMapper();
public static <T extends ViewDataBinding> T inflate(Context context, int layoutId,
ViewGroup parent, boolean attachToParent) {
@@ -51,6 +36,6 @@
@SuppressWarnings("unchecked")
public static <T extends ViewDataBinding> T bindTo(View root, int layoutId) {
- return (T) getMapper().getDataBinder(root, layoutId);
+ return (T) sMapper.getDataBinder(root, layoutId);
}
}