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;
21import android.databinding.tool.writer.KCode;
22
23import java.util.List;
24
25public class InstanceOfExpr extends Expr {
26    final String mTypeStr;
27    ModelClass mType;
28
29    InstanceOfExpr(Expr left, String type) {
30        super(left);
31        mTypeStr = type;
32    }
33
34    @Override
35    protected String computeUniqueKey() {
36        return join("instanceof", super.computeUniqueKey(), mTypeStr);
37    }
38
39    @Override
40    protected KCode generateCode(boolean expand) {
41        return new KCode()
42                .app("", getExpr().toCode(expand))
43                .app(" instanceof ")
44                .app(getType().toJavaCode());
45    }
46
47    @Override
48    protected ModelClass resolveType(ModelAnalyzer modelAnalyzer) {
49        mType = modelAnalyzer.findClass(mTypeStr, getModel().getImports());
50        return modelAnalyzer.loadPrimitive("boolean");
51    }
52
53    @Override
54    protected List<Dependency> constructDependencies() {
55        return constructDynamicChildrenDependencies();
56    }
57
58    public Expr getExpr() {
59        return getChildren().get(0);
60    }
61
62    public ModelClass getType() {
63        return mType;
64    }
65
66    @Override
67    public String getInvertibleError() {
68        return "two-way binding can't target a value with the 'instanceof' operator";
69    }
70}
71