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

Commit 0e7ca405 authored by George Mount's avatar George Mount
Browse files

Support static method and field access and improve method finding.

Bug 19425630
Bug 19336295

Change-Id: I4c04db32492edfa093e94c3c15bf7799128b1e03
parent 371450b5
Loading
Loading
Loading
Loading
+74 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.databinding.testapp;

import com.android.databinding.testapp.generated.FindMethodTestBinder;
import com.android.databinding.testapp.vo.FindMethodBindingObject;

import android.widget.TextView;

public class FindMethodTest
        extends BindingAdapterTestBase<FindMethodTestBinder, FindMethodBindingObject> {

    public FindMethodTest() {
        super(FindMethodTestBinder.class, FindMethodBindingObject.class, R.layout.find_method_test);
    }

    public void testNoArg() throws Throwable {
        TextView textView = mBinder.getTextView6();
        assertEquals("no arg", textView.getText().toString());
    }

    public void testIntArg() throws Throwable {
        TextView textView = mBinder.getTextView0();
        assertEquals("1", textView.getText().toString());
    }

    public void testFloatArg() throws Throwable {
        TextView textView = mBinder.getTextView1();
        assertEquals("1.25", textView.getText().toString());
    }

    public void testStringArg() throws Throwable {
        TextView textView = mBinder.getTextView2();
        assertEquals("hello", textView.getText().toString());
    }

    public void testBoxedArg() throws Throwable {
        TextView textView = mBinder.getTextView3();
        assertEquals("1", textView.getText().toString());
    }

    public void testInheritedMethod() throws Throwable {
        TextView textView = mBinder.getTextView4();
        assertEquals("base", textView.getText().toString());
    }

    public void testInheritedMethodInt() throws Throwable {
        TextView textView = mBinder.getTextView5();
        assertEquals("base 2", textView.getText().toString());
    }

    public void testStaticMethod() throws Throwable {
        TextView textView = mBinder.getTextView7();
        assertEquals("world", textView.getText().toString());
    }

    public void testStaticField() throws Throwable {
        TextView textView = mBinder.getTextView8();
        assertEquals("hello world", textView.getText().toString());
    }
}
+35 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.databinding.testapp.vo;

public class FindMethodBindingObject extends FindMethodBindingObjectBase {
    public String method() { return "no arg"; }

    public String method(int i) { return String.valueOf(i); }

    public String method(float f) { return String.valueOf(f); }

    public String method(String value) { return value; }

    public static String staticMethod() { return "world"; }

    public static Foo foo = new Foo();


    public static class Foo {
        public final String bar = "hello world";
    }
}
+30 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.databinding.testapp.vo;

public class FindMethodBindingObjectBase extends BindingAdapterBindingObject {
    public String inheritedMethod() {
        return "base";
    }

    public String inheritedMethod(int i) {
        return "base " + i;
    }

    @Override
    public void changeValues() {
    }
}
+46 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
    <variable name="obj" type="com.android.databinding.testapp.vo.FindMethodBindingObject"/>
    <!--
    <import name="FMBO" type=""/>
-->
    <TextView
            android:id="@+id/textView0"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:text="@{obj.method(1)}"/>
    <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:text="@{obj.method(1.25f}"/>
    <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:text="@{obj.method(`hello`}"/>
    <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:text="@{obj.method((java.lang.Integer) 1)}"/>
    <TextView
            android:id="@+id/textView4"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:text="@{obj.inheritedMethod()}"/>
    <TextView
            android:id="@+id/textView5"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:text="@{obj.inheritedMethod(2)}"/>
    <TextView
            android:id="@+id/textView6"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:text="@{obj.method()}"/>
    <TextView
            android:id="@+id/textView7"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:text="@{com.android.databinding.testapp.vo.FindMethodBindingObject.staticMethod()}"/>
    <TextView
            android:id="@+id/textView8"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:text="@{com.android.databinding.testapp.vo.FindMethodBindingObject.foo.bar}"/>
</LinearLayout>
 No newline at end of file
+40 −5
Original line number Diff line number Diff line
@@ -16,11 +16,6 @@

package com.android.databinding.expr;

import java.util.ArrayList;
import java.util.BitSet;
import java.util.Collections;
import java.util.List;

import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
@@ -31,6 +26,11 @@ import com.google.common.collect.Lists;
import com.android.databinding.reflection.ModelAnalyzer;
import com.android.databinding.reflection.ModelClass;

import java.util.ArrayList;
import java.util.BitSet;
import java.util.Collections;
import java.util.List;

abstract public class Expr {

    public static final int NO_ID = -1;
@@ -570,6 +570,41 @@ abstract public class Expr {
    }

    public void updateExpr(ModelAnalyzer modelAnalyzer) {
        for (Expr child : mChildren) {
            child.updateExpr(modelAnalyzer);
        }
    }

    protected void replaceStaticAccess(ModelAnalyzer modelAnalyzer) {
        for (int i = 0; i < mChildren.size(); i++) {
            Expr child = mChildren.get(i);
            String packageName = child.asPackage();
            if (packageName != null) {
                ModelClass modelClass = modelAnalyzer.findClass(packageName);
                if (modelClass != null) {
                    Expr staticAccessExpr = getModel().staticAccessExpr(modelClass);
                    staticAccessExpr.getParents().add(this);
                    mChildren.set(i, staticAccessExpr);
                    child.removeParentAndUnregisterIfOrphan(this);
                }
            }
        }
    }

    private void removeParentAndUnregisterIfOrphan(Expr parent) {
        while (mParents.remove(parent)) {
        }
        if (getParents().isEmpty()) {
            getModel().unregister(this);
            for (Expr expr : mChildren) {
                expr.removeParentAndUnregisterIfOrphan(this);
            }
            mChildren.clear();
        }
    }

    protected String asPackage() {
        return null;
    }

    static class Node {
Loading