InjectedMethod.java revision 11df39c91611b9ff2d7c87a9a9829251a015bccf
1/*
2 * Copyright (C) 2016 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.reflection;
18
19import java.util.List;
20import java.util.Map;
21
22/**
23 * A class that can be used by ModelAnalyzer without any backing model. This is used
24 * for methods on ViewDataBinding subclasses that haven't been generated yet.
25 *
26 * @see ModelAnalyzer#injectViewDataBinding(String, Map, Map)
27 */
28public class InjectedMethod extends ModelMethod {
29    private final InjectedClass mContainingClass;
30    private final String mName;
31    private final String mReturnTypeName;
32    private final String[] mParameterTypeNames;
33    private ModelClass[] mParameterTypes;
34    private ModelClass mReturnType;
35    private boolean mIsStatic;
36
37    public InjectedMethod(InjectedClass containingClass, boolean isStatic, String name,
38            String returnType, String... parameters) {
39        mContainingClass = containingClass;
40        mName = name;
41        mIsStatic = isStatic;
42        mReturnTypeName = returnType;
43        mParameterTypeNames = parameters;
44    }
45
46    @Override
47    public ModelClass getDeclaringClass() {
48        return mContainingClass;
49    }
50
51    @Override
52    public ModelClass[] getParameterTypes() {
53        if (mParameterTypes == null) {
54            if (mParameterTypeNames == null) {
55                mParameterTypes = new ModelClass[0];
56            } else {
57                mParameterTypes = new ModelClass[mParameterTypeNames.length];
58                ModelAnalyzer modelAnalyzer = ModelAnalyzer.getInstance();
59                for (int i = 0; i < mParameterTypeNames.length; i++) {
60                    mParameterTypes[i] = modelAnalyzer.findClass(mParameterTypeNames[i], null);
61                }
62            }
63        }
64        return mParameterTypes;
65    }
66
67    @Override
68    public String getName() {
69        return mName;
70    }
71
72    @Override
73    public ModelClass getReturnType(List<ModelClass> args) {
74        if (mReturnType == null) {
75            mReturnType = ModelAnalyzer.getInstance().findClass(mReturnTypeName, null);
76        }
77        return mReturnType;
78    }
79
80    @Override
81    public boolean isVoid() {
82        return getReturnType().isVoid();
83    }
84
85    @Override
86    public boolean isPublic() {
87        return true;
88    }
89
90    @Override
91    public boolean isProtected() {
92        return false;
93    }
94
95    @Override
96    public boolean isStatic() {
97        return mIsStatic;
98    }
99
100    @Override
101    public boolean isAbstract() {
102        return true;
103    }
104
105    @Override
106    public boolean isBindable() {
107        return false;
108    }
109
110    @Override
111    public int getMinApi() {
112        return 0;
113    }
114
115    @Override
116    public String getJniDescription() {
117        return TypeUtil.getInstance().getDescription(this);
118    }
119
120    @Override
121    public boolean isVarArgs() {
122        return false;
123    }
124
125    @Override
126    public String toString() {
127        StringBuilder sb = new StringBuilder("public ");
128        if (mIsStatic) {
129            sb.append("static ");
130        }
131        sb.append(mReturnTypeName)
132                .append(' ')
133                .append(mName)
134                .append("(");
135        if (mParameterTypeNames != null) {
136            for (int i = 0; i < mParameterTypeNames.length; i++) {
137                if (i != 0) {
138                    sb.append(", ");
139                }
140                sb.append(mParameterTypeNames[i]);
141            }
142        }
143        sb.append(')');
144        return sb.toString();
145    }
146}
147