KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > refactoring > typeconstraints > types > TType


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types;
12
13 import java.util.List JavaDoc;
14 import java.util.Map JavaDoc;
15
16 import org.eclipse.core.runtime.Assert;
17
18 import org.eclipse.jdt.core.dom.ITypeBinding;
19
20
21 public abstract class TType {
22     
23     public static final int NULL_TYPE= 1;
24     public static final int VOID_TYPE= 2;
25     public static final int PRIMITIVE_TYPE= 3;
26     
27     public static final int ARRAY_TYPE= 4;
28     
29     public static final int STANDARD_TYPE= 5;
30     public static final int GENERIC_TYPE= 6;
31     public static final int PARAMETERIZED_TYPE= 7;
32     public static final int RAW_TYPE= 8;
33     
34     public static final int UNBOUND_WILDCARD_TYPE= 9;
35     public static final int SUPER_WILDCARD_TYPE= 10;
36     public static final int EXTENDS_WILDCARD_TYPE= 11;
37     
38     public static final int TYPE_VARIABLE= 12;
39     public static final int CAPTURE_TYPE= 13;
40     
41     protected static final int WILDCARD_TYPE_SHIFT= 3;
42     protected static final int ARRAY_TYPE_SHIFT= 5;
43     
44     private static final int F_IS_CLASS= 1 << 0;
45     private static final int F_IS_INTERFACE= 1 << 1;
46     private static final int F_IS_ENUM= 1 << 2;
47     private static final int F_IS_ANNOTATION= 1 << 3;
48     
49     private static final int F_IS_TOP_LEVEL= 1 << 4;
50     private static final int F_IS_NESTED= 1 << 5;
51     private static final int F_IS_MEMBER= 1 << 6;
52     private static final int F_IS_LOCAL= 1 << 7;
53     private static final int F_IS_ANONYMOUS= 1 << 8;
54     
55     protected static final TType[] EMPTY_TYPE_ARRAY= new TType[0];
56     
57     private TypeEnvironment fEnvironment;
58     private String JavaDoc fBindingKey;
59     private int fModifiers;
60     private int fFlags;
61     
62     /**
63      * Creates a new type with the given environment as an owner
64      */

65     protected TType(TypeEnvironment environment) {
66         fEnvironment= environment;
67     }
68     
69     /**
70      * Creates a new type with the given environment as an owner
71      */

72     protected TType(TypeEnvironment environment, String JavaDoc key) {
73         this(environment);
74         Assert.isNotNull(key);
75         fBindingKey= key;
76     }
77     
78     /**
79      * Initialized the type from the given binding
80      *
81      * @param binding the binding to initialize from
82      */

83     protected void initialize(ITypeBinding binding) {
84         fBindingKey= binding.getKey();
85         Assert.isNotNull(fBindingKey);
86         fModifiers= binding.getModifiers();
87         if (binding.isClass()) {
88             fFlags= F_IS_CLASS;
89         // the annotation test has to be done before test for interface
90
// since annotations are interfaces as well.
91
} else if (binding.isAnnotation()) {
92             fFlags= F_IS_ANNOTATION | F_IS_INTERFACE;
93         } else if (binding.isInterface()) {
94             fFlags= F_IS_INTERFACE;
95         } else if (binding.isEnum()) {
96             fFlags= F_IS_ENUM;
97         }
98
99         if (binding.isTopLevel()) {
100             fFlags|= F_IS_TOP_LEVEL;
101         } else if (binding.isNested()) {
102             fFlags|= F_IS_NESTED;
103             if (binding.isMember()) {
104                 fFlags|= F_IS_MEMBER;
105             } else if (binding.isLocal()) {
106                 fFlags|= F_IS_LOCAL;
107             } else if (binding.isAnonymous()) {
108                 fFlags|= F_IS_ANONYMOUS;
109             }
110         }
111     }
112     
113     /**
114      * Returns the type's environment
115      *
116      * @return the types's environment
117      */

118     public TypeEnvironment getEnvironment() {
119         return fEnvironment;
120     }
121     
122     /**
123      * Returns the key of the binding from which this type
124      * got constructed.
125      *
126      * @return the binding key
127      */

128     public String JavaDoc getBindingKey() {
129         return fBindingKey;
130     }
131     
132     /**
133      * Returns the modifiers for this type.
134      *
135      * @return the bit-wise or of <code>Modifier</code> constants
136      * @see org.eclipse.jdt.core.dom.IBinding#getModifiers()
137      * @see org.eclipse.jdt.core.dom.Modifier
138      */

139     public int getModifiers() {
140         return fModifiers;
141     }
142     
143     /**
144      * Returns the element kind
145      *
146      * @return the element kind.
147      */

148     public abstract int getKind();
149     
150     /**
151      * Returns whether this type represents <code>java.lang.Object</code> or
152      * not.
153      *
154      * @return whether this type is <code>java.lang.Object</code> or not
155      */

156     public boolean isJavaLangObject() {
157         return false;
158     }
159     
160     /**
161      * Returns whether this type represents <code>java.lang.Cloneable</code>
162      * or not.
163      *
164      * @return whether this type is <code>java.lang.Cloneable</code> or not
165      */

166     public boolean isJavaLangCloneable() {
167         return false;
168     }
169     
170     /**
171      * Returns whether this type represents <code>java.io.Serializable</code>
172      * or not.
173      *
174      * @return whether this type is <code>java.io.Serializable</code> or not
175      */

176     public boolean isJavaIoSerializable() {
177         return false;
178     }
179     
180     /**
181      * Returns <code>true</code> if the given type represents the null type.
182      * Otherwise <code>false</code> is returned.
183      *
184      * @return whether this type is the null type or not
185      */

186     public final boolean isNullType() {
187         return getKind() == NULL_TYPE;
188     }
189     
190     /**
191      * Returns <code>true</code> if the given type represents the void type.
192      * Otherwise <code>false</code> is returned.
193      *
194      * @return whether this type is the void type or not
195      */

196     public final boolean isVoidType() {
197         return getKind() == VOID_TYPE;
198     }
199     
200     /**
201      * Returns <code>true</code> if the given type represents a primitive type.
202      * Otherwise <code>false</code> is returned.
203      *
204      * @return whether this type is a primitive type or not
205      */

206     public final boolean isPrimitiveType() {
207         return getKind() == PRIMITIVE_TYPE;
208     }
209     
210     /**
211      * Returns <code>true</code> if the given type represents an array type.
212      * Otherwise <code>false</code> is returned.
213      *
214      * @return whether this type is an array type or not
215      */

216     public final boolean isArrayType() {
217         return getKind() == ARRAY_TYPE;
218     }
219     
220     /**
221      * Returns <code>true</code> if the given type represents a hierarchy type.
222      * Otherwise <code>false</code> is returned.
223      *
224      * @return whether this type is a hierarchy type or not
225      */

226     public final boolean isHierarchyType() {
227         int elementType= getKind();
228         return elementType == RAW_TYPE || elementType == PARAMETERIZED_TYPE
229             || elementType == GENERIC_TYPE || elementType == STANDARD_TYPE;
230     }
231     
232     /**
233      * Returns <code>true</code> if the given type represents a standard type.
234      * Otherwise <code>false</code> is returned.
235      *
236      * @return whether this type is a standard type or not
237      */

238     public final boolean isStandardType() {
239         return getKind() == STANDARD_TYPE;
240     }
241     
242     /**
243      * Returns <code>true</code> if the given type represents a raw type.
244      * Otherwise <code>false</code> is returned.
245      *
246      * @return whether this type is a raw type or not
247      */

248     public final boolean isRawType() {
249         return getKind() == RAW_TYPE;
250     }
251     
252     /**
253      * Returns <code>true</code> if the given type represents a parameterized type.
254      * Otherwise <code>false</code> is returned.
255      *
256      * @return whether this type is a parameterized type or not
257      */

258     public final boolean isParameterizedType() {
259         return getKind() == PARAMETERIZED_TYPE;
260     }
261     
262     /**
263      * Returns <code>true</code> if the given type represents a generic type.
264      * Otherwise <code>false</code> is returned.
265      *
266      * @return whether this type is a generic type or not
267      */

268     public final boolean isGenericType() {
269         return getKind() == GENERIC_TYPE;
270     }
271     
272     /**
273      * Returns <code>true</code> if the given type represents a type variable.
274      * Otherwise <code>false</code> is returned.
275      *
276      * @return whether this type is a type variable or not
277      */

278     public final boolean isTypeVariable() {
279         return getKind() == TYPE_VARIABLE;
280     }
281     
282     /**
283      * Returns <code>true</code> if the given type represents a capture type.
284      * Otherwise <code>false</code> is returned.
285      *
286      * @return whether this type is a capture type or not
287      */

288     public final boolean isCaptureType() {
289         return getKind() == CAPTURE_TYPE;
290     }
291     
292     /**
293      * Returns <code>true</code> if the given type represents a wildcard type.
294      * Otherwise <code>false</code> is returned.
295      *
296      * @return whether this type is a wildcard type or not
297      */

298     public final boolean isWildcardType() {
299         int elementType= getKind();
300         return elementType == EXTENDS_WILDCARD_TYPE || elementType == UNBOUND_WILDCARD_TYPE
301             || elementType == SUPER_WILDCARD_TYPE;
302     }
303     
304     /**
305      * Returns <code>true</code> if the given type represents a unbound wildcard type.
306      * Otherwise <code>false</code> is returned.
307      *
308      * @return whether this type is a unbound wildcard type or not
309      */

310     public final boolean isUnboundWildcardType() {
311         return getKind() == UNBOUND_WILDCARD_TYPE;
312     }
313     
314     /**
315      * Returns <code>true</code> if the given type represents an extends wildcard type.
316      * Otherwise <code>false</code> is returned.
317      *
318      * @return whether this type is an extends wildcard type or not
319      */

320     public final boolean isExtendsWildcardType() {
321         return getKind() == EXTENDS_WILDCARD_TYPE;
322     }
323     
324     /**
325      * Returns <code>true</code> if the given type represents a super wildcard type.
326      * Otherwise <code>false</code> is returned.
327      *
328      * @return whether this type is a super wildcard type or not
329      */

330     public final boolean isSuperWildcardType() {
331         return getKind() == SUPER_WILDCARD_TYPE;
332     }
333     
334     /**
335      * Returns whether this type represents a class.
336      *
337      * @return whether this type represents a class
338      * @see ITypeBinding#isClass()
339      */

340     public final boolean isClass() {
341         return (fFlags & F_IS_CLASS) != 0;
342     }
343     
344     /**
345      * Returns whether this type represents a interface.
346      *
347      * @return whether this type represents a interface
348      * @see ITypeBinding#isInterface()
349      */

350     public final boolean isInterface() {
351         return (fFlags & F_IS_INTERFACE) != 0;
352     }
353     
354     /**
355      * Returns whether this type represents a enumeration.
356      *
357      * @return whether this type represents a enumeration
358      * @see ITypeBinding#isEnum()
359      */

360     public final boolean isEnum() {
361         return (fFlags & F_IS_ENUM) != 0;
362     }
363     
364     /**
365      * Returns whether this type represents an annotation.
366      *
367      * @return whether this type represents an annotation
368      * @see ITypeBinding#isAnnotation()
369      */

370     public final boolean isAnnotation() {
371         return (fFlags & F_IS_ANNOTATION) != 0;
372     }
373     
374     /**
375      * Returns whether this type represents a top level type.
376      *
377      * @return whether this type represents a top level type
378      * @see ITypeBinding#isTopLevel()
379      */

380     public final boolean isTopLevel() {
381         return (fFlags & F_IS_TOP_LEVEL) != 0;
382     }
383     
384     /**
385      * Returns whether this type represents a nested type.
386      *
387      * @return whether this type represents a nested type
388      * @see ITypeBinding#isNested()
389      */

390     public final boolean isNested() {
391         return (fFlags & F_IS_NESTED) != 0;
392     }
393     
394     /**
395      * Returns whether this type represents a member type.
396      *
397      * @return whether this type represents a member type
398      * @see ITypeBinding#isMember()
399      */

400     public final boolean isMember() {
401         return (fFlags & F_IS_MEMBER) != 0;
402     }
403     
404     /**
405      * Returns whether this type represents a local type.
406      *
407      * @return whether this type represents a local type
408      * @see ITypeBinding#isLocal()
409      */

410     public final boolean isLocal() {
411         return (fFlags & F_IS_LOCAL) != 0;
412     }
413     
414     /**
415      * Returns whether this type represents an anonymous type.
416      *
417      * @return whether this type represents an anonymous type
418      * @see ITypeBinding#isAnonymous()
419      */

420     public final boolean isAnonymous() {
421         return (fFlags & F_IS_ANONYMOUS) != 0;
422     }
423     
424     /**
425      * Returns the super classes of this type or <code>null</code>.
426      *
427      * @return the super class of this type
428      */

429     public TType getSuperclass() {
430         return null;
431     }
432     
433     /**
434      * Returns the interfaces this type implements or extends.
435      *
436      * @return the "super" interfaces or an empty array
437      */

438     public TType[] getInterfaces() {
439         return EMPTY_TYPE_ARRAY;
440     }
441     
442     public boolean isEqualTo(ITypeBinding binding) {
443         if (binding == null)
444             return false;
445         return binding.getKey().equals(fBindingKey);
446     }
447     
448     /**
449      * {@inheritDoc}
450      */

451     public final boolean equals(Object JavaDoc other) {
452         if (this == other)
453             return true;
454         if (!(other instanceof TType))
455             return false;
456         TType otherType= (TType)other;
457         if (getKind() != otherType.getKind())
458             return false;
459         return doEquals(otherType);
460     }
461     
462     /**
463      * Performs the actual equals check.
464      *
465      * @param type The right hand side of the equals operation. The dynamic type
466      * of the actual argument must be the same as the receiver type.
467      */

468     protected abstract boolean doEquals(TType type);
469     
470     /**
471      * Returns the erasure of this type as defined by ITypeBinding#getErasure().
472      *
473      * @return the erasure of this type
474      */

475     public TType getErasure() {
476         return this;
477     }
478     
479     /**
480      * Returns the type for the type declaration corresponding to this type.
481      *
482      * @return the type representing the declaration of this type
483      * @see ITypeBinding#getTypeDeclaration()
484      */

485     public TType getTypeDeclaration() {
486         return this;
487     }
488     
489     /**
490      * @return direct subtypes of this type
491      * @throws IllegalStateException if this type's TypeEnvironment
492      * was not created with rememberSubtypes == true
493      */

494     public TType[] getSubTypes() throws IllegalStateException JavaDoc {
495         Map JavaDoc subTypes= fEnvironment.getSubTypes();
496         if (subTypes == null)
497             throw new IllegalStateException JavaDoc("This TypeEnvironment does not remember subtypes"); //$NON-NLS-1$
498
List JavaDoc subtypes= (List JavaDoc) subTypes.get(this);
499         if (subtypes == null)
500             return EMPTY_TYPE_ARRAY;
501         else
502             return (TType[]) subtypes.toArray(new TType[subtypes.size()]);
503     }
504     
505     /**
506      * Answer <code>true</code> if the receiver of this method can be assigned
507      * to the argument lhs (e.g lhs= this is a valid assignment).
508      *
509      * @param lhs the left hand side of the assignment
510      * @return whether or not this type can be assigned to lhs
511      */

512     public final boolean canAssignTo(TType lhs) {
513         if (this.isTypeEquivalentTo(lhs))
514             return true;
515         return doCanAssignTo(lhs);
516     }
517     
518     /**
519      * Returns whether the receiver type is type equivalent to the other type.
520      * This method considers the erasure for generic, raw and parameterized
521      * types.
522      *
523      * @return whether the receiver is type equivalent to other
524      */

525     protected boolean isTypeEquivalentTo(TType other) {
526         return this.equals(other);
527     }
528     
529     /**
530      * Checks whether the <code>this</code> left hand side type interpreted as
531      * a type argument of a parameterized type is compatible with the given type
532      * <code>rhs</code>. For example if
533      * <code>List&lt;this&gt;= List&lt;rhs&gt;</code> is a valid assignment.
534      *
535      * @return <code>true</code> iff <code>this</code> contains <code>rhs</code> according to JLS3 4.5.1.1
536      */

537     protected boolean checkTypeArgument(TType rhs) {
538         return this.equals(rhs);
539     }
540     
541     /**
542      * Hook method to perform the actual can assign test
543      *
544      * @param lhs the left hand side of the assignment
545      * @return whether or not this type can be assigned to lhs
546      */

547     protected abstract boolean doCanAssignTo(TType lhs);
548
549     /**
550      * Returns the name of this type as defined by {@link ITypeBinding#getName()}.
551      *
552      * @return the name of this type
553      * @see ITypeBinding#getName()
554      */

555     public abstract String JavaDoc getName();
556     
557     /**
558      * Returns a signature of this type which can be presented to the user.
559      *
560      * @return a pretty signature for this type
561      */

562     public String JavaDoc getPrettySignature() {
563         return getPlainPrettySignature();
564     }
565     
566     /**
567      * Computes a plain pretty signature. For type with bounds (e.g
568      * type variables and wildcards) the plain signature is different
569      * than the full pretty signature.
570      *
571      * @return a plain pretty signature for this type
572      */

573     protected abstract String JavaDoc getPlainPrettySignature();
574     
575     /**
576      * {@inheritDoc}
577      */

578     public String JavaDoc toString() {
579         return getPrettySignature();
580     }
581 }
582
Popular Tags