IdentifierExpr.java revision 23910cf498c35704a03ba4f3889de2ab97ccbe21
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.processing.ErrorMessages;
20import android.databinding.tool.reflection.ModelAnalyzer;
21import android.databinding.tool.reflection.ModelClass;
22import android.databinding.tool.util.Preconditions;
23import android.databinding.tool.writer.KCode;
24import android.databinding.tool.writer.WriterPackage;
25
26import java.util.ArrayList;
27import java.util.List;
28
29public class IdentifierExpr extends Expr {
30    String mName;
31    String mUserDefinedType;
32    private boolean mIsDeclared;
33
34    IdentifierExpr(String name) {
35        mName = name;
36    }
37
38    public String getName() {
39        return mName;
40    }
41
42    /**
43     * If this is root, its type should be set while parsing the XML document
44     * @param userDefinedType The type of this identifier
45     */
46    public void setUserDefinedType(String userDefinedType) {
47        mUserDefinedType = userDefinedType;
48    }
49
50    @Override
51    protected String computeUniqueKey() {
52        return join(mName, super.computeUniqueKey());
53    }
54
55    public String getUserDefinedType() {
56        return mUserDefinedType;
57    }
58
59    @Override
60    public boolean isDynamic() {
61        return true;
62    }
63
64    @Override
65    protected ModelClass resolveType(final ModelAnalyzer modelAnalyzer) {
66        Preconditions.checkNotNull(mUserDefinedType, ErrorMessages.UNDEFINED_VARIABLE, mName);
67        return modelAnalyzer.findClass(mUserDefinedType, getModel().getImports());
68    }
69
70    @Override
71    protected List<Dependency> constructDependencies() {
72        return new ArrayList<>();
73    }
74
75    @Override
76    protected String asPackage() {
77        return mUserDefinedType == null ? mName : null;
78    }
79
80    @Override
81    protected KCode generateCode() {
82        return new KCode(WriterPackage.getExecutePendingLocalName(this));
83    }
84
85    public void setDeclared() {
86        mIsDeclared = true;
87    }
88
89    public boolean isDeclared() {
90        return mIsDeclared;
91    }
92}
93