]> git.uio.no Git - ifi-stolz-refaktor.git/blame - case-study/jdt-before/core refactoring/org/eclipse/jdt/internal/corext/refactoring/changes/ClasspathChange.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-before / core refactoring / org / eclipse / jdt / internal / corext / refactoring / changes / ClasspathChange.java
CommitLineData
1b2798f6
EK
1/*******************************************************************************
2 * Copyright (c) 2007, 2011 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 *******************************************************************************/
11package org.eclipse.jdt.internal.corext.refactoring.changes;
12
13import java.util.ArrayList;
14
15import org.eclipse.core.runtime.CoreException;
16import org.eclipse.core.runtime.IPath;
17import org.eclipse.core.runtime.IProgressMonitor;
18import org.eclipse.core.runtime.IStatus;
19import org.eclipse.core.runtime.SubProgressMonitor;
20
21import org.eclipse.core.resources.IResource;
22
23import org.eclipse.ltk.core.refactoring.Change;
24import org.eclipse.ltk.core.refactoring.NullChange;
25import org.eclipse.ltk.core.refactoring.resource.ResourceChange;
26
27import org.eclipse.jdt.core.IClasspathEntry;
28import org.eclipse.jdt.core.IJavaProject;
29import org.eclipse.jdt.core.JavaConventions;
30import org.eclipse.jdt.core.JavaModelException;
31
32import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages;
33
34public class ClasspathChange extends ResourceChange {
35
36 public static ClasspathChange addEntryChange(IJavaProject project, IClasspathEntry entryToAdd) throws JavaModelException {
37 IClasspathEntry[] rawClasspath= project.getRawClasspath();
38 IClasspathEntry[] newClasspath= new IClasspathEntry[rawClasspath.length + 1];
39 System.arraycopy(rawClasspath, 0, newClasspath, 0, rawClasspath.length);
40 newClasspath[rawClasspath.length]= entryToAdd;
41
42 IPath outputLocation= project.getOutputLocation();
43
44 return newChange(project, newClasspath, outputLocation);
45 }
46
47 public static ClasspathChange removeEntryChange(IJavaProject project, IClasspathEntry entryToRemove) throws JavaModelException {
48 IClasspathEntry[] rawClasspath= project.getRawClasspath();
49 ArrayList<IClasspathEntry> newClasspath= new ArrayList<IClasspathEntry>();
50 for (int i= 0; i < rawClasspath.length; i++) {
51 IClasspathEntry curr= rawClasspath[i];
52 if (curr.getEntryKind() != entryToRemove.getEntryKind() || !curr.getPath().equals(entryToRemove.getPath())) {
53 newClasspath.add(curr);
54 }
55 }
56 IClasspathEntry[] entries= newClasspath.toArray(new IClasspathEntry[newClasspath.size()]);
57 IPath outputLocation= project.getOutputLocation();
58
59 return newChange(project, entries, outputLocation);
60 }
61
62 public static ClasspathChange newChange(IJavaProject project, IClasspathEntry[] newClasspath, IPath outputLocation) {
63 if (!JavaConventions.validateClasspath(project, newClasspath, outputLocation).matches(IStatus.ERROR)) {
64 return new ClasspathChange(project, newClasspath, outputLocation);
65 }
66 return null;
67 }
68
69 private IJavaProject fProject;
70 private IClasspathEntry[] fNewClasspath;
71 private final IPath fOutputLocation;
72
73 public ClasspathChange(IJavaProject project, IClasspathEntry[] newClasspath, IPath outputLocation) {
74 fProject= project;
75 fNewClasspath= newClasspath;
76 fOutputLocation= outputLocation;
77
78 setValidationMethod(VALIDATE_NOT_DIRTY | VALIDATE_NOT_READ_ONLY);
79 }
80
81 @Override
82 public Change perform(IProgressMonitor pm) throws CoreException {
83 pm.beginTask(RefactoringCoreMessages.ClasspathChange_progress_message, 1);
84 try {
85 if (!JavaConventions.validateClasspath(fProject, fNewClasspath, fOutputLocation).matches(IStatus.ERROR)) {
86 IClasspathEntry[] oldClasspath= fProject.getRawClasspath();
87 IPath oldOutputLocation= fProject.getOutputLocation();
88
89 fProject.setRawClasspath(fNewClasspath, fOutputLocation, new SubProgressMonitor(pm, 1));
90
91 return new ClasspathChange(fProject, oldClasspath, oldOutputLocation);
92 } else {
93 return new NullChange();
94 }
95 } finally {
96 pm.done();
97 }
98 }
99
100 @Override
101 public String getName() {
102 return RefactoringCoreMessages.ClasspathChange_change_name;
103 }
104
105 @Override
106 protected IResource getModifiedResource() {
107 return fProject.getResource();
108 }
109
110 @Override
111 public Object getModifiedElement() {
112 return fProject;
113 }
114}