KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > ArrayType


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 1997-1999 Raja Vallee-Rai
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */

19
20 /*
21  * Modified by the Sable Research Group and others 1997-1999.
22  * See the 'credits' file distributed with Soot for the complete list of
23  * contributors. (Soot is distributed at http://www.sable.mcgill.ca/soot)
24  */

25
26
27 package soot;
28
29 import soot.util.*;
30 import java.util.*;
31
32
33 /**
34  * A class that models Java's array types. ArrayTypes are parametrized by a Type and
35  * and an integer representing the array's dimension count..
36  * Two ArrayType are 'equal' if they are parametrized equally.
37  *
38  *
39  *
40  */

41 public class ArrayType extends RefLikeType
42 {
43     /**
44      * baseType can be any type except for an array type, null and void
45      *
46      * What is the base type of the array? That is, for an array of type
47      * A[][][], how do I find out what the A is? The accepted way of
48      * doing this has always been to look at the public field baseType
49      * in ArrayType, ever since the very beginning of Soot.
50      */

51     public final Type baseType;
52     
53     /** dimension count for the array type*/
54     public final int numDimensions;
55
56    
57     private ArrayType(Type baseType, int numDimensions)
58     {
59         if( !( baseType instanceof PrimType || baseType instanceof RefType ) )
60             throw new RuntimeException JavaDoc( "oops" );
61         if( numDimensions < 1 ) throw new RuntimeException JavaDoc( "attempt to create array with "+numDimensions+" dimensions" );
62         this.baseType = baseType;
63         this.numDimensions = numDimensions;
64     }
65
66      /**
67      * Creates an ArrayType parametrized by a given Type and dimension count.
68      * @param baseType a Type to parametrize the ArrayType
69      * @param numDimensions the dimension count to parametrize the ArrayType.
70      * @return an ArrayType parametrized accrodingly.
71      */

72     public static ArrayType v(Type baseType, int numDimensions)
73     {
74         if( numDimensions < 1 ) throw new RuntimeException JavaDoc( "attempt to create array with "+numDimensions+" dimensions" );
75         ArrayType ret;
76         Type elementType;
77         if( numDimensions == 1 ) {
78             elementType = baseType;
79         } else {
80             elementType = ArrayType.v( baseType, numDimensions-1 );
81         }
82         ret = elementType.getArrayType();
83         if( ret == null ) {
84             ret = new ArrayType(baseType, numDimensions);
85             elementType.setArrayType( ret );
86         }
87         return ret;
88     }
89
90     /**
91      * Two ArrayType are 'equal' if they are parametrized identically.
92      * (ie have same Type and dimension count.
93      * @param t object to test for equality
94      * @return true if t is an ArrayType and is parametrized identically to this.
95      */

96     public boolean equals(Object JavaDoc t)
97     {
98         return t == this;
99         /*
100         if(t instanceof ArrayType)
101         {
102             ArrayType arrayType = (ArrayType) t;
103
104             return this.numDimensions == arrayType.numDimensions &&
105                 this.baseType.equals(arrayType.baseType);
106         }
107         else
108             return false;
109             */

110     }
111
112     public void toString(UnitPrinter up)
113     {
114         up.type( baseType );
115
116         for(int i = 0; i < numDimensions; i++)
117             up.literal("[]");
118     }
119
120     public String JavaDoc toString()
121     {
122         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
123
124         buffer.append(baseType.toString());
125
126         for(int i = 0; i < numDimensions; i++)
127             buffer.append("[]");
128
129         return buffer.toString();
130     }
131
132     public int hashCode()
133     {
134         return baseType.hashCode() + 0x432E0341 * numDimensions;
135     }
136
137     public void apply(Switch sw)
138     {
139         ((TypeSwitch) sw).caseArrayType(this);
140     }
141
142     /**
143      * If I have a variable x of declared type t, what is a good
144      * declared type for the expression ((Object[]) x)[i]? The
145      * getArrayElementType() method in RefLikeType was introduced to
146      * answer this question for all classes implementing RefLikeType. If
147      * t is an array, then the answer is the same as getElementType().
148      * But t could also be Object, Serializable, or Cloneable, which can
149      * all hold any array, so then the answer is Object.
150      */

151     public Type getArrayElementType() {
152     return getElementType();
153     }
154     /**
155      * If I get an element of the array, what will be its type? That
156      * is, if I have an array a of type A[][][], what is the type of
157      * a[] (it's A[][])? The getElementType() method in ArrayType was
158      * introduced to answer this question.
159      */

160     public Type getElementType() {
161     if( numDimensions > 1 ) {
162         return ArrayType.v( baseType, numDimensions-1 );
163     } else {
164         return baseType;
165     }
166     }
167     public ArrayType makeArrayType() {
168         return ArrayType.v( baseType, numDimensions+1 );
169     }
170 }
171
172
Popular Tags