SymbolExpr.java revision 6047998943beebd81e0ae1068df39c0cbee38628
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.solver.ExecutionPath;
22import android.databinding.tool.writer.KCode;
23import android.databinding.tool.writer.LayoutBinderWriterKt;
24
25import java.util.ArrayList;
26import java.util.List;
27
28public class SymbolExpr extends Expr {
29    String mText;
30    Class mType;
31
32    SymbolExpr(String text, Class type) {
33        super();
34        mText = text;
35        mType = type;
36    }
37
38    public String getText() {
39        return mText;
40    }
41
42    @Override
43    protected ModelClass resolveType(ModelAnalyzer modelAnalyzer) {
44        return modelAnalyzer.findClass(mType);
45    }
46
47    @Override
48    protected String computeUniqueKey() {
49        return mType.getSimpleName() + mText;
50    }
51
52    @Override
53    public String getInvertibleError() {
54        return "Symbol '" + mText + "' cannot be the target of a two-way binding expression";
55    }
56
57    @Override
58    protected KCode generateCode(boolean expand) {
59        return new KCode(getText());
60    }
61
62    @Override
63    protected List<Dependency> constructDependencies() {
64        return new ArrayList<Dependency>();
65    }
66
67    @Override
68    public boolean canBeEvaluatedToAVariable() {
69        return !void.class.equals(mType);
70    }
71
72    @Override
73    public List<ExecutionPath> toExecutionPath(List<ExecutionPath> paths) {
74        if (void.class.equals(mType)) {
75            return paths;
76        }
77        return super.toExecutionPath(paths);
78    }
79}
80