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