Skip to main content
summaryrefslogtreecommitdiffstats
blob: ec0ccf277b47d0a9c847b81a92ab15a4e5d22041 (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
/*******************************************************************************
 * Copyright (c) 2000, 2010 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.jdt.internal.core;

import org.eclipse.core.resources.IResourceRuleFactory;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.jobs.ISchedulingRule;
import org.eclipse.core.runtime.jobs.MultiRule;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaModelStatus;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.internal.core.JavaModelManager.PerProjectInfo;

/**
 * This operation sets an <code>IJavaProject</code>'s classpath.
 *
 * @see IJavaProject
 */
public class SetClasspathOperation extends ChangeClasspathOperation {

	IClasspathEntry[] newRawClasspath;
	IClasspathEntry[] referencedEntries;
	IPath newOutputLocation;
	JavaProject project;

	public SetClasspathOperation(
		JavaProject project,
		IClasspathEntry[] newRawClasspath,
		IPath newOutputLocation,
		boolean canChangeResource) {

		this(project, newRawClasspath, null, newOutputLocation, canChangeResource);
	}
	
	/**
	 * When executed, this operation sets the raw classpath and output location of the given project.
	 */
	public SetClasspathOperation(
		JavaProject project,
		IClasspathEntry[] newRawClasspath,
		IClasspathEntry[] referencedEntries,
		IPath newOutputLocation,
		boolean canChangeResource) {

		super(new IJavaElement[] { project }, canChangeResource);
		this.project = project;
		this.newRawClasspath = newRawClasspath;
		this.referencedEntries = referencedEntries;
		this.newOutputLocation = newOutputLocation;
	}

	/**
	 * Sets the classpath of the pre-specified project.
	 */
	protected void executeOperation() throws JavaModelException {
		checkCanceled();
		try {
			// set raw classpath and null out resolved info
			PerProjectInfo perProjectInfo = this.project.getPerProjectInfo();
			ClasspathChange classpathChange = perProjectInfo.setRawClasspath(this.newRawClasspath, this.referencedEntries, this.newOutputLocation, JavaModelStatus.VERIFIED_OK/*format is ok*/);

			// if needed, generate delta, update project ref, create markers, ...
			classpathChanged(classpathChange, true/*refresh if external linked folder already exists*/);

			// write .classpath file
			if (this.canChangeResources && perProjectInfo.writeAndCacheClasspath(this.project, this.newRawClasspath, this.newOutputLocation))
				setAttribute(HAS_MODIFIED_RESOURCE_ATTR, TRUE);
		} finally {
			done();
		}
	}
	
	protected ISchedulingRule getSchedulingRule() {
		if (this.canChangeResources) {
			IResourceRuleFactory ruleFactory = ResourcesPlugin.getWorkspace().getRuleFactory();
			return new MultiRule(new ISchedulingRule[] {
				// use project modification rule as this is needed to create the .classpath file if it doesn't exist yet, or to update project references
				ruleFactory.modifyRule(this.project.getProject()),
				
				// and external project modification rule in case the external folders are modified
				ruleFactory.modifyRule(JavaModelManager.getExternalManager().getExternalFoldersProject())
			});
		}
		return super.getSchedulingRule();
	}

	public String toString(){
		StringBuffer buffer = new StringBuffer(20);
		buffer.append("SetClasspathOperation\n"); //$NON-NLS-1$
		buffer.append(" - classpath : "); //$NON-NLS-1$
		buffer.append("{"); //$NON-NLS-1$
		for (int i = 0; i < this.newRawClasspath.length; i++) {
			if (i > 0) buffer.append(","); //$NON-NLS-1$
			IClasspathEntry element = this.newRawClasspath[i];
			buffer.append(" ").append(element.toString()); //$NON-NLS-1$
		}
		buffer.append("\n - output location : ");  //$NON-NLS-1$
		buffer.append(this.newOutputLocation.toString());
		return buffer.toString();
	}

	public IJavaModelStatus verify() {
		IJavaModelStatus status = super.verify();
		if (!status.isOK())
			return status;
		this.project.flushClasspathProblemMarkers(false, false, true);
		return ClasspathEntry.validateClasspath(	this.project, this.newRawClasspath, this.newOutputLocation);
	}

}

Back to the top