1 /*******************************************************************************
2  * Copyright (c) 2000, 2016 IBM Corporation and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types;
15 
16 import org.eclipse.core.runtime.Assert;
17 
18 import org.eclipse.jdt.core.ITypeParameter;
19 import org.eclipse.jdt.core.dom.ITypeBinding;
20 
21 
22 
23 public final class TypeVariable extends AbstractTypeVariable {
24 
25 	private ITypeParameter fJavaTypeParameter;
26 
TypeVariable(TypeEnvironment environment)27 	protected TypeVariable(TypeEnvironment environment) {
28 		super(environment);
29 	}
30 
initialize(ITypeBinding binding, ITypeParameter javaTypeParameter)31 	protected void initialize(ITypeBinding binding, ITypeParameter javaTypeParameter) {
32 		Assert.isTrue(binding.isTypeVariable());
33 		Assert.isNotNull(javaTypeParameter);
34 		fJavaTypeParameter= javaTypeParameter;
35 		super.initialize(binding);
36 	}
37 
38 	@Override
getKind()39 	public int getKind() {
40 		return TYPE_VARIABLE;
41 	}
42 
43 	@Override
doEquals(TType type)44 	public boolean doEquals(TType type) {
45 		return fJavaTypeParameter.equals(((TypeVariable)type).fJavaTypeParameter);
46 	}
47 
48 	@Override
hashCode()49 	public int hashCode() {
50 		return fJavaTypeParameter.hashCode();
51 	}
52 
53 	@Override
doCanAssignTo(TType lhs)54 	protected boolean doCanAssignTo(TType lhs) {
55 		switch (lhs.getKind()) {
56 			case NULL_TYPE:
57 			case VOID_TYPE: return false;
58 			case PRIMITIVE_TYPE:
59 
60 			case ARRAY_TYPE: return false;
61 
62 			case GENERIC_TYPE: return false;
63 
64 			case STANDARD_TYPE:
65 			case PARAMETERIZED_TYPE:
66 			case RAW_TYPE:
67 				return canAssignOneBoundTo(lhs);
68 
69 			case UNBOUND_WILDCARD_TYPE:
70 			case EXTENDS_WILDCARD_TYPE:
71 			case SUPER_WILDCARD_TYPE:
72 				return ((WildcardType)lhs).checkAssignmentBound(this);
73 
74 			case TYPE_VARIABLE:
75 				return doExtends((TypeVariable)lhs);
76 			case CAPTURE_TYPE:
77 				return ((CaptureType)lhs).checkLowerBound(this);
78 		}
79 		return false;
80 	}
81 
doExtends(TypeVariable other)82 	private boolean doExtends(TypeVariable other) {
83 		for (TType bound : fBounds) {
84 			if (other.equals(bound) || (bound.getKind() == TYPE_VARIABLE && ((TypeVariable)bound).doExtends(other)))
85 				return true;
86 		}
87 		return false;
88 	}
89 
90 	@Override
getName()91 	public String getName() {
92 		return fJavaTypeParameter.getElementName();
93 	}
94 
95 	@Override
getPrettySignature()96 	public String getPrettySignature() {
97 		if (fBounds.length == 1 && fBounds[0].isJavaLangObject())
98 			return fJavaTypeParameter.getElementName(); // don't print the trivial bound
99 
100 		StringBuilder result= new StringBuilder(fJavaTypeParameter.getElementName());
101 		if (fBounds.length > 0) {
102 			result.append(" extends "); //$NON-NLS-1$
103 			result.append(fBounds[0].getPlainPrettySignature());
104 			for (int i= 1; i < fBounds.length; i++) {
105 				result.append(" & "); //$NON-NLS-1$
106 				result.append(fBounds[i].getPlainPrettySignature());
107 			}
108 		}
109 		return result.toString();
110 	}
111 
112 	@Override
getPlainPrettySignature()113 	protected String getPlainPrettySignature() {
114 		return fJavaTypeParameter.getElementName();
115 	}
116 }
117