KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > polyglot > ext > jl > types > ReferenceType_c


1 package polyglot.ext.jl.types;
2
3 import polyglot.types.*;
4 import polyglot.util.*;
5 import java.util.*;
6
7 /**
8  * A <code>ReferenceType</code> represents a reference type --
9  * a type on which contains methods and fields and is a subtype of
10  * Object.
11  */

12 public abstract class ReferenceType_c extends Type_c implements ReferenceType
13 {
14     protected ReferenceType_c() {
15     super();
16     }
17
18     public ReferenceType_c(TypeSystem ts) {
19     this(ts, null);
20     }
21
22     public ReferenceType_c(TypeSystem ts, Position pos) {
23     super(ts, pos);
24     }
25
26     public boolean isReference() { return true; }
27     public ReferenceType toReference() { return this; }
28
29     /**
30      * Returns a list of MethodInstances for all the methods declared in this.
31      * It does not return methods declared in supertypes.
32      */

33     public abstract List methods();
34
35     /**
36      * Returns a list of FieldInstances for all the fields declared in this.
37      * It does not return fields declared in supertypes.
38      */

39     public abstract List fields();
40
41     /**
42      * Returns the supertype of this class. For every class except Object,
43      * this is non-null.
44      */

45     public abstract Type superType();
46
47     /**
48      * Returns a list of the types of this class's interfaces.
49      */

50     public abstract List interfaces();
51
52     /** Return true if t has a method mi */
53     public final boolean hasMethod(MethodInstance mi) {
54         return ts.hasMethod(this, mi);
55     }
56
57     /** Return true if t has a method mi */
58     public boolean hasMethodImpl(MethodInstance mi) {
59         for (Iterator j = methods().iterator(); j.hasNext(); ) {
60             MethodInstance mj = (MethodInstance) j.next();
61
62             if (ts.isSameMethod(mi, mj)) {
63                 return true;
64             }
65         }
66
67         return false;
68     }
69
70     public boolean descendsFromImpl(Type ancestor) {
71         if (! ancestor.isCanonical()) {
72             return false;
73         }
74
75         if (ancestor.isNull()) {
76             return false;
77         }
78
79         if (ts.equals(this, ancestor)) {
80             return false;
81         }
82
83         if (! ancestor.isReference()) {
84             return false;
85         }
86
87         if (ts.equals(ancestor, ts.Object())) {
88             return true;
89         }
90
91         // Next check interfaces.
92
for (Iterator i = interfaces().iterator(); i.hasNext(); ) {
93             Type parentType = (Type) i.next();
94
95             if (ts.isSubtype(parentType, ancestor)) {
96                 return true;
97             }
98         }
99
100         return false;
101     }
102
103     public boolean isImplicitCastValidImpl(Type toType) {
104         return ts.isSubtype(this, toType);
105     }
106
107     public List methodsNamed(String JavaDoc name) {
108         List l = new LinkedList();
109
110         for (Iterator i = methods().iterator(); i.hasNext(); ) {
111             MethodInstance mi = (MethodInstance) i.next();
112             if (mi.name().equals(name)) {
113                 l.add(mi);
114             }
115         }
116
117         return l;
118     }
119
120     public List methods(String JavaDoc name, List argTypes) {
121         List l = new LinkedList();
122
123         for (Iterator i = methodsNamed(name).iterator(); i.hasNext(); ) {
124             MethodInstance mi = (MethodInstance) i.next();
125             if (mi.hasFormals(argTypes)) {
126                 l.add(mi);
127             }
128         }
129
130         return l;
131     }
132
133     /**
134      * Requires: all type arguments are canonical. ToType is not a NullType.
135      *
136      * Returns true iff a cast from this to toType is valid; in other
137      * words, some non-null members of this are also members of toType.
138      **/

139     public boolean isCastValidImpl(Type toType) {
140         if (! toType.isReference()) return false;
141         return ts.isSubtype(this, toType) || ts.isSubtype(toType, this);
142     }
143 }
144
Popular Tags