KickJava   Java API By Example, From Geeks To Geeks.

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


1 package polyglot.ext.jl.types;
2
3 import polyglot.types.*;
4 import polyglot.util.*;
5 import java.util.*;
6
7 /**
8  * An <code>ArrayType</code> represents an array of base java types.
9  */

10 public class ArrayType_c extends ReferenceType_c implements ArrayType
11 {
12     protected Type base;
13     protected List fields;
14     protected List methods;
15     protected List interfaces;
16
17     /** Used for deserializing types. */
18     protected ArrayType_c() { }
19
20     public ArrayType_c(TypeSystem ts, Position pos, Type base) {
21     super(ts, pos);
22     this.base = base;
23
24         methods = null;
25         fields = null;
26         interfaces = null;
27     }
28
29     void init() {
30         if (methods == null) {
31             methods = new ArrayList(1);
32
33             // Add method public Object clone()
34
methods.add(ts.methodInstance(position(),
35                                           this,
36                                           ts.Public(),
37                                           ts.Object(),
38                                           "clone",
39                                           Collections.EMPTY_LIST,
40                                           Collections.EMPTY_LIST));
41         }
42
43         if (fields == null) {
44             fields = new ArrayList(2);
45
46             // Add field public final int length
47
fields.add(ts.fieldInstance(position(),
48                                         this,
49                                         ts.Public().Final(),
50                                         ts.Int(),
51                                         "length"));
52         }
53
54         if (interfaces == null) {
55             interfaces = new ArrayList(2);
56             interfaces.add(ts.Cloneable());
57             interfaces.add(ts.Serializable());
58         }
59     }
60
61     /** Get the base type of the array. */
62     public Type base() {
63         return base;
64     }
65
66     /** Set the base type of the array. */
67     public ArrayType base(Type base) {
68         if (base == this.base)
69             return this;
70     ArrayType_c n = (ArrayType_c) copy();
71     n.base = base;
72     return n;
73     }
74
75     /** Get the ulitimate base type of the array. */
76     public Type ultimateBase() {
77         if (base().isArray()) {
78             return base().toArray().ultimateBase();
79         }
80
81         return base();
82     }
83
84     public int dims() {
85         return 1 + (base().isArray() ? base().toArray().dims() : 0);
86     }
87
88     public String JavaDoc toString() {
89         return base().toString() + "[]";
90     }
91
92     /** Translate the type. */
93     public String JavaDoc translate(Resolver c) {
94         return base().translate(c) + "[]";
95     }
96
97     /** Returns true iff the type is canonical. */
98     public boolean isCanonical() {
99     return base().isCanonical();
100     }
101
102     public boolean isArray() { return true; }
103     public ArrayType toArray() { return this; }
104
105     /** Get the methods implemented by the array type. */
106     public List methods() {
107         init();
108     return Collections.unmodifiableList(methods);
109     }
110
111     /** Get the fields of the array type. */
112     public List fields() {
113         init();
114     return Collections.unmodifiableList(fields);
115     }
116
117     /** Get the clone() method. */
118     public MethodInstance cloneMethod() {
119     return (MethodInstance) methods().get(0);
120     }
121
122     /** Get a field of the type by name. */
123     public FieldInstance fieldNamed(String JavaDoc name) {
124         FieldInstance fi = lengthField();
125         return name.equals(fi.name()) ? fi : null;
126     }
127
128     /** Get the length field. */
129     public FieldInstance lengthField() {
130     return (FieldInstance) fields().get(0);
131     }
132
133     /** Get the super type of the array type. */
134     public Type superType() {
135     return ts.Object();
136     }
137
138     /** Get the interfaces implemented by the array type. */
139     public List interfaces() {
140         init();
141     return Collections.unmodifiableList(interfaces);
142     }
143
144     public int hashCode() {
145     return base().hashCode() << 1;
146     }
147
148     public boolean equalsImpl(TypeObject t) {
149         if (t instanceof ArrayType) {
150             ArrayType a = (ArrayType) t;
151             return ts.equals(base(), a.base());
152         }
153     return false;
154     }
155
156     public boolean isImplicitCastValidImpl(Type toType) {
157         if (toType.isArray()) {
158             if (base().isPrimitive() || toType.toArray().base().isPrimitive()) {
159                 return ts.equals(base(), toType.toArray().base());
160             }
161             else {
162                 return ts.isImplicitCastValid(base(), toType.toArray().base());
163             }
164         }
165
166         // toType is not an array, but this is. Check if the array
167
// is a subtype of the toType. This happens when toType
168
// is java.lang.Object.
169
return ts.isSubtype(this, toType);
170     }
171
172     /**
173      * Requires: all type arguments are canonical. ToType is not a NullType.
174      *
175      * Returns true iff a cast from this to toType is valid; in other
176      * words, some non-null members of this are also members of toType.
177      **/

178     public boolean isCastValidImpl(Type toType) {
179         if (! toType.isReference()) return false;
180
181     if (toType.isArray()) {
182         Type fromBase = base();
183         Type toBase = toType.toArray().base();
184
185         if (fromBase.isPrimitive()) return ts.equals(toBase, fromBase);
186         if (toBase.isPrimitive()) return false;
187
188         if (fromBase.isNull()) return false;
189         if (toBase.isNull()) return false;
190
191         // Both are reference types.
192
return ts.isCastValid(fromBase, toBase);
193     }
194
195         // Ancestor is not an array, but child is. Check if the array
196
// is a subtype of the ancestor. This happens when ancestor
197
// is java.lang.Object.
198
return ts.isSubtype(this, toType);
199     }
200 }
201
Popular Tags