KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > core > SetContainerOperation


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.core;
12
13 import org.eclipse.core.resources.ResourcesPlugin;
14 import org.eclipse.core.runtime.CoreException;
15 import org.eclipse.core.runtime.IPath;
16 import org.eclipse.jdt.core.IClasspathContainer;
17 import org.eclipse.jdt.core.IClasspathEntry;
18 import org.eclipse.jdt.core.IJavaElement;
19 import org.eclipse.jdt.core.IJavaProject;
20 import org.eclipse.jdt.core.JavaModelException;
21 import org.eclipse.jdt.internal.core.util.Util;
22
23 public class SetContainerOperation extends ChangeClasspathOperation {
24     
25     IPath containerPath;
26     IJavaProject[] affectedProjects;
27     IClasspathContainer[] respectiveContainers;
28     
29     /*
30      * Creates a new SetContainerOperation.
31      */

32     public SetContainerOperation(IPath containerPath, IJavaProject[] affectedProjects, IClasspathContainer[] respectiveContainers) {
33         super(new IJavaElement[] {JavaModelManager.getJavaModelManager().getJavaModel()}, !ResourcesPlugin.getWorkspace().isTreeLocked());
34         this.containerPath = containerPath;
35         this.affectedProjects = affectedProjects;
36         this.respectiveContainers = respectiveContainers;
37     }
38
39     protected void executeOperation() throws JavaModelException {
40         checkCanceled();
41         try {
42             beginTask("", 1); //$NON-NLS-1$
43
if (JavaModelManager.CP_RESOLVE_VERBOSE)
44                 verbose_set_container();
45             if (JavaModelManager.CP_RESOLVE_VERBOSE_ADVANCED)
46                 verbose_set_container_invocation_trace();
47             
48             JavaModelManager manager = JavaModelManager.getJavaModelManager();
49             if (manager.containerPutIfInitializingWithSameEntries(this.containerPath, this.affectedProjects, this.respectiveContainers))
50                 return;
51     
52             final int projectLength = this.affectedProjects.length;
53             final IJavaProject[] modifiedProjects;
54             System.arraycopy(this.affectedProjects, 0, modifiedProjects = new IJavaProject[projectLength], 0, projectLength);
55             final IClasspathEntry[][] oldResolvedPaths = new IClasspathEntry[projectLength][];
56                 
57             // filter out unmodified project containers
58
int remaining = 0;
59             for (int i = 0; i < projectLength; i++){
60                 if (isCanceled())
61                     return;
62                 JavaProject affectedProject = (JavaProject) this.affectedProjects[i];
63                 IClasspathContainer newContainer = this.respectiveContainers[i];
64                 if (newContainer == null) newContainer = JavaModelManager.CONTAINER_INITIALIZATION_IN_PROGRESS; // 30920 - prevent infinite loop
65
boolean found = false;
66                 if (JavaProject.hasJavaNature(affectedProject.getProject())){
67                     IClasspathEntry[] rawClasspath = affectedProject.getRawClasspath();
68                     for (int j = 0, cpLength = rawClasspath.length; j <cpLength; j++) {
69                         IClasspathEntry entry = rawClasspath[j];
70                         if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER && entry.getPath().equals(this.containerPath)){
71                             found = true;
72                             break;
73                         }
74                     }
75                 }
76                 if (!found) {
77                     modifiedProjects[i] = null; // filter out this project - does not reference the container path, or isnt't yet Java project
78
manager.containerPut(affectedProject, this.containerPath, newContainer);
79                     continue;
80                 }
81                 IClasspathContainer oldContainer = manager.containerGet(affectedProject, this.containerPath);
82                 if (oldContainer == JavaModelManager.CONTAINER_INITIALIZATION_IN_PROGRESS) {
83                     oldContainer = null;
84                 }
85                 if ((oldContainer != null && oldContainer.equals(this.respectiveContainers[i]))
86                         || (oldContainer == this.respectiveContainers[i])/*handle case where old and new containers are null (see bug 149043*/) {
87                     modifiedProjects[i] = null; // filter out this project - container did not change
88
continue;
89                 }
90                 remaining++;
91                 oldResolvedPaths[i] = affectedProject.getResolvedClasspath();
92                 manager.containerPut(affectedProject, this.containerPath, newContainer);
93             }
94             
95             if (remaining == 0) return;
96             
97             // trigger model refresh
98
try {
99                 for(int i = 0; i < projectLength; i++){
100                     if (isCanceled())
101                         return;
102                     
103                     JavaProject affectedProject = (JavaProject)modifiedProjects[i];
104                     if (affectedProject == null) continue; // was filtered out
105
if (JavaModelManager.CP_RESOLVE_VERBOSE_ADVANCED)
106                         verbose_update_project(affectedProject);
107         
108                     // force resolved classpath to be recomputed
109
affectedProject.getPerProjectInfo().resetResolvedClasspath();
110                     
111                     // if needed, generate delta, update project ref, create markers, ...
112
classpathChanged(affectedProject);
113                     
114                     if (this.canChangeResources) {
115                         // touch project to force a build if needed
116
try {
117                             affectedProject.getProject().touch(this.progressMonitor);
118                         } catch (CoreException e) {
119                             // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=148970
120
if (!ExternalJavaProject.EXTERNAL_PROJECT_NAME.equals(affectedProject.getElementName()))
121                                 throw e;
122                         }
123                     }
124                 }
125             } catch(CoreException e) {
126                 if (JavaModelManager.CP_RESOLVE_VERBOSE)
127                     verbose_failure(e);
128                 if (e instanceof JavaModelException) {
129                     throw (JavaModelException)e;
130                 } else {
131                     throw new JavaModelException(e);
132                 }
133             } finally {
134                 for (int i = 0; i < projectLength; i++) {
135                     if (this.respectiveContainers[i] == null) {
136                         manager.containerPut(this.affectedProjects[i], this.containerPath, null); // reset init in progress marker
137
}
138                 }
139             }
140         } finally {
141             done();
142         }
143     }
144
145     private void verbose_failure(CoreException e) {
146         Util.verbose(
147             "CPContainer SET - FAILED DUE TO EXCEPTION\n" + //$NON-NLS-1$
148
" container path: " + this.containerPath, //$NON-NLS-1$
149
System.err);
150         e.printStackTrace();
151     }
152
153     private void verbose_update_project(JavaProject affectedProject) {
154         Util.verbose(
155             "CPContainer SET - updating affected project due to setting container\n" + //$NON-NLS-1$
156
" project: " + affectedProject.getElementName() + '\n' + //$NON-NLS-1$
157
" container path: " + this.containerPath); //$NON-NLS-1$
158
}
159
160     private void verbose_set_container() {
161         Util.verbose(
162             "CPContainer SET - setting container\n" + //$NON-NLS-1$
163
" container path: " + this.containerPath + '\n' + //$NON-NLS-1$
164
" projects: {" +//$NON-NLS-1$
165
org.eclipse.jdt.internal.compiler.util.Util.toString(
166                 this.affectedProjects,
167                 new org.eclipse.jdt.internal.compiler.util.Util.Displayable(){
168                     public String JavaDoc displayString(Object JavaDoc o) { return ((IJavaProject) o).getElementName(); }
169                 }) +
170             "}\n values: {\n" +//$NON-NLS-1$
171
org.eclipse.jdt.internal.compiler.util.Util.toString(
172                 this.respectiveContainers,
173                 new org.eclipse.jdt.internal.compiler.util.Util.Displayable(){
174                     public String JavaDoc displayString(Object JavaDoc o) {
175                         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(" "); //$NON-NLS-1$
176
if (o == null) {
177                             buffer.append("<null>"); //$NON-NLS-1$
178
return buffer.toString();
179                         }
180                         IClasspathContainer container = (IClasspathContainer) o;
181                         buffer.append(container.getDescription());
182                         buffer.append(" {\n"); //$NON-NLS-1$
183
IClasspathEntry[] entries = container.getClasspathEntries();
184                         if (entries != null){
185                             for (int i = 0; i < entries.length; i++){
186                                 buffer.append(" "); //$NON-NLS-1$
187
buffer.append(entries[i]);
188                                 buffer.append('\n');
189                             }
190                         }
191                         buffer.append(" }"); //$NON-NLS-1$
192
return buffer.toString();
193                     }
194                 }) +
195             "\n }");//$NON-NLS-1$
196
}
197     
198     private void verbose_set_container_invocation_trace() {
199         Util.verbose(
200             "CPContainer SET - setting container\n" + //$NON-NLS-1$
201
" invocation stack trace:"); //$NON-NLS-1$
202
new Exception JavaDoc("<Fake exception>").printStackTrace(System.out); //$NON-NLS-1$
203
}
204     
205 }
206
Popular Tags