BracketExpr.java revision fead9ca09b117136b35bc5bf137340a754f9eddd
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.expr; 18 19import android.databinding.tool.reflection.ModelAnalyzer; 20import android.databinding.tool.reflection.ModelClass; 21 22import java.util.List; 23 24public class BracketExpr extends Expr { 25 26 public static enum BracketAccessor { 27 ARRAY, 28 LIST, 29 MAP, 30 } 31 32 private BracketAccessor mAccessor; 33 34 BracketExpr(Expr target, Expr arg) { 35 super(target, arg); 36 } 37 38 @Override 39 protected ModelClass resolveType(ModelAnalyzer modelAnalyzer) { 40 ModelClass targetType = getTarget().resolveType(modelAnalyzer); 41 if (targetType.isArray()) { 42 mAccessor = BracketAccessor.ARRAY; 43 } else if (targetType.isList()) { 44 mAccessor = BracketAccessor.LIST; 45 } else if (targetType.isMap()) { 46 mAccessor = BracketAccessor.MAP; 47 } else { 48 throw new IllegalArgumentException("Cannot determine variable type used in [] " + 49 "expression. Cast the value to List, Map, " + 50 "or array. Type detected: " + targetType.toJavaCode()); 51 } 52 return targetType.getComponentType(); 53 } 54 55 @Override 56 protected List<Dependency> constructDependencies() { 57 return constructDynamicChildrenDependencies(); 58 } 59 60 protected String computeUniqueKey() { 61 return sUniqueKeyJoiner.join(getTarget().computeUniqueKey(), "$", getArg().computeUniqueKey(), "$"); 62 } 63 64 public Expr getTarget() { 65 return getChildren().get(0); 66 } 67 68 public Expr getArg() { 69 return getChildren().get(1); 70 } 71 72 public BracketAccessor getAccessor() { 73 return mAccessor; 74 } 75 76 public boolean argCastsInteger() { 77 return Object.class.equals(getArg().getResolvedType()); 78 } 79} 80