summaryrefslogtreecommitdiffstats
path: root/tools/droiddoc/src/TypeInfo.java
blob: c1119de5ea3b71e201a52a5fb68b316bdadbe336 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import org.clearsilver.HDF;
import org.clearsilver.CS;
import java.util.*;
import java.io.*;

public class TypeInfo
{
    public TypeInfo(boolean isPrimitive, String dimension,
            String simpleTypeName, String qualifiedTypeName,
            ClassInfo cl)
    {
        mIsPrimitive = isPrimitive;
        mDimension = dimension;
        mSimpleTypeName = simpleTypeName;
        mQualifiedTypeName = qualifiedTypeName;
        mClass = cl;
    }

    public ClassInfo asClassInfo()
    {
        return mClass;
    }

    public boolean isPrimitive()
    {
        return mIsPrimitive;
    }

    public String dimension()
    {
        return mDimension;
    }

    public String simpleTypeName()
    {
        return mSimpleTypeName;
    }

    public String qualifiedTypeName()
    {
        return mQualifiedTypeName;
    }

    public String fullName()
    {
        if (mFullName != null) {
            return mFullName;
        } else {
            return fullName(new HashSet());
        }
    }

    public static String typeArgumentsName(TypeInfo[] args, HashSet<String> typeVars)
    {
        String result = "<";
        for (int i=0; i<args.length; i++) {
            result += args[i].fullName(typeVars);
            if (i != args.length-1) {
                result += ", ";
            }
        }
        result += ">";
        return result;
    }

    public String fullName(HashSet<String> typeVars)
    {
        mFullName = fullNameNoDimension(typeVars) + mDimension;
        return mFullName;
    }

    public String fullNameNoDimension(HashSet<String> typeVars)
    {
        String fullName = null;
        if (mIsTypeVariable) {
            if (typeVars.contains(mQualifiedTypeName)) {
                // don't recurse forever with the parameters.  This handles
                // Enum<K extends Enum<K>>
                return mQualifiedTypeName;
            }
            typeVars.add(mQualifiedTypeName);
        }
/*
        if (fullName != null) {
            return fullName;
        }
*/
        fullName = mQualifiedTypeName;
        if (mTypeArguments != null && mTypeArguments.length > 0) {
            fullName += typeArgumentsName(mTypeArguments, typeVars);
        }
        else if (mSuperBounds != null && mSuperBounds.length > 0) {
            fullName += " super " + mSuperBounds[0].fullName(typeVars);
            for (int i=1; i<mSuperBounds.length; i++) {
                fullName += " & " + mSuperBounds[i].fullName(typeVars);
            }
        }
        else if (mExtendsBounds != null && mExtendsBounds.length > 0) {
            fullName += " extends " + mExtendsBounds[0].fullName(typeVars);
            for (int i=1; i<mExtendsBounds.length; i++) {
                fullName += " & " + mExtendsBounds[i].fullName(typeVars);
            }
        }
        return fullName;
    }

    public TypeInfo[] typeArguments()
    {
        return mTypeArguments;
    }

    public void makeHDF(HDF data, String base)
    {
        makeHDFRecursive(data, base, false, false, new HashSet<String>());
    }

    public void makeQualifiedHDF(HDF data, String base)
    {
        makeHDFRecursive(data, base, true, false, new HashSet<String>());
    }

    public void makeHDF(HDF data, String base, boolean isLastVararg,
            HashSet<String> typeVariables)
    {
        makeHDFRecursive(data, base, false, isLastVararg, typeVariables);
    }

    public void makeQualifiedHDF(HDF data, String base, HashSet<String> typeVariables)
    {
        makeHDFRecursive(data, base, true, false, typeVariables);
    }

    private void makeHDFRecursive(HDF data, String base, boolean qualified,
            boolean isLastVararg, HashSet<String> typeVars)
    {
        String label = qualified ? qualifiedTypeName() : simpleTypeName();
        label += (isLastVararg) ? "..." : dimension();
        data.setValue(base + ".label", label);
        ClassInfo cl = asClassInfo();
        if (mIsTypeVariable || mIsWildcard) {
            // could link to an @param tag on the class to describe this
            // but for now, just don't make it a link
        }
        else if (!isPrimitive() && cl != null && cl.isIncluded()) {
            data.setValue(base + ".link", cl.htmlPage());
        }

        if (mIsTypeVariable) {
            if (typeVars.contains(qualifiedTypeName())) {
                // don't recurse forever with the parameters.  This handles
                // Enum<K extends Enum<K>>
                return;
            }
            typeVars.add(qualifiedTypeName());
        }
        if (mTypeArguments != null) {
            TypeInfo.makeHDF(data, base + ".typeArguments", mTypeArguments, qualified, typeVars);
        }
        if (mSuperBounds != null) {
            TypeInfo.makeHDF(data, base + ".superBounds", mSuperBounds, qualified, typeVars);
        }
        if (mExtendsBounds != null) {
            TypeInfo.makeHDF(data, base + ".extendsBounds", mExtendsBounds, qualified, typeVars);
        }
    }

    public static void makeHDF(HDF data, String base, TypeInfo[] types, boolean qualified,
            HashSet<String> typeVariables)
    {
        final int N = types.length;
        for (int i=0; i<N; i++) {
            types[i].makeHDFRecursive(data, base + "." + i, qualified, false, typeVariables);
        }
    }

    public static void makeHDF(HDF data, String base, TypeInfo[] types, boolean qualified)
    {
        makeHDF(data, base, types, qualified, new HashSet<String>());
    }

    void setTypeArguments(TypeInfo[] args)
    {
        mTypeArguments = args;
    }

    void setBounds(TypeInfo[] superBounds, TypeInfo[] extendsBounds)
    {
        mSuperBounds = superBounds;
        mExtendsBounds = extendsBounds;
    }

    void setIsTypeVariable(boolean b)
    {
        mIsTypeVariable = b;
    }

    void setIsWildcard(boolean b)
    {
        mIsWildcard = b;
    }

    static HashSet<String> typeVariables(TypeInfo[] params)
    {
        return typeVariables(params, new HashSet());
    }

    static HashSet<String> typeVariables(TypeInfo[] params, HashSet<String> result)
    {
        for (TypeInfo t: params) {
            if (t.mIsTypeVariable) {
                result.add(t.mQualifiedTypeName);
            }
        }
        return result;
    }


    public boolean isTypeVariable()
    {
        return mIsTypeVariable;
    }

    public String defaultValue() {
        if (mIsPrimitive) {
            if ("boolean".equals(mSimpleTypeName)) {
                return "false";
            } else {
                return "0";
            }
        } else {
            return "null";
        }
    }

    public String toString(){
      String returnString = "";
      returnString += "Primitive?: " + mIsPrimitive + " TypeVariable?: " +
      mIsTypeVariable + " Wildcard?: " + mIsWildcard + " Dimension: " + mDimension
      + " QualifedTypeName: " + mQualifiedTypeName;

      if (mTypeArguments != null){
        returnString += "\nTypeArguments: ";
        for (TypeInfo tA : mTypeArguments){
          returnString += tA.qualifiedTypeName() + "(" + tA + ") ";
        }
      }
      if (mSuperBounds != null){
        returnString += "\nSuperBounds: ";
        for (TypeInfo tA : mSuperBounds){
          returnString += tA.qualifiedTypeName() + "(" + tA + ") ";
        }
      }
      if (mExtendsBounds != null){
        returnString += "\nExtendsBounds: ";
        for (TypeInfo tA : mExtendsBounds){
          returnString += tA.qualifiedTypeName() + "(" + tA + ") ";
        }
      }
      return returnString;
    }

    private boolean mIsPrimitive;
    private boolean mIsTypeVariable;
    private boolean mIsWildcard;
    private String mDimension;
    private String mSimpleTypeName;
    private String mQualifiedTypeName;
    private ClassInfo mClass;
    private TypeInfo[] mTypeArguments;
    private TypeInfo[] mSuperBounds;
    private TypeInfo[] mExtendsBounds;
    private String mFullName;
}