BindingTarget.java revision 4d4979490e1fa374c0d7f3599fed0a9e83a579d0
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 isFragment() { 96 return "fragment".equals(getViewClass()); 97 } 98 99 public boolean supportsTag() { 100 return !SetterStore.get(ModelAnalyzer.getInstance()) 101 .isUntaggable(mBundle.getFullClassName()); 102 } 103 104 public List<Binding> getBindings() { 105 return mBindings; 106 } 107 108 public ExprModel getModel() { 109 return mModel; 110 } 111 112 public void setModel(ExprModel model) { 113 mModel = model; 114 } 115 116 /** 117 * Called after BindingTarget is finalized. 118 * <p> 119 * We traverse all bindings and ask SetterStore to figure out if any can be combined. 120 * When N bindings are combined, they are demoted from being a binding expression and a new 121 * ArgList expression is added as the new binding expression that depends on others. 122 */ 123 public void resolveMultiSetters() { 124 L.d("resolving multi setters for %s", getId()); 125 final SetterStore setterStore = SetterStore.get(ModelAnalyzer.getInstance()); 126 final String[] attributes = new String[mBindings.size()]; 127 final ModelClass[] types = new ModelClass[mBindings.size()]; 128 for (int i = 0; i < mBindings.size(); i ++) { 129 Binding binding = mBindings.get(i); 130 attributes[i] = binding.getName(); 131 types[i] = binding.getExpr().getResolvedType(); 132 } 133 final List<SetterStore.MultiAttributeSetter> multiAttributeSetterCalls = setterStore 134 .getMultiAttributeSetterCalls(attributes, getResolvedType(), types); 135 if (multiAttributeSetterCalls.isEmpty()) { 136 return; 137 } 138 final Map<String, Binding> lookup = new HashMap<String, Binding>(); 139 for (Binding binding : mBindings) { 140 String name = binding.getName(); 141 if (name.startsWith("android:")) { 142 lookup.put(name, binding); 143 } else { 144 int ind = name.indexOf(":"); 145 if (ind == -1) { 146 lookup.put(name, binding); 147 } else { 148 lookup.put(name.substring(ind + 1), binding); 149 } 150 } 151 } 152 List<MergedBinding> mergeBindings = new ArrayList<MergedBinding>(); 153 for (final SetterStore.MultiAttributeSetter setter : multiAttributeSetterCalls) { 154 L.d("resolved %s", setter); 155 final Binding[] mergedBindings = Iterables.toArray( 156 Iterables.transform(Arrays.asList(setter.attributes), 157 new Function<String, Binding>() { 158 @Override 159 public Binding apply(final String attribute) { 160 L.d("looking for binding for attribute %s", attribute); 161 return lookup.get(attribute); 162 } 163 }), Binding.class) ; 164 Preconditions.checkArgument(mergedBindings.length == setter.attributes.length); 165 for (Binding binding : mergedBindings) { 166 binding.getExpr().setBindingExpression(false); 167 mBindings.remove(binding); 168 } 169 MergedBinding mergedBinding = new MergedBinding(getModel(), setter, this, 170 Arrays.asList(mergedBindings)); 171 mergeBindings.add(mergedBinding); 172 } 173 for (MergedBinding binding : mergeBindings) { 174 mBindings.add(binding); 175 } 176 } 177} 178