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;
15 import org.eclipse.core.runtime.Assert;
17 import org.eclipse.jdt.core.IType;
18 import org.eclipse.jdt.core.dom.ITypeBinding;
21 public abstract class HierarchyType extends TType {
22 private HierarchyType fSuperclass;
23 private HierarchyType[] fInterfaces;
24 private IType fJavaElementType;
26 protected HierarchyType(TypeEnvironment environment) {
30 protected void initialize(ITypeBinding binding, IType javaElementType) {
31 super.initialize(binding);
32 Assert.isNotNull(javaElementType);
33 fJavaElementType= javaElementType;
34 TypeEnvironment environment= getEnvironment();
35 ITypeBinding superclass= binding.getSuperclass();
36 if (superclass != null) {
37 fSuperclass= (HierarchyType)environment.create(superclass);
39 ITypeBinding[] interfaces= binding.getInterfaces();
40 fInterfaces= new HierarchyType[interfaces.length];
41 for (int i= 0; i < interfaces.length; i++) {
42 fInterfaces[i]= (HierarchyType)environment.create(interfaces[i]);
47 public TType getSuperclass() {
52 public TType[] getInterfaces() {
56 public IType getJavaElementType() {
57 return fJavaElementType;
60 public boolean isSubType(HierarchyType other) {
61 if (getEnvironment() == other.getEnvironment()) {
62 Map<TypeTuple, Boolean> cache= getEnvironment().getSubTypeCache();
63 TypeTuple key= new TypeTuple(this, other);
64 Boolean value= cache.get(key);
66 return value.booleanValue();
67 boolean isSub= doIsSubType(other);
68 value= Boolean.valueOf(isSub);
69 cache.put(key, value);
72 return doIsSubType(other);
75 private boolean doIsSubType(HierarchyType other) {
76 if (fSuperclass != null && (other.isTypeEquivalentTo(fSuperclass) || fSuperclass.doIsSubType(other)))
78 for (int i= 0; i < fInterfaces.length; i++) {
79 if (other.isTypeEquivalentTo(fInterfaces[i]) || fInterfaces[i].doIsSubType(other))
85 protected boolean canAssignToStandardType(StandardType target) {
86 if (target.isJavaLangObject())
88 return isSubType(target);