KickJava   Java API By Example, From Geeks To Geeks.

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


1 package polyglot.ext.jl.types;
2
3 import polyglot.types.*;
4 import polyglot.util.*;
5 import polyglot.types.Package;
6 import java.io.*;
7
8 /**
9  * Abstract implementation of a <code>Type</code>. This implements most of
10  * the "isa" and "cast" methods of the type and methods which just dispatch to
11  * the type system.
12  */

13 public abstract class Type_c extends TypeObject_c implements Type
14 {
15     /** Used for deserializing types. */
16     protected Type_c() { }
17     
18     /** Creates a new type in the given a TypeSystem. */
19     public Type_c(TypeSystem ts) {
20         this(ts, null);
21     }
22
23     /** Creates a new type in the given a TypeSystem at a given position. */
24     public Type_c(TypeSystem ts, Position pos) {
25         super(ts, pos);
26     }
27
28     /**
29      * Return a string into which to translate the type.
30      * @param c A resolver in which to lookup this type to determine if
31      * the type is unique in the given resolver.
32      */

33     public abstract String JavaDoc translate(Resolver c);
34
35     public boolean isType() { return true; }
36     public boolean isPackage() { return false; }
37     public Type toType() { return this; }
38     public Package JavaDoc toPackage() { return null; }
39
40     /* To be filled in by subtypes. */
41     public boolean isCanonical() { return false; }
42
43     public boolean isPrimitive() { return false; }
44     public boolean isNumeric() { return false; }
45     public boolean isIntOrLess() { return false; }
46     public boolean isLongOrLess() { return false; }
47     public boolean isVoid() { return false; }
48     public boolean isBoolean() { return false; }
49     public boolean isChar() { return false; }
50     public boolean isByte() { return false; }
51     public boolean isShort() { return false; }
52     public boolean isInt() { return false; }
53     public boolean isLong() { return false; }
54     public boolean isFloat() { return false; }
55     public boolean isDouble() { return false; }
56
57     public boolean isReference() { return false; }
58     public boolean isNull() { return false; }
59     public boolean isClass() { return false; }
60     public boolean isArray() { return false; }
61
62     /**
63      * Return true if a subclass of Throwable.
64      */

65     public boolean isThrowable() {
66     return false;
67     }
68
69     /**
70      * Return true if an unchecked exception.
71      */

72     public boolean isUncheckedException() {
73     return false;
74     }
75     
76     /** Returns a non-null iff isClass() returns true. */
77     public ClassType toClass() {
78     return null;
79     }
80
81     /** Returns a non-null iff isNull() returns true. */
82     public NullType toNull() {
83     return null;
84     }
85
86     /** Returns a non-null iff isReference() returns true. */
87     public ReferenceType toReference() {
88     return null;
89     }
90
91     /** Returns a non-null iff isPrimitive() returns true. */
92     public PrimitiveType toPrimitive() {
93     return null;
94     }
95
96     /** Returns a non-null iff isArray() returns true. */
97     public ArrayType toArray() {
98     return null;
99     }
100
101     /**
102      * Return a <code>dims</code>-array of this type.
103      */

104     public ArrayType arrayOf(int dims) {
105     return ts.arrayOf(this, dims);
106     }
107
108     /**
109      * Return an array of this type.
110      */

111     public ArrayType arrayOf() {
112     return ts.arrayOf(this);
113     }
114     
115     /**
116      * Return true if this type is a subtype of <code>ancestor</code>.
117      */

118     public final boolean isSubtype(Type t) {
119     return ts.isSubtype(this, t);
120     }
121
122     /**
123      * Return true if this type is a subtype of <code>ancestor</code>.
124      */

125     public boolean isSubtypeImpl(Type t) {
126     return ts.equals(this, t) || ts.descendsFrom(this, t);
127     }
128     
129     /**
130      * Return true if this type descends from <code>ancestor</code>.
131      */

132     public final boolean descendsFrom(Type t) {
133         return ts.descendsFrom(this, t);
134     }
135
136     /**
137      * Return true if this type descends from <code>ancestor</code>.
138      */

139     public boolean descendsFromImpl(Type t) {
140         return false;
141     }
142
143     /**
144      * Return true if this type can be cast to <code>toType</code>.
145      */

146     public final boolean isCastValid(Type toType) {
147     return ts.isCastValid(this, toType);
148     }
149     
150     /**
151      * Return true if this type can be cast to <code>toType</code>.
152      */

153     public boolean isCastValidImpl(Type toType) {
154     return false;
155     }
156     
157     /**
158      * Return true if a value of this type can be assigned to a variable of
159      * type <code>toType</code>.
160      */

161     public final boolean isImplicitCastValid(Type toType) {
162         return ts.isImplicitCastValid(this, toType);
163     }
164
165     /**
166      * Return true if a value of this type can be assigned to a variable of
167      * type <code>toType</code>.
168      */

169     public boolean isImplicitCastValidImpl(Type toType) {
170         return false;
171     }
172
173     /**
174      * Return true a literal <code>value</code> can be converted to this type.
175      * This method should be removed. It is kept for backward compatibility.
176      */

177     public final boolean numericConversionValid(long value) {
178         return ts.numericConversionValid(this, value);
179     }
180     
181     /**
182      * Return true a literal <code>value</code> can be converted to this type.
183      * This method should be removed. It is kept for backward compatibility.
184      */

185     public boolean numericConversionValidImpl(long value) {
186         return false;
187     }
188     
189     /**
190      * Return true a literal <code>value</code> can be converted to this type.
191      */

192     public final boolean numericConversionValid(Object JavaDoc value) {
193         return ts.numericConversionValid(this, value);
194     }
195     
196     /**
197      * Return true a literal <code>value</code> can be converted to this type.
198      */

199     public boolean numericConversionValidImpl(Object JavaDoc value) {
200         return false;
201     }
202     
203     /**
204      * Return true if the types can be compared; that is, if they have
205      * the same type system.
206      */

207     public boolean isComparable(Type t) {
208     return t.typeSystem() == ts;
209     }
210
211     /**
212      * Yields a string representing this type. The string
213      * should be consistent with equality. That is,
214      * if this.equals(anotherType), then it should be
215      * that this.toString().equals(anotherType.toString()).
216      *
217      * The string does not have to be a legal Java identifier.
218      * It is suggested, but not required, that it be an
219      * easily human readable representation, and thus useful
220      * in error messages and generated output.
221      */

222     public abstract String JavaDoc toString();
223
224     private void writeObject(ObjectOutputStream out) throws IOException {
225         // Write out the full name first so we can install correctly
226
// when we read back.
227
if (this instanceof Named) {
228             String JavaDoc name = ((Named) this).fullName();
229             out.writeObject(name);
230             String JavaDoc memberName = null;
231             if (name != null && this.isClass() && this.toClass().isMember()) {
232                 memberName = this.typeSystem().getTransformedClassName(this.toClass());
233             }
234             out.writeObject(memberName);
235         }
236
237         out.defaultWriteObject();
238     }
239
240     private void readObject(ObjectInputStream in)
241         throws IOException, ClassNotFoundException JavaDoc
242     {
243         // Store the type in the system resolver to avoid infinite loop.
244
if (this instanceof Named) {
245             String JavaDoc name = (String JavaDoc) in.readObject();
246             String JavaDoc memberName = (String JavaDoc) in.readObject();
247             TypeSystem ts = ((TypeInputStream) in).getTypeSystem();
248
249             if (name != null) {
250                 ((CachingResolver) ts.systemResolver()).install(name, (Named) this);
251             }
252             
253             if (memberName != null) {
254                 ((CachingResolver) ts.systemResolver()).install(memberName, (Named) this);
255             }
256         }
257
258         in.defaultReadObject();
259     }
260 }
261
Popular Tags