IdentifierExpr.java revision e52882df6130221462bf07f5f2b52de5c4b0f8de
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    IdentifierExpr(String name) {
33        mName = name;
34    }
35
36    public String getName() {
37        return mName;
38    }
39
40    /**
41     * If this is root, its type should be set while parsing the XML document
42     * @param userDefinedType The type of this identifier
43     */
44    public void setUserDefinedType(String userDefinedType) {
45        mUserDefinedType = userDefinedType;
46    }
47
48    @Override
49    protected String computeUniqueKey() {
50        return join(mName, super.computeUniqueKey());
51    }
52
53    public String getUserDefinedType() {
54        return mUserDefinedType;
55    }
56
57    @Override
58    public boolean isDynamic() {
59        return true;
60    }
61
62    @Override
63    protected ModelClass resolveType(final ModelAnalyzer modelAnalyzer) {
64        Preconditions.checkNotNull(mUserDefinedType, ErrorMessages.UNDEFINED_VARIABLE, mName);
65        return modelAnalyzer.findClass(mUserDefinedType, getModel().getImports());
66    }
67
68    @Override
69    protected List<Dependency> constructDependencies() {
70        return new ArrayList<>();
71    }
72
73    @Override
74    protected String asPackage() {
75        return mUserDefinedType == null ? mName : null;
76    }
77
78    @Override
79    protected KCode generateCode() {
80        return new KCode(WriterPackage.getExecutePendingLocalName(this));
81    }
82}
83