BindingTarget.java revision 2611838bffef5a009ca71e3e9e59a93f29b098ed
1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.databinding.tool;
18
19import android.databinding.tool.expr.Expr;
20import android.databinding.tool.expr.ExprModel;
21import android.databinding.tool.reflection.ModelAnalyzer;
22import android.databinding.tool.reflection.ModelClass;
23import android.databinding.tool.store.ResourceBundle;
24import android.databinding.tool.store.SetterStore;
25import android.databinding.tool.util.L;
26import android.databinding.tool.util.Preconditions;
27
28import java.util.ArrayList;
29import java.util.Arrays;
30import java.util.HashMap;
31import java.util.List;
32import java.util.Map;
33
34public class BindingTarget {
35    List<Binding> mBindings = new ArrayList<Binding>();
36    ExprModel mModel;
37    ModelClass mResolvedClass;
38
39    // if this target presents itself in multiple layout files with different view types,
40    // it receives an interface type and should use it in the getter instead.
41    private ResourceBundle.BindingTargetBundle mBundle;
42
43    public BindingTarget(ResourceBundle.BindingTargetBundle bundle) {
44        mBundle = bundle;
45    }
46
47    public boolean isUsed() {
48        return mBundle.isUsed();
49    }
50
51    public void addBinding(String name, Expr expr) {
52        mBindings.add(new Binding(this, name, expr));
53    }
54
55    public String getInterfaceType() {
56        return mBundle.getInterfaceType() == null ? mBundle.getFullClassName() : mBundle.getInterfaceType();
57    }
58
59    public String getId() {
60        return mBundle.getId();
61    }
62
63    public String getTag() {
64        return mBundle.getTag();
65    }
66
67    public String getOriginalTag() {
68        return mBundle.getOriginalTag();
69    }
70
71    public String getViewClass() {
72        return mBundle.getFullClassName();
73    }
74
75    public ModelClass getResolvedType() {
76        if (mResolvedClass == null) {
77            mResolvedClass = ModelAnalyzer.getInstance().findClass(mBundle.getFullClassName(),
78                    mModel.getImports());
79        }
80        return mResolvedClass;
81    }
82
83    public String getIncludedLayout() {
84        return mBundle.getIncludedLayout();
85    }
86
87    public boolean isBinder() {
88        return getIncludedLayout() != null;
89    }
90
91    public boolean supportsTag() {
92        return !SetterStore.get(ModelAnalyzer.getInstance())
93                .isUntaggable(mBundle.getFullClassName());
94    }
95
96    public List<Binding> getBindings() {
97        return mBindings;
98    }
99
100    public ExprModel getModel() {
101        return mModel;
102    }
103
104    public void setModel(ExprModel model) {
105        mModel = model;
106    }
107
108    /**
109     * Called after BindingTarget is finalized.
110     * <p>
111     * We traverse all bindings and ask SetterStore to figure out if any can be combined.
112     * When N bindings are combined, they are demoted from being a binding expression and a new
113     * ArgList expression is added as the new binding expression that depends on others.
114     */
115    public void resolveMultiSetters() {
116        L.d("resolving multi setters for %s", getId());
117        final SetterStore setterStore = SetterStore.get(ModelAnalyzer.getInstance());
118        final String[] attributes = new String[mBindings.size()];
119        final ModelClass[] types = new ModelClass[mBindings.size()];
120        for (int i = 0; i < mBindings.size(); i ++) {
121            Binding binding = mBindings.get(i);
122            attributes[i] = binding.getName();
123            types[i] = binding.getExpr().getResolvedType();
124        }
125        final List<SetterStore.MultiAttributeSetter> multiAttributeSetterCalls = setterStore
126                .getMultiAttributeSetterCalls(attributes, getResolvedType(), types);
127        if (multiAttributeSetterCalls.isEmpty()) {
128            return;
129        }
130        final Map<String, Binding> lookup = new HashMap<String, Binding>();
131        for (Binding binding : mBindings) {
132            String name = binding.getName();
133            if (name.startsWith("android:")) {
134                lookup.put(name, binding);
135            } else {
136                int ind = name.indexOf(":");
137                if (ind == -1) {
138                    lookup.put(name, binding);
139                } else {
140                    lookup.put(name.substring(ind + 1), binding);
141                }
142            }
143        }
144        List<MergedBinding> mergeBindings = new ArrayList<MergedBinding>();
145        for (final SetterStore.MultiAttributeSetter setter : multiAttributeSetterCalls) {
146            L.d("resolved %s", setter);
147            final List<Binding> mergedBindings = new ArrayList<>();
148            for (String attribute : setter.attributes) {
149                Binding binding = lookup.get(attribute);
150                Preconditions.checkNotNull(binding, "cannot find binding for %s", attribute);
151                mergedBindings.add(binding);
152            }
153
154            for (Binding binding : mergedBindings) {
155                binding.getExpr().setBindingExpression(false);
156                mBindings.remove(binding);
157            }
158            MergedBinding mergedBinding = new MergedBinding(getModel(), setter, this,
159                    mergedBindings);
160            mergeBindings.add(mergedBinding);
161        }
162        for (MergedBinding binding : mergeBindings) {
163            mBindings.add(binding);
164        }
165    }
166}
167