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 17 package android.databinding.tool.expr; 18 19 import android.databinding.tool.processing.ErrorMessages; 20 import android.databinding.tool.reflection.ModelAnalyzer; 21 import android.databinding.tool.reflection.ModelClass; 22 import android.databinding.tool.util.Preconditions; 23 import android.databinding.tool.writer.KCode; 24 import android.databinding.tool.writer.LayoutBinderWriterKt; 25 26 import java.util.ArrayList; 27 import java.util.List; 28 29 public class IdentifierExpr extends Expr { 30 String mName; 31 String mUserDefinedType; 32 private boolean mIsDeclared; 33 IdentifierExpr(String name)34 IdentifierExpr(String name) { 35 mName = name; 36 } 37 getName()38 public String getName() { 39 return mName; 40 } 41 42 /** 43 * If this is root, its type should be set while parsing the XML document 44 * @param userDefinedType The type of this identifier 45 */ setUserDefinedType(String userDefinedType)46 public void setUserDefinedType(String userDefinedType) { 47 mUserDefinedType = userDefinedType; 48 } 49 50 @Override computeUniqueKey()51 protected String computeUniqueKey() { 52 return join(mName, super.computeUniqueKey()); 53 } 54 getUserDefinedType()55 public String getUserDefinedType() { 56 return mUserDefinedType; 57 } 58 59 @Override isDynamic()60 public boolean isDynamic() { 61 return true; 62 } 63 64 @Override resolveType(final ModelAnalyzer modelAnalyzer)65 protected ModelClass resolveType(final ModelAnalyzer modelAnalyzer) { 66 Preconditions.checkNotNull(mUserDefinedType, ErrorMessages.UNDEFINED_VARIABLE, mName); 67 return modelAnalyzer.findClass(mUserDefinedType, getModel().getImports()); 68 } 69 70 @Override constructDependencies()71 protected List<Dependency> constructDependencies() { 72 return new ArrayList<Dependency>(); 73 } 74 75 @Override asPackage()76 protected String asPackage() { 77 return mUserDefinedType == null ? mName : null; 78 } 79 80 @Override generateCode(boolean expand)81 protected KCode generateCode(boolean expand) { 82 if (expand) { 83 return new KCode(LayoutBinderWriterKt.getFieldName(this)); 84 } else { 85 return new KCode(LayoutBinderWriterKt.getExecutePendingLocalName(this)); 86 } 87 } 88 setDeclared()89 public void setDeclared() { 90 mIsDeclared = true; 91 } 92 isDeclared()93 public boolean isDeclared() { 94 return mIsDeclared; 95 } 96 97 @Override getInvertibleError()98 public String getInvertibleError() { 99 return null; 100 } 101 102 @Override toInverseCode(KCode value)103 public KCode toInverseCode(KCode value) { 104 return new KCode().app(LayoutBinderWriterKt.getSetterName(this)).app("(", value).app(");"); 105 } 106 } 107