KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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 org.eclipse.core.runtime.Assert;
14
15 import org.eclipse.jdt.core.ITypeParameter;
16 import org.eclipse.jdt.core.dom.ITypeBinding;
17
18
19
20 public final class TypeVariable extends AbstractTypeVariable {
21     
22     private ITypeParameter fJavaTypeParameter;
23     
24     protected TypeVariable(TypeEnvironment environment) {
25         super(environment);
26     }
27
28     protected void initialize(ITypeBinding binding, ITypeParameter javaTypeParameter) {
29         Assert.isTrue(binding.isTypeVariable());
30         Assert.isNotNull(javaTypeParameter);
31         fJavaTypeParameter= javaTypeParameter;
32         super.initialize(binding);
33     }
34     
35     public int getKind() {
36         return TYPE_VARIABLE;
37     }
38     
39     public boolean doEquals(TType type) {
40         return fJavaTypeParameter.equals(((TypeVariable)type).fJavaTypeParameter);
41     }
42     
43     public int hashCode() {
44         return fJavaTypeParameter.hashCode();
45     }
46     
47     protected boolean doCanAssignTo(TType lhs) {
48         switch (lhs.getKind()) {
49             case NULL_TYPE:
50             case VOID_TYPE: return false;
51             case PRIMITIVE_TYPE:
52                 
53             case ARRAY_TYPE: return false;
54             
55             case GENERIC_TYPE: return false;
56             
57             case STANDARD_TYPE:
58             case PARAMETERIZED_TYPE:
59             case RAW_TYPE:
60                 return canAssignOneBoundTo(lhs);
61
62             case UNBOUND_WILDCARD_TYPE:
63             case EXTENDS_WILDCARD_TYPE:
64             case SUPER_WILDCARD_TYPE:
65                 return ((WildcardType)lhs).checkAssignmentBound(this);
66                 
67             case TYPE_VARIABLE:
68                 return doExtends((TypeVariable)lhs);
69             case CAPTURE_TYPE:
70                 return ((CaptureType)lhs).checkLowerBound(this);
71         }
72         return false;
73     }
74     
75     private boolean doExtends(TypeVariable other) {
76         for (int i= 0; i < fBounds.length; i++) {
77             TType bound= fBounds[i];
78             if (other.equals(bound) || (bound.getKind() == TYPE_VARIABLE && ((TypeVariable)bound).doExtends(other)))
79                 return true;
80         }
81         return false;
82     }
83     
84     public String JavaDoc getName() {
85         return fJavaTypeParameter.getElementName();
86     }
87     
88     public String JavaDoc getPrettySignature() {
89         if (fBounds.length == 1 && fBounds[0].isJavaLangObject())
90             return fJavaTypeParameter.getElementName(); // don't print the trivial bound
91

92         StringBuffer JavaDoc result= new StringBuffer JavaDoc(fJavaTypeParameter.getElementName());
93         if (fBounds.length > 0) {
94             result.append(" extends "); //$NON-NLS-1$
95
result.append(fBounds[0].getPlainPrettySignature());
96             for (int i= 1; i < fBounds.length; i++) {
97                 result.append(" & "); //$NON-NLS-1$
98
result.append(fBounds[i].getPlainPrettySignature());
99             }
100         }
101         return result.toString();
102     }
103     
104     protected String JavaDoc getPlainPrettySignature() {
105         return fJavaTypeParameter.getElementName();
106     }
107 }
108
Popular Tags