1 /*******************************************************************************
2 * Copyright (c) 2000, 2004 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.core;
13 import java.util.Collections;
14 import java.util.HashMap;
15 import java.util.HashSet;
16 import java.util.Hashtable;
17 import java.util.Iterator;
20 import net.sourceforge.phpdt.core.IClasspathEntry;
21 import net.sourceforge.phpdt.core.IElementChangedListener;
22 import net.sourceforge.phpdt.core.IJavaModel;
23 import net.sourceforge.phpdt.core.IJavaProject;
24 import net.sourceforge.phpdt.core.JavaModelException;
25 import net.sourceforge.phpdt.internal.core.util.Util;
27 import org.eclipse.core.resources.IProject;
28 import org.eclipse.core.resources.IProjectDescription;
29 import org.eclipse.core.resources.IResourceChangeEvent;
30 import org.eclipse.core.resources.IResourceChangeListener;
31 import org.eclipse.core.resources.IWorkspaceRoot;
32 import org.eclipse.core.runtime.CoreException;
33 import org.eclipse.core.runtime.ISafeRunnable;
34 import org.eclipse.core.runtime.Platform;
37 * Keep the global states used during Java element delta processing.
39 public class DeltaProcessingState implements IResourceChangeListener {
42 * Collection of listeners for Java element deltas
44 public IElementChangedListener[] elementChangedListeners = new IElementChangedListener[5];
45 public int[] elementChangedListenerMasks = new int[5];
46 public int elementChangedListenerCount = 0;
49 * Collection of pre Java resource change listeners
51 public IResourceChangeListener[] preResourceChangeListeners = new IResourceChangeListener[1];
52 public int preResourceChangeListenerCount = 0;
55 * The delta processor for the current thread.
57 private ThreadLocal deltaProcessors = new ThreadLocal();
59 /* A table from IPath (from a classpath entry) to RootInfo */
60 public HashMap roots = new HashMap();
62 /* A table from IPath (from a classpath entry) to ArrayList of RootInfo
63 * Used when an IPath corresponds to more than one root */
64 public HashMap otherRoots = new HashMap();
66 /* A table from IPath (from a classpath entry) to RootInfo
67 * from the last time the delta processor was invoked. */
68 public HashMap oldRoots = new HashMap();
70 /* A table from IPath (from a classpath entry) to ArrayList of RootInfo
71 * from the last time the delta processor was invoked.
72 * Used when an IPath corresponds to more than one root */
73 public HashMap oldOtherRoots = new HashMap();
75 /* A table from IPath (a source attachment path from a classpath entry) to IPath (a root path) */
76 public HashMap sourceAttachments = new HashMap();
78 /* Whether the roots tables should be recomputed */
79 public boolean rootsAreStale = true;
81 /* Threads that are currently running initializeRoots() */
82 private Set initializingThreads = Collections.synchronizedSet(new HashSet());
84 public Hashtable externalTimeStamps = new Hashtable();
86 public HashMap projectUpdates = new HashMap();
88 public static class ProjectUpdateInfo {
90 IClasspathEntry[] oldResolvedPath;
91 IClasspathEntry[] newResolvedPath;
92 IClasspathEntry[] newRawPath;
95 * Update projects references so that the build order is consistent with the classpath
97 public void updateProjectReferencesIfNecessary() throws JavaModelException {
99 String[] oldRequired = this.project.projectPrerequisites(this.oldResolvedPath);
101 if (this.newResolvedPath == null) {
102 this.newResolvedPath = this.project.getResolvedClasspath(this.newRawPath, null, true, true, null/*no reverse map*/);
104 String[] newRequired = this.project.projectPrerequisites(this.newResolvedPath);
106 IProject projectResource = this.project.getProject();
107 IProjectDescription description = projectResource.getDescription();
109 IProject[] projectReferences = description.getDynamicReferences();
111 HashSet oldReferences = new HashSet(projectReferences.length);
112 for (int i = 0; i < projectReferences.length; i++){
113 String projectName = projectReferences[i].getName();
114 oldReferences.add(projectName);
116 HashSet newReferences = (HashSet)oldReferences.clone();
118 for (int i = 0; i < oldRequired.length; i++){
119 String projectName = oldRequired[i];
120 newReferences.remove(projectName);
122 for (int i = 0; i < newRequired.length; i++){
123 String projectName = newRequired[i];
124 newReferences.add(projectName);
128 int newSize = newReferences.size();
131 if (oldReferences.size() == newSize){
132 iter = newReferences.iterator();
133 while (iter.hasNext()){
134 if (!oldReferences.contains(iter.next())){
141 String[] requiredProjectNames = new String[newSize];
143 iter = newReferences.iterator();
144 while (iter.hasNext()){
145 requiredProjectNames[index++] = (String)iter.next();
147 Util.sort(requiredProjectNames); // ensure that if changed, the order is consistent
149 IProject[] requiredProjectArray = new IProject[newSize];
150 IWorkspaceRoot wksRoot = projectResource.getWorkspace().getRoot();
151 for (int i = 0; i < newSize; i++){
152 requiredProjectArray[i] = wksRoot.getProject(requiredProjectNames[i]);
154 description.setDynamicReferences(requiredProjectArray);
155 projectResource.setDescription(description, null);
157 } catch(CoreException e){
158 throw new JavaModelException(e);
165 * Workaround for bug 15168 circular errors not reported
166 * This is a cache of the projects before any project addition/deletion has started.
168 public IJavaProject[] modelProjectsCache;
171 * Need to clone defensively the listener information, in case some listener is reacting to some notification iteration by adding/changing/removing
172 * any of the other (for example, if it deregisters itself).
174 public void addElementChangedListener(IElementChangedListener listener, int eventMask) {
175 for (int i = 0; i < this.elementChangedListenerCount; i++){
176 if (this.elementChangedListeners[i].equals(listener)){
178 // only clone the masks, since we could be in the middle of notifications and one listener decide to change
179 // any event mask of another listeners (yet not notified).
180 int cloneLength = this.elementChangedListenerMasks.length;
181 System.arraycopy(this.elementChangedListenerMasks, 0, this.elementChangedListenerMasks = new int[cloneLength], 0, cloneLength);
182 this.elementChangedListenerMasks[i] = eventMask; // could be different
186 // may need to grow, no need to clone, since iterators will have cached original arrays and max boundary and we only add to the end.
188 if ((length = this.elementChangedListeners.length) == this.elementChangedListenerCount){
189 System.arraycopy(this.elementChangedListeners, 0, this.elementChangedListeners = new IElementChangedListener[length*2], 0, length);
190 System.arraycopy(this.elementChangedListenerMasks, 0, this.elementChangedListenerMasks = new int[length*2], 0, length);
192 this.elementChangedListeners[this.elementChangedListenerCount] = listener;
193 this.elementChangedListenerMasks[this.elementChangedListenerCount] = eventMask;
194 this.elementChangedListenerCount++;
197 public void addPreResourceChangedListener(IResourceChangeListener listener) {
198 for (int i = 0; i < this.preResourceChangeListenerCount; i++){
199 if (this.preResourceChangeListeners[i].equals(listener)) {
203 // may need to grow, no need to clone, since iterators will have cached original arrays and max boundary and we only add to the end.
205 if ((length = this.preResourceChangeListeners.length) == this.preResourceChangeListenerCount){
206 System.arraycopy(this.preResourceChangeListeners, 0, this.preResourceChangeListeners = new IResourceChangeListener[length*2], 0, length);
208 this.preResourceChangeListeners[this.preResourceChangeListenerCount] = listener;
209 this.preResourceChangeListenerCount++;
212 public DeltaProcessor getDeltaProcessor() {
213 DeltaProcessor deltaProcessor = (DeltaProcessor)this.deltaProcessors.get();
214 if (deltaProcessor != null) return deltaProcessor;
215 deltaProcessor = new DeltaProcessor(this, JavaModelManager.getJavaModelManager());
216 this.deltaProcessors.set(deltaProcessor);
217 return deltaProcessor;
220 public void performClasspathResourceChange(JavaProject project, IClasspathEntry[] oldResolvedPath, IClasspathEntry[] newResolvedPath, IClasspathEntry[] newRawPath, boolean canChangeResources) throws JavaModelException {
221 ProjectUpdateInfo info = new ProjectUpdateInfo();
222 info.project = project;
223 info.oldResolvedPath = oldResolvedPath;
224 info.newResolvedPath = newResolvedPath;
225 info.newRawPath = newRawPath;
226 if (canChangeResources) {
227 this.projectUpdates.remove(project); // remove possibly awaiting one
228 info.updateProjectReferencesIfNecessary();
231 this.recordProjectUpdate(info);
234 public void initializeRoots() {
236 // recompute root infos only if necessary
237 HashMap newRoots = null;
238 HashMap newOtherRoots = null;
239 HashMap newSourceAttachments = null;
240 if (this.rootsAreStale) {
241 Thread currentThread = Thread.currentThread();
242 boolean addedCurrentThread = false;
244 // if reentering initialization (through a container initializer for example) no need to compute roots again
245 // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=47213
246 if (!this.initializingThreads.add(currentThread)) return;
247 addedCurrentThread = true;
249 newRoots = new HashMap();
250 newOtherRoots = new HashMap();
251 newSourceAttachments = new HashMap();
253 IJavaModel model = JavaModelManager.getJavaModelManager().getJavaModel();
254 IJavaProject[] projects;
256 projects = model.getJavaProjects();
257 } catch (JavaModelException e) {
258 // nothing can be done
261 for (int i = 0, length = projects.length; i < length; i++) {
262 JavaProject project = (JavaProject) projects[i];
263 // IClasspathEntry[] classpath;
265 // classpath = project.getResolvedClasspath(true/*ignoreUnresolvedEntry*/, false/*don't generateMarkerOnError*/, false/*don't returnResolutionInProgress*/);
266 // } catch (JavaModelException e) {
267 // // continue with next project
270 // for (int j= 0, classpathLength = classpath.length; j < classpathLength; j++) {
271 // IClasspathEntry entry = classpath[j];
272 // if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT) continue;
275 // IPath path = entry.getPath();
276 // if (newRoots.get(path) == null) {
277 // newRoots.put(path, new DeltaProcessor.RootInfo(project, path, ((ClasspathEntry)entry).fullInclusionPatternChars(), ((ClasspathEntry)entry).fullExclusionPatternChars(), entry.getEntryKind()));
279 // ArrayList rootList = (ArrayList)newOtherRoots.get(path);
280 // if (rootList == null) {
281 // rootList = new ArrayList();
282 // newOtherRoots.put(path, rootList);
284 // rootList.add(new DeltaProcessor.RootInfo(project, path, ((ClasspathEntry)entry).fullInclusionPatternChars(), ((ClasspathEntry)entry).fullExclusionPatternChars(), entry.getEntryKind()));
287 // // source attachment path
288 // if (entry.getEntryKind() != IClasspathEntry.CPE_LIBRARY) continue;
289 // QualifiedName qName = new QualifiedName(JavaCore.PLUGIN_ID, "sourceattachment: " + path.toOSString()); //$NON-NLS-1$;
290 // String propertyString = null;
292 // propertyString = ResourcesPlugin.getWorkspace().getRoot().getPersistentProperty(qName);
293 // } catch (CoreException e) {
296 // IPath sourceAttachmentPath;
297 // if (propertyString != null) {
298 // int index= propertyString.lastIndexOf(PackageFragmentRoot.ATTACHMENT_PROPERTY_DELIMITER);
299 // sourceAttachmentPath = (index < 0) ? new Path(propertyString) : new Path(propertyString.substring(0, index));
301 // sourceAttachmentPath = entry.getSourceAttachmentPath();
303 // if (sourceAttachmentPath != null) {
304 // newSourceAttachments.put(sourceAttachmentPath, path);
309 if (addedCurrentThread) {
310 this.initializingThreads.remove(currentThread);
315 this.oldRoots = this.roots;
316 this.oldOtherRoots = this.otherRoots;
317 if (this.rootsAreStale && newRoots != null) { // double check again
318 this.roots = newRoots;
319 this.otherRoots = newOtherRoots;
320 this.sourceAttachments = newSourceAttachments;
321 this.rootsAreStale = false;
326 public synchronized void recordProjectUpdate(ProjectUpdateInfo newInfo) {
328 JavaProject project = newInfo.project;
329 ProjectUpdateInfo oldInfo = (ProjectUpdateInfo) this.projectUpdates.get(project);
330 if (oldInfo != null) { // refresh new classpath information
331 oldInfo.newRawPath = newInfo.newRawPath;
332 oldInfo.newResolvedPath = newInfo.newResolvedPath;
334 this.projectUpdates.put(project, newInfo);
337 public synchronized ProjectUpdateInfo[] removeAllProjectUpdates() {
338 int length = this.projectUpdates.size();
339 if (length == 0) return null;
340 ProjectUpdateInfo[] updates = new ProjectUpdateInfo[length];
341 this.projectUpdates.values().toArray(updates);
342 this.projectUpdates.clear();
346 public void removeElementChangedListener(IElementChangedListener listener) {
348 for (int i = 0; i < this.elementChangedListenerCount; i++){
350 if (this.elementChangedListeners[i].equals(listener)){
352 // need to clone defensively since we might be in the middle of listener notifications (#fire)
353 int length = this.elementChangedListeners.length;
354 IElementChangedListener[] newListeners = new IElementChangedListener[length];
355 System.arraycopy(this.elementChangedListeners, 0, newListeners, 0, i);
356 int[] newMasks = new int[length];
357 System.arraycopy(this.elementChangedListenerMasks, 0, newMasks, 0, i);
359 // copy trailing listeners
360 int trailingLength = this.elementChangedListenerCount - i - 1;
361 if (trailingLength > 0){
362 System.arraycopy(this.elementChangedListeners, i+1, newListeners, i, trailingLength);
363 System.arraycopy(this.elementChangedListenerMasks, i+1, newMasks, i, trailingLength);
366 // update manager listener state (#fire need to iterate over original listeners through a local variable to hold onto
367 // the original ones)
368 this.elementChangedListeners = newListeners;
369 this.elementChangedListenerMasks = newMasks;
370 this.elementChangedListenerCount--;
376 public void removePreResourceChangedListener(IResourceChangeListener listener) {
378 for (int i = 0; i < this.preResourceChangeListenerCount; i++){
380 if (this.preResourceChangeListeners[i].equals(listener)){
382 // need to clone defensively since we might be in the middle of listener notifications (#fire)
383 int length = this.preResourceChangeListeners.length;
384 IResourceChangeListener[] newListeners = new IResourceChangeListener[length];
385 System.arraycopy(this.preResourceChangeListeners, 0, newListeners, 0, i);
387 // copy trailing listeners
388 int trailingLength = this.preResourceChangeListenerCount - i - 1;
389 if (trailingLength > 0){
390 System.arraycopy(this.preResourceChangeListeners, i+1, newListeners, i, trailingLength);
393 // update manager listener state (#fire need to iterate over original listeners through a local variable to hold onto
394 // the original ones)
395 this.preResourceChangeListeners = newListeners;
396 this.preResourceChangeListenerCount--;
402 public void resourceChanged(final IResourceChangeEvent event) {
403 boolean isPostChange = event.getType() == IResourceChangeEvent.POST_CHANGE;
405 for (int i = 0; i < this.preResourceChangeListenerCount; i++) {
406 // wrap callbacks with Safe runnable for subsequent listeners to be called when some are causing grief
407 final IResourceChangeListener listener = this.preResourceChangeListeners[i];
408 Platform.run(new ISafeRunnable() {
409 public void handleException(Throwable exception) {
410 Util.log(exception, "Exception occurred in listener of pre Java resource change notification"); //$NON-NLS-1$
412 public void run() throws Exception {
413 listener.resourceChanged(event);
419 getDeltaProcessor().resourceChanged(event);
421 // TODO (jerome) see 47631, may want to get rid of following so as to reuse delta processor ?
423 this.deltaProcessors.set(null);
430 * Update the roots that are affected by the addition or the removal of the given container resource.
432 // public synchronized void updateRoots(IPath containerPath, IResourceDelta containerDelta, DeltaProcessor deltaProcessor) {
434 // Map otherUpdatedRoots;
435 // if (containerDelta.getKind() == IResourceDelta.REMOVED) {
436 // updatedRoots = this.oldRoots;
437 // otherUpdatedRoots = this.oldOtherRoots;
439 // updatedRoots = this.roots;
440 // otherUpdatedRoots = this.otherRoots;
442 // Iterator iterator = updatedRoots.keySet().iterator();
443 // while (iterator.hasNext()) {
444 // IPath path = (IPath)iterator.next();
445 // if (containerPath.isPrefixOf(path) && !containerPath.equals(path)) {
446 // IResourceDelta rootDelta = containerDelta.findMember(path.removeFirstSegments(1));
447 // if (rootDelta == null) continue;
448 // DeltaProcessor.RootInfo rootInfo = (DeltaProcessor.RootInfo)updatedRoots.get(path);
450 // if (!rootInfo.project.getPath().isPrefixOf(path)) { // only consider roots that are not included in the container
451 // deltaProcessor.updateCurrentDeltaAndIndex(rootDelta, IJavaElement.PACKAGE_FRAGMENT_ROOT, rootInfo);
454 // ArrayList rootList = (ArrayList)otherUpdatedRoots.get(path);
455 // if (rootList != null) {
456 // Iterator otherProjects = rootList.iterator();
457 // while (otherProjects.hasNext()) {
458 // rootInfo = (DeltaProcessor.RootInfo)otherProjects.next();
459 // if (!rootInfo.project.getPath().isPrefixOf(path)) { // only consider roots that are not included in the container
460 // deltaProcessor.updateCurrentDeltaAndIndex(rootDelta, IJavaElement.PACKAGE_FRAGMENT_ROOT, rootInfo);