BindingTarget.java revision d6527ee28cc3aa05818799af8def9593346f91bc
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 com.google.common.base.Function; 20import com.google.common.base.Preconditions; 21import com.google.common.base.Predicate; 22import com.google.common.collect.Iterables; 23 24import android.databinding.tool.expr.Expr; 25import android.databinding.tool.expr.ExprModel; 26import android.databinding.tool.reflection.ModelAnalyzer; 27import android.databinding.tool.reflection.ModelClass; 28import android.databinding.tool.store.ResourceBundle; 29import android.databinding.tool.store.SetterStore; 30import android.databinding.tool.util.L; 31 32import java.util.ArrayList; 33import java.util.Arrays; 34import java.util.HashMap; 35import java.util.List; 36import java.util.Map; 37 38public class BindingTarget { 39 List<Binding> mBindings = new ArrayList<Binding>(); 40 ExprModel mModel; 41 ModelClass mResolvedClass; 42 43 // if this target presents itself in multiple layout files with different view types, 44 // it receives an interface type and should use it in the getter instead. 45 private ResourceBundle.BindingTargetBundle mBundle; 46 47 public BindingTarget(ResourceBundle.BindingTargetBundle bundle) { 48 mBundle = bundle; 49 } 50 51 public boolean isUsed() { 52 return mBundle.isUsed(); 53 } 54 55 public void addBinding(String name, Expr expr) { 56 mBindings.add(new Binding(this, name, expr)); 57 } 58 59 public String getInterfaceType() { 60 return mBundle.getInterfaceType() == null ? mBundle.getFullClassName() : mBundle.getInterfaceType(); 61 } 62 63 public String getId() { 64 return mBundle.getId(); 65 } 66 67 public String getTag() { 68 return mBundle.getTag(); 69 } 70 71 public String getOriginalTag() { 72 return mBundle.getOriginalTag(); 73 } 74 75 public String getViewClass() { 76 return mBundle.getFullClassName(); 77 } 78 79 public ModelClass getResolvedType() { 80 if (mResolvedClass == null) { 81 mResolvedClass = ModelAnalyzer.getInstance().findClass(mBundle.getFullClassName(), 82 mModel.getImports()); 83 } 84 return mResolvedClass; 85 } 86 87 public String getIncludedLayout() { 88 return mBundle.getIncludedLayout(); 89 } 90 91 public boolean isBinder() { 92 return getIncludedLayout() != null; 93 } 94 95 public boolean supportsTag() { 96 return !SetterStore.get(ModelAnalyzer.getInstance()) 97 .isUntaggable(mBundle.getFullClassName()); 98 } 99 100 public List<Binding> getBindings() { 101 return mBindings; 102 } 103 104 public ExprModel getModel() { 105 return mModel; 106 } 107 108 public void setModel(ExprModel model) { 109 mModel = model; 110 } 111 112 /** 113 * Called after BindingTarget is finalized. 114 * <p> 115 * We traverse all bindings and ask SetterStore to figure out if any can be combined. 116 * When N bindings are combined, they are demoted from being a binding expression and a new 117 * ArgList expression is added as the new binding expression that depends on others. 118 */ 119 public void resolveMultiSetters() { 120 L.d("resolving multi setters for %s", getId()); 121 final SetterStore setterStore = SetterStore.get(ModelAnalyzer.getInstance()); 122 final String[] attributes = new String[mBindings.size()]; 123 final ModelClass[] types = new ModelClass[mBindings.size()]; 124 for (int i = 0; i < mBindings.size(); i ++) { 125 Binding binding = mBindings.get(i); 126 attributes[i] = binding.getName(); 127 types[i] = binding.getExpr().getResolvedType(); 128 } 129 final List<SetterStore.MultiAttributeSetter> multiAttributeSetterCalls = setterStore 130 .getMultiAttributeSetterCalls(attributes, getResolvedType(), types); 131 if (multiAttributeSetterCalls.isEmpty()) { 132 return; 133 } 134 final Map<String, Binding> lookup = new HashMap<String, Binding>(); 135 for (Binding binding : mBindings) { 136 String name = binding.getName(); 137 if (name.startsWith("android:")) { 138 lookup.put(name, binding); 139 } else { 140 int ind = name.indexOf(":"); 141 if (ind == -1) { 142 lookup.put(name, binding); 143 } else { 144 lookup.put(name.substring(ind + 1), binding); 145 } 146 } 147 } 148 List<MergedBinding> mergeBindings = new ArrayList<MergedBinding>(); 149 for (final SetterStore.MultiAttributeSetter setter : multiAttributeSetterCalls) { 150 L.d("resolved %s", setter); 151 final Binding[] mergedBindings = Iterables.toArray( 152 Iterables.transform(Arrays.asList(setter.attributes), 153 new Function<String, Binding>() { 154 @Override 155 public Binding apply(final String attribute) { 156 L.d("looking for binding for attribute %s", attribute); 157 return lookup.get(attribute); 158 } 159 }), Binding.class) ; 160 Preconditions.checkArgument(mergedBindings.length == setter.attributes.length); 161 for (Binding binding : mergedBindings) { 162 binding.getExpr().setBindingExpression(false); 163 mBindings.remove(binding); 164 } 165 MergedBinding mergedBinding = new MergedBinding(getModel(), setter, this, 166 Arrays.asList(mergedBindings)); 167 mergeBindings.add(mergedBinding); 168 } 169 for (MergedBinding binding : mergeBindings) { 170 mBindings.add(binding); 171 } 172 } 173} 174