]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-before/core refactoring/org/eclipse/jdt/internal/corext/refactoring/typeconstraints/types/HierarchyType.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-before / core refactoring / org / eclipse / jdt / internal / corext / refactoring / typeconstraints / types / HierarchyType.java
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
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;
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         @Override
47         public TType getSuperclass() {
48                 return fSuperclass;
49         }
50
51         @Override
52         public TType[] getInterfaces() {
53                 return fInterfaces;
54         }
55
56         public IType getJavaElementType() {
57                 return fJavaElementType;
58         }
59
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);
65                         if (value != null)
66                                 return value.booleanValue();
67                         boolean isSub= doIsSubType(other);
68                         value= Boolean.valueOf(isSub);
69                         cache.put(key, value);
70                         return isSub;
71                 }
72                 return doIsSubType(other);
73         }
74
75         private boolean doIsSubType(HierarchyType other) {
76                 if (fSuperclass != null && (other.isTypeEquivalentTo(fSuperclass) || fSuperclass.doIsSubType(other)))
77                         return true;
78                 for (int i= 0; i < fInterfaces.length; i++) {
79                         if (other.isTypeEquivalentTo(fInterfaces[i]) || fInterfaces[i].doIsSubType(other))
80                                 return true;
81                 }
82                 return false;
83         }
84
85         protected boolean canAssignToStandardType(StandardType target) {
86                 if (target.isJavaLangObject())
87                         return true;
88                 return isSubType(target);
89         }
90 }