View | Details | Raw Unified | Return to bug 220565 | Differences between
and this patch

Collapse All | Expand All

(-)model/org/eclipse/jdt/internal/core/CreatePackageDeclarationOperation.java (-2 / +1 lines)
Lines 29-35 Link Here
29
import org.eclipse.jdt.core.dom.StructuralPropertyDescriptor;
29
import org.eclipse.jdt.core.dom.StructuralPropertyDescriptor;
30
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
30
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
31
import org.eclipse.jdt.internal.core.util.Messages;
31
import org.eclipse.jdt.internal.core.util.Messages;
32
import org.eclipse.jface.text.IDocument;
33
32
34
/**
33
/**
35
 * <p>This operation adds/replaces a package declaration in an existing compilation unit.
34
 * <p>This operation adds/replaces a package declaration in an existing compilation unit.
Lines 56-62 Link Here
56
protected StructuralPropertyDescriptor getChildPropertyDescriptor(ASTNode parent) {
55
protected StructuralPropertyDescriptor getChildPropertyDescriptor(ASTNode parent) {
57
	return CompilationUnit.PACKAGE_PROPERTY;
56
	return CompilationUnit.PACKAGE_PROPERTY;
58
}
57
}
59
protected ASTNode generateElementAST(ASTRewrite rewriter, IDocument document, ICompilationUnit cu) throws JavaModelException {
58
protected ASTNode generateElementAST(ASTRewrite rewriter, ICompilationUnit cu) throws JavaModelException {
60
	//look for an existing package declaration
59
	//look for an existing package declaration
61
	IJavaElement[] children = getCompilationUnit().getChildren();
60
	IJavaElement[] children = getCompilationUnit().getChildren();
62
	for (int i = 0; i < children.length; i++) {
61
	for (int i = 0; i < children.length; i++) {
(-)model/org/eclipse/jdt/internal/core/JavaModelOperation.java (-10 lines)
Lines 19-25 Link Here
19
import org.eclipse.core.runtime.jobs.ISchedulingRule;
19
import org.eclipse.core.runtime.jobs.ISchedulingRule;
20
import org.eclipse.jdt.core.*;
20
import org.eclipse.jdt.core.*;
21
import org.eclipse.jdt.internal.core.util.Messages;
21
import org.eclipse.jdt.internal.core.util.Messages;
22
import org.eclipse.jface.text.IDocument;
23
22
24
/**
23
/**
25
 * Defines behavior common to all Java Model operations
24
 * Defines behavior common to all Java Model operations
Lines 421-435 Link Here
421
		}
420
		}
422
		return stack;
421
		return stack;
423
	}
422
	}
424
	/*
425
	 * Returns the existing document for the given cu, or a DocumentAdapter if none.
426
	 */
427
	protected IDocument getDocument(ICompilationUnit cu) throws JavaModelException {
428
		IBuffer buffer = cu.getBuffer();
429
		if (buffer instanceof IDocument)
430
			return (IDocument) buffer;
431
		return new DocumentAdapter(buffer);
432
	}
433
	/**
423
	/**
434
	 * Returns the element to which this operation applies,
424
	 * Returns the element to which this operation applies,
435
	 * or <code>null</code> if not applicable.
425
	 * or <code>null</code> if not applicable.
(-)model/org/eclipse/jdt/internal/core/CreateFieldOperation.java (-3 / +2 lines)
Lines 25-31 Link Here
25
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
25
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
26
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
26
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
27
import org.eclipse.jdt.internal.core.util.Messages;
27
import org.eclipse.jdt.internal.core.util.Messages;
28
import org.eclipse.jface.text.IDocument;
29
28
30
/**
29
/**
31
 * <p>This operation creates a field declaration in a type.
30
 * <p>This operation creates a field declaration in a type.
Lines 48-55 Link Here
48
public CreateFieldOperation(IType parentElement, String source, boolean force) {
47
public CreateFieldOperation(IType parentElement, String source, boolean force) {
49
	super(parentElement, source, force);
48
	super(parentElement, source, force);
50
}
49
}
51
protected ASTNode generateElementAST(ASTRewrite rewriter, IDocument document, ICompilationUnit cu) throws JavaModelException {
50
protected ASTNode generateElementAST(ASTRewrite rewriter, ICompilationUnit cu) throws JavaModelException {
52
	ASTNode node = super.generateElementAST(rewriter, document, cu);
51
	ASTNode node = super.generateElementAST(rewriter, cu);
53
	if (node.getNodeType() != ASTNode.FIELD_DECLARATION)
52
	if (node.getNodeType() != ASTNode.FIELD_DECLARATION)
54
		throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.INVALID_CONTENTS));
53
		throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.INVALID_CONTENTS));
55
	return node;
54
	return node;
(-)model/org/eclipse/jdt/internal/core/CreateTypeOperation.java (-3 / +2 lines)
Lines 21-27 Link Here
21
import org.eclipse.jdt.core.dom.SimpleName;
21
import org.eclipse.jdt.core.dom.SimpleName;
22
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
22
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
23
import org.eclipse.jdt.internal.core.util.Messages;
23
import org.eclipse.jdt.internal.core.util.Messages;
24
import org.eclipse.jface.text.IDocument;
25
24
26
/**
25
/**
27
 * <p>This operation creates a class or interface.
26
 * <p>This operation creates a class or interface.
Lines 40-47 Link Here
40
public CreateTypeOperation(IJavaElement parentElement, String source, boolean force) {
39
public CreateTypeOperation(IJavaElement parentElement, String source, boolean force) {
41
	super(parentElement, source, force);
40
	super(parentElement, source, force);
42
}
41
}
43
protected ASTNode generateElementAST(ASTRewrite rewriter, IDocument document, ICompilationUnit cu) throws JavaModelException {
42
protected ASTNode generateElementAST(ASTRewrite rewriter, ICompilationUnit cu) throws JavaModelException {
44
	ASTNode node = super.generateElementAST(rewriter, document, cu);
43
	ASTNode node = super.generateElementAST(rewriter, cu);
45
	if (!(node instanceof AbstractTypeDeclaration))
44
	if (!(node instanceof AbstractTypeDeclaration))
46
		throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.INVALID_CONTENTS));
45
		throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.INVALID_CONTENTS));
47
	return node;
46
	return node;
(-)model/org/eclipse/jdt/internal/core/CompilationUnit.java (-3 / +3 lines)
Lines 32-37 Link Here
32
import org.eclipse.jdt.internal.core.util.Util;
32
import org.eclipse.jdt.internal.core.util.Util;
33
33
34
import org.eclipse.jface.text.BadLocationException;
34
import org.eclipse.jface.text.BadLocationException;
35
import org.eclipse.jface.text.IDocument;
35
36
36
import org.eclipse.text.edits.MalformedTreeException;
37
import org.eclipse.text.edits.MalformedTreeException;
37
import org.eclipse.text.edits.TextEdit;
38
import org.eclipse.text.edits.TextEdit;
Lines 68-81 Link Here
68
 * @see ICompilationUnit#applyTextEdit(TextEdit, IProgressMonitor)
69
 * @see ICompilationUnit#applyTextEdit(TextEdit, IProgressMonitor)
69
 */
70
 */
70
public UndoEdit applyTextEdit(TextEdit edit, IProgressMonitor monitor) throws JavaModelException {
71
public UndoEdit applyTextEdit(TextEdit edit, IProgressMonitor monitor) throws JavaModelException {
71
	IBuffer buffer= getBuffer();
72
	IBuffer buffer = getBuffer();
72
	if (buffer instanceof IBuffer.ITextEditCapability) {
73
	if (buffer instanceof IBuffer.ITextEditCapability) {
73
		return ((IBuffer.ITextEditCapability) buffer).applyTextEdit(edit, monitor);
74
		return ((IBuffer.ITextEditCapability) buffer).applyTextEdit(edit, monitor);
74
	} else if (buffer != null) {
75
	} else if (buffer != null) {
75
		DocumentAdapter document= new DocumentAdapter(buffer);
76
		IDocument document = buffer instanceof IDocument ? (IDocument) buffer : new DocumentAdapter(buffer);
76
		try {
77
		try {
77
			UndoEdit undoEdit= edit.apply(document);
78
			UndoEdit undoEdit= edit.apply(document);
78
			buffer.setContents(document.get());
79
			return undoEdit;
79
			return undoEdit;
80
		} catch (MalformedTreeException e) {
80
		} catch (MalformedTreeException e) {
81
			throw new JavaModelException(e, IJavaModelStatusConstants.BAD_TEXT_EDIT_LOCATION);
81
			throw new JavaModelException(e, IJavaModelStatusConstants.BAD_TEXT_EDIT_LOCATION);
(-)model/org/eclipse/jdt/internal/core/CreateTypeMemberOperation.java (-4 / +7 lines)
Lines 13-18 Link Here
13
import java.util.List;
13
import java.util.List;
14
import java.util.Map;
14
import java.util.Map;
15
15
16
import org.eclipse.jdt.core.IBuffer;
16
import org.eclipse.jdt.core.ICompilationUnit;
17
import org.eclipse.jdt.core.ICompilationUnit;
17
import org.eclipse.jdt.core.IJavaElement;
18
import org.eclipse.jdt.core.IJavaElement;
18
import org.eclipse.jdt.core.IJavaModelStatus;
19
import org.eclipse.jdt.core.IJavaModelStatus;
Lines 76-84 Link Here
76
			return TypeDeclaration.BODY_DECLARATIONS_PROPERTY;
77
			return TypeDeclaration.BODY_DECLARATIONS_PROPERTY;
77
	}
78
	}
78
}
79
}
79
protected ASTNode generateElementAST(ASTRewrite rewriter, IDocument document, ICompilationUnit cu) throws JavaModelException {
80
protected ASTNode generateElementAST(ASTRewrite rewriter, ICompilationUnit cu) throws JavaModelException {
80
	if (this.createdNode == null) {
81
	if (this.createdNode == null) {
81
		this.source = removeIndentAndNewLines(this.source, document, cu);
82
		this.source = removeIndentAndNewLines(this.source, cu);
82
		ASTParser parser = ASTParser.newParser(AST.JLS3);
83
		ASTParser parser = ASTParser.newParser(AST.JLS3);
83
		parser.setSource(this.source.toCharArray());
84
		parser.setSource(this.source.toCharArray());
84
		parser.setProject(getCompilationUnit().getJavaProject());
85
		parser.setProject(getCompilationUnit().getJavaProject());
Lines 120-126 Link Here
120
	// return a string place holder (instead of the created node) so has to not lose comments and formatting
121
	// return a string place holder (instead of the created node) so has to not lose comments and formatting
121
	return rewriter.createStringPlaceholder(this.source, this.createdNode.getNodeType());
122
	return rewriter.createStringPlaceholder(this.source, this.createdNode.getNodeType());
122
}
123
}
123
private String removeIndentAndNewLines(String code, IDocument document, ICompilationUnit cu) {
124
private String removeIndentAndNewLines(String code, ICompilationUnit cu) throws JavaModelException {
124
	IJavaProject project = cu.getJavaProject();
125
	IJavaProject project = cu.getJavaProject();
125
	Map options = project.getOptions(true/*inherit JavaCore options*/);
126
	Map options = project.getOptions(true/*inherit JavaCore options*/);
126
	int tabWidth = IndentManipulation.getTabWidth(options);
127
	int tabWidth = IndentManipulation.getTabWidth(options);
Lines 135-140 Link Here
135
	while (lastNonWhiteSpace > 0)
136
	while (lastNonWhiteSpace > 0)
136
		if (!ScannerHelper.isWhitespace(code.charAt(--lastNonWhiteSpace)))
137
		if (!ScannerHelper.isWhitespace(code.charAt(--lastNonWhiteSpace)))
137
			break;
138
			break;
139
	IBuffer buffer = cu.getBuffer();
140
	IDocument document = buffer instanceof IDocument ? (IDocument) buffer : new DocumentAdapter(buffer);
138
	String lineDelimiter = TextUtilities.getDefaultLineDelimiter(document);
141
	String lineDelimiter = TextUtilities.getDefaultLineDelimiter(document);
139
	return IndentManipulation.changeIndent(code.substring(firstNonWhiteSpace, lastNonWhiteSpace+1), indent, tabWidth, indentWidth, "", lineDelimiter); //$NON-NLS-1$
142
	return IndentManipulation.changeIndent(code.substring(firstNonWhiteSpace, lastNonWhiteSpace+1), indent, tabWidth, indentWidth, "", lineDelimiter); //$NON-NLS-1$
140
}
143
}
Lines 199-205 Link Here
199
		//check for name collisions
202
		//check for name collisions
200
		try {
203
		try {
201
			ICompilationUnit cu = getCompilationUnit();
204
			ICompilationUnit cu = getCompilationUnit();
202
			generateElementAST(null, getDocument(cu), cu);
205
			generateElementAST(null, cu);
203
		} catch (JavaModelException jme) {
206
		} catch (JavaModelException jme) {
204
			return jme.getJavaModelStatus();
207
			return jme.getJavaModelStatus();
205
		}
208
		}
(-)model/org/eclipse/jdt/internal/core/CreateImportOperation.java (-2 / +1 lines)
Lines 33-39 Link Here
33
import org.eclipse.jdt.core.dom.StructuralPropertyDescriptor;
33
import org.eclipse.jdt.core.dom.StructuralPropertyDescriptor;
34
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
34
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
35
import org.eclipse.jdt.internal.core.util.Messages;
35
import org.eclipse.jdt.internal.core.util.Messages;
36
import org.eclipse.jface.text.IDocument;
37
36
38
/**
37
/**
39
 * <p>This operation adds an import declaration to an existing compilation unit.
38
 * <p>This operation adds an import declaration to an existing compilation unit.
Lines 76-82 Link Here
76
protected StructuralPropertyDescriptor getChildPropertyDescriptor(ASTNode parent) {
75
protected StructuralPropertyDescriptor getChildPropertyDescriptor(ASTNode parent) {
77
	return CompilationUnit.IMPORTS_PROPERTY;
76
	return CompilationUnit.IMPORTS_PROPERTY;
78
}
77
}
79
protected ASTNode generateElementAST(ASTRewrite rewriter, IDocument document, ICompilationUnit cu) throws JavaModelException {
78
protected ASTNode generateElementAST(ASTRewrite rewriter, ICompilationUnit cu) throws JavaModelException {
80
	// ensure no duplicate
79
	// ensure no duplicate
81
	Iterator imports = this.cuAST.imports().iterator();
80
	Iterator imports = this.cuAST.imports().iterator();
82
	boolean onDemand = this.importName.endsWith(".*"); //$NON-NLS-1$
81
	boolean onDemand = this.importName.endsWith(".*"); //$NON-NLS-1$
(-)model/org/eclipse/jdt/internal/core/CreateMethodOperation.java (-3 / +2 lines)
Lines 27-33 Link Here
27
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
27
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
28
import org.eclipse.jdt.internal.core.util.Messages;
28
import org.eclipse.jdt.internal.core.util.Messages;
29
import org.eclipse.jdt.internal.core.util.Util;
29
import org.eclipse.jdt.internal.core.util.Util;
30
import org.eclipse.jface.text.IDocument;
31
30
32
/**
31
/**
33
 * <p>This operation creates an instance method. 
32
 * <p>This operation creates an instance method. 
Lines 74-81 Link Here
74
	}
73
	}
75
	return this.parameterTypes;
74
	return this.parameterTypes;
76
}
75
}
77
protected ASTNode generateElementAST(ASTRewrite rewriter, IDocument document, ICompilationUnit cu) throws JavaModelException {
76
protected ASTNode generateElementAST(ASTRewrite rewriter, ICompilationUnit cu) throws JavaModelException {
78
	ASTNode node = super.generateElementAST(rewriter, document, cu);
77
	ASTNode node = super.generateElementAST(rewriter, cu);
79
	if (node.getNodeType() != ASTNode.METHOD_DECLARATION)
78
	if (node.getNodeType() != ASTNode.METHOD_DECLARATION)
80
		throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.INVALID_CONTENTS));
79
		throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.INVALID_CONTENTS));
81
	return node;
80
	return node;
(-)model/org/eclipse/jdt/internal/core/CreateInitializerOperation.java (-3 / +2 lines)
Lines 19-25 Link Here
19
import org.eclipse.jdt.core.dom.SimpleName;
19
import org.eclipse.jdt.core.dom.SimpleName;
20
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
20
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
21
import org.eclipse.jdt.internal.core.util.Messages;
21
import org.eclipse.jdt.internal.core.util.Messages;
22
import org.eclipse.jface.text.IDocument;
23
22
24
/**
23
/**
25
 * <p>This operation creates a initializer in a type.
24
 * <p>This operation creates a initializer in a type.
Lines 47-54 Link Here
47
public CreateInitializerOperation(IType parentElement, String source) {
46
public CreateInitializerOperation(IType parentElement, String source) {
48
	super(parentElement, source, false);
47
	super(parentElement, source, false);
49
}
48
}
50
protected ASTNode generateElementAST(ASTRewrite rewriter, IDocument document, ICompilationUnit cu) throws JavaModelException {
49
protected ASTNode generateElementAST(ASTRewrite rewriter, ICompilationUnit cu) throws JavaModelException {
51
	ASTNode node = super.generateElementAST(rewriter, document, cu);
50
	ASTNode node = super.generateElementAST(rewriter, cu);
52
	if (node.getNodeType() != ASTNode.INITIALIZER)
51
	if (node.getNodeType() != ASTNode.INITIALIZER)
53
		throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.INVALID_CONTENTS));
52
		throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.INVALID_CONTENTS));
54
	return node;
53
	return node;
(-)model/org/eclipse/jdt/internal/core/DeleteElementsOperation.java (-9 / +2 lines)
Lines 29-36 Link Here
29
import org.eclipse.jdt.core.dom.CompilationUnit;
29
import org.eclipse.jdt.core.dom.CompilationUnit;
30
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
30
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
31
import org.eclipse.jdt.internal.core.util.Messages;
31
import org.eclipse.jdt.internal.core.util.Messages;
32
import org.eclipse.jface.text.BadLocationException;
33
import org.eclipse.jface.text.IDocument;
34
import org.eclipse.text.edits.TextEdit;
32
import org.eclipse.text.edits.TextEdit;
35
33
36
/**
34
/**
Lines 74-89 Link Here
74
		ASTNode node = ((JavaElement) elementToRemove).findNode(astCU);
72
		ASTNode node = ((JavaElement) elementToRemove).findNode(astCU);
75
		if (node == null) 
73
		if (node == null) 
76
			Assert.isTrue(false, "Failed to locate " + elementToRemove.getElementName() + " in " + cu.getElementName()); //$NON-NLS-1$//$NON-NLS-2$
74
			Assert.isTrue(false, "Failed to locate " + elementToRemove.getElementName() + " in " + cu.getElementName()); //$NON-NLS-1$//$NON-NLS-2$
77
		IDocument document = getDocument(cu);
78
		AST ast = astCU.getAST();
75
		AST ast = astCU.getAST();
79
		ASTRewrite rewriter = ASTRewrite.create(ast);
76
		ASTRewrite rewriter = ASTRewrite.create(ast);
80
		rewriter.remove(node, null);
77
		rewriter.remove(node, null);
81
 		TextEdit edits = rewriter.rewriteAST(document, null);
78
 		TextEdit edits = rewriter.rewriteAST();
82
 		try {
79
 		cu.applyTextEdit(edits, this.progressMonitor);
83
	 		edits.apply(document);
84
 		} catch (BadLocationException e) {
85
 			throw new JavaModelException(e, IJavaModelStatusConstants.INVALID_CONTENTS);
86
 		}
87
	}
80
	}
88
81
89
	private void initASTParser() {
82
	private void initASTParser() {
(-)model/org/eclipse/jdt/internal/core/CreateElementInCUOperation.java (-16 / +4 lines)
Lines 10-17 Link Here
10
 *******************************************************************************/
10
 *******************************************************************************/
11
package org.eclipse.jdt.internal.core;
11
package org.eclipse.jdt.internal.core;
12
12
13
import java.util.Map;
14
15
import org.eclipse.core.resources.IResource;
13
import org.eclipse.core.resources.IResource;
16
import org.eclipse.core.resources.IWorkspace;
14
import org.eclipse.core.resources.IWorkspace;
17
import org.eclipse.core.runtime.jobs.ISchedulingRule;
15
import org.eclipse.core.runtime.jobs.ISchedulingRule;
Lines 29-36 Link Here
29
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
27
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
30
import org.eclipse.jdt.core.dom.rewrite.ListRewrite;
28
import org.eclipse.jdt.core.dom.rewrite.ListRewrite;
31
import org.eclipse.jdt.internal.core.util.Util;
29
import org.eclipse.jdt.internal.core.util.Util;
32
import org.eclipse.jface.text.BadLocationException;
33
import org.eclipse.jface.text.IDocument;
34
import org.eclipse.text.edits.TextEdit;
30
import org.eclipse.text.edits.TextEdit;
35
31
36
/**
32
/**
Lines 93-106 Link Here
93
		super(null, new IJavaElement[]{parentElement});
89
		super(null, new IJavaElement[]{parentElement});
94
		initializeDefaultPosition();
90
		initializeDefaultPosition();
95
	}
91
	}
96
	protected void apply(ASTRewrite rewriter, IDocument document, Map options) throws JavaModelException {
97
		TextEdit edits = rewriter.rewriteAST(document, options);
98
 		try {
99
	 		edits.apply(document);
100
 		} catch (BadLocationException e) {
101
 			throw new JavaModelException(e, IJavaModelStatusConstants.INVALID_CONTENTS);
102
 		}
103
	}
104
	/**
92
	/**
105
	 * Only allow cancelling if this operation is not nested.
93
	 * Only allow cancelling if this operation is not nested.
106
	 */
94
	 */
Lines 168-174 Link Here
168
	/*
156
	/*
169
	 * Returns an AST node for the element being created.
157
	 * Returns an AST node for the element being created.
170
	 */
158
	 */
171
	protected abstract ASTNode generateElementAST(ASTRewrite rewriter, IDocument document, ICompilationUnit cu) throws JavaModelException;
159
	protected abstract ASTNode generateElementAST(ASTRewrite rewriter, ICompilationUnit cu) throws JavaModelException;
172
	/*
160
	/*
173
	 * Generates a new AST for this operation and applies it to the given cu
161
	 * Generates a new AST for this operation and applies it to the given cu
174
	 */
162
	 */
Lines 177-190 Link Here
177
		
165
		
178
		AST ast = this.cuAST.getAST();
166
		AST ast = this.cuAST.getAST();
179
		ASTRewrite rewriter = ASTRewrite.create(ast);
167
		ASTRewrite rewriter = ASTRewrite.create(ast);
180
		IDocument document = getDocument(cu);
168
		ASTNode child = generateElementAST(rewriter, cu);
181
		ASTNode child = generateElementAST(rewriter, document, cu);
182
		if (child != null) {
169
		if (child != null) {
183
			ASTNode parent = ((JavaElement) getParentElement()).findNode(this.cuAST);
170
			ASTNode parent = ((JavaElement) getParentElement()).findNode(this.cuAST);
184
			if (parent == null)
171
			if (parent == null)
185
				parent = this.cuAST;
172
				parent = this.cuAST;
186
			insertASTNode(rewriter, parent, child);
173
			insertASTNode(rewriter, parent, child);
187
			apply(rewriter, document, cu.getJavaProject().getOptions(true));
174
			TextEdit edits = rewriter.rewriteAST();
175
			cu.applyTextEdit(edits, this.progressMonitor);
188
		}
176
		}
189
		worked(1);
177
		worked(1);
190
	}
178
	}
(-)model/org/eclipse/jdt/internal/core/CopyResourceElementsOperation.java (-26 / +13 lines)
Lines 29-36 Link Here
29
import org.eclipse.jdt.internal.compiler.util.SuffixConstants;
29
import org.eclipse.jdt.internal.compiler.util.SuffixConstants;
30
import org.eclipse.jdt.internal.core.util.Messages;
30
import org.eclipse.jdt.internal.core.util.Messages;
31
import org.eclipse.jdt.internal.core.util.Util;
31
import org.eclipse.jdt.internal.core.util.Util;
32
import org.eclipse.jface.text.BadLocationException;
33
import org.eclipse.jface.text.IDocument;
34
import org.eclipse.text.edits.TextEdit;
32
import org.eclipse.text.edits.TextEdit;
35
33
36
/**
34
/**
Lines 228-234 Link Here
228
	private void processCompilationUnitResource(ICompilationUnit source, PackageFragment dest) throws JavaModelException {
226
	private void processCompilationUnitResource(ICompilationUnit source, PackageFragment dest) throws JavaModelException {
229
		String newCUName = getNewNameFor(source);
227
		String newCUName = getNewNameFor(source);
230
		String destName = (newCUName != null) ? newCUName : source.getElementName();
228
		String destName = (newCUName != null) ? newCUName : source.getElementName();
231
		ASTRewrite rewrite = updateContent(source, dest, newCUName); // null if unchanged
229
		TextEdit edit = updateContent(source, dest, newCUName); // null if unchanged
232
	
230
	
233
		// TODO (frederic) remove when bug 67606 will be fixed (bug 67823)
231
		// TODO (frederic) remove when bug 67606 will be fixed (bug 67823)
234
		// store encoding (fix bug 66898)
232
		// store encoding (fix bug 66898)
Lines 265-271 Link Here
265
						flags |= IResource.KEEP_HISTORY;
263
						flags |= IResource.KEEP_HISTORY;
266
						sourceResource.move(destFile.getFullPath(), flags, getSubProgressMonitor(1));
264
						sourceResource.move(destFile.getFullPath(), flags, getSubProgressMonitor(1));
267
					} else {
265
					} else {
268
						if (rewrite != null) flags |= IResource.KEEP_HISTORY;
266
						if (edit != null) flags |= IResource.KEEP_HISTORY;
269
						sourceResource.copy(destFile.getFullPath(), flags, getSubProgressMonitor(1));
267
						sourceResource.copy(destFile.getFullPath(), flags, getSubProgressMonitor(1));
270
					}
268
					}
271
					setAttribute(HAS_MODIFIED_RESOURCE_ATTR, TRUE); 
269
					setAttribute(HAS_MODIFIED_RESOURCE_ATTR, TRUE); 
Lines 279-288 Link Here
279
			}
277
			}
280
	
278
	
281
			// update new resource content
279
			// update new resource content
282
			if (rewrite != null){
280
			if (edit != null){
283
				boolean wasReadOnly = destFile.isReadOnly();
281
				boolean wasReadOnly = destFile.isReadOnly();
284
				try {
282
				try {
285
					saveContent(dest, destName, rewrite, sourceEncoding, destFile);
283
					saveContent(dest, destName, edit, sourceEncoding, destFile);
286
				} catch (CoreException e) {
284
				} catch (CoreException e) {
287
					if (e instanceof JavaModelException) throw (JavaModelException) e;
285
					if (e instanceof JavaModelException) throw (JavaModelException) e;
288
					throw new JavaModelException(e);
286
					throw new JavaModelException(e);
Lines 308-315 Link Here
308
			// update new resource content
306
			// update new resource content
309
			// in case we do a saveas on the same resource we have to simply update the contents
307
			// in case we do a saveas on the same resource we have to simply update the contents
310
			// see http://dev.eclipse.org/bugs/show_bug.cgi?id=9351
308
			// see http://dev.eclipse.org/bugs/show_bug.cgi?id=9351
311
			if (rewrite != null){
309
			if (edit != null){
312
				saveContent(dest, destName, rewrite, sourceEncoding, destFile);
310
				saveContent(dest, destName, edit, sourceEncoding, destFile);
313
			}
311
			}
314
		}
312
		}
315
	}
313
	}
Lines 462-474 Link Here
462
						AST ast = astCU.getAST();
460
						AST ast = astCU.getAST();
463
						ASTRewrite rewrite = ASTRewrite.create(ast);
461
						ASTRewrite rewrite = ASTRewrite.create(ast);
464
						updatePackageStatement(astCU, newFragName, rewrite);
462
						updatePackageStatement(astCU, newFragName, rewrite);
465
						IDocument document = getDocument(cu);
463
						TextEdit edits = rewrite.rewriteAST();
466
						TextEdit edits = rewrite.rewriteAST(document, null);
464
						cu.applyTextEdit(edits, this.progressMonitor);
467
						try {
468
							edits.apply(document);
469
						} catch (BadLocationException e) {
470
							throw new JavaModelException(e, IJavaModelStatusConstants.INVALID_CONTENTS);
471
						}			
472
						cu.save(null, false);
465
						cu.save(null, false);
473
					}
466
					}
474
				}
467
				}
Lines 523-529 Link Here
523
			throw new JavaModelException(ce);
516
			throw new JavaModelException(ce);
524
		}
517
		}
525
	}
518
	}
526
	private void saveContent(PackageFragment dest, String destName, ASTRewrite rewrite, String sourceEncoding, IFile destFile) throws JavaModelException {
519
	private void saveContent(PackageFragment dest, String destName, TextEdit edits, String sourceEncoding, IFile destFile) throws JavaModelException {
527
		try {
520
		try {
528
			// TODO (frederic) remove when bug 67606 will be fixed (bug 67823)
521
			// TODO (frederic) remove when bug 67606 will be fixed (bug 67823)
529
			// fix bug 66898
522
			// fix bug 66898
Lines 534-550 Link Here
534
			// use no encoding
527
			// use no encoding
535
		}
528
		}
536
		// when the file was copied, its read-only flag was preserved -> temporary set it to false
529
		// when the file was copied, its read-only flag was preserved -> temporary set it to false
537
		// note this doesn't interfer with repository providers as this is a new resource that cannot be under
530
		// note this doesn't interfere with repository providers as this is a new resource that cannot be under
538
		// version control yet
531
		// version control yet
539
		Util.setReadOnly(destFile, false);
532
		Util.setReadOnly(destFile, false);
540
		ICompilationUnit destCU = dest.getCompilationUnit(destName);
533
		ICompilationUnit destCU = dest.getCompilationUnit(destName);
541
		IDocument document = getDocument(destCU);
534
		destCU.applyTextEdit(edits, this.progressMonitor);
542
		TextEdit edits = rewrite.rewriteAST(document, null);
543
		try {
544
			edits.apply(document);
545
		} catch (BadLocationException e) {
546
			throw new JavaModelException(e, IJavaModelStatusConstants.INVALID_CONTENTS);
547
		}
548
		destCU.save(getSubProgressMonitor(1), this.force);
535
		destCU.save(getSubProgressMonitor(1), this.force);
549
	}
536
	}
550
	/**
537
	/**
Lines 553-559 Link Here
553
	 *
540
	 *
554
	 * @return an AST rewrite or null if no rewrite needed
541
	 * @return an AST rewrite or null if no rewrite needed
555
	 */
542
	 */
556
	private ASTRewrite updateContent(ICompilationUnit cu, PackageFragment dest, String newName) throws JavaModelException {
543
	private TextEdit updateContent(ICompilationUnit cu, PackageFragment dest, String newName) throws JavaModelException {
557
		String[] currPackageName = ((PackageFragment) cu.getParent()).names;
544
		String[] currPackageName = ((PackageFragment) cu.getParent()).names;
558
		String[] destPackageName = dest.names;
545
		String[] destPackageName = dest.names;
559
		if (Util.equalArraysOrNull(currPackageName, destPackageName) && newName == null) {
546
		if (Util.equalArraysOrNull(currPackageName, destPackageName) && newName == null) {
Lines 567-573 Link Here
567
			ASTRewrite rewrite = ASTRewrite.create(ast);
554
			ASTRewrite rewrite = ASTRewrite.create(ast);
568
			updateTypeName(cu, astCU, cu.getElementName(), newName, rewrite);
555
			updateTypeName(cu, astCU, cu.getElementName(), newName, rewrite);
569
			updatePackageStatement(astCU, destPackageName, rewrite);
556
			updatePackageStatement(astCU, destPackageName, rewrite);
570
			return rewrite;
557
			return rewrite.rewriteAST();
571
		}
558
		}
572
	}
559
	}
573
	private void updatePackageStatement(CompilationUnit astCU, String[] pkgName, ASTRewrite rewriter) throws JavaModelException {
560
	private void updatePackageStatement(CompilationUnit astCU, String[] pkgName, ASTRewrite rewriter) throws JavaModelException {

Return to bug 220565