Home | History | Annotate | Download | only in expr
      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.reflection.ModelAnalyzer;
     20 import android.databinding.tool.reflection.ModelClass;
     21 import android.databinding.tool.writer.KCode;
     22 
     23 import java.util.ArrayList;
     24 import java.util.List;
     25 
     26 public class BuiltInVariableExpr extends IdentifierExpr {
     27     private final String mAccessCode;
     28 
     29     BuiltInVariableExpr(String name, String type, String accessCode) {
     30         super(name);
     31         super.setUserDefinedType(type);
     32         this.mAccessCode = accessCode;
     33     }
     34 
     35     @Override
     36     public boolean isDynamic() {
     37         return false;
     38     }
     39 
     40     @Override
     41     protected ModelClass resolveType(ModelAnalyzer modelAnalyzer) {
     42         ModelClass modelClass = super.resolveType(modelAnalyzer);
     43         return modelClass;
     44     }
     45 
     46     @Override
     47     protected List<Dependency> constructDependencies() {
     48         return new ArrayList<Dependency>();
     49     }
     50 
     51     @Override
     52     protected KCode generateCode() {
     53         if (mAccessCode == null) {
     54             return new KCode().app(mName);
     55         } else {
     56             return new KCode().app(mAccessCode);
     57         }
     58     }
     59 
     60     public boolean isDeclared() {
     61         return false;
     62     }
     63 
     64     @Override
     65     public String getInvertibleError() {
     66         return "Built-in variables may not be the target of two-way binding";
     67     }
     68 
     69     @Override
     70     public Expr cloneToModel(ExprModel model) {
     71         return model.builtInVariable(mName, mUserDefinedType, mAccessCode);
     72     }
     73 }
     74