1 /*******************************************************************************
2 * Copyright (c) 2000, 2011 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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types;
13 import java.util.HashMap;
15 import org.eclipse.core.runtime.Assert;
17 import org.eclipse.jdt.core.ITypeParameter;
18 import org.eclipse.jdt.core.dom.ITypeBinding;
20 import org.eclipse.jdt.internal.corext.refactoring.generics.InferTypeArgumentsTCModel;
21 import org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.CollectionElementVariable2;
22 import org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.ConstraintVariable2;
26 public final class TypeVariable extends AbstractTypeVariable {
28 private ITypeParameter fJavaTypeParameter;
30 protected TypeVariable(TypeEnvironment environment) {
34 protected void initialize(ITypeBinding binding, ITypeParameter javaTypeParameter) {
35 Assert.isTrue(binding.isTypeVariable());
36 Assert.isNotNull(javaTypeParameter);
37 fJavaTypeParameter= javaTypeParameter;
38 super.initialize(binding);
42 public int getKind() {
47 public boolean doEquals(TType type) {
48 return fJavaTypeParameter.equals(((TypeVariable)type).fJavaTypeParameter);
52 public int hashCode() {
53 return fJavaTypeParameter.hashCode();
57 protected boolean doCanAssignTo(TType lhs) {
58 switch (lhs.getKind()) {
60 case VOID_TYPE: return false;
63 case ARRAY_TYPE: return false;
65 case GENERIC_TYPE: return false;
68 case PARAMETERIZED_TYPE:
70 return canAssignOneBoundTo(lhs);
72 case UNBOUND_WILDCARD_TYPE:
73 case EXTENDS_WILDCARD_TYPE:
74 case SUPER_WILDCARD_TYPE:
75 return ((WildcardType)lhs).checkAssignmentBound(this);
78 return doExtends((TypeVariable)lhs);
80 return ((CaptureType)lhs).checkLowerBound(this);
85 private boolean doExtends(TypeVariable other) {
86 return other.generated_6736747279550072243(this);
90 public String getName() {
91 return fJavaTypeParameter.getElementName();
95 public String getPrettySignature() {
96 if (fBounds.length == 1 && fBounds[0].isJavaLangObject())
97 return fJavaTypeParameter.getElementName(); // don't print the trivial bound
99 StringBuffer result= new StringBuffer(fJavaTypeParameter.getElementName());
100 if (fBounds.length > 0) {
101 result.append(" extends "); //$NON-NLS-1$
102 result.append(fBounds[0].getPlainPrettySignature());
103 for (int i= 1; i < fBounds.length; i++) {
104 result.append(" & "); //$NON-NLS-1$
105 result.append(fBounds[i].getPlainPrettySignature());
108 return result.toString();
112 protected String getPlainPrettySignature() {
113 return fJavaTypeParameter.getElementName();
116 public void generated_6416044053138177621(ConstraintVariable2 expressionCv, int i, InferTypeArgumentsTCModel infertypeargumentstcmodel) {
117 infertypeargumentstcmodel.makeElementVariable(expressionCv, this, i);
118 if (getBounds().length != 0) {
119 //TODO: create subtype constraints for bounds
123 public CollectionElementVariable2 generated_2783228730288621572(ConstraintVariable2 constraintVariable) {
124 Assert.isTrue(isTypeVariable()); // includes null check
125 HashMap<String, CollectionElementVariable2> typeVarToElementVars= (HashMap<String, CollectionElementVariable2>) constraintVariable.getData(InferTypeArgumentsTCModel.INDEXED_COLLECTION_ELEMENTS);
126 if (typeVarToElementVars == null)
128 return typeVarToElementVars.get(getBindingKey());
131 public boolean generated_6736747279550072243(TypeVariable typevariable) {
132 for (int i= 0; i < typevariable.fBounds.length; i++) {
133 TType bound= typevariable.fBounds[i];
134 if (equals(bound) || (bound.getKind() == TType.TYPE_VARIABLE && ((TypeVariable)bound).doExtends(this)))