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 */ 16package android.databinding.tool.expr; 17 18import android.databinding.InverseBindingListener; 19import android.databinding.tool.InverseBinding; 20import android.databinding.tool.reflection.ModelAnalyzer; 21import android.databinding.tool.reflection.ModelClass; 22import android.databinding.tool.writer.KCode; 23import android.databinding.tool.writer.LayoutBinderWriterKt; 24 25import java.util.List; 26 27/** 28 * TwoWayListenerExpr is used to set the event listener for a two-way binding expression. 29 */ 30public class TwoWayListenerExpr extends Expr { 31 final InverseBinding mInverseBinding; 32 33 public TwoWayListenerExpr(InverseBinding inverseBinding) { 34 mInverseBinding = inverseBinding; 35 } 36 37 @Override 38 protected ModelClass resolveType(ModelAnalyzer modelAnalyzer) { 39 return modelAnalyzer.findClass(InverseBindingListener.class); 40 } 41 42 @Override 43 protected List<Dependency> constructDependencies() { 44 return constructDynamicChildrenDependencies(); 45 } 46 47 @Override 48 protected KCode generateCode() { 49 final String fieldName = LayoutBinderWriterKt.getFieldName(mInverseBinding); 50 return new KCode(fieldName); 51 } 52 53 @Override 54 public Expr cloneToModel(ExprModel model) { 55 return model.twoWayListenerExpr(mInverseBinding); 56 } 57 58 @Override 59 protected String computeUniqueKey() { 60 return "event(" + mInverseBinding.getEventAttribute() + ", " + 61 System.identityHashCode(mInverseBinding) + ")"; 62 } 63 64 @Override 65 public String getInvertibleError() { 66 return "Inverted expressions are already inverted!"; 67 } 68} 69