KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jode > type > RangeType


1 /* RangeType Copyright (C) 1998-2002 Jochen Hoenicke.
2  *
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation; either version 2, or (at your option)
6  * any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; see the file COPYING. If not, write to
15  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
16  *
17  * $Id: RangeType.java,v 4.15.2.2 2002/05/28 17:34:22 hoenicke Exp $
18  */

19
20 package jode.type;
21 import jode.AssertError;
22 import jode.GlobalOptions;
23 import java.util.Hashtable JavaDoc;
24
25 /**
26  * This class represents a set of reference types. The set contains
27  * all types that are castable to all the bottom types by a widening
28  * cast and to which one of the top types can be casted to by a
29  * widening cast. <br>
30  *
31  * For a totally unknown reference type bottomType is tObject and
32  * topType is tNull. The bottomType is always guaranteed to be not
33  * tNull. And all topTypes must be castable to all bottom types with
34  * a widening cast. <br>
35  *
36  * To do intersection on range types, the reference types need three
37  * more operations: specialization, generalization and
38  * createRange. <p>
39  *
40  * specialization chooses all common sub type of two types. It is
41  * used to find the bottom of the intersected interval. <p>
42  *
43  * generalization chooses the common super type of two types. It
44  * is used to find the top of the intersected interval. <p>
45  *
46  * When the new interval is created with <code>createRangeType</code>
47  * the bottom and top are adjusted so that they only consists of
48  * possible types. It then decides, if it needs a range type, or if
49  * the reference types already represents all types.
50  *
51  * @author Jochen Hoenicke
52  * @see ReferenceType
53  * @date 98/08/06 */

54 public class RangeType extends Type {
55     /**
56      * The bottom type set. It is special in that its interpretation
57      * depends on what ReferenceType class it implements:
58      *
59      * <dl>
60      * <dt>ClassInterfacesType</dt>
61      * <dd>All types in this range must be widening castable to all interfaces
62      * and to the class in the bottomType</dd>
63      * <dt>ArrayType</dt>
64      * <dd>All types in this range must be of the bottomType, or the
65      * NullType.</dd>
66      * <dt>NullType</dt>
67      * <dd>not allowed</dd>
68      * </dl> */

69     final ReferenceType bottomType;
70     /**
71      * The top type set. For each type in this range type, there is a
72      * top type, that can be casted to this type.
73      */

74     final ReferenceType topType;
75
76     /**
77      * Create a new range type with the given bottom and top set.
78      */

79     public RangeType(ReferenceType bottomType,
80              ReferenceType topType) {
81         super(TC_RANGE);
82     if (bottomType == tNull)
83         throw new jode.AssertError("bottom is NULL");
84     this.bottomType = bottomType;
85     this.topType = topType;
86     }
87
88     /**
89      * Returns the bottom type set. All types in this range type can
90      * be casted to all bottom types by a widening cast.
91      * @return the bottom type set
92      */

93     public ReferenceType getBottom() {
94         return bottomType;
95     }
96
97     /**
98      * Returns the top type set. For each type in this range type,
99      * there is a top type, that can be casted to this type.
100      * @return the top type set
101      */

102     public ReferenceType getTop() {
103         return topType;
104     }
105
106     
107     /**
108      * Returns the hint type of this range type set. This returns the
109      * singleton set containing only the first top type, except if it
110      * is null and there is a unique bottom type, in which case it
111      * returns the bottom type.
112      * @return the hint type.
113      */

114     public Type getHint() {
115     Type bottomHint = bottomType.getHint();
116     Type topHint = topType.getHint();
117     
118     if (topType == tNull && bottomType.equals(bottomHint))
119         return bottomHint;
120
121     return topHint;
122     }
123
124     /**
125      * Returns the canonic type of this range type set. This returns the
126      * singleton set containing only the first top type.
127      * @return the canonic type.
128      */

129     public Type getCanonic() {
130     return topType.getCanonic();
131     }
132
133     /**
134      * The set of super types of this type. This is the set of
135      * super types of the top type.
136      * @return the set of super types.
137      */

138     public Type getSuperType() {
139     return topType.getSuperType();
140     }
141
142     /**
143      * The set of sub types of this type. This is the set of
144      * sub types of the bottom types.
145      * @return the set of super types.
146      */

147     public Type getSubType() {
148     return tRange(bottomType, tNull);
149     }
150         
151     /**
152      * Checks if we need to cast to a middle type, before we can cast from
153      * fromType to this type.
154      * @return the middle type, or null if it is not necessary.
155      */

156     public Type getCastHelper(Type fromType) {
157     return topType.getCastHelper(fromType);
158     }
159
160     public String JavaDoc getTypeSignature() {
161         if (topType.isClassType() || !bottomType.isValidType())
162             return topType.getTypeSignature();
163         else
164             return bottomType.getTypeSignature();
165     }
166
167     public Class JavaDoc getTypeClass() throws ClassNotFoundException JavaDoc {
168         if (topType.isClassType() || !bottomType.isValidType())
169             return topType.getTypeClass();
170         else
171             return bottomType.getTypeClass();
172     }
173
174     public String JavaDoc toString()
175     {
176     return "<" + bottomType + "-" + topType + ">";
177     }
178
179     public String JavaDoc getDefaultName() {
180     throw new AssertError("getDefaultName() called on range");
181     }
182
183     public int hashCode() {
184     int hashcode = topType.hashCode();
185     return (hashcode << 16 | hashcode >>> 16) ^ bottomType.hashCode();
186     }
187
188     public boolean equals(Object JavaDoc o) {
189         if (o instanceof RangeType) {
190             RangeType type = (RangeType) o;
191             return topType.equals(type.topType)
192                 && bottomType.equals(type.bottomType);
193         }
194         return false;
195     }
196
197     /**
198      * Intersect this type with another type and return the new type.
199      * @param type the other type.
200      * @return the intersection, or tError, if a type conflict happens.
201      */

202     public Type intersection(Type type) {
203     if (type == tError)
204         return type;
205     if (type == Type.tUnknown)
206         return this;
207
208     Type top, bottom, result;
209     bottom = bottomType.getSpecializedType(type);
210     top = topType.getGeneralizedType(type);
211     if (top.equals(bottom))
212         result = top;
213     else if (top instanceof ReferenceType
214          && bottom instanceof ReferenceType)
215         result = ((ReferenceType)top)
216         .createRangeType((ReferenceType)bottom);
217     else
218         result = tError;
219
220         if ((GlobalOptions.debuggingFlags & GlobalOptions.DEBUG_TYPES) != 0) {
221         GlobalOptions.err.println("intersecting "+ this +" and "+ type +
222                       " to " + result);
223     }
224         return result;
225     }
226 }
227
228
Popular Tags