KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jode > type > ReferenceType


1 /* ReferenceType Copyright (C) 1999-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: ReferenceType.java,v 1.5.4.1 2002/05/28 17:34:22 hoenicke Exp $
18  */

19
20 package jode.type;
21 import jode.GlobalOptions;
22 import jode.bytecode.ClassInfo;
23 import java.util.Vector JavaDoc;
24 import java.util.Stack JavaDoc;
25
26 /**
27  * This is an abstrace super class of all reference types. Reference
28  * types are ClassInterfacesType, ArrayType and NullType. <p>
29  *
30  * To do intersection on range types, the reference types need three
31  * more operations: specialization, generalization and
32  * createRange. <p>
33  *
34  * specialization chooses all common sub type of two types. It is
35  * used to find the bottom of the intersected interval. <p>
36  *
37  * generalization chooses the common super type of two types. It
38  * is used to find the top of the intersected interval. <p>
39  *
40  * When the new interval is created with <code>createRangeType</code>
41  * the bottom and top are adjusted so that they only consists of
42  * possible types. It then decides, if it needs a range type, or if
43  * the reference types already represents all types.
44  *
45  * @author Jochen Hoenicke
46  */

47 public abstract class ReferenceType extends Type {
48     public ReferenceType(int typecode) {
49     super(typecode);
50     }
51     
52     /**
53      * Returns the specialized type set of this and type. The result
54      * should be a type set, so that every type, extends all types in
55      * type and this, iff it extends all types in the resulting type
56      * set.
57      * @param type the other type.
58      * @return the specialized type. */

59     public abstract Type getSpecializedType(Type type);
60     /**
61      * Returns the generalized type set of this and type. The result
62      * should be a type set, so that every type, is extended/implemented
63      * by one type in this and one type in <code>type</code>, iff it is
64      * extended/implemented by one type in the resulting type set.
65      * @param type the other type.
66      * @return the generalized type
67      */

68     public abstract Type getGeneralizedType(Type type);
69     /**
70      * Creates a range type set of this and bottom. The resulting type set
71      * contains all types, that extend all types in bottom and are extended
72      * by at least one type in this. <br>
73      * Note that a RangeType will do this, but we normalize the bottom and
74      * top set.
75      * @param bottom the bottom type.
76      * @return the range type set.
77      */

78     public abstract Type createRangeType(ReferenceType bottom);
79
80     /**
81      * Tells if all otherIfaces, are implemented by at least one
82      * ifaces or by clazz.
83      *
84      * This is a useful function for generalizing/specializing interface
85      * types or arrays.
86      * @param clazz The clazz, can be null.
87      * @param ifaces The ifaces.
88      * @param otherifaces The other ifaces, that must be implemented.
89      * @return true, if all otherIfaces are implemented.
90      */

91     protected static boolean implementsAllIfaces(ClassInfo clazz,
92                          ClassInfo[] ifaces,
93                          ClassInfo[] otherIfaces) {
94     big:
95         for (int i=0; i < otherIfaces.length; i++) {
96             ClassInfo iface = otherIfaces[i];
97             if (clazz != null && iface.implementedBy(clazz))
98                 continue big;
99             for (int j=0; j < ifaces.length; j++) {
100                 if (iface.implementedBy(ifaces[j]))
101                         continue big;
102             }
103             return false;
104         }
105         return true;
106     }
107
108     public Type getSuperType() {
109     return (this == tObject) ? tObject : tRange(tObject, this);
110     }
111
112     public abstract Type getSubType();
113
114     /**
115      * Intersect this type with another type and return the new type.
116      * @param type the other type.
117      * @return the intersection, or tError, if a type conflict happens.
118      */

119     public Type intersection(Type type) {
120     if (type == tError)
121         return type;
122     if (type == Type.tUnknown)
123         return this;
124
125     Type newBottom = getSpecializedType(type);
126     Type newTop = getGeneralizedType(type);
127     Type result;
128     if (newTop.equals(newBottom))
129         result = newTop;
130     else if (newTop instanceof ReferenceType
131          && newBottom instanceof ReferenceType)
132         result = ((ReferenceType) newTop)
133         .createRangeType((ReferenceType) newBottom);
134     else
135         result = tError;
136
137         if ((GlobalOptions.debuggingFlags & GlobalOptions.DEBUG_TYPES) != 0) {
138         GlobalOptions.err.println("intersecting "+ this +" and "+ type +
139                                    " to " + result);
140     }
141         return result;
142     }
143 }
144
Popular Tags