Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: dd61954263c608943abf27d52d94cca8ee46168a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*******************************************************************************
 * Copyright (c) 2000, 2016 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types;

import java.util.Map;

import org.eclipse.core.runtime.Assert;

import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.dom.ITypeBinding;


public abstract class HierarchyType extends TType {
	private HierarchyType fSuperclass;
	private HierarchyType[] fInterfaces;
	private IType fJavaElementType;

	protected HierarchyType(TypeEnvironment environment) {
		super(environment);
	}

	protected void initialize(ITypeBinding binding, IType javaElementType) {
		super.initialize(binding);
		Assert.isNotNull(javaElementType);
		fJavaElementType= javaElementType;
		TypeEnvironment environment= getEnvironment();
		ITypeBinding superclass= binding.getSuperclass();
		if (superclass != null) {
			fSuperclass= (HierarchyType)environment.create(superclass);
		}
		ITypeBinding[] interfaces= binding.getInterfaces();
		fInterfaces= new HierarchyType[interfaces.length];
		for (int i= 0; i < interfaces.length; i++) {
			fInterfaces[i]= (HierarchyType)environment.create(interfaces[i]);
		}
	}

	@Override
	public TType getSuperclass() {
		return fSuperclass;
	}

	@Override
	public TType[] getInterfaces() {
		return fInterfaces;
	}

	public IType getJavaElementType() {
		return fJavaElementType;
	}

	public boolean isSubType(HierarchyType other) {
		if (getEnvironment() == other.getEnvironment()) {
			Map<TypeTuple, Boolean> cache= getEnvironment().getSubTypeCache();
			TypeTuple key= new TypeTuple(this, other);
			Boolean value= cache.get(key);
			if (value != null)
				return value.booleanValue();
			boolean isSub= doIsSubType(other);
			value= Boolean.valueOf(isSub);
			cache.put(key, value);
			return isSub;
		}
		return doIsSubType(other);
	}

	private boolean doIsSubType(HierarchyType other) {
		if (fSuperclass != null && (other.isTypeEquivalentTo(fSuperclass) || fSuperclass.doIsSubType(other)))
			return true;
		for (int i= 0; i < fInterfaces.length; i++) {
			if (other.isTypeEquivalentTo(fInterfaces[i]) || fInterfaces[i].doIsSubType(other))
				return true;
		}
		return false;
	}

	protected boolean canAssignToStandardType(StandardType target) {
		if (target.isJavaLangObject())
			return true;
		return isSubType(target);
	}
}

Back to the top