Home | History | Annotate | Download | only in src
      1 /*
      2  * Copyright (C) 2008 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 
     17 import org.clearsilver.HDF;
     18 import org.clearsilver.CS;
     19 import java.util.*;
     20 import java.io.*;
     21 
     22 public class TypeInfo
     23 {
     24     public TypeInfo(boolean isPrimitive, String dimension,
     25             String simpleTypeName, String qualifiedTypeName,
     26             ClassInfo cl)
     27     {
     28         mIsPrimitive = isPrimitive;
     29         mDimension = dimension;
     30         mSimpleTypeName = simpleTypeName;
     31         mQualifiedTypeName = qualifiedTypeName;
     32         mClass = cl;
     33     }
     34 
     35     public ClassInfo asClassInfo()
     36     {
     37         return mClass;
     38     }
     39 
     40     public boolean isPrimitive()
     41     {
     42         return mIsPrimitive;
     43     }
     44 
     45     public String dimension()
     46     {
     47         return mDimension;
     48     }
     49 
     50     public String simpleTypeName()
     51     {
     52         return mSimpleTypeName;
     53     }
     54 
     55     public String qualifiedTypeName()
     56     {
     57         return mQualifiedTypeName;
     58     }
     59 
     60     public String fullName()
     61     {
     62         if (mFullName != null) {
     63             return mFullName;
     64         } else {
     65             return fullName(new HashSet());
     66         }
     67     }
     68 
     69     public static String typeArgumentsName(TypeInfo[] args, HashSet<String> typeVars)
     70     {
     71         String result = "<";
     72         for (int i=0; i<args.length; i++) {
     73             result += args[i].fullName(typeVars);
     74             if (i != args.length-1) {
     75                 result += ", ";
     76             }
     77         }
     78         result += ">";
     79         return result;
     80     }
     81 
     82     public String fullName(HashSet<String> typeVars)
     83     {
     84         mFullName = fullNameNoDimension(typeVars) + mDimension;
     85         return mFullName;
     86     }
     87 
     88     public String fullNameNoDimension(HashSet<String> typeVars)
     89     {
     90         String fullName = null;
     91         if (mIsTypeVariable) {
     92             if (typeVars.contains(mQualifiedTypeName)) {
     93                 // don't recurse forever with the parameters.  This handles
     94                 // Enum<K extends Enum<K>>
     95                 return mQualifiedTypeName;
     96             }
     97             typeVars.add(mQualifiedTypeName);
     98         }
     99 /*
    100         if (fullName != null) {
    101             return fullName;
    102         }
    103 */
    104         fullName = mQualifiedTypeName;
    105         if (mTypeArguments != null && mTypeArguments.length > 0) {
    106             fullName += typeArgumentsName(mTypeArguments, typeVars);
    107         }
    108         else if (mSuperBounds != null && mSuperBounds.length > 0) {
    109             fullName += " super " + mSuperBounds[0].fullName(typeVars);
    110             for (int i=1; i<mSuperBounds.length; i++) {
    111                 fullName += " & " + mSuperBounds[i].fullName(typeVars);
    112             }
    113         }
    114         else if (mExtendsBounds != null && mExtendsBounds.length > 0) {
    115             fullName += " extends " + mExtendsBounds[0].fullName(typeVars);
    116             for (int i=1; i<mExtendsBounds.length; i++) {
    117                 fullName += " & " + mExtendsBounds[i].fullName(typeVars);
    118             }
    119         }
    120         return fullName;
    121     }
    122 
    123     public TypeInfo[] typeArguments()
    124     {
    125         return mTypeArguments;
    126     }
    127 
    128     public void makeHDF(HDF data, String base)
    129     {
    130         makeHDFRecursive(data, base, false, false, new HashSet<String>());
    131     }
    132 
    133     public void makeQualifiedHDF(HDF data, String base)
    134     {
    135         makeHDFRecursive(data, base, true, false, new HashSet<String>());
    136     }
    137 
    138     public void makeHDF(HDF data, String base, boolean isLastVararg,
    139             HashSet<String> typeVariables)
    140     {
    141         makeHDFRecursive(data, base, false, isLastVararg, typeVariables);
    142     }
    143 
    144     public void makeQualifiedHDF(HDF data, String base, HashSet<String> typeVariables)
    145     {
    146         makeHDFRecursive(data, base, true, false, typeVariables);
    147     }
    148 
    149     private void makeHDFRecursive(HDF data, String base, boolean qualified,
    150             boolean isLastVararg, HashSet<String> typeVars)
    151     {
    152         String label = qualified ? qualifiedTypeName() : simpleTypeName();
    153         label += (isLastVararg) ? "..." : dimension();
    154         data.setValue(base + ".label", label);
    155         ClassInfo cl = asClassInfo();
    156         if (mIsTypeVariable || mIsWildcard) {
    157             // could link to an @param tag on the class to describe this
    158             // but for now, just don't make it a link
    159         }
    160         else if (!isPrimitive() && cl != null && cl.isIncluded()) {
    161             data.setValue(base + ".link", cl.htmlPage());
    162             data.setValue(base + ".since", cl.getSince());
    163         }
    164 
    165         if (mIsTypeVariable) {
    166             if (typeVars.contains(qualifiedTypeName())) {
    167                 // don't recurse forever with the parameters.  This handles
    168                 // Enum<K extends Enum<K>>
    169                 return;
    170             }
    171             typeVars.add(qualifiedTypeName());
    172         }
    173         if (mTypeArguments != null) {
    174             TypeInfo.makeHDF(data, base + ".typeArguments", mTypeArguments, qualified, typeVars);
    175         }
    176         if (mSuperBounds != null) {
    177             TypeInfo.makeHDF(data, base + ".superBounds", mSuperBounds, qualified, typeVars);
    178         }
    179         if (mExtendsBounds != null) {
    180             TypeInfo.makeHDF(data, base + ".extendsBounds", mExtendsBounds, qualified, typeVars);
    181         }
    182     }
    183 
    184     public static void makeHDF(HDF data, String base, TypeInfo[] types, boolean qualified,
    185             HashSet<String> typeVariables)
    186     {
    187         final int N = types.length;
    188         for (int i=0; i<N; i++) {
    189             types[i].makeHDFRecursive(data, base + "." + i, qualified, false, typeVariables);
    190         }
    191     }
    192 
    193     public static void makeHDF(HDF data, String base, TypeInfo[] types, boolean qualified)
    194     {
    195         makeHDF(data, base, types, qualified, new HashSet<String>());
    196     }
    197 
    198     void setTypeArguments(TypeInfo[] args)
    199     {
    200         mTypeArguments = args;
    201     }
    202 
    203     void setBounds(TypeInfo[] superBounds, TypeInfo[] extendsBounds)
    204     {
    205         mSuperBounds = superBounds;
    206         mExtendsBounds = extendsBounds;
    207     }
    208 
    209     void setIsTypeVariable(boolean b)
    210     {
    211         mIsTypeVariable = b;
    212     }
    213 
    214     void setIsWildcard(boolean b)
    215     {
    216         mIsWildcard = b;
    217     }
    218 
    219     static HashSet<String> typeVariables(TypeInfo[] params)
    220     {
    221         return typeVariables(params, new HashSet());
    222     }
    223 
    224     static HashSet<String> typeVariables(TypeInfo[] params, HashSet<String> result)
    225     {
    226         for (TypeInfo t: params) {
    227             if (t.mIsTypeVariable) {
    228                 result.add(t.mQualifiedTypeName);
    229             }
    230         }
    231         return result;
    232     }
    233 
    234 
    235     public boolean isTypeVariable()
    236     {
    237         return mIsTypeVariable;
    238     }
    239 
    240     public String defaultValue() {
    241         if (mIsPrimitive) {
    242             if ("boolean".equals(mSimpleTypeName)) {
    243                 return "false";
    244             } else {
    245                 return "0";
    246             }
    247         } else {
    248             return "null";
    249         }
    250     }
    251 
    252     @Override
    253     public String toString(){
    254       String returnString = "";
    255       returnString += "Primitive?: " + mIsPrimitive + " TypeVariable?: " +
    256       mIsTypeVariable + " Wildcard?: " + mIsWildcard + " Dimension: " + mDimension
    257       + " QualifedTypeName: " + mQualifiedTypeName;
    258 
    259       if (mTypeArguments != null){
    260         returnString += "\nTypeArguments: ";
    261         for (TypeInfo tA : mTypeArguments){
    262           returnString += tA.qualifiedTypeName() + "(" + tA + ") ";
    263         }
    264       }
    265       if (mSuperBounds != null){
    266         returnString += "\nSuperBounds: ";
    267         for (TypeInfo tA : mSuperBounds){
    268           returnString += tA.qualifiedTypeName() + "(" + tA + ") ";
    269         }
    270       }
    271       if (mExtendsBounds != null){
    272         returnString += "\nExtendsBounds: ";
    273         for (TypeInfo tA : mExtendsBounds){
    274           returnString += tA.qualifiedTypeName() + "(" + tA + ") ";
    275         }
    276       }
    277       return returnString;
    278     }
    279 
    280     private boolean mIsPrimitive;
    281     private boolean mIsTypeVariable;
    282     private boolean mIsWildcard;
    283     private String mDimension;
    284     private String mSimpleTypeName;
    285     private String mQualifiedTypeName;
    286     private ClassInfo mClass;
    287     private TypeInfo[] mTypeArguments;
    288     private TypeInfo[] mSuperBounds;
    289     private TypeInfo[] mExtendsBounds;
    290     private String mFullName;
    291 }
    292