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

(-)dom/org/eclipse/jdt/core/dom/rewrite/ASTRewrite.java (-4 / +7 lines)
Lines 11-16 Link Here
11
package org.eclipse.jdt.core.dom.rewrite;
11
package org.eclipse.jdt.core.dom.rewrite;
12
12
13
import java.util.Iterator;
13
import java.util.Iterator;
14
import java.util.List;
14
import java.util.Map;
15
import java.util.Map;
15
16
16
import org.eclipse.jdt.core.ICompilationUnit;
17
import org.eclipse.jdt.core.ICompilationUnit;
Lines 182-188 Link Here
182
		LineInformation lineInfo= LineInformation.create(document);
183
		LineInformation lineInfo= LineInformation.create(document);
183
		String lineDelim= TextUtilities.getDefaultLineDelimiter(document);
184
		String lineDelim= TextUtilities.getDefaultLineDelimiter(document);
184
		
185
		
185
		return internalRewriteAST(content, lineInfo, lineDelim, options, rootNode);
186
		ASTNode astRoot= rootNode.getRoot();
187
		List commentNodes= astRoot instanceof CompilationUnit ? ((CompilationUnit) astRoot).getCommentList() : null;
188
		return internalRewriteAST(content, lineInfo, lineDelim, commentNodes, options, rootNode);
186
	}
189
	}
187
	
190
	
188
	/**
191
	/**
Lines 238-254 Link Here
238
		String lineDelim= cu.findRecommendedLineSeparator();
241
		String lineDelim= cu.findRecommendedLineSeparator();
239
		Map options= cu.getJavaProject().getOptions(true);
242
		Map options= cu.getJavaProject().getOptions(true);
240
		
243
		
241
		return internalRewriteAST(content, lineInfo, lineDelim, options, rootNode);
244
		return internalRewriteAST(content, lineInfo, lineDelim, astRoot.getCommentList(), options, rootNode);
242
	}
245
	}
243
	
246
	
244
	private TextEdit internalRewriteAST(char[] content, LineInformation lineInfo, String lineDelim, Map options, ASTNode rootNode) {
247
	private TextEdit internalRewriteAST(char[] content, LineInformation lineInfo, String lineDelim, List commentNodes, Map options, ASTNode rootNode) {
245
		TextEdit result= new MultiTextEdit();
248
		TextEdit result= new MultiTextEdit();
246
		//validateASTNotModified(rootNode);
249
		//validateASTNotModified(rootNode);
247
		
250
		
248
		TargetSourceRangeComputer sourceRangeComputer= getExtendedSourceRangeComputer();
251
		TargetSourceRangeComputer sourceRangeComputer= getExtendedSourceRangeComputer();
249
		this.eventStore.prepareMovedNodes(sourceRangeComputer);
252
		this.eventStore.prepareMovedNodes(sourceRangeComputer);
250
		
253
		
251
		ASTRewriteAnalyzer visitor= new ASTRewriteAnalyzer(content, lineInfo, lineDelim, result, this.eventStore, this.nodeStore, options, sourceRangeComputer);
254
		ASTRewriteAnalyzer visitor= new ASTRewriteAnalyzer(content, lineInfo, lineDelim, result, this.eventStore, this.nodeStore, commentNodes, options, sourceRangeComputer);
252
		rootNode.accept(visitor); // throws IllegalArgumentException
255
		rootNode.accept(visitor); // throws IllegalArgumentException
253
		
256
		
254
		this.eventStore.revertMovedNodes();
257
		this.eventStore.revertMovedNodes();
(-)dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteAnalyzer.java (-2 / +23 lines)
Lines 67-77 Link Here
67
	private final ASTRewriteFormatter formatter;
67
	private final ASTRewriteFormatter formatter;
68
	private final NodeInfoStore nodeInfos;
68
	private final NodeInfoStore nodeInfos;
69
	private final TargetSourceRangeComputer extendedSourceRangeComputer;
69
	private final TargetSourceRangeComputer extendedSourceRangeComputer;
70
	private final LineCommentEndOffsets lineCommentEndOffsets;
70
	
71
	
71
	/*
72
	/**
72
	 * Constructor for ASTRewriteAnalyzer.
73
	 * Constructor for ASTRewriteAnalyzer.
74
	 * @param content the content of the compilation unit to rewrite.
75
	 * @param lineInfo line information for the content of the compilation unit to rewrite.
76
	 * @param rootEdit the edit to add all generated edits to
77
	 * @param eventStore the event store containing the description of changes
78
	 * @param nodeInfos annotations to nodes, such as if a node is a string placeholder or a copy target
79
	 * @param comments list of comments of the compilation unit to rewrite (elements of type <code>Comment</code>) or <code>null</code>.
80
	 * 	@param options the current jdt.core options (formatting/compliance) or <code>null</code>.
81
	 * 	@param extendedSourceRangeComputer the source range computer to use
73
	 */
82
	 */
74
	public ASTRewriteAnalyzer(char[] content, LineInformation lineInfo, String lineDelim, TextEdit rootEdit, RewriteEventStore eventStore, NodeInfoStore nodeInfos, Map options, TargetSourceRangeComputer extendedSourceRangeComputer) {
83
	public ASTRewriteAnalyzer(char[] content, LineInformation lineInfo, String lineDelim, TextEdit rootEdit, RewriteEventStore eventStore, NodeInfoStore nodeInfos, List comments, Map options, TargetSourceRangeComputer extendedSourceRangeComputer) {
75
		this.eventStore= eventStore;
84
		this.eventStore= eventStore;
76
		this.content= content;
85
		this.content= content;
77
		this.lineInfo= lineInfo;
86
		this.lineInfo= lineInfo;
Lines 84-89 Link Here
84
		this.formatter= new ASTRewriteFormatter(nodeInfos, eventStore, options, lineDelim);
93
		this.formatter= new ASTRewriteFormatter(nodeInfos, eventStore, options, lineDelim);
85
		
94
		
86
		this.extendedSourceRangeComputer = extendedSourceRangeComputer;
95
		this.extendedSourceRangeComputer = extendedSourceRangeComputer;
96
		this.lineCommentEndOffsets= new LineCommentEndOffsets(comments);
87
	}
97
	}
88
		
98
		
89
	final TokenScanner getScanner() {
99
	final TokenScanner getScanner() {
Lines 230-235 Link Here
230
	
240
	
231
	final void doTextInsert(int offset, String insertString, TextEditGroup editGroup) {
241
	final void doTextInsert(int offset, String insertString, TextEditGroup editGroup) {
232
		if (insertString.length() > 0) {
242
		if (insertString.length() > 0) {
243
			// bug fix for 95839: problem with inserting at the end of a line comment
244
			if (this.lineCommentEndOffsets.isEndOfLineComment(offset, this.content)) {
245
				if (!insertString.startsWith(getLineDelimiter())) {
246
					TextEdit edit= new InsertEdit(offset, getLineDelimiter());  // add a line delimiter
247
					addEdit(edit);
248
					if (editGroup != null) {
249
						addEditGroup(editGroup, edit);
250
					}
251
				}
252
				this.lineCommentEndOffsets.remove(offset); // only one line delimiter per line comment required
253
			}
233
			TextEdit edit= new InsertEdit(offset, insertString);
254
			TextEdit edit= new InsertEdit(offset, insertString);
234
			addEdit(edit);
255
			addEdit(edit);
235
			if (editGroup != null) {
256
			if (editGroup != null) {
(-)dom/org/eclipse/jdt/core/dom/InternalASTRewrite.java (-1 / +2 lines)
Lines 87-94 Link Here
87
			char[] content= document.get().toCharArray();
87
			char[] content= document.get().toCharArray();
88
			LineInformation lineInfo= LineInformation.create(document);
88
			LineInformation lineInfo= LineInformation.create(document);
89
			String lineDelim= TextUtilities.getDefaultLineDelimiter(document);
89
			String lineDelim= TextUtilities.getDefaultLineDelimiter(document);
90
			List comments= rootNode.getCommentList();
90
			
91
			
91
			ASTRewriteAnalyzer visitor = new ASTRewriteAnalyzer(content, lineInfo, lineDelim, result, this.eventStore, this.nodeStore, options, xsrComputer);
92
			ASTRewriteAnalyzer visitor = new ASTRewriteAnalyzer(content, lineInfo, lineDelim, result, this.eventStore, this.nodeStore, comments, options, xsrComputer);
92
			rootNode.accept(visitor);
93
			rootNode.accept(visitor);
93
		}
94
		}
94
		return result;
95
		return result;
(-)dom/org/eclipse/jdt/internal/core/dom/rewrite/LineCommentEndOffsets.java (+84 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2006 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials 
4
 * are made available under the terms of the Common 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
12
package org.eclipse.jdt.internal.core.dom.rewrite;
13
14
import java.util.Arrays;
15
import java.util.List;
16
17
import org.eclipse.jdt.core.dom.LineComment;
18
import org.eclipse.jdt.core.formatter.IndentManipulation;
19
20
public class LineCommentEndOffsets {
21
	
22
	private int[] offsets;
23
	private final List commentList;
24
	
25
	public LineCommentEndOffsets(List commentList) {
26
		this.commentList= commentList;
27
		this.offsets= null; // create on demand
28
	}
29
	
30
	private int[] getOffsets() {
31
		if (this.offsets == null) {
32
			if (this.commentList != null) {
33
				int nComments= this.commentList.size();
34
				// count the number of line comments
35
				int count= 0;
36
				for (int i= 0; i < nComments; i++) {
37
					Object curr= this.commentList.get(i);
38
					if (curr instanceof LineComment) {
39
						count++;
40
					}
41
				}
42
				// fill the offset table
43
				this.offsets= new int[count];
44
				for (int i= 0, k= 0; i < nComments; i++) {
45
					Object curr= this.commentList.get(i);
46
					if (curr instanceof LineComment) {
47
						LineComment comment= (LineComment) curr;
48
						this.offsets[k++]= comment.getStartPosition() + comment.getLength();
49
					}
50
				}
51
			} else {
52
				this.offsets= new int[0];
53
			}
54
		}
55
		return this.offsets;
56
	}
57
	
58
	public boolean isEndOfLineComment(int offset) {
59
		return offset >= 0 && Arrays.binarySearch(getOffsets(), offset) >= 0;
60
	}
61
	
62
	public boolean isEndOfLineComment(int offset, char[] content) {
63
		if (offset < 0 || (offset < content.length && !IndentManipulation.isLineDelimiterChar(content[offset]))) {
64
			return false;
65
		}
66
		return Arrays.binarySearch(getOffsets(), offset) >= 0;
67
	}
68
	
69
	public boolean remove(int offset) {
70
		int[] offsetArray= getOffsets(); // returns the shared array
71
		int index= Arrays.binarySearch(offsetArray, offset);
72
		if (index >= 0) {
73
			if (index > 0) {
74
				// shift from the beginning and insert -1 (smallest number) at the beginning
75
				// 1, 2, 3, x, 4, 5 -> -1, 1, 2, 3, 4, 5
76
				System.arraycopy(offsetArray, 0, offsetArray, 1, index);
77
			}
78
			offsetArray[0]= -1;
79
			return true;
80
		}
81
		return false;
82
	}
83
	
84
}
(-)src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingTest.java (+1 lines)
Lines 61-66 Link Here
61
		suite.addTest(ASTRewritingGroupNodeTest.allTests());
61
		suite.addTest(ASTRewritingGroupNodeTest.allTests());
62
		suite.addTest(SourceModifierTest.allTests());
62
		suite.addTest(SourceModifierTest.allTests());
63
		suite.addTest(ImportRewriteTest.allTests());
63
		suite.addTest(ImportRewriteTest.allTests());
64
		suite.addTest(LineCommentOffsetsTest.allTests());
64
		return suite;
65
		return suite;
65
	}
66
	}
66
67
(-)src/org/eclipse/jdt/core/tests/rewrite/describing/LineCommentOffsetsTest.java (+537 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2006 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.core.tests.rewrite.describing;
12
13
import java.util.HashSet;
14
15
import junit.framework.Test;
16
import junit.framework.TestSuite;
17
18
import org.eclipse.jdt.core.ICompilationUnit;
19
import org.eclipse.jdt.core.IPackageFragment;
20
import org.eclipse.jdt.core.dom.*;
21
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
22
import org.eclipse.jdt.core.dom.rewrite.ListRewrite;
23
import org.eclipse.jdt.internal.core.dom.rewrite.LineCommentEndOffsets;
24
25
/**
26
 *
27
 */
28
public class LineCommentOffsetsTest extends ASTRewritingTest {
29
	
30
	private static final Class THIS= LineCommentOffsetsTest.class;
31
32
	public LineCommentOffsetsTest(String name) {
33
		super(name);
34
	}
35
36
	public static Test allTests() {
37
		return new Suite(THIS);
38
	}
39
	
40
	public static Test setUpTest(Test someTest) {
41
		TestSuite suite= new Suite("one test");
42
		suite.addTest(someTest);
43
		return suite;
44
	}
45
	
46
	public static Test suite() {
47
		return allTests();
48
	}
49
	
50
	public void testEmptyLineComments() throws Exception {
51
		
52
		StringBuffer buf= new StringBuffer();
53
		buf.append("\n");
54
		
55
		LineCommentEndOffsets offsets= new LineCommentEndOffsets(null);
56
		boolean res= offsets.isEndOfLineComment(0);
57
		assertFalse(res);
58
		res= offsets.remove(0);
59
		assertFalse(res);
60
	}
61
	
62
	public void testRemove() throws Exception {
63
		
64
		IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null);
65
66
		StringBuffer buf= new StringBuffer();
67
		buf.append("package test1;//comment Y\n");
68
		buf.append("public class E//comment Y\n");
69
		buf.append("{//comment Y\n");
70
		buf.append("}//comment Y");	
71
		String contents= buf.toString();
72
		ICompilationUnit cu= pack1.createCompilationUnit("E.java", contents, false, null);
73
		
74
		CompilationUnit astRoot= createAST3(cu);
75
		
76
		LineCommentEndOffsets offsets= new LineCommentEndOffsets(astRoot.getCommentList());
77
78
		int p1= contents.indexOf('Y') + 1;
79
		int p2= contents.indexOf('Y', p1) + 1;
80
		int p3= contents.indexOf('Y', p2) + 1;
81
		int p4= contents.indexOf('Y', p3) + 1;
82
		
83
		assertFalse(offsets.isEndOfLineComment(0));
84
		assertTrue(offsets.isEndOfLineComment(p1));
85
		assertTrue(offsets.isEndOfLineComment(p2));
86
		assertTrue(offsets.isEndOfLineComment(p3));
87
		assertTrue(offsets.isEndOfLineComment(p4));
88
		
89
		boolean res= offsets.remove(p2);
90
		assertTrue(res);
91
		
92
		res= offsets.remove(p2);
93
		assertFalse(res);
94
		
95
		assertFalse(offsets.isEndOfLineComment(0));
96
		assertTrue(offsets.isEndOfLineComment(p1));
97
		assertFalse(offsets.isEndOfLineComment(p2));
98
		assertTrue(offsets.isEndOfLineComment(p3));
99
		assertTrue(offsets.isEndOfLineComment(p4));
100
		
101
		res= offsets.remove(p4);
102
		assertTrue(res);
103
		
104
		assertFalse(offsets.isEndOfLineComment(0));
105
		assertTrue(offsets.isEndOfLineComment(p1));
106
		assertFalse(offsets.isEndOfLineComment(p2));
107
		assertTrue(offsets.isEndOfLineComment(p3));
108
		assertFalse(offsets.isEndOfLineComment(p4));
109
		
110
		res= offsets.remove(p1);
111
		assertTrue(res);
112
		
113
		assertFalse(offsets.isEndOfLineComment(0));
114
		assertFalse(offsets.isEndOfLineComment(p1));
115
		assertFalse(offsets.isEndOfLineComment(p2));
116
		assertTrue(offsets.isEndOfLineComment(p3));
117
		assertFalse(offsets.isEndOfLineComment(p4));
118
	}
119
	
120
	
121
	
122
	public void testLineCommentEndOffsets() throws Exception {
123
		IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null);
124
		
125
		
126
		StringBuffer buf= new StringBuffer();
127
		buf.append("package test1;\n");
128
		buf.append("/* comment */\n");
129
		buf.append("// comment Y\n");
130
		buf.append("public class E {\n");
131
		buf.append("    public void foo() {\n");
132
		buf.append("        while (i == 0) {\n");
133
		buf.append("            foo();\n");
134
		buf.append("            i++; // comment Y\n");
135
		buf.append("            i++;\n");
136
		buf.append("        }// comment// comment Y\n");
137
		buf.append("        return;\n");
138
		buf.append("    }\n");
139
		buf.append("} // comment Y");
140
		String content= buf.toString();
141
		
142
		ICompilationUnit cu= pack1.createCompilationUnit("E.java", content, false, null);
143
		CompilationUnit astRoot= createAST(cu);
144
		
145
		LineCommentEndOffsets offsets= new LineCommentEndOffsets(astRoot.getCommentList());
146
		HashSet expectedOffsets= new HashSet();
147
148
		for (int i= 0; i < content.length(); i++) {
149
			char ch= content.charAt(i);
150
			if (ch == 'Y') {
151
				expectedOffsets.add(new Integer(i + 1));
152
			}
153
		}
154
		
155
		int count= 0;
156
		
157
		char[] charContent= content.toCharArray();
158
		for (int i= 0; i <= content.length() + 5; i++) {
159
			boolean expected= i > 0 && i <= content.length() && charContent[i - 1] == 'Y';
160
			boolean actual= offsets.isEndOfLineComment(i, charContent);
161
			assertEquals(expected, actual);
162
			
163
			actual= offsets.isEndOfLineComment(i);
164
			assertEquals(expected, actual);
165
			
166
			if (expected) {
167
				count++;
168
			}
169
			
170
		}
171
		assertEquals(4, count);
172
	}
173
	
174
	public void testLineCommentEndOffsetsMixedLineDelimiter() throws Exception {
175
		IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null);
176
		
177
		StringBuffer buf= new StringBuffer();
178
		buf.append("package test1;\n");
179
		buf.append("/* comment */\r\n");
180
		buf.append("// comment Y\n");
181
		buf.append("public class E {\r\n");
182
		buf.append("    public void foo() {\n");
183
		buf.append("        while (i == 0) {\n");
184
		buf.append("            foo();\n");
185
		buf.append("            i++; // comment Y\r\n");
186
		buf.append("            i++;\n");
187
		buf.append("        }// comment// comment Y\r");
188
		buf.append("        return;\n");
189
		buf.append("    }\r\n");
190
		buf.append("} // comment Y");
191
		String content= buf.toString();
192
		
193
		ICompilationUnit cu= pack1.createCompilationUnit("E.java", content, false, null);
194
		CompilationUnit astRoot= createAST(cu);
195
		
196
		LineCommentEndOffsets offsets= new LineCommentEndOffsets(astRoot.getCommentList());
197
		HashSet expectedOffsets= new HashSet();
198
199
		for (int i= 0; i < content.length(); i++) {
200
			char ch= content.charAt(i);
201
			if (ch == 'Y') {
202
				expectedOffsets.add(new Integer(i + 1));
203
			}
204
		}
205
		
206
		int count= 0;
207
		
208
		char[] charContent= content.toCharArray();
209
		for (int i= 0; i <= content.length() + 5; i++) {
210
			boolean expected= i > 0 && i <= content.length() && charContent[i - 1] == 'Y';
211
			boolean actual= offsets.isEndOfLineComment(i, charContent);
212
			assertEquals(expected, actual);
213
			if (expected) {
214
				count++;
215
			}
216
			
217
		}
218
		assertEquals(4, count);
219
	}
220
	
221
	public void testCommentInLists() throws Exception {
222
		
223
		IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null);
224
		StringBuffer buf= new StringBuffer();
225
		buf.append("package test1;\n");
226
		buf.append("public class E implements A //comment\n");
227
		buf.append("{\n");
228
		buf.append("}\n");	
229
		ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
230
		
231
		CompilationUnit astRoot= createAST3(cu);
232
		ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST());
233
		
234
		AST ast= astRoot.getAST();
235
		
236
		assertTrue("Parse errors", (astRoot.getFlags() & ASTNode.MALFORMED) == 0);
237
		TypeDeclaration type= findTypeDeclaration(astRoot, "E");
238
		
239
		ListRewrite listRewrite= rewrite.getListRewrite(type, TypeDeclaration.SUPER_INTERFACE_TYPES_PROPERTY);
240
		SimpleType newInterface= ast.newSimpleType(ast.newSimpleName("B"));
241
		listRewrite.insertLast(newInterface, null);
242
			
243
		String preview= evaluateRewrite(cu, rewrite);
244
		
245
		buf= new StringBuffer();
246
		buf.append("package test1;\n");
247
		buf.append("public class E implements A //comment\n");
248
		buf.append(", B\n");
249
		buf.append("{\n");
250
		buf.append("}\n");	
251
		assertEqualString(preview, buf.toString());
252
	}
253
	
254
	public void testCommentInType() throws Exception {
255
		
256
		IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null);
257
		StringBuffer buf= new StringBuffer();
258
		buf.append("package test1;\n");
259
		buf.append("public class E //comment\n");
260
		buf.append("{\n");
261
		buf.append("}\n");	
262
		ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
263
		
264
		CompilationUnit astRoot= createAST3(cu);
265
		ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST());
266
		
267
		AST ast= astRoot.getAST();
268
		
269
		assertTrue("Parse errors", (astRoot.getFlags() & ASTNode.MALFORMED) == 0);
270
		TypeDeclaration type= findTypeDeclaration(astRoot, "E");
271
		
272
		ListRewrite listRewrite= rewrite.getListRewrite(type, TypeDeclaration.SUPER_INTERFACE_TYPES_PROPERTY);
273
		SimpleType newInterface= ast.newSimpleType(ast.newSimpleName("B"));
274
		listRewrite.insertLast(newInterface, null);
275
			
276
		String preview= evaluateRewrite(cu, rewrite);
277
		
278
		buf= new StringBuffer();
279
		buf.append("package test1;\n");
280
		buf.append("public class E //comment\n");
281
		buf.append(" implements B\n");
282
		buf.append("{\n");
283
		buf.append("}\n");	
284
		assertEqualString(preview, buf.toString());
285
	}
286
	
287
	public void testBug103340() throws Exception {
288
		
289
		IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null);
290
		StringBuffer buf= new StringBuffer();
291
		buf.append("package test1;\n");
292
		buf.append("public class E //implements List\n");
293
		buf.append("{\n");
294
		buf.append("}\n");	
295
		ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
296
		
297
		CompilationUnit astRoot= createAST3(cu);
298
		ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST());
299
		
300
		AST ast= astRoot.getAST();
301
		
302
		assertTrue("Parse errors", (astRoot.getFlags() & ASTNode.MALFORMED) == 0);
303
		TypeDeclaration type= findTypeDeclaration(astRoot, "E");
304
		
305
		ListRewrite listRewrite= rewrite.getListRewrite(type, TypeDeclaration.TYPE_PARAMETERS_PROPERTY);
306
		TypeParameter newType= ast.newTypeParameter();
307
		newType.setName(ast.newSimpleName("X"));
308
		listRewrite.insertLast(newType, null);
309
			
310
		String preview= evaluateRewrite(cu, rewrite);
311
		
312
		buf= new StringBuffer();
313
		buf.append("package test1;\n");
314
		buf.append("public class E //implements List\n");
315
		buf.append("<X>\n");
316
		buf.append("{\n");
317
		buf.append("}\n");	
318
		assertEqualString(preview, buf.toString());
319
	}
320
	
321
	public void testBug95839() throws Exception {
322
		
323
		IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null);
324
		StringBuffer buf= new StringBuffer();
325
		buf.append("package test1;\n");
326
		buf.append("public class E {\n");
327
		buf.append("  void foo() {\n");
328
		buf.append("    object.method(\n");
329
		buf.append("      param1, // text about param1\n");
330
		buf.append("      param2  // text about param2\n");
331
		buf.append("    );\n");
332
		buf.append("  }\n");	
333
		buf.append("}\n");	
334
		ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
335
		
336
		CompilationUnit astRoot= createAST3(cu);
337
		ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST());
338
		
339
		AST ast= astRoot.getAST();
340
		
341
		assertTrue("Parse errors", (astRoot.getFlags() & ASTNode.MALFORMED) == 0);
342
		TypeDeclaration type= findTypeDeclaration(astRoot, "E");
343
		ExpressionStatement statement= (ExpressionStatement) ((MethodDeclaration) type.bodyDeclarations().get(0)).getBody().statements().get(0);
344
		MethodInvocation inv= (MethodInvocation) statement.getExpression();
345
		
346
		ListRewrite listRewrite= rewrite.getListRewrite(inv, MethodInvocation.ARGUMENTS_PROPERTY);
347
		listRewrite.insertLast(ast.newSimpleName("param3"), null);
348
			
349
		String preview= evaluateRewrite(cu, rewrite);
350
		
351
		buf= new StringBuffer();
352
		buf.append("package test1;\n");
353
		buf.append("public class E {\n");
354
		buf.append("  void foo() {\n");
355
		buf.append("    object.method(\n");
356
		buf.append("      param1, // text about param1\n");
357
		buf.append("      param2  // text about param2\n");
358
		buf.append(", param3\n");
359
		buf.append("    );\n");
360
		buf.append("  }\n");	
361
		buf.append("}\n");	
362
		assertEqualString(preview, buf.toString());
363
	}
364
	
365
	public void testBug114418() throws Exception {
366
		
367
		IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null);
368
		StringBuffer buf= new StringBuffer();
369
		buf.append("package test1;\n");
370
		buf.append("public class E {\n");
371
		buf.append("  void foo() {\n");
372
		buf.append("    try {\n");
373
		buf.append("    } catch (IOException e) {\n");
374
		buf.append("    }\n");
375
		buf.append("    // comment\n");	
376
		buf.append("  }\n");	
377
		buf.append("}\n");	
378
		ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
379
		
380
		CompilationUnit astRoot= createAST3(cu);
381
		ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST());
382
		
383
		AST ast= astRoot.getAST();
384
		
385
		assertTrue("Parse errors", (astRoot.getFlags() & ASTNode.MALFORMED) == 0);
386
		TypeDeclaration type= findTypeDeclaration(astRoot, "E");
387
		TryStatement statement= (TryStatement) ((MethodDeclaration) type.bodyDeclarations().get(0)).getBody().statements().get(0);
388
		
389
		ListRewrite listRewrite= rewrite.getListRewrite(statement, TryStatement.CATCH_CLAUSES_PROPERTY);
390
		CatchClause clause= ast.newCatchClause();
391
		SingleVariableDeclaration newSingleVariableDeclaration= ast.newSingleVariableDeclaration();
392
		newSingleVariableDeclaration.setName(ast.newSimpleName("e"));
393
		newSingleVariableDeclaration.setType(ast.newSimpleType(ast.newSimpleName("MyException")));
394
		clause.setException(newSingleVariableDeclaration);
395
		
396
		listRewrite.insertLast(clause, null);
397
			
398
		String preview= evaluateRewrite(cu, rewrite);
399
		
400
		buf= new StringBuffer();
401
		buf.append("package test1;\n");
402
		buf.append("public class E {\n");
403
		buf.append("  void foo() {\n");
404
		buf.append("    try {\n");
405
		buf.append("    } catch (IOException e) {\n");
406
		buf.append("    }\n");
407
		buf.append("    // comment\n");
408
		buf.append(" catch (MyException e) {\n");
409
		buf.append("    }\n");
410
		buf.append("  }\n");	
411
		buf.append("}\n");	
412
		assertEqualString(preview, buf.toString());
413
	}
414
	
415
	public void testBug128818() throws Exception {
416
		
417
		IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null);
418
		StringBuffer buf= new StringBuffer();
419
		buf.append("package test1;\n");
420
		buf.append("public class E {\n");
421
		buf.append("  void foo() {\n");
422
		buf.append("    if (true) {\n");
423
		buf.append("    } // comment\n");
424
		buf.append("    else\n");
425
		buf.append("      return;\n");
426
		buf.append("  }\n");	
427
		buf.append("}\n");	
428
		ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
429
		
430
		CompilationUnit astRoot= createAST3(cu);
431
		ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST());
432
		
433
		AST ast= astRoot.getAST();
434
		
435
		assertTrue("Parse errors", (astRoot.getFlags() & ASTNode.MALFORMED) == 0);
436
		TypeDeclaration type= findTypeDeclaration(astRoot, "E");
437
		IfStatement statement= (IfStatement) ((MethodDeclaration) type.bodyDeclarations().get(0)).getBody().statements().get(0);
438
		
439
		rewrite.set(statement, IfStatement.ELSE_STATEMENT_PROPERTY, ast.newBlock(), null);
440
			
441
		String preview= evaluateRewrite(cu, rewrite);
442
		
443
		buf= new StringBuffer();
444
		buf.append("package test1;\n");
445
		buf.append("public class E {\n");
446
		buf.append("  void foo() {\n");
447
		buf.append("    if (true) {\n");
448
		buf.append("    } // comment\n");
449
		buf.append(" else {\n");
450
		buf.append("    }\n");
451
		buf.append("  }\n");	
452
		buf.append("}\n");	
453
		assertEqualString(preview, buf.toString());
454
	}
455
	
456
	/* not yet working
457
	public void testBug128422() throws Exception {
458
		
459
		IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null);
460
		StringBuffer buf= new StringBuffer();
461
		buf.append("package test1;\n");
462
		buf.append("public class E {\n");
463
		buf.append("  void foo() {\n");
464
		buf.append("    if (i != 0 //I don't like 0\n");
465
		buf.append("                 && i != 10) {\n");
466
		buf.append("    }\n");
467
		buf.append("  }\n");	
468
		buf.append("}\n");	
469
		ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
470
		
471
		CompilationUnit astRoot= createAST3(cu);
472
		ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST());
473
		
474
		AST ast= astRoot.getAST();
475
		
476
		assertTrue("Parse errors", (astRoot.getFlags() & ASTNode.MALFORMED) == 0);
477
		TypeDeclaration type= findTypeDeclaration(astRoot, "E");
478
		IfStatement statement= (IfStatement) ((MethodDeclaration) type.bodyDeclarations().get(0)).getBody().statements().get(0);
479
		Expression expression= ((InfixExpression) statement.getExpression()).getLeftOperand();
480
		
481
		ParenthesizedExpression parenthesizedExpression= ast.newParenthesizedExpression();
482
		parenthesizedExpression.setExpression( (Expression) rewrite.createCopyTarget(expression));
483
		rewrite.replace(expression, parenthesizedExpression, null);
484
			
485
		String preview= evaluateRewrite(cu, rewrite);
486
		
487
		buf= new StringBuffer();
488
		buf.append("package test1;\n");
489
		buf.append("public class E {\n");
490
		buf.append("  void foo() {\n");
491
		buf.append("    if ((i != 0 //I don't like 0\n");
492
		buf.append(")\n");
493
		buf.append("                 && i != 10) {\n");
494
		buf.append("    }\n");
495
		buf.append("  }\n");	
496
		buf.append("}\n");	
497
		assertEqualString(preview, buf.toString());
498
	}*/
499
	
500
	public void testCommentAtEnd() throws Exception {
501
		
502
		IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null);
503
		StringBuffer buf= new StringBuffer();
504
		buf.append("package test1;\n");
505
		buf.append("public class E \n");
506
		buf.append("{\n");
507
		buf.append("}//comment");	
508
		ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
509
		
510
		CompilationUnit astRoot= createAST3(cu);
511
		ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST());
512
		
513
		AST ast= astRoot.getAST();
514
		
515
		assertTrue("Parse errors", (astRoot.getFlags() & ASTNode.MALFORMED) == 0);
516
		
517
		ListRewrite listRewrite= rewrite.getListRewrite(astRoot, CompilationUnit.TYPES_PROPERTY);
518
		TypeDeclaration newType= ast.newTypeDeclaration();
519
		newType.setName(ast.newSimpleName("B"));
520
		listRewrite.insertLast(newType, null);
521
			
522
		String preview= evaluateRewrite(cu, rewrite);
523
		
524
		buf= new StringBuffer();
525
		buf.append("package test1;\n");
526
		buf.append("public class E \n");
527
		buf.append("{\n");
528
		buf.append("}//comment\n");
529
		buf.append("\n");
530
		buf.append("class B {\n");
531
		buf.append("}");
532
		assertEqualString(preview, buf.toString());
533
	}
534
	
535
536
	
537
}

Return to bug 95839