KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > polyglot > ext > jl > types > ProcedureInstance_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>ProcedureInstance_c</code> contains the type information for a Java
9  * procedure (either a method or a constructor).
10  */

11 public abstract class ProcedureInstance_c extends TypeObject_c
12                                        implements ProcedureInstance
13 {
14     protected ReferenceType container;
15     protected Flags flags;
16     protected List formalTypes;
17     protected List excTypes;
18
19     /** Used for deserializing types. */
20     protected ProcedureInstance_c() { }
21
22     public ProcedureInstance_c(TypeSystem ts, Position pos,
23                    ReferenceType container,
24                    Flags flags, List formalTypes, List excTypes) {
25         super(ts, pos);
26     this.container = container;
27     this.flags = flags;
28     this.formalTypes = TypedList.copyAndCheck(formalTypes, Type.class, true);
29     this.excTypes = TypedList.copyAndCheck(excTypes, Type.class, true);
30     }
31
32     public ReferenceType container() {
33         return container;
34     }
35
36     public Flags flags() {
37         return flags;
38     }
39
40     public List formalTypes() {
41         return Collections.unmodifiableList(formalTypes);
42     }
43
44     public List throwTypes() {
45         return Collections.unmodifiableList(excTypes);
46     }
47
48     public int hashCode() {
49         return container.hashCode() + flags.hashCode();
50     }
51
52     public boolean equalsImpl(TypeObject o) {
53         if (o instanceof ProcedureInstance) {
54         ProcedureInstance i = (ProcedureInstance) o;
55         // FIXME: Check excTypes too?
56
return flags.equals(i.flags())
57             && ts.equals(container, i.container())
58             && ts.hasFormals(this, i.formalTypes());
59     }
60
61     return false;
62     }
63
64     protected boolean listIsCanonical(List l) {
65     for (Iterator i = l.iterator(); i.hasNext(); ) {
66         TypeObject o = (TypeObject) i.next();
67         if (! o.isCanonical()) {
68         return false;
69         }
70     }
71
72     return true;
73     }
74
75     public final boolean moreSpecific(ProcedureInstance p) {
76         return ts.moreSpecific(this, p);
77     }
78
79
80     /**
81      * Returns whether <code>this</code> is <i>more specific</i> than
82      * <code>p</code>, where <i>more specific</i> is defined as JLS
83      * 15.12.2.2.
84      *<p>
85      * <b>Note:</b> There is a fair amount of guesswork since the JLS
86      * does not include any info regarding Java 1.2, so all inner class
87      * rules are found empirically using jikes and javac.
88      */

89     public boolean moreSpecificImpl(ProcedureInstance p) {
90         ProcedureInstance p1 = this;
91         ProcedureInstance p2 = p;
92
93         // rule 1:
94
ReferenceType t1 = p1.container();
95         ReferenceType t2 = p2.container();
96
97         if (t1.isClass() && t2.isClass()) {
98             if (! t1.isSubtype(t2) &&
99                 ! t1.toClass().isEnclosed(t2.toClass())) {
100                 return false;
101             }
102         }
103         else {
104             if (! t1.isSubtype(t2)) {
105                 return false;
106             }
107         }
108
109         // rule 2:
110
return p2.callValid(p1.formalTypes());
111     }
112
113     /** Returns true if the procedure has the given formal parameter types. */
114     public final boolean hasFormals(List formalTypes) {
115         return ts.hasFormals(this, formalTypes);
116     }
117
118     /** Returns true if the procedure has the given formal parameter types. */
119     public boolean hasFormalsImpl(List formalTypes) {
120         List l1 = this.formalTypes();
121         List l2 = formalTypes;
122
123         Iterator i1 = l1.iterator();
124         Iterator i2 = l2.iterator();
125
126         while (i1.hasNext() && i2.hasNext()) {
127             Type t1 = (Type) i1.next();
128             Type t2 = (Type) i2.next();
129
130             if (! ts.equals(t1, t2)) {
131                 return false;
132             }
133         }
134
135         return ! (i1.hasNext() || i2.hasNext());
136     }
137
138     /** Returns true iff <code>this</code> throws fewer exceptions than
139      * <code>p</code>. */

140     public final boolean throwsSubset(ProcedureInstance p) {
141         return ts.throwsSubset(this, p);
142     }
143
144     /** Returns true iff <code>this</code> throws fewer exceptions than
145      * <code>p</code>. */

146     public boolean throwsSubsetImpl(ProcedureInstance p) {
147         SubtypeSet s1 = new SubtypeSet(ts.Throwable());
148         SubtypeSet s2 = new SubtypeSet(ts.Throwable());
149
150         s1.addAll(this.throwTypes());
151         s2.addAll(p.throwTypes());
152
153         for (Iterator i = s1.iterator(); i.hasNext(); ) {
154             Type t = (Type) i.next();
155             if (! ts.isUncheckedException(t) && ! s2.contains(t)) {
156                 return false;
157             }
158         }
159
160         return true;
161     }
162
163     /** Returns true if a call can be made with the given argument types. */
164     public final boolean callValid(List argTypes) {
165         return ts.callValid(this, argTypes);
166     }
167
168     /** Returns true if a call can be made with the given argument types. */
169     public boolean callValidImpl(List argTypes) {
170         List l1 = this.formalTypes();
171         List l2 = argTypes;
172
173         Iterator i1 = l1.iterator();
174         Iterator i2 = l2.iterator();
175
176         while (i1.hasNext() && i2.hasNext()) {
177             Type t1 = (Type) i1.next();
178             Type t2 = (Type) i2.next();
179
180             if (! ts.isImplicitCastValid(t2, t1)) {
181                 return false;
182             }
183         }
184
185         return ! (i1.hasNext() || i2.hasNext());
186     }
187 }
188
Popular Tags