Skip to main content
summaryrefslogtreecommitdiffstats
blob: a901eb4c96c4eae65c7b20cb35e16cb17f08c065 (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/**********************************************************************
 * This file is part of "Object Teams Development Tooling"-Software
 * 
 * Copyright 2004, 2006 Fraunhofer Gesellschaft, Munich, Germany,
 * for its Fraunhofer Institute for Computer Architecture and Software
 * Technology (FIRST), Berlin, Germany and Technical University Berlin,
 * Germany.
 * 
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 * $Id: OTTypeMapping.java 23416 2010-02-03 19:59:31Z stephan $
 * 
 * Please visit http://www.eclipse.org/objectteams for updates and contact.
 * 
 * Contributors:
 * Fraunhofer FIRST - Initial API and implementation
 * Technical University Berlin - Initial API and implementation
 **********************************************************************/
package org.eclipse.objectteams.otdt.internal.core;

import java.util.List;

import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IType;
import org.eclipse.objectteams.otdt.core.IOTType;


/**
 * Maps an IType from the JavaModel to an OTType. 
 * 
 * @author kaiser
 * @version $Id: OTTypeMapping.java 23416 2010-02-03 19:59:31Z stephan $
 */
public class OTTypeMapping
{
	private CompilationUnitMapping data;

    public OTTypeMapping()
    {
    	this.data = new CompilationUnitMapping();
    }

	public void put(IType type, IOTType otType)
	{		
		if (type != null && otType != null)
		{
			IJavaElement key= getParent(type);
						
			if (this.data.contains(key))
			{
				this.data.getOTTypes(key).add(otType);        
			}
			else
			{
				this.data.add(key, new OTTypeList(otType));
			}						
		}
	}

	/**
	 * Removes the given IType from cache and all its children. The entire 
	 * mapping (ICompilationUnit::OTTypeList) is remove if no more bound type 
	 * is cached.
	 */
	public void remove(IType type)
	{
		if (type != null)
		{
			ICompilationUnit unit = type.getCompilationUnit(); 
			
			OTTypeList list   = this.data.getOTTypes(unit);
			IOTType    otType = get(type);

			if (otType != null)
			{							
				IType[] innerTypes = otType.getInnerTypes();
				for (int idx = 0; idx < innerTypes.length; idx++)
				{
				    // TODO(jwl): simplify later
					remove((IType)((IOTType)innerTypes[idx]).getCorrespondingJavaElement());
				}					
			
				list.remove(type.getElementName());
			}
			
			if (list.getSize() == 0)
			{
				this.data.remove(unit);
			}
		}
	}

	/**
	 * Removes an changed IType, if it is not of the same instance as the given
	 * IType. This method is used for JavaElementDelta.ElementChanged. It 
	 * preserves the newly created type from removal.
	 * 
	 * @see org.eclipse.objectteams.otdt.core.OTModelReconcileListener
	 */
	public void removeChangedElement(IType elem)
	{
		IOTType otType = get(elem);
				
		// removes changed element only if they are not equal, this is necessary
		// to avoid removal if an element has been newly created
		if ((otType != null) && (otType.getCorrespondingJavaElement() != elem))
		{
			remove(elem);
		}		
	}

    public IOTType get(IType type)
	{
		IOTType otElem = null;
		
		if (type != null) 
		{
			IJavaElement key = getParent(type);
			if (key != null)
			{
				OTTypeList elems = this.data.getOTTypes(key);
				
				if (elems != null)
				{
					otElem = elems.get(type.getFullyQualifiedName());				
				}
			}
		}
		
		return otElem;
	}
	
	public List<OTType> getOTElements()
	{
		return this.data.getOTElements();
	}
	
    public boolean contains(IType type)
    {
        return (get(type) != null);
    }

	/**
	 * Get type's parent (for use as a hash key).
	 */
	private IJavaElement getParent(IType type) {
		IJavaElement parent;
		if (type.isBinary())
		{
			parent = type.getClassFile();
		}
		else
		{
			parent = type.getCompilationUnit();
		}
		return parent;
	}


}

Back to the top