KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.Map JavaDoc;
14
15 import org.eclipse.core.runtime.Assert;
16
17 import org.eclipse.jdt.core.IType;
18 import org.eclipse.jdt.core.dom.ITypeBinding;
19
20
21 public abstract class HierarchyType extends TType {
22     private HierarchyType fSuperclass;
23     private HierarchyType[] fInterfaces;
24     private IType fJavaElementType;
25     
26     protected HierarchyType(TypeEnvironment environment) {
27         super(environment);
28     }
29
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);
38         }
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]);
43         }
44     }
45     
46     public TType getSuperclass() {
47         return fSuperclass;
48     }
49     
50     public TType[] getInterfaces() {
51         return fInterfaces;
52     }
53     
54     public IType getJavaElementType() {
55         return fJavaElementType;
56     }
57     
58     public boolean isSubType(HierarchyType other) {
59         if (getEnvironment() == other.getEnvironment()) {
60             Map JavaDoc cache= getEnvironment().getSubTypeCache();
61             TypeTuple key= new TypeTuple(this, other);
62             Boolean JavaDoc value= (Boolean JavaDoc)cache.get(key);
63             if (value != null)
64                 return value.booleanValue();
65             boolean isSub= doIsSubType(other);
66             value= Boolean.valueOf(isSub);
67             cache.put(key, value);
68             return isSub;
69         }
70         return doIsSubType(other);
71     }
72
73     private boolean doIsSubType(HierarchyType other) {
74         if (fSuperclass != null && (other.isTypeEquivalentTo(fSuperclass) || fSuperclass.doIsSubType(other)))
75             return true;
76         for (int i= 0; i < fInterfaces.length; i++) {
77             if (other.isTypeEquivalentTo(fInterfaces[i]) || fInterfaces[i].doIsSubType(other))
78                 return true;
79         }
80         return false;
81     }
82     
83     protected boolean canAssignToStandardType(StandardType target) {
84         if (target.isJavaLangObject())
85             return true;
86         return isSubType(target);
87     }
88 }
89
Popular Tags