Debian Codegraph @

compiler/src/main/java/android/databinding/tool/expr/CastExpr.java

raw
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.List;
24
25 public class CastExpr extends Expr {
26
27 final String mType;
28
29 CastExpr(String type, Expr expr) {
30 super(expr);
31 mType = type;
32 }
33
34 @Override
35 protected ModelClass resolveType(ModelAnalyzer modelAnalyzer) {
36 return modelAnalyzer.findClass(mType, getModel().getImports());
37 }
38
39 @Override
40 protected List<Dependency> constructDependencies() {
41 final List<Dependency> dependencies = constructDynamicChildrenDependencies();
42 for (Dependency dependency : dependencies) {
43 dependency.setMandatory(true);
44 }
45 return dependencies;
46 }
47
48 protected String computeUniqueKey() {
49 return join(mType, getCastExpr().computeUniqueKey());
50 }
51
52 public Expr getCastExpr() {
53 return getChildren().get(0);
54 }
55
56 public String getCastType() {
57 return getResolvedType().toJavaCode();
58 }
59
60 @Override
61 protected KCode generateCode() {
62 return new KCode()
63 .app("(")
64 .app(getCastType())
65 .app(") (", getCastExpr().toCode())
66 .app(")");
67 }
68
69 @Override
70 public String getInvertibleError() {
71 return getCastExpr().getInvertibleError();
72 }
73
74 @Override
75 public Expr generateInverse(ExprModel model, Expr value, String bindingClassName) {
76 Expr castExpr = getCastExpr();
77 ModelClass exprType = castExpr.getResolvedType();
78 Expr castValue = model.castExpr(exprType.toJavaCode(), value);
79 return castExpr.generateInverse(model, castValue, bindingClassName);
80 }
81
82 @Override
83 public Expr cloneToModel(ExprModel model) {
84 return model.castExpr(mType, getCastExpr().cloneToModel(model));
85 }
86
87 @Override
88 public String toString() {
89 return "(" + mType + ") " + getCastExpr();
90 }
91 }