Skip to main content
summaryrefslogtreecommitdiffstats
blob: 57d926c2731b4485666c4a93e08327f1801385a1 (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
/**********************************************************************
 * 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: OTModelReconcileListener.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.core;

import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jdt.core.ElementChangedEvent;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IElementChangedListener;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaElementDelta;
import org.eclipse.jdt.core.IParent;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.objectteams.otdt.internal.core.OTType;


/**
 * The OTModelListener performs the necessary updating of the OTM in order
 * to sync with the JavaModel.
 *
 * @author kaiser
 * @version $Id: OTModelReconcileListener.java 23416 2010-02-03 19:59:31Z stephan $
 */
public class OTModelReconcileListener implements IElementChangedListener
{
	/**
	 * Invoked when some or more JavaModel elements have changed, which may
	 * require additional changes within the OTM
	 * @param event - event describing changes
	 */
    @Override
	public void elementChanged(ElementChangedEvent event)
    {
    	removeAffectedModelElements( new IJavaElementDelta[] { event.getDelta() } );
    }

	/**
	 * Removes from JavaModel changes affected OTM elements. Actually it removes
	 * ITypes only which have been removed in the JavaModel.
	 * NOTE: recursive method
	 * PRE: deltas != null
	 */
    private void removeAffectedModelElements(IJavaElementDelta[] deltas)
    {
    	assert (deltas != null);

        for (int idx = 0; idx < deltas.length; idx++)
        {
			IJavaElementDelta delta = deltas[idx];
			IJavaElement      elem  = delta.getElement();

			// check for nested deltas
			if (elem instanceof IParent)
			{
				// visit child deltas
				removeAffectedModelElements(delta.getAffectedChildren());
			}

			// remove corresponding OTM elements if JavaModel IType has been changed/removed
			if (elem instanceof IType)
			{
				// check for changed modifiers on element-changed deltas because
				// this means that the JavaModel IType element has been recreated
				// and old instances need to be removed from the OTM
				if ((delta.getKind() == IJavaElementDelta.CHANGED)
					&& ((delta.getFlags() & IJavaElementDelta.F_MODIFIERS) != 0))
				{
					OTModelManager.removeOTElement((IType)elem, true);
				}
				else if (delta.getKind() == IJavaElementDelta.REMOVED)
				{
					OTModelManager.removeOTElement((IType)elem);
				}
			}
		    // TODO (carp): do we need special support for WorkingCopies, e.g. when creating
		    // or discarding them?
//			checkWorkingCopies(delta, elem);
        }
    }

    @SuppressWarnings("unused") // see above
	private void checkWorkingCopies(IJavaElementDelta delta, IJavaElement elem)
    {
		if (elem instanceof ICompilationUnit)
		{
			ICompilationUnit unit = (ICompilationUnit) elem;
			if ((delta.getFlags() & IJavaElementDelta.REMOVED) != 0)
			{
			    if (unit.exists())
			    {
			        try
                    {
                        IType[] types = unit.getTypes();
                        System.out.println(types);
                    }
                    catch (JavaModelException ex)
                    {
                        ex.printStackTrace();
                    }
			    }
			}

			if ((delta.getFlags() & IJavaElementDelta.F_PRIMARY_WORKING_COPY) != 0)
			{
				if (!unit.isWorkingCopy() && unit.exists())
				{
					try {
				        ICompilationUnit wc = unit.getWorkingCopy(new NullProgressMonitor());
				        IType[] wcTypes = wc.getTypes();
						IType[] types = unit.getAllTypes();
						for (int i = 0; i < types.length; i++)
						{
                            IType wcType = wcTypes[i];
						    IType currentType = types[i];
							OTType otType = (OTType) OTModelManager.getOTElement(currentType);
							IOTType wcOTType = OTModelManager.getOTElement(wcType);
							if (otType == wcOTType)
							{
								// FIXME(SH): fetching into unused locals? WHY? Incomplete implementation??
								IOTType t1 = OTModelManager.getOTElement(currentType);
								IOTType t2 = OTModelManager.getOTElement(wcType);
							}

							if (otType != null) // need to update the workingcopy
							{
							    ICompilationUnit otUnit = otType.getCompilationUnit();
							    if (otUnit.isWorkingCopy())
							    {
							        otType.setCorrespondingJavaElement(currentType);
							    }
							}
						}
					}
					catch (JavaModelException ignored) {
						// ignore
					}
				}
			}
		}
    }
}

Back to the top