Binding.java revision 97d6ddf47f4ff1abb3ed5201ce5232163f5325b1
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 com.android.databinding;
18
19import com.android.databinding.reflection.ModelAnalyzer;
20import com.android.databinding.reflection.ModelClass;
21import com.android.databinding.reflection.SdkUtil;
22import com.android.databinding.store.SetterStore;
23import com.android.databinding.expr.Expr;
24
25public class Binding {
26
27    private final String mName;
28    private final Expr mExpr;
29    private final BindingTarget mTarget;
30    private SetterStore.SetterCall mSetterCall;
31
32    public Binding(BindingTarget target, String name, Expr expr) {
33        mTarget = target;
34        mName = name;
35        mExpr = expr;
36    }
37
38    private SetterStore.SetterCall getSetterCall() {
39        if (mSetterCall == null) {
40            ModelClass viewType = mTarget.getResolvedType();
41            mSetterCall = SetterStore.get(ModelAnalyzer.getInstance()).getSetterCall(mName,
42                    viewType, mExpr.getResolvedType(), mExpr.getModel().getImports());
43        }
44        return mSetterCall;
45    }
46
47    public BindingTarget getTarget() {
48        return mTarget;
49    }
50
51    public String toJavaCode(String targetViewName, String expressionCode) {
52        return getSetterCall().toJava(targetViewName, expressionCode);
53    }
54
55    /**
56     * The min api level in which this binding should be executed.
57     * <p>
58     * This should be the minimum value among the dependencies of this binding. For now, we only
59     * check the setter.
60     */
61    public int getMinApi() {
62        return getSetterCall().getMinApi();
63    }
64
65//    private String resolveJavaCode(ModelAnalyzer modelAnalyzer) {
66//
67//    }
68////        return modelAnalyzer.findMethod(mTarget.getResolvedType(), mName,
69////                Arrays.asList(mExpr.getResolvedType()));
70//    //}
71//
72
73
74    public String getName() {
75        return mName;
76    }
77
78    public Expr getExpr() {
79        return mExpr;
80    }
81}
82