Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 808ebfa3 authored by George Mount's avatar George Mount
Browse files

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.
parent 4cd742ff
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -61,7 +61,7 @@ public class CompilerChef {
        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());
        }
+1 −2
Original line number Diff line number Diff line
@@ -20,8 +20,7 @@ class DataBinderWriter(val pkg: String, val projectPackage: String, val classNam
            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 {
+6 −0
Original line number Diff line number Diff line
@@ -53,6 +53,7 @@ android {
        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 @@ uploadArchives {
    }
}

afterEvaluate {
    tasks['packageReleaseJar'].exclude('android/databinding/DataBinderMapper.*')
    tasks['packageDebugJar'].exclude('android/databinding/DataBinderMapper.*')
}

uploadJarArchives {
    repositories {
        mavenDeployer {
+12 −3
Original line number Diff line number Diff line
@@ -18,7 +18,16 @@ package android.databinding;

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;
    }
}
+2 −17
Original line number Diff line number Diff line
@@ -25,22 +25,7 @@ import android.view.ViewGroup;
 * 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 @@ public class DataBindingUtil {

    @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);
    }
}