### Eclipse Workspace Patch 1.0
#P org.eclipse.jdt.core
Index: dom/org/eclipse/jdt/core/dom/rewrite/ASTRewrite.java
===================================================================
RCS file: /home/eclipse/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/rewrite/ASTRewrite.java,v
retrieving revision 1.27
diff -u -r1.27 ASTRewrite.java
--- dom/org/eclipse/jdt/core/dom/rewrite/ASTRewrite.java 27 Apr 2006 00:38:23 -0000 1.27
+++ dom/org/eclipse/jdt/core/dom/rewrite/ASTRewrite.java 27 Apr 2006 15:19:24 -0000
@@ -11,6 +11,7 @@
package org.eclipse.jdt.core.dom.rewrite;
import java.util.Iterator;
+import java.util.List;
import java.util.Map;
import org.eclipse.jdt.core.ICompilationUnit;
@@ -182,7 +183,9 @@
LineInformation lineInfo= LineInformation.create(document);
String lineDelim= TextUtilities.getDefaultLineDelimiter(document);
- return internalRewriteAST(content, lineInfo, lineDelim, options, rootNode);
+ ASTNode astRoot= rootNode.getRoot();
+ List commentNodes= astRoot instanceof CompilationUnit ? ((CompilationUnit) astRoot).getCommentList() : null;
+ return internalRewriteAST(content, lineInfo, lineDelim, commentNodes, options, rootNode);
}
/**
@@ -238,17 +241,17 @@
String lineDelim= cu.findRecommendedLineSeparator();
Map options= cu.getJavaProject().getOptions(true);
- return internalRewriteAST(content, lineInfo, lineDelim, options, rootNode);
+ return internalRewriteAST(content, lineInfo, lineDelim, astRoot.getCommentList(), options, rootNode);
}
- private TextEdit internalRewriteAST(char[] content, LineInformation lineInfo, String lineDelim, Map options, ASTNode rootNode) {
+ private TextEdit internalRewriteAST(char[] content, LineInformation lineInfo, String lineDelim, List commentNodes, Map options, ASTNode rootNode) {
TextEdit result= new MultiTextEdit();
//validateASTNotModified(rootNode);
TargetSourceRangeComputer sourceRangeComputer= getExtendedSourceRangeComputer();
this.eventStore.prepareMovedNodes(sourceRangeComputer);
- ASTRewriteAnalyzer visitor= new ASTRewriteAnalyzer(content, lineInfo, lineDelim, result, this.eventStore, this.nodeStore, options, sourceRangeComputer);
+ ASTRewriteAnalyzer visitor= new ASTRewriteAnalyzer(content, lineInfo, lineDelim, result, this.eventStore, this.nodeStore, commentNodes, options, sourceRangeComputer);
rootNode.accept(visitor); // throws IllegalArgumentException
this.eventStore.revertMovedNodes();
Index: dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteAnalyzer.java
===================================================================
RCS file: /home/eclipse/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteAnalyzer.java,v
retrieving revision 1.44
diff -u -r1.44 ASTRewriteAnalyzer.java
--- dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteAnalyzer.java 12 Apr 2006 15:36:01 -0000 1.44
+++ dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteAnalyzer.java 27 Apr 2006 15:19:25 -0000
@@ -67,11 +67,20 @@
private final ASTRewriteFormatter formatter;
private final NodeInfoStore nodeInfos;
private final TargetSourceRangeComputer extendedSourceRangeComputer;
+ private final LineCommentEndOffsets lineCommentEndOffsets;
- /*
+ /**
* Constructor for ASTRewriteAnalyzer.
+ * @param content the content of the compilation unit to rewrite.
+ * @param lineInfo line information for the content of the compilation unit to rewrite.
+ * @param rootEdit the edit to add all generated edits to
+ * @param eventStore the event store containing the description of changes
+ * @param nodeInfos annotations to nodes, such as if a node is a string placeholder or a copy target
+ * @param comments list of comments of the compilation unit to rewrite (elements of type Comment
) or null
.
+ * @param options the current jdt.core options (formatting/compliance) or null
.
+ * @param extendedSourceRangeComputer the source range computer to use
*/
- public ASTRewriteAnalyzer(char[] content, LineInformation lineInfo, String lineDelim, TextEdit rootEdit, RewriteEventStore eventStore, NodeInfoStore nodeInfos, Map options, TargetSourceRangeComputer extendedSourceRangeComputer) {
+ public ASTRewriteAnalyzer(char[] content, LineInformation lineInfo, String lineDelim, TextEdit rootEdit, RewriteEventStore eventStore, NodeInfoStore nodeInfos, List comments, Map options, TargetSourceRangeComputer extendedSourceRangeComputer) {
this.eventStore= eventStore;
this.content= content;
this.lineInfo= lineInfo;
@@ -84,6 +93,7 @@
this.formatter= new ASTRewriteFormatter(nodeInfos, eventStore, options, lineDelim);
this.extendedSourceRangeComputer = extendedSourceRangeComputer;
+ this.lineCommentEndOffsets= new LineCommentEndOffsets(comments);
}
final TokenScanner getScanner() {
@@ -230,6 +240,17 @@
final void doTextInsert(int offset, String insertString, TextEditGroup editGroup) {
if (insertString.length() > 0) {
+ // bug fix for 95839: problem with inserting at the end of a line comment
+ if (this.lineCommentEndOffsets.isEndOfLineComment(offset, this.content)) {
+ if (!insertString.startsWith(getLineDelimiter())) {
+ TextEdit edit= new InsertEdit(offset, getLineDelimiter()); // add a line delimiter
+ addEdit(edit);
+ if (editGroup != null) {
+ addEditGroup(editGroup, edit);
+ }
+ }
+ this.lineCommentEndOffsets.remove(offset); // only one line delimiter per line comment required
+ }
TextEdit edit= new InsertEdit(offset, insertString);
addEdit(edit);
if (editGroup != null) {
Index: dom/org/eclipse/jdt/core/dom/InternalASTRewrite.java
===================================================================
RCS file: /home/eclipse/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/InternalASTRewrite.java,v
retrieving revision 1.8
diff -u -r1.8 InternalASTRewrite.java
--- dom/org/eclipse/jdt/core/dom/InternalASTRewrite.java 28 Nov 2005 08:29:09 -0000 1.8
+++ dom/org/eclipse/jdt/core/dom/InternalASTRewrite.java 27 Apr 2006 15:19:24 -0000
@@ -87,8 +87,9 @@
char[] content= document.get().toCharArray();
LineInformation lineInfo= LineInformation.create(document);
String lineDelim= TextUtilities.getDefaultLineDelimiter(document);
+ List comments= rootNode.getCommentList();
- ASTRewriteAnalyzer visitor = new ASTRewriteAnalyzer(content, lineInfo, lineDelim, result, this.eventStore, this.nodeStore, options, xsrComputer);
+ ASTRewriteAnalyzer visitor = new ASTRewriteAnalyzer(content, lineInfo, lineDelim, result, this.eventStore, this.nodeStore, comments, options, xsrComputer);
rootNode.accept(visitor);
}
return result;
Index: dom/org/eclipse/jdt/internal/core/dom/rewrite/LineCommentEndOffsets.java
===================================================================
RCS file: dom/org/eclipse/jdt/internal/core/dom/rewrite/LineCommentEndOffsets.java
diff -N dom/org/eclipse/jdt/internal/core/dom/rewrite/LineCommentEndOffsets.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ dom/org/eclipse/jdt/internal/core/dom/rewrite/LineCommentEndOffsets.java 1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,84 @@
+/*******************************************************************************
+ * Copyright (c) 2000, 2006 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Common 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.dom.rewrite;
+
+import java.util.Arrays;
+import java.util.List;
+
+import org.eclipse.jdt.core.dom.LineComment;
+import org.eclipse.jdt.core.formatter.IndentManipulation;
+
+public class LineCommentEndOffsets {
+
+ private int[] offsets;
+ private final List commentList;
+
+ public LineCommentEndOffsets(List commentList) {
+ this.commentList= commentList;
+ this.offsets= null; // create on demand
+ }
+
+ private int[] getOffsets() {
+ if (this.offsets == null) {
+ if (this.commentList != null) {
+ int nComments= this.commentList.size();
+ // count the number of line comments
+ int count= 0;
+ for (int i= 0; i < nComments; i++) {
+ Object curr= this.commentList.get(i);
+ if (curr instanceof LineComment) {
+ count++;
+ }
+ }
+ // fill the offset table
+ this.offsets= new int[count];
+ for (int i= 0, k= 0; i < nComments; i++) {
+ Object curr= this.commentList.get(i);
+ if (curr instanceof LineComment) {
+ LineComment comment= (LineComment) curr;
+ this.offsets[k++]= comment.getStartPosition() + comment.getLength();
+ }
+ }
+ } else {
+ this.offsets= new int[0];
+ }
+ }
+ return this.offsets;
+ }
+
+ public boolean isEndOfLineComment(int offset) {
+ return offset >= 0 && Arrays.binarySearch(getOffsets(), offset) >= 0;
+ }
+
+ public boolean isEndOfLineComment(int offset, char[] content) {
+ if (offset < 0 || (offset < content.length && !IndentManipulation.isLineDelimiterChar(content[offset]))) {
+ return false;
+ }
+ return Arrays.binarySearch(getOffsets(), offset) >= 0;
+ }
+
+ public boolean remove(int offset) {
+ int[] offsetArray= getOffsets(); // returns the shared array
+ int index= Arrays.binarySearch(offsetArray, offset);
+ if (index >= 0) {
+ if (index > 0) {
+ // shift from the beginning and insert -1 (smallest number) at the beginning
+ // 1, 2, 3, x, 4, 5 -> -1, 1, 2, 3, 4, 5
+ System.arraycopy(offsetArray, 0, offsetArray, 1, index);
+ }
+ offsetArray[0]= -1;
+ return true;
+ }
+ return false;
+ }
+
+}
#P org.eclipse.jdt.core.tests.model
Index: src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingTest.java
===================================================================
RCS file: /home/eclipse/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingTest.java,v
retrieving revision 1.18
diff -u -r1.18 ASTRewritingTest.java
--- src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingTest.java 29 Mar 2006 04:03:08 -0000 1.18
+++ src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingTest.java 27 Apr 2006 15:19:30 -0000
@@ -61,6 +61,7 @@
suite.addTest(ASTRewritingGroupNodeTest.allTests());
suite.addTest(SourceModifierTest.allTests());
suite.addTest(ImportRewriteTest.allTests());
+ suite.addTest(LineCommentOffsetsTest.allTests());
return suite;
}
Index: src/org/eclipse/jdt/core/tests/rewrite/describing/LineCommentOffsetsTest.java
===================================================================
RCS file: src/org/eclipse/jdt/core/tests/rewrite/describing/LineCommentOffsetsTest.java
diff -N src/org/eclipse/jdt/core/tests/rewrite/describing/LineCommentOffsetsTest.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ src/org/eclipse/jdt/core/tests/rewrite/describing/LineCommentOffsetsTest.java 1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,537 @@
+/*******************************************************************************
+ * Copyright (c) 2000, 2006 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.core.tests.rewrite.describing;
+
+import java.util.HashSet;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+import org.eclipse.jdt.core.ICompilationUnit;
+import org.eclipse.jdt.core.IPackageFragment;
+import org.eclipse.jdt.core.dom.*;
+import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
+import org.eclipse.jdt.core.dom.rewrite.ListRewrite;
+import org.eclipse.jdt.internal.core.dom.rewrite.LineCommentEndOffsets;
+
+/**
+ *
+ */
+public class LineCommentOffsetsTest extends ASTRewritingTest {
+
+ private static final Class THIS= LineCommentOffsetsTest.class;
+
+ public LineCommentOffsetsTest(String name) {
+ super(name);
+ }
+
+ public static Test allTests() {
+ return new Suite(THIS);
+ }
+
+ public static Test setUpTest(Test someTest) {
+ TestSuite suite= new Suite("one test");
+ suite.addTest(someTest);
+ return suite;
+ }
+
+ public static Test suite() {
+ return allTests();
+ }
+
+ public void testEmptyLineComments() throws Exception {
+
+ StringBuffer buf= new StringBuffer();
+ buf.append("\n");
+
+ LineCommentEndOffsets offsets= new LineCommentEndOffsets(null);
+ boolean res= offsets.isEndOfLineComment(0);
+ assertFalse(res);
+ res= offsets.remove(0);
+ assertFalse(res);
+ }
+
+ public void testRemove() throws Exception {
+
+ IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null);
+
+ StringBuffer buf= new StringBuffer();
+ buf.append("package test1;//comment Y\n");
+ buf.append("public class E//comment Y\n");
+ buf.append("{//comment Y\n");
+ buf.append("}//comment Y");
+ String contents= buf.toString();
+ ICompilationUnit cu= pack1.createCompilationUnit("E.java", contents, false, null);
+
+ CompilationUnit astRoot= createAST3(cu);
+
+ LineCommentEndOffsets offsets= new LineCommentEndOffsets(astRoot.getCommentList());
+
+ int p1= contents.indexOf('Y') + 1;
+ int p2= contents.indexOf('Y', p1) + 1;
+ int p3= contents.indexOf('Y', p2) + 1;
+ int p4= contents.indexOf('Y', p3) + 1;
+
+ assertFalse(offsets.isEndOfLineComment(0));
+ assertTrue(offsets.isEndOfLineComment(p1));
+ assertTrue(offsets.isEndOfLineComment(p2));
+ assertTrue(offsets.isEndOfLineComment(p3));
+ assertTrue(offsets.isEndOfLineComment(p4));
+
+ boolean res= offsets.remove(p2);
+ assertTrue(res);
+
+ res= offsets.remove(p2);
+ assertFalse(res);
+
+ assertFalse(offsets.isEndOfLineComment(0));
+ assertTrue(offsets.isEndOfLineComment(p1));
+ assertFalse(offsets.isEndOfLineComment(p2));
+ assertTrue(offsets.isEndOfLineComment(p3));
+ assertTrue(offsets.isEndOfLineComment(p4));
+
+ res= offsets.remove(p4);
+ assertTrue(res);
+
+ assertFalse(offsets.isEndOfLineComment(0));
+ assertTrue(offsets.isEndOfLineComment(p1));
+ assertFalse(offsets.isEndOfLineComment(p2));
+ assertTrue(offsets.isEndOfLineComment(p3));
+ assertFalse(offsets.isEndOfLineComment(p4));
+
+ res= offsets.remove(p1);
+ assertTrue(res);
+
+ assertFalse(offsets.isEndOfLineComment(0));
+ assertFalse(offsets.isEndOfLineComment(p1));
+ assertFalse(offsets.isEndOfLineComment(p2));
+ assertTrue(offsets.isEndOfLineComment(p3));
+ assertFalse(offsets.isEndOfLineComment(p4));
+ }
+
+
+
+ public void testLineCommentEndOffsets() throws Exception {
+ IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null);
+
+
+ StringBuffer buf= new StringBuffer();
+ buf.append("package test1;\n");
+ buf.append("/* comment */\n");
+ buf.append("// comment Y\n");
+ buf.append("public class E {\n");
+ buf.append(" public void foo() {\n");
+ buf.append(" while (i == 0) {\n");
+ buf.append(" foo();\n");
+ buf.append(" i++; // comment Y\n");
+ buf.append(" i++;\n");
+ buf.append(" }// comment// comment Y\n");
+ buf.append(" return;\n");
+ buf.append(" }\n");
+ buf.append("} // comment Y");
+ String content= buf.toString();
+
+ ICompilationUnit cu= pack1.createCompilationUnit("E.java", content, false, null);
+ CompilationUnit astRoot= createAST(cu);
+
+ LineCommentEndOffsets offsets= new LineCommentEndOffsets(astRoot.getCommentList());
+ HashSet expectedOffsets= new HashSet();
+
+ for (int i= 0; i < content.length(); i++) {
+ char ch= content.charAt(i);
+ if (ch == 'Y') {
+ expectedOffsets.add(new Integer(i + 1));
+ }
+ }
+
+ int count= 0;
+
+ char[] charContent= content.toCharArray();
+ for (int i= 0; i <= content.length() + 5; i++) {
+ boolean expected= i > 0 && i <= content.length() && charContent[i - 1] == 'Y';
+ boolean actual= offsets.isEndOfLineComment(i, charContent);
+ assertEquals(expected, actual);
+
+ actual= offsets.isEndOfLineComment(i);
+ assertEquals(expected, actual);
+
+ if (expected) {
+ count++;
+ }
+
+ }
+ assertEquals(4, count);
+ }
+
+ public void testLineCommentEndOffsetsMixedLineDelimiter() throws Exception {
+ IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null);
+
+ StringBuffer buf= new StringBuffer();
+ buf.append("package test1;\n");
+ buf.append("/* comment */\r\n");
+ buf.append("// comment Y\n");
+ buf.append("public class E {\r\n");
+ buf.append(" public void foo() {\n");
+ buf.append(" while (i == 0) {\n");
+ buf.append(" foo();\n");
+ buf.append(" i++; // comment Y\r\n");
+ buf.append(" i++;\n");
+ buf.append(" }// comment// comment Y\r");
+ buf.append(" return;\n");
+ buf.append(" }\r\n");
+ buf.append("} // comment Y");
+ String content= buf.toString();
+
+ ICompilationUnit cu= pack1.createCompilationUnit("E.java", content, false, null);
+ CompilationUnit astRoot= createAST(cu);
+
+ LineCommentEndOffsets offsets= new LineCommentEndOffsets(astRoot.getCommentList());
+ HashSet expectedOffsets= new HashSet();
+
+ for (int i= 0; i < content.length(); i++) {
+ char ch= content.charAt(i);
+ if (ch == 'Y') {
+ expectedOffsets.add(new Integer(i + 1));
+ }
+ }
+
+ int count= 0;
+
+ char[] charContent= content.toCharArray();
+ for (int i= 0; i <= content.length() + 5; i++) {
+ boolean expected= i > 0 && i <= content.length() && charContent[i - 1] == 'Y';
+ boolean actual= offsets.isEndOfLineComment(i, charContent);
+ assertEquals(expected, actual);
+ if (expected) {
+ count++;
+ }
+
+ }
+ assertEquals(4, count);
+ }
+
+ public void testCommentInLists() throws Exception {
+
+ IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null);
+ StringBuffer buf= new StringBuffer();
+ buf.append("package test1;\n");
+ buf.append("public class E implements A //comment\n");
+ buf.append("{\n");
+ buf.append("}\n");
+ ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
+
+ CompilationUnit astRoot= createAST3(cu);
+ ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST());
+
+ AST ast= astRoot.getAST();
+
+ assertTrue("Parse errors", (astRoot.getFlags() & ASTNode.MALFORMED) == 0);
+ TypeDeclaration type= findTypeDeclaration(astRoot, "E");
+
+ ListRewrite listRewrite= rewrite.getListRewrite(type, TypeDeclaration.SUPER_INTERFACE_TYPES_PROPERTY);
+ SimpleType newInterface= ast.newSimpleType(ast.newSimpleName("B"));
+ listRewrite.insertLast(newInterface, null);
+
+ String preview= evaluateRewrite(cu, rewrite);
+
+ buf= new StringBuffer();
+ buf.append("package test1;\n");
+ buf.append("public class E implements A //comment\n");
+ buf.append(", B\n");
+ buf.append("{\n");
+ buf.append("}\n");
+ assertEqualString(preview, buf.toString());
+ }
+
+ public void testCommentInType() throws Exception {
+
+ IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null);
+ StringBuffer buf= new StringBuffer();
+ buf.append("package test1;\n");
+ buf.append("public class E //comment\n");
+ buf.append("{\n");
+ buf.append("}\n");
+ ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
+
+ CompilationUnit astRoot= createAST3(cu);
+ ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST());
+
+ AST ast= astRoot.getAST();
+
+ assertTrue("Parse errors", (astRoot.getFlags() & ASTNode.MALFORMED) == 0);
+ TypeDeclaration type= findTypeDeclaration(astRoot, "E");
+
+ ListRewrite listRewrite= rewrite.getListRewrite(type, TypeDeclaration.SUPER_INTERFACE_TYPES_PROPERTY);
+ SimpleType newInterface= ast.newSimpleType(ast.newSimpleName("B"));
+ listRewrite.insertLast(newInterface, null);
+
+ String preview= evaluateRewrite(cu, rewrite);
+
+ buf= new StringBuffer();
+ buf.append("package test1;\n");
+ buf.append("public class E //comment\n");
+ buf.append(" implements B\n");
+ buf.append("{\n");
+ buf.append("}\n");
+ assertEqualString(preview, buf.toString());
+ }
+
+ public void testBug103340() throws Exception {
+
+ IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null);
+ StringBuffer buf= new StringBuffer();
+ buf.append("package test1;\n");
+ buf.append("public class E //implements List\n");
+ buf.append("{\n");
+ buf.append("}\n");
+ ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
+
+ CompilationUnit astRoot= createAST3(cu);
+ ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST());
+
+ AST ast= astRoot.getAST();
+
+ assertTrue("Parse errors", (astRoot.getFlags() & ASTNode.MALFORMED) == 0);
+ TypeDeclaration type= findTypeDeclaration(astRoot, "E");
+
+ ListRewrite listRewrite= rewrite.getListRewrite(type, TypeDeclaration.TYPE_PARAMETERS_PROPERTY);
+ TypeParameter newType= ast.newTypeParameter();
+ newType.setName(ast.newSimpleName("X"));
+ listRewrite.insertLast(newType, null);
+
+ String preview= evaluateRewrite(cu, rewrite);
+
+ buf= new StringBuffer();
+ buf.append("package test1;\n");
+ buf.append("public class E //implements List\n");
+ buf.append("\n");
+ buf.append("{\n");
+ buf.append("}\n");
+ assertEqualString(preview, buf.toString());
+ }
+
+ public void testBug95839() throws Exception {
+
+ IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null);
+ StringBuffer buf= new StringBuffer();
+ buf.append("package test1;\n");
+ buf.append("public class E {\n");
+ buf.append(" void foo() {\n");
+ buf.append(" object.method(\n");
+ buf.append(" param1, // text about param1\n");
+ buf.append(" param2 // text about param2\n");
+ buf.append(" );\n");
+ buf.append(" }\n");
+ buf.append("}\n");
+ ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
+
+ CompilationUnit astRoot= createAST3(cu);
+ ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST());
+
+ AST ast= astRoot.getAST();
+
+ assertTrue("Parse errors", (astRoot.getFlags() & ASTNode.MALFORMED) == 0);
+ TypeDeclaration type= findTypeDeclaration(astRoot, "E");
+ ExpressionStatement statement= (ExpressionStatement) ((MethodDeclaration) type.bodyDeclarations().get(0)).getBody().statements().get(0);
+ MethodInvocation inv= (MethodInvocation) statement.getExpression();
+
+ ListRewrite listRewrite= rewrite.getListRewrite(inv, MethodInvocation.ARGUMENTS_PROPERTY);
+ listRewrite.insertLast(ast.newSimpleName("param3"), null);
+
+ String preview= evaluateRewrite(cu, rewrite);
+
+ buf= new StringBuffer();
+ buf.append("package test1;\n");
+ buf.append("public class E {\n");
+ buf.append(" void foo() {\n");
+ buf.append(" object.method(\n");
+ buf.append(" param1, // text about param1\n");
+ buf.append(" param2 // text about param2\n");
+ buf.append(", param3\n");
+ buf.append(" );\n");
+ buf.append(" }\n");
+ buf.append("}\n");
+ assertEqualString(preview, buf.toString());
+ }
+
+ public void testBug114418() throws Exception {
+
+ IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null);
+ StringBuffer buf= new StringBuffer();
+ buf.append("package test1;\n");
+ buf.append("public class E {\n");
+ buf.append(" void foo() {\n");
+ buf.append(" try {\n");
+ buf.append(" } catch (IOException e) {\n");
+ buf.append(" }\n");
+ buf.append(" // comment\n");
+ buf.append(" }\n");
+ buf.append("}\n");
+ ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
+
+ CompilationUnit astRoot= createAST3(cu);
+ ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST());
+
+ AST ast= astRoot.getAST();
+
+ assertTrue("Parse errors", (astRoot.getFlags() & ASTNode.MALFORMED) == 0);
+ TypeDeclaration type= findTypeDeclaration(astRoot, "E");
+ TryStatement statement= (TryStatement) ((MethodDeclaration) type.bodyDeclarations().get(0)).getBody().statements().get(0);
+
+ ListRewrite listRewrite= rewrite.getListRewrite(statement, TryStatement.CATCH_CLAUSES_PROPERTY);
+ CatchClause clause= ast.newCatchClause();
+ SingleVariableDeclaration newSingleVariableDeclaration= ast.newSingleVariableDeclaration();
+ newSingleVariableDeclaration.setName(ast.newSimpleName("e"));
+ newSingleVariableDeclaration.setType(ast.newSimpleType(ast.newSimpleName("MyException")));
+ clause.setException(newSingleVariableDeclaration);
+
+ listRewrite.insertLast(clause, null);
+
+ String preview= evaluateRewrite(cu, rewrite);
+
+ buf= new StringBuffer();
+ buf.append("package test1;\n");
+ buf.append("public class E {\n");
+ buf.append(" void foo() {\n");
+ buf.append(" try {\n");
+ buf.append(" } catch (IOException e) {\n");
+ buf.append(" }\n");
+ buf.append(" // comment\n");
+ buf.append(" catch (MyException e) {\n");
+ buf.append(" }\n");
+ buf.append(" }\n");
+ buf.append("}\n");
+ assertEqualString(preview, buf.toString());
+ }
+
+ public void testBug128818() throws Exception {
+
+ IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null);
+ StringBuffer buf= new StringBuffer();
+ buf.append("package test1;\n");
+ buf.append("public class E {\n");
+ buf.append(" void foo() {\n");
+ buf.append(" if (true) {\n");
+ buf.append(" } // comment\n");
+ buf.append(" else\n");
+ buf.append(" return;\n");
+ buf.append(" }\n");
+ buf.append("}\n");
+ ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
+
+ CompilationUnit astRoot= createAST3(cu);
+ ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST());
+
+ AST ast= astRoot.getAST();
+
+ assertTrue("Parse errors", (astRoot.getFlags() & ASTNode.MALFORMED) == 0);
+ TypeDeclaration type= findTypeDeclaration(astRoot, "E");
+ IfStatement statement= (IfStatement) ((MethodDeclaration) type.bodyDeclarations().get(0)).getBody().statements().get(0);
+
+ rewrite.set(statement, IfStatement.ELSE_STATEMENT_PROPERTY, ast.newBlock(), null);
+
+ String preview= evaluateRewrite(cu, rewrite);
+
+ buf= new StringBuffer();
+ buf.append("package test1;\n");
+ buf.append("public class E {\n");
+ buf.append(" void foo() {\n");
+ buf.append(" if (true) {\n");
+ buf.append(" } // comment\n");
+ buf.append(" else {\n");
+ buf.append(" }\n");
+ buf.append(" }\n");
+ buf.append("}\n");
+ assertEqualString(preview, buf.toString());
+ }
+
+ /* not yet working
+ public void testBug128422() throws Exception {
+
+ IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null);
+ StringBuffer buf= new StringBuffer();
+ buf.append("package test1;\n");
+ buf.append("public class E {\n");
+ buf.append(" void foo() {\n");
+ buf.append(" if (i != 0 //I don't like 0\n");
+ buf.append(" && i != 10) {\n");
+ buf.append(" }\n");
+ buf.append(" }\n");
+ buf.append("}\n");
+ ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
+
+ CompilationUnit astRoot= createAST3(cu);
+ ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST());
+
+ AST ast= astRoot.getAST();
+
+ assertTrue("Parse errors", (astRoot.getFlags() & ASTNode.MALFORMED) == 0);
+ TypeDeclaration type= findTypeDeclaration(astRoot, "E");
+ IfStatement statement= (IfStatement) ((MethodDeclaration) type.bodyDeclarations().get(0)).getBody().statements().get(0);
+ Expression expression= ((InfixExpression) statement.getExpression()).getLeftOperand();
+
+ ParenthesizedExpression parenthesizedExpression= ast.newParenthesizedExpression();
+ parenthesizedExpression.setExpression( (Expression) rewrite.createCopyTarget(expression));
+ rewrite.replace(expression, parenthesizedExpression, null);
+
+ String preview= evaluateRewrite(cu, rewrite);
+
+ buf= new StringBuffer();
+ buf.append("package test1;\n");
+ buf.append("public class E {\n");
+ buf.append(" void foo() {\n");
+ buf.append(" if ((i != 0 //I don't like 0\n");
+ buf.append(")\n");
+ buf.append(" && i != 10) {\n");
+ buf.append(" }\n");
+ buf.append(" }\n");
+ buf.append("}\n");
+ assertEqualString(preview, buf.toString());
+ }*/
+
+ public void testCommentAtEnd() throws Exception {
+
+ IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null);
+ StringBuffer buf= new StringBuffer();
+ buf.append("package test1;\n");
+ buf.append("public class E \n");
+ buf.append("{\n");
+ buf.append("}//comment");
+ ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
+
+ CompilationUnit astRoot= createAST3(cu);
+ ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST());
+
+ AST ast= astRoot.getAST();
+
+ assertTrue("Parse errors", (astRoot.getFlags() & ASTNode.MALFORMED) == 0);
+
+ ListRewrite listRewrite= rewrite.getListRewrite(astRoot, CompilationUnit.TYPES_PROPERTY);
+ TypeDeclaration newType= ast.newTypeDeclaration();
+ newType.setName(ast.newSimpleName("B"));
+ listRewrite.insertLast(newType, null);
+
+ String preview= evaluateRewrite(cu, rewrite);
+
+ buf= new StringBuffer();
+ buf.append("package test1;\n");
+ buf.append("public class E \n");
+ buf.append("{\n");
+ buf.append("}//comment\n");
+ buf.append("\n");
+ buf.append("class B {\n");
+ buf.append("}");
+ assertEqualString(preview, buf.toString());
+ }
+
+
+
+}