View | Details | Raw Unified | Return to bug 217921
Collapse All | Expand All

(-)src/org/eclipse/pde/internal/core/ClasspathComputer.java (+339 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2005, 2008 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.pde.internal.core;
12
13
import java.util.*;
14
import org.eclipse.core.resources.*;
15
import org.eclipse.core.runtime.CoreException;
16
import org.eclipse.core.runtime.IPath;
17
import org.eclipse.jdt.core.*;
18
import org.eclipse.jdt.launching.JavaRuntime;
19
import org.eclipse.jdt.launching.environments.IExecutionEnvironment;
20
import org.eclipse.jdt.launching.environments.IExecutionEnvironmentsManager;
21
import org.eclipse.osgi.service.resolver.BundleDescription;
22
import org.eclipse.pde.core.build.*;
23
import org.eclipse.pde.core.plugin.IPluginLibrary;
24
import org.eclipse.pde.core.plugin.IPluginModelBase;
25
import org.eclipse.pde.internal.core.build.WorkspaceBuildModel;
26
import org.eclipse.pde.internal.core.util.CoreUtility;
27
import org.eclipse.team.core.RepositoryProvider;
28
29
public class ClasspathComputer {
30
31
	private static Hashtable fSeverityTable = null;
32
	private static final int SEVERITY_ERROR = 3;
33
	private static final int SEVERITY_WARNING = 2;
34
	private static final int SEVERITY_IGNORE = 1;
35
36
	public static void setClasspath(IProject project, IPluginModelBase model) throws CoreException {
37
		IClasspathEntry[] entries = getClasspath(project, model, false);
38
		JavaCore.create(project).setRawClasspath(entries, null);
39
	}
40
41
	public static IClasspathEntry[] getClasspath(IProject project, IPluginModelBase model, boolean clear) throws CoreException {
42
		IJavaProject javaProject = JavaCore.create(project);
43
		ArrayList result = new ArrayList();
44
		IBuild build = getBuild(project);
45
46
		// add JRE and set compliance options
47
		String ee = getExecutionEnvironment(model.getBundleDescription());
48
		result.add(createEntryUsingPreviousEntry(javaProject, ee, PDECore.JRE_CONTAINER_PATH));
49
		setComplianceOptions(JavaCore.create(project), ExecutionEnvironmentAnalyzer.getCompliance(ee));
50
51
		// add pde container
52
		result.add(createEntryUsingPreviousEntry(javaProject, ee, PDECore.REQUIRED_PLUGINS_CONTAINER_PATH));
53
54
		// add own libraries/source
55
		addSourceAndLibraries(project, model, build, clear, result);
56
57
		IClasspathEntry[] entries = (IClasspathEntry[]) result.toArray(new IClasspathEntry[result.size()]);
58
		IJavaModelStatus validation = JavaConventions.validateClasspath(javaProject, entries, javaProject.getOutputLocation());
59
		if (!validation.isOK()) {
60
			PDECore.logErrorMessage(validation.getMessage());
61
			throw new CoreException(validation);
62
		}
63
		return (IClasspathEntry[]) result.toArray(new IClasspathEntry[result.size()]);
64
	}
65
66
	public static void addSourceAndLibraries(IProject project, IPluginModelBase model, IBuild build, boolean clear, ArrayList result) throws CoreException {
67
68
		HashSet paths = new HashSet();
69
70
		// keep existing source folders
71
		if (!clear) {
72
			IClasspathEntry[] entries = JavaCore.create(project).getRawClasspath();
73
			for (int i = 0; i < entries.length; i++) {
74
				IClasspathEntry entry = entries[i];
75
				if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
76
					if (paths.add(entry.getPath()))
77
						result.add(entry);
78
				}
79
			}
80
		}
81
82
		IClasspathAttribute[] attrs = getClasspathAttributes(project, model);
83
		IPluginLibrary[] libraries = model.getPluginBase().getLibraries();
84
		for (int i = 0; i < libraries.length; i++) {
85
			IBuildEntry buildEntry = build == null ? null : build.getEntry("source." + libraries[i].getName()); //$NON-NLS-1$
86
			if (buildEntry != null) {
87
				addSourceFolder(buildEntry, project, paths, result);
88
			} else {
89
				if (libraries[i].getName().equals(".")) //$NON-NLS-1$
90
					addJARdPlugin(project, ClasspathUtilCore.getFilename(model), attrs, result);
91
				else
92
					addLibraryEntry(project, libraries[i], attrs, result);
93
			}
94
		}
95
		if (libraries.length == 0) {
96
			if (build != null) {
97
				IBuildEntry buildEntry = build == null ? null : build.getEntry("source.."); //$NON-NLS-1$
98
				if (buildEntry != null) {
99
					addSourceFolder(buildEntry, project, paths, result);
100
				}
101
			} else if (ClasspathUtilCore.hasBundleStructure(model)) {
102
				addJARdPlugin(project, ClasspathUtilCore.getFilename(model), attrs, result);
103
			}
104
		}
105
	}
106
107
	private static IClasspathAttribute[] getClasspathAttributes(IProject project, IPluginModelBase model) {
108
		IClasspathAttribute[] attributes = new IClasspathAttribute[0];
109
		if (!RepositoryProvider.isShared(project)) {
110
			JavadocLocationManager manager = PDECore.getDefault().getJavadocLocationManager();
111
			String javadoc = manager.getJavadocLocation(model);
112
			if (javadoc != null) {
113
				attributes = new IClasspathAttribute[] {JavaCore.newClasspathAttribute(IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME, javadoc)};
114
			}
115
		}
116
		return attributes;
117
	}
118
119
	private static void addSourceFolder(IBuildEntry buildEntry, IProject project, HashSet paths, ArrayList result) throws CoreException {
120
		String[] folders = buildEntry.getTokens();
121
		for (int j = 0; j < folders.length; j++) {
122
			String folder = folders[j];
123
			IPath path = project.getFullPath().append(folder);
124
			if (paths.add(path)) {
125
				if (project.findMember(folder) == null) {
126
					CoreUtility.createFolder(project.getFolder(folder));
127
				} else {
128
					IPackageFragmentRoot root = JavaCore.create(project).getPackageFragmentRoot(path.toString());
129
					if (root.exists() && root.getKind() == IPackageFragmentRoot.K_BINARY) {
130
						result.add(root.getRawClasspathEntry());
131
						continue;
132
					}
133
				}
134
				result.add(JavaCore.newSourceEntry(path));
135
			}
136
		}
137
	}
138
139
	protected static IBuild getBuild(IProject project) throws CoreException {
140
		IFile buildFile = project.getFile("build.properties"); //$NON-NLS-1$
141
		IBuildModel buildModel = null;
142
		if (buildFile.exists()) {
143
			buildModel = new WorkspaceBuildModel(buildFile);
144
			buildModel.load();
145
		}
146
		return (buildModel != null) ? buildModel.getBuild() : null;
147
	}
148
149
	private static void addLibraryEntry(IProject project, IPluginLibrary library, IClasspathAttribute[] attrs, ArrayList result) throws JavaModelException {
150
		String name = ClasspathUtilCore.expandLibraryName(library.getName());
151
		IResource jarFile = project.findMember(name);
152
		if (jarFile == null)
153
			return;
154
155
		IPackageFragmentRoot root = JavaCore.create(project).getPackageFragmentRoot(jarFile);
156
		if (root.exists() && root.getKind() == IPackageFragmentRoot.K_BINARY) {
157
			IClasspathEntry oldEntry = root.getRawClasspathEntry();
158
			if (oldEntry.getSourceAttachmentPath() != null && !result.contains(oldEntry)) {
159
				result.add(oldEntry);
160
				return;
161
			}
162
		}
163
164
		IClasspathEntry entry = createClasspathEntry(project, jarFile, name, attrs, library.isExported());
165
		if (!result.contains(entry))
166
			result.add(entry);
167
	}
168
169
	private static void addJARdPlugin(IProject project, String filename, IClasspathAttribute[] attrs, ArrayList result) {
170
		String name = ClasspathUtilCore.expandLibraryName(filename);
171
		IResource jarFile = project.findMember(name);
172
		if (jarFile != null) {
173
			IClasspathEntry entry = createClasspathEntry(project, jarFile, filename, attrs, true);
174
			if (!result.contains(entry))
175
				result.add(entry);
176
		}
177
	}
178
179
	private static IClasspathEntry createClasspathEntry(IProject project, IResource library, String fileName, IClasspathAttribute[] attrs, boolean isExported) {
180
		String sourceZipName = ClasspathUtilCore.getSourceZipName(fileName);
181
		IResource resource = project.findMember(sourceZipName);
182
		// if zip file does not exist, see if a directory with the source does.  This in necessary how we import source for individual source bundles.
183
		if (resource == null && sourceZipName.endsWith(".zip")) { //$NON-NLS-1$
184
			resource = project.findMember(sourceZipName.substring(0, sourceZipName.length() - 4));
185
			if (resource == null)
186
				// if we can't find the the source for a library, then try to find the common source location set up to share source from one jar to all libraries.
187
				// see PluginImportOperation.linkSourceArchives
188
				resource = project.getFile(project.getName() + "src.zip"); //$NON-NLS-1$
189
		}
190
		IPath srcAttachment = resource != null ? resource.getFullPath() : library.getFullPath();
191
		return JavaCore.newLibraryEntry(library.getFullPath(), srcAttachment, null, new IAccessRule[0], attrs, isExported);
192
	}
193
194
	private static String getExecutionEnvironment(BundleDescription bundleDescription) {
195
		if (bundleDescription != null) {
196
			String[] envs = bundleDescription.getExecutionEnvironments();
197
			if (envs.length > 0)
198
				return envs[0];
199
		}
200
		return null;
201
	}
202
203
	public static void setComplianceOptions(IJavaProject project, String compliance) {
204
		Map map = project.getOptions(false);
205
		if (compliance == null) {
206
			if (map.size() > 0) {
207
				map.remove(JavaCore.COMPILER_COMPLIANCE);
208
				map.remove(JavaCore.COMPILER_SOURCE);
209
				map.remove(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM);
210
				map.remove(JavaCore.COMPILER_PB_ASSERT_IDENTIFIER);
211
				map.remove(JavaCore.COMPILER_PB_ENUM_IDENTIFIER);
212
			} else {
213
				return;
214
			}
215
		} else if (JavaCore.VERSION_1_6.equals(compliance)) {
216
			map.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_6);
217
			map.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_6);
218
			map.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_6);
219
			map.put(JavaCore.COMPILER_PB_ASSERT_IDENTIFIER, JavaCore.ERROR);
220
			map.put(JavaCore.COMPILER_PB_ENUM_IDENTIFIER, JavaCore.ERROR);
221
		} else if (JavaCore.VERSION_1_5.equals(compliance)) {
222
			map.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_5);
223
			map.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_5);
224
			map.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_5);
225
			map.put(JavaCore.COMPILER_PB_ASSERT_IDENTIFIER, JavaCore.ERROR);
226
			map.put(JavaCore.COMPILER_PB_ENUM_IDENTIFIER, JavaCore.ERROR);
227
		} else if (JavaCore.VERSION_1_4.equals(compliance)) {
228
			map.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_4);
229
			map.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_3);
230
			map.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_2);
231
			updateSeverityComplianceOption(map, JavaCore.COMPILER_PB_ASSERT_IDENTIFIER, JavaCore.WARNING);
232
			updateSeverityComplianceOption(map, JavaCore.COMPILER_PB_ENUM_IDENTIFIER, JavaCore.WARNING);
233
		} else if (JavaCore.VERSION_1_3.equals(compliance)) {
234
			map.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_3);
235
			map.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_3);
236
			map.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_1);
237
			updateSeverityComplianceOption(map, JavaCore.COMPILER_PB_ASSERT_IDENTIFIER, JavaCore.IGNORE);
238
			updateSeverityComplianceOption(map, JavaCore.COMPILER_PB_ENUM_IDENTIFIER, JavaCore.IGNORE);
239
		}
240
		project.setOptions(map);
241
	}
242
243
	private static void updateSeverityComplianceOption(Map map, String key, String value) {
244
		Integer current_value = null;
245
		Integer new_value = null;
246
		String current_string_value = null;
247
		int current_int_value = 0;
248
		int new_int_value = 0;
249
		// Initialize the severity table (only once)
250
		if (fSeverityTable == null) {
251
			fSeverityTable = new Hashtable(SEVERITY_ERROR);
252
			fSeverityTable.put(JavaCore.IGNORE, new Integer(SEVERITY_IGNORE));
253
			fSeverityTable.put(JavaCore.WARNING, new Integer(SEVERITY_WARNING));
254
			fSeverityTable.put(JavaCore.ERROR, new Integer(SEVERITY_ERROR));
255
		}
256
		// Get the current severity
257
		current_string_value = (String) map.get(key);
258
		if (current_string_value != null) {
259
			current_value = (Integer) fSeverityTable.get(current_string_value);
260
			if (current_value != null) {
261
				current_int_value = current_value.intValue();
262
			}
263
		}
264
		// Get the new severity
265
		new_value = (Integer) fSeverityTable.get(value);
266
		if (new_value != null) {
267
			new_int_value = new_value.intValue();
268
		}
269
		// If the current severity is not higher than the new severity, replace it
270
		if (new_int_value > current_int_value) {
271
			map.put(key, value);
272
		}
273
	}
274
275
	/**
276
	 * Returns a new classpath container entry for the given execution environment.  If the given java project
277
	 * has an existing JRE/EE classpath entry, the access rules, extra attributes and isExported settings of
278
	 * the existing entry will be added to the new execution entry.
279
	 *  
280
	 * @param javaProject project to check for existing classpath entries
281
	 * @param ee id of the execution environment to create an entry for
282
	 * @param path id of the container to create an entry for
283
	 * 
284
	 * @return new classpath container entry
285
	 * @throws CoreException if there is a problem accessing the classpath entries of the project
286
	 */
287
	public static IClasspathEntry createEntryUsingPreviousEntry(IJavaProject javaProject, String ee, IPath path) throws CoreException {
288
		IClasspathEntry[] entries = javaProject.getRawClasspath();
289
		for (int i = 0; i < entries.length; i++) {
290
			if (entries[i].getPath().equals(path)) {
291
				if (path.equals(PDECore.JRE_CONTAINER_PATH))
292
					return JavaCore.newContainerEntry(getEEPath(ee), entries[i].getAccessRules(), entries[i].getExtraAttributes(), entries[i].isExported());
293
294
				return JavaCore.newContainerEntry(path, entries[i].getAccessRules(), entries[i].getExtraAttributes(), entries[i].isExported());
295
			}
296
		}
297
298
		if (path.equals(PDECore.JRE_CONTAINER_PATH))
299
			return createJREEntry(ee);
300
301
		return JavaCore.newContainerEntry(path);
302
	}
303
304
	/**
305
	 * Returns a classpath container entry for the given execution environment.
306
	 * @param ee id of the execution environment
307
	 * @return classpath container entry
308
	 */
309
	public static IClasspathEntry createJREEntry(String ee) {
310
		return JavaCore.newContainerEntry(getEEPath(ee));
311
	}
312
313
	/**
314
	 * Returns the JRE container path for the execution environment with the given id.
315
	 * @param ee execution environment id
316
	 * @return JRE container path for the execution environment
317
	 */
318
	private static IPath getEEPath(String ee) {
319
		IPath path = null;
320
		if (ee != null) {
321
			IExecutionEnvironmentsManager manager = JavaRuntime.getExecutionEnvironmentsManager();
322
			IExecutionEnvironment env = manager.getEnvironment(ee);
323
			if (env != null)
324
				path = JavaRuntime.newJREContainerPath(env);
325
		}
326
		if (path == null) {
327
			path = JavaRuntime.newDefaultJREContainerPath();
328
		}
329
		return path;
330
	}
331
332
	/**
333
	 * @return a new classpath container entry for a required plugin container
334
	 */
335
	public static IClasspathEntry createContainerEntry() {
336
		return JavaCore.newContainerEntry(PDECore.REQUIRED_PLUGINS_CONTAINER_PATH);
337
	}
338
339
}
(-)src/org/eclipse/pde/internal/ui/editor/plugin/ExecutionEnvironmentSection.java (-1 / +1 lines)
Lines 24-29 Link Here
24
import org.eclipse.pde.core.IModelChangedEvent;
24
import org.eclipse.pde.core.IModelChangedEvent;
25
import org.eclipse.pde.core.plugin.IPluginModelBase;
25
import org.eclipse.pde.core.plugin.IPluginModelBase;
26
import org.eclipse.pde.core.plugin.PluginRegistry;
26
import org.eclipse.pde.core.plugin.PluginRegistry;
27
import org.eclipse.pde.internal.core.ClasspathComputer;
27
import org.eclipse.pde.internal.core.ibundle.*;
28
import org.eclipse.pde.internal.core.ibundle.*;
28
import org.eclipse.pde.internal.core.text.bundle.*;
29
import org.eclipse.pde.internal.core.text.bundle.*;
29
import org.eclipse.pde.internal.ui.*;
30
import org.eclipse.pde.internal.ui.*;
Lines 33-39 Link Here
33
import org.eclipse.pde.internal.ui.parts.EditableTablePart;
34
import org.eclipse.pde.internal.ui.parts.EditableTablePart;
34
import org.eclipse.pde.internal.ui.parts.TablePart;
35
import org.eclipse.pde.internal.ui.parts.TablePart;
35
import org.eclipse.pde.internal.ui.preferences.PDEPreferencesUtil;
36
import org.eclipse.pde.internal.ui.preferences.PDEPreferencesUtil;
36
import org.eclipse.pde.internal.ui.wizards.plugin.ClasspathComputer;
37
import org.eclipse.swt.SWT;
37
import org.eclipse.swt.SWT;
38
import org.eclipse.swt.graphics.Image;
38
import org.eclipse.swt.graphics.Image;
39
import org.eclipse.swt.layout.GridData;
39
import org.eclipse.swt.layout.GridData;
(-)src/org/eclipse/pde/internal/ui/wizards/plugin/ClasspathComputer.java (-340 lines)
Removed Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2005, 2008 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.pde.internal.ui.wizards.plugin;
12
13
import java.util.*;
14
import org.eclipse.core.resources.*;
15
import org.eclipse.core.runtime.CoreException;
16
import org.eclipse.core.runtime.IPath;
17
import org.eclipse.jdt.core.*;
18
import org.eclipse.jdt.launching.JavaRuntime;
19
import org.eclipse.jdt.launching.environments.IExecutionEnvironment;
20
import org.eclipse.jdt.launching.environments.IExecutionEnvironmentsManager;
21
import org.eclipse.osgi.service.resolver.BundleDescription;
22
import org.eclipse.pde.core.build.*;
23
import org.eclipse.pde.core.plugin.IPluginLibrary;
24
import org.eclipse.pde.core.plugin.IPluginModelBase;
25
import org.eclipse.pde.internal.core.*;
26
import org.eclipse.pde.internal.core.build.WorkspaceBuildModel;
27
import org.eclipse.pde.internal.core.util.CoreUtility;
28
import org.eclipse.team.core.RepositoryProvider;
29
30
public class ClasspathComputer {
31
32
	private static Hashtable fSeverityTable = null;
33
	private static final int SEVERITY_ERROR = 3;
34
	private static final int SEVERITY_WARNING = 2;
35
	private static final int SEVERITY_IGNORE = 1;
36
37
	public static void setClasspath(IProject project, IPluginModelBase model) throws CoreException {
38
		IClasspathEntry[] entries = getClasspath(project, model, false);
39
		JavaCore.create(project).setRawClasspath(entries, null);
40
	}
41
42
	public static IClasspathEntry[] getClasspath(IProject project, IPluginModelBase model, boolean clear) throws CoreException {
43
		IJavaProject javaProject = JavaCore.create(project);
44
		ArrayList result = new ArrayList();
45
		IBuild build = getBuild(project);
46
47
		// add JRE and set compliance options
48
		String ee = getExecutionEnvironment(model.getBundleDescription());
49
		result.add(createEntryUsingPreviousEntry(javaProject, ee, PDECore.JRE_CONTAINER_PATH));
50
		setComplianceOptions(JavaCore.create(project), ExecutionEnvironmentAnalyzer.getCompliance(ee));
51
52
		// add pde container
53
		result.add(createEntryUsingPreviousEntry(javaProject, ee, PDECore.REQUIRED_PLUGINS_CONTAINER_PATH));
54
55
		// add own libraries/source
56
		addSourceAndLibraries(project, model, build, clear, result);
57
58
		IClasspathEntry[] entries = (IClasspathEntry[]) result.toArray(new IClasspathEntry[result.size()]);
59
		IJavaModelStatus validation = JavaConventions.validateClasspath(javaProject, entries, javaProject.getOutputLocation());
60
		if (!validation.isOK()) {
61
			PDECore.logErrorMessage(validation.getMessage());
62
			throw new CoreException(validation);
63
		}
64
		return (IClasspathEntry[]) result.toArray(new IClasspathEntry[result.size()]);
65
	}
66
67
	public static void addSourceAndLibraries(IProject project, IPluginModelBase model, IBuild build, boolean clear, ArrayList result) throws CoreException {
68
69
		HashSet paths = new HashSet();
70
71
		// keep existing source folders
72
		if (!clear) {
73
			IClasspathEntry[] entries = JavaCore.create(project).getRawClasspath();
74
			for (int i = 0; i < entries.length; i++) {
75
				IClasspathEntry entry = entries[i];
76
				if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
77
					if (paths.add(entry.getPath()))
78
						result.add(entry);
79
				}
80
			}
81
		}
82
83
		IClasspathAttribute[] attrs = getClasspathAttributes(project, model);
84
		IPluginLibrary[] libraries = model.getPluginBase().getLibraries();
85
		for (int i = 0; i < libraries.length; i++) {
86
			IBuildEntry buildEntry = build == null ? null : build.getEntry("source." + libraries[i].getName()); //$NON-NLS-1$
87
			if (buildEntry != null) {
88
				addSourceFolder(buildEntry, project, paths, result);
89
			} else {
90
				if (libraries[i].getName().equals(".")) //$NON-NLS-1$
91
					addJARdPlugin(project, ClasspathUtilCore.getFilename(model), attrs, result);
92
				else
93
					addLibraryEntry(project, libraries[i], attrs, result);
94
			}
95
		}
96
		if (libraries.length == 0) {
97
			if (build != null) {
98
				IBuildEntry buildEntry = build == null ? null : build.getEntry("source.."); //$NON-NLS-1$
99
				if (buildEntry != null) {
100
					addSourceFolder(buildEntry, project, paths, result);
101
				}
102
			} else if (ClasspathUtilCore.hasBundleStructure(model)) {
103
				addJARdPlugin(project, ClasspathUtilCore.getFilename(model), attrs, result);
104
			}
105
		}
106
	}
107
108
	private static IClasspathAttribute[] getClasspathAttributes(IProject project, IPluginModelBase model) {
109
		IClasspathAttribute[] attributes = new IClasspathAttribute[0];
110
		if (!RepositoryProvider.isShared(project)) {
111
			JavadocLocationManager manager = PDECore.getDefault().getJavadocLocationManager();
112
			String javadoc = manager.getJavadocLocation(model);
113
			if (javadoc != null) {
114
				attributes = new IClasspathAttribute[] {JavaCore.newClasspathAttribute(IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME, javadoc)};
115
			}
116
		}
117
		return attributes;
118
	}
119
120
	private static void addSourceFolder(IBuildEntry buildEntry, IProject project, HashSet paths, ArrayList result) throws CoreException {
121
		String[] folders = buildEntry.getTokens();
122
		for (int j = 0; j < folders.length; j++) {
123
			String folder = folders[j];
124
			IPath path = project.getFullPath().append(folder);
125
			if (paths.add(path)) {
126
				if (project.findMember(folder) == null) {
127
					CoreUtility.createFolder(project.getFolder(folder));
128
				} else {
129
					IPackageFragmentRoot root = JavaCore.create(project).getPackageFragmentRoot(path.toString());
130
					if (root.exists() && root.getKind() == IPackageFragmentRoot.K_BINARY) {
131
						result.add(root.getRawClasspathEntry());
132
						continue;
133
					}
134
				}
135
				result.add(JavaCore.newSourceEntry(path));
136
			}
137
		}
138
	}
139
140
	protected static IBuild getBuild(IProject project) throws CoreException {
141
		IFile buildFile = project.getFile("build.properties"); //$NON-NLS-1$
142
		IBuildModel buildModel = null;
143
		if (buildFile.exists()) {
144
			buildModel = new WorkspaceBuildModel(buildFile);
145
			buildModel.load();
146
		}
147
		return (buildModel != null) ? buildModel.getBuild() : null;
148
	}
149
150
	private static void addLibraryEntry(IProject project, IPluginLibrary library, IClasspathAttribute[] attrs, ArrayList result) throws JavaModelException {
151
		String name = ClasspathUtilCore.expandLibraryName(library.getName());
152
		IResource jarFile = project.findMember(name);
153
		if (jarFile == null)
154
			return;
155
156
		IPackageFragmentRoot root = JavaCore.create(project).getPackageFragmentRoot(jarFile);
157
		if (root.exists() && root.getKind() == IPackageFragmentRoot.K_BINARY) {
158
			IClasspathEntry oldEntry = root.getRawClasspathEntry();
159
			if (oldEntry.getSourceAttachmentPath() != null && !result.contains(oldEntry)) {
160
				result.add(oldEntry);
161
				return;
162
			}
163
		}
164
165
		IClasspathEntry entry = createClasspathEntry(project, jarFile, name, attrs, library.isExported());
166
		if (!result.contains(entry))
167
			result.add(entry);
168
	}
169
170
	private static void addJARdPlugin(IProject project, String filename, IClasspathAttribute[] attrs, ArrayList result) {
171
		String name = ClasspathUtilCore.expandLibraryName(filename);
172
		IResource jarFile = project.findMember(name);
173
		if (jarFile != null) {
174
			IClasspathEntry entry = createClasspathEntry(project, jarFile, filename, attrs, true);
175
			if (!result.contains(entry))
176
				result.add(entry);
177
		}
178
	}
179
180
	private static IClasspathEntry createClasspathEntry(IProject project, IResource library, String fileName, IClasspathAttribute[] attrs, boolean isExported) {
181
		String sourceZipName = ClasspathUtilCore.getSourceZipName(fileName);
182
		IResource resource = project.findMember(sourceZipName);
183
		// if zip file does not exist, see if a directory with the source does.  This in necessary how we import source for individual source bundles.
184
		if (resource == null && sourceZipName.endsWith(".zip")) { //$NON-NLS-1$
185
			resource = project.findMember(sourceZipName.substring(0, sourceZipName.length() - 4));
186
			if (resource == null)
187
				// if we can't find the the source for a library, then try to find the common source location set up to share source from one jar to all libraries.
188
				// see PluginImportOperation.linkSourceArchives
189
				resource = project.getFile(project.getName() + "src.zip"); //$NON-NLS-1$
190
		}
191
		IPath srcAttachment = resource != null ? resource.getFullPath() : library.getFullPath();
192
		return JavaCore.newLibraryEntry(library.getFullPath(), srcAttachment, null, new IAccessRule[0], attrs, isExported);
193
	}
194
195
	private static String getExecutionEnvironment(BundleDescription bundleDescription) {
196
		if (bundleDescription != null) {
197
			String[] envs = bundleDescription.getExecutionEnvironments();
198
			if (envs.length > 0)
199
				return envs[0];
200
		}
201
		return null;
202
	}
203
204
	public static void setComplianceOptions(IJavaProject project, String compliance) {
205
		Map map = project.getOptions(false);
206
		if (compliance == null) {
207
			if (map.size() > 0) {
208
				map.remove(JavaCore.COMPILER_COMPLIANCE);
209
				map.remove(JavaCore.COMPILER_SOURCE);
210
				map.remove(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM);
211
				map.remove(JavaCore.COMPILER_PB_ASSERT_IDENTIFIER);
212
				map.remove(JavaCore.COMPILER_PB_ENUM_IDENTIFIER);
213
			} else {
214
				return;
215
			}
216
		} else if (JavaCore.VERSION_1_6.equals(compliance)) {
217
			map.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_6);
218
			map.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_6);
219
			map.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_6);
220
			map.put(JavaCore.COMPILER_PB_ASSERT_IDENTIFIER, JavaCore.ERROR);
221
			map.put(JavaCore.COMPILER_PB_ENUM_IDENTIFIER, JavaCore.ERROR);
222
		} else if (JavaCore.VERSION_1_5.equals(compliance)) {
223
			map.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_5);
224
			map.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_5);
225
			map.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_5);
226
			map.put(JavaCore.COMPILER_PB_ASSERT_IDENTIFIER, JavaCore.ERROR);
227
			map.put(JavaCore.COMPILER_PB_ENUM_IDENTIFIER, JavaCore.ERROR);
228
		} else if (JavaCore.VERSION_1_4.equals(compliance)) {
229
			map.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_4);
230
			map.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_3);
231
			map.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_2);
232
			updateSeverityComplianceOption(map, JavaCore.COMPILER_PB_ASSERT_IDENTIFIER, JavaCore.WARNING);
233
			updateSeverityComplianceOption(map, JavaCore.COMPILER_PB_ENUM_IDENTIFIER, JavaCore.WARNING);
234
		} else if (JavaCore.VERSION_1_3.equals(compliance)) {
235
			map.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_3);
236
			map.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_3);
237
			map.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_1);
238
			updateSeverityComplianceOption(map, JavaCore.COMPILER_PB_ASSERT_IDENTIFIER, JavaCore.IGNORE);
239
			updateSeverityComplianceOption(map, JavaCore.COMPILER_PB_ENUM_IDENTIFIER, JavaCore.IGNORE);
240
		}
241
		project.setOptions(map);
242
	}
243
244
	private static void updateSeverityComplianceOption(Map map, String key, String value) {
245
		Integer current_value = null;
246
		Integer new_value = null;
247
		String current_string_value = null;
248
		int current_int_value = 0;
249
		int new_int_value = 0;
250
		// Initialize the severity table (only once)
251
		if (fSeverityTable == null) {
252
			fSeverityTable = new Hashtable(SEVERITY_ERROR);
253
			fSeverityTable.put(JavaCore.IGNORE, new Integer(SEVERITY_IGNORE));
254
			fSeverityTable.put(JavaCore.WARNING, new Integer(SEVERITY_WARNING));
255
			fSeverityTable.put(JavaCore.ERROR, new Integer(SEVERITY_ERROR));
256
		}
257
		// Get the current severity
258
		current_string_value = (String) map.get(key);
259
		if (current_string_value != null) {
260
			current_value = (Integer) fSeverityTable.get(current_string_value);
261
			if (current_value != null) {
262
				current_int_value = current_value.intValue();
263
			}
264
		}
265
		// Get the new severity
266
		new_value = (Integer) fSeverityTable.get(value);
267
		if (new_value != null) {
268
			new_int_value = new_value.intValue();
269
		}
270
		// If the current severity is not higher than the new severity, replace it
271
		if (new_int_value > current_int_value) {
272
			map.put(key, value);
273
		}
274
	}
275
276
	/**
277
	 * Returns a new classpath container entry for the given execution environment.  If the given java project
278
	 * has an existing JRE/EE classpath entry, the access rules, extra attributes and isExported settings of
279
	 * the existing entry will be added to the new execution entry.
280
	 *  
281
	 * @param javaProject project to check for existing classpath entries
282
	 * @param ee id of the execution environment to create an entry for
283
	 * @param path id of the container to create an entry for
284
	 * 
285
	 * @return new classpath container entry
286
	 * @throws CoreException if there is a problem accessing the classpath entries of the project
287
	 */
288
	public static IClasspathEntry createEntryUsingPreviousEntry(IJavaProject javaProject, String ee, IPath path) throws CoreException {
289
		IClasspathEntry[] entries = javaProject.getRawClasspath();
290
		for (int i = 0; i < entries.length; i++) {
291
			if (entries[i].getPath().equals(path)) {
292
				if (path.equals(PDECore.JRE_CONTAINER_PATH))
293
					return JavaCore.newContainerEntry(getEEPath(ee), entries[i].getAccessRules(), entries[i].getExtraAttributes(), entries[i].isExported());
294
295
				return JavaCore.newContainerEntry(path, entries[i].getAccessRules(), entries[i].getExtraAttributes(), entries[i].isExported());
296
			}
297
		}
298
299
		if (path.equals(PDECore.JRE_CONTAINER_PATH))
300
			return createJREEntry(ee);
301
302
		return JavaCore.newContainerEntry(path);
303
	}
304
305
	/**
306
	 * Returns a classpath container entry for the given execution environment.
307
	 * @param ee id of the execution environment
308
	 * @return classpath container entry
309
	 */
310
	public static IClasspathEntry createJREEntry(String ee) {
311
		return JavaCore.newContainerEntry(getEEPath(ee));
312
	}
313
314
	/**
315
	 * Returns the JRE container path for the execution environment with the given id.
316
	 * @param ee execution environment id
317
	 * @return JRE container path for the execution environment
318
	 */
319
	private static IPath getEEPath(String ee) {
320
		IPath path = null;
321
		if (ee != null) {
322
			IExecutionEnvironmentsManager manager = JavaRuntime.getExecutionEnvironmentsManager();
323
			IExecutionEnvironment env = manager.getEnvironment(ee);
324
			if (env != null)
325
				path = JavaRuntime.newJREContainerPath(env);
326
		}
327
		if (path == null) {
328
			path = JavaRuntime.newDefaultJREContainerPath();
329
		}
330
		return path;
331
	}
332
333
	/**
334
	 * @return a new classpath container entry for a required plugin container
335
	 */
336
	public static IClasspathEntry createContainerEntry() {
337
		return JavaCore.newContainerEntry(PDECore.REQUIRED_PLUGINS_CONTAINER_PATH);
338
	}
339
340
}
(-)src/org/eclipse/pde/internal/ui/wizards/tools/UpdateClasspathJob.java (-1 / +1 lines)
Lines 15-23 Link Here
15
import org.eclipse.core.runtime.jobs.Job;
15
import org.eclipse.core.runtime.jobs.Job;
16
import org.eclipse.jdt.core.JavaCore;
16
import org.eclipse.jdt.core.JavaCore;
17
import org.eclipse.pde.core.plugin.IPluginModelBase;
17
import org.eclipse.pde.core.plugin.IPluginModelBase;
18
import org.eclipse.pde.internal.core.ClasspathComputer;
18
import org.eclipse.pde.internal.core.builders.PDEMarkerFactory;
19
import org.eclipse.pde.internal.core.builders.PDEMarkerFactory;
19
import org.eclipse.pde.internal.ui.*;
20
import org.eclipse.pde.internal.ui.*;
20
import org.eclipse.pde.internal.ui.wizards.plugin.ClasspathComputer;
21
21
22
public class UpdateClasspathJob extends Job {
22
public class UpdateClasspathJob extends Job {
23
	IPluginModelBase[] fModels;
23
	IPluginModelBase[] fModels;
(-)src/org/eclipse/pde/internal/ui/wizards/tools/ConvertProjectToPluginOperation.java (-3 / +1 lines)
Lines 21-28 Link Here
21
import org.eclipse.pde.core.build.IBuild;
21
import org.eclipse.pde.core.build.IBuild;
22
import org.eclipse.pde.core.build.IBuildEntry;
22
import org.eclipse.pde.core.build.IBuildEntry;
23
import org.eclipse.pde.core.plugin.*;
23
import org.eclipse.pde.core.plugin.*;
24
import org.eclipse.pde.internal.core.ICoreConstants;
24
import org.eclipse.pde.internal.core.*;
25
import org.eclipse.pde.internal.core.TargetPlatformHelper;
26
import org.eclipse.pde.internal.core.build.WorkspaceBuildModel;
25
import org.eclipse.pde.internal.core.build.WorkspaceBuildModel;
27
import org.eclipse.pde.internal.core.bundle.WorkspaceBundlePluginModel;
26
import org.eclipse.pde.internal.core.bundle.WorkspaceBundlePluginModel;
28
import org.eclipse.pde.internal.core.ibundle.IBundle;
27
import org.eclipse.pde.internal.core.ibundle.IBundle;
Lines 34-40 Link Here
34
import org.eclipse.pde.internal.ui.PDEUIMessages;
33
import org.eclipse.pde.internal.ui.PDEUIMessages;
35
import org.eclipse.pde.internal.ui.util.ModelModification;
34
import org.eclipse.pde.internal.ui.util.ModelModification;
36
import org.eclipse.pde.internal.ui.util.PDEModelUtility;
35
import org.eclipse.pde.internal.ui.util.PDEModelUtility;
37
import org.eclipse.pde.internal.ui.wizards.plugin.ClasspathComputer;
38
import org.eclipse.ui.actions.WorkspaceModifyOperation;
36
import org.eclipse.ui.actions.WorkspaceModifyOperation;
39
import org.osgi.framework.Constants;
37
import org.osgi.framework.Constants;
40
38
(-)src/org/eclipse/pde/internal/ui/wizards/imports/PluginImportOperation.java (-1 lines)
Lines 38-44 Link Here
38
import org.eclipse.pde.internal.core.util.CoreUtility;
38
import org.eclipse.pde.internal.core.util.CoreUtility;
39
import org.eclipse.pde.internal.ui.PDEPlugin;
39
import org.eclipse.pde.internal.ui.PDEPlugin;
40
import org.eclipse.pde.internal.ui.PDEUIMessages;
40
import org.eclipse.pde.internal.ui.PDEUIMessages;
41
import org.eclipse.pde.internal.ui.wizards.plugin.ClasspathComputer;
42
import org.eclipse.swt.widgets.Display;
41
import org.eclipse.swt.widgets.Display;
43
import org.eclipse.team.core.RepositoryProvider;
42
import org.eclipse.team.core.RepositoryProvider;
44
import org.eclipse.team.core.TeamException;
43
import org.eclipse.team.core.TeamException;
(-)src/org/eclipse/pde/internal/ui/correction/UpdateClasspathResolution.java (-1 / +1 lines)
Lines 16-23 Link Here
16
import org.eclipse.pde.core.IBaseModel;
16
import org.eclipse.pde.core.IBaseModel;
17
import org.eclipse.pde.core.plugin.IPluginModelBase;
17
import org.eclipse.pde.core.plugin.IPluginModelBase;
18
import org.eclipse.pde.core.plugin.PluginRegistry;
18
import org.eclipse.pde.core.plugin.PluginRegistry;
19
import org.eclipse.pde.internal.core.ClasspathComputer;
19
import org.eclipse.pde.internal.ui.PDEUIMessages;
20
import org.eclipse.pde.internal.ui.PDEUIMessages;
20
import org.eclipse.pde.internal.ui.wizards.plugin.ClasspathComputer;
21
21
22
public class UpdateClasspathResolution extends AbstractPDEMarkerResolution {
22
public class UpdateClasspathResolution extends AbstractPDEMarkerResolution {
23
23
(-)src/org/eclipse/pde/internal/ui/wizards/RequiredPluginsContainerPage.java (-1 / +1 lines)
Lines 20-29 Link Here
20
import org.eclipse.jface.wizard.WizardPage;
20
import org.eclipse.jface.wizard.WizardPage;
21
import org.eclipse.pde.core.plugin.IPluginModelBase;
21
import org.eclipse.pde.core.plugin.IPluginModelBase;
22
import org.eclipse.pde.core.plugin.PluginRegistry;
22
import org.eclipse.pde.core.plugin.PluginRegistry;
23
import org.eclipse.pde.internal.core.ClasspathComputer;
23
import org.eclipse.pde.internal.core.RequiredPluginsClasspathContainer;
24
import org.eclipse.pde.internal.core.RequiredPluginsClasspathContainer;
24
import org.eclipse.pde.internal.ui.*;
25
import org.eclipse.pde.internal.ui.*;
25
import org.eclipse.pde.internal.ui.elements.DefaultContentProvider;
26
import org.eclipse.pde.internal.ui.elements.DefaultContentProvider;
26
import org.eclipse.pde.internal.ui.wizards.plugin.ClasspathComputer;
27
import org.eclipse.swt.SWT;
27
import org.eclipse.swt.SWT;
28
import org.eclipse.swt.graphics.Image;
28
import org.eclipse.swt.graphics.Image;
29
import org.eclipse.swt.layout.GridData;
29
import org.eclipse.swt.layout.GridData;

Return to bug 217921