MethodCallExpr.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 com.google.common.collect.Iterables; 20 21import android.databinding.tool.reflection.Callable; 22import android.databinding.tool.reflection.Callable.Type; 23import android.databinding.tool.reflection.ModelAnalyzer; 24import android.databinding.tool.reflection.ModelClass; 25import android.databinding.tool.reflection.ModelMethod; 26import android.databinding.tool.util.L; 27 28import java.util.ArrayList; 29import java.util.Arrays; 30import java.util.List; 31 32public class MethodCallExpr extends Expr { 33 final String mName; 34 35 Callable mGetter; 36 37 MethodCallExpr(Expr target, String name, List<Expr> args) { 38 super(Iterables.concat(Arrays.asList(target), args)); 39 mName = name; 40 } 41 42 @Override 43 public void updateExpr(ModelAnalyzer modelAnalyzer) { 44 resolveType(modelAnalyzer); 45 super.updateExpr(modelAnalyzer); 46 } 47 48 @Override 49 protected ModelClass resolveType(ModelAnalyzer modelAnalyzer) { 50 if (mGetter == null) { 51 List<ModelClass> args = new ArrayList<ModelClass>(); 52 for (Expr expr : getArgs()) { 53 args.add(expr.getResolvedType()); 54 } 55 56 Expr target = getTarget(); 57 boolean isStatic = target instanceof StaticIdentifierExpr; 58 ModelMethod method = target.getResolvedType().getMethod(mName, args, isStatic); 59 if (method == null) { 60 String message = "cannot find method '" + mName + "' in class " + 61 target.getResolvedType().toJavaCode(); 62 IllegalArgumentException e = new IllegalArgumentException(message); 63 L.e(e, "cannot find method %s in class %s", mName, 64 target.getResolvedType().toJavaCode()); 65 throw e; 66 } 67 mGetter = new Callable(Type.METHOD, method.getName(), method.getReturnType(args), true, 68 false); 69 } 70 return mGetter.resolvedType; 71 } 72 73 @Override 74 protected List<Dependency> constructDependencies() { 75 final List<Dependency> dependencies = constructDynamicChildrenDependencies(); 76 for (Dependency dependency : dependencies) { 77 if (dependency.getOther() == getTarget()) { 78 dependency.setMandatory(true); 79 } 80 } 81 return dependencies; 82 } 83 84 @Override 85 protected String computeUniqueKey() { 86 return sUniqueKeyJoiner.join(getTarget().computeUniqueKey(), mName, 87 super.computeUniqueKey()); 88 } 89 90 public Expr getTarget() { 91 return getChildren().get(0); 92 } 93 94 public String getName() { 95 return mName; 96 } 97 98 public List<Expr> getArgs() { 99 return getChildren().subList(1, getChildren().size()); 100 } 101 102 public Callable getGetter() { 103 return mGetter; 104 } 105} 106