Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Corbat2014-08-15 11:41:03 +0000
committerThomas Corbat2014-08-18 06:46:56 +0000
commitec2fe61a040dba9241887e89d5fab88f001de3c9 (patch)
treef077ab5d0a86e4640c339f0b78946818e7c3ae7d
parent05d86fc1d150b343dceb35c4eb3ec45a714c3d4a (diff)
downloadorg.eclipse.cdt-ec2fe61a040dba9241887e89d5fab88f001de3c9.tar.gz
org.eclipse.cdt-ec2fe61a040dba9241887e89d5fab88f001de3c9.tar.xz
org.eclipse.cdt-ec2fe61a040dba9241887e89d5fab88f001de3c9.zip
Bug 441859 - [ASTRewrite] Replacing Replaced Node Throws IllegalArgEx
Changed ASTRewrite to also accept the root node as valid target for replacement. Added a test to show the resulting modification store setup works as expected. Change-Id: I471d4399690f3a5bb7dcddca45b3f848826b696c Signed-off-by: Thomas Corbat <tcorbat@hsr.ch> Reviewed-on: https://git.eclipse.org/r/31747 Tested-by: Hudson CI Reviewed-by: Sergey Prigogin <eclipse.sprigogin@gmail.com>
-rw-r--r--core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/replace/ReplaceReplacedNodeTest.java69
-rw-r--r--core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/replace/ReplaceTestSuite.java1
-rw-r--r--core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/rewrite/ASTRewrite.java6
3 files changed, 72 insertions, 4 deletions
diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/replace/ReplaceReplacedNodeTest.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/replace/ReplaceReplacedNodeTest.java
new file mode 100644
index 00000000000..77cbdf8b62b
--- /dev/null
+++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/replace/ReplaceReplacedNodeTest.java
@@ -0,0 +1,69 @@
+/*******************************************************************************
+ * Copyright (c) 2014 Institute for Software, HSR Hochschule fuer Technik
+ * Rapperswil, University of applied sciences 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:
+ * Thomas Corbat (IFS) - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.replace;
+
+import junit.framework.Test;
+
+import org.eclipse.cdt.core.dom.ast.ASTVisitor;
+import org.eclipse.cdt.core.dom.ast.IASTName;
+import org.eclipse.cdt.core.dom.ast.INodeFactory;
+import org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.ChangeGeneratorTest;
+import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification;
+import org.eclipse.cdt.internal.core.dom.rewrite.ASTModificationStore;
+
+import static org.eclipse.cdt.internal.core.dom.rewrite.ASTModification.ModificationKind.*;
+
+public class ReplaceReplacedNodeTest extends ChangeGeneratorTest {
+
+ public ReplaceReplacedNodeTest() {
+ super("ReplaceReplacedNodeTest"); //$NON-NLS-1$
+ }
+
+ @Override
+ protected void setUp() throws Exception {
+ source =
+ "void foo() {\n" +
+ "}";
+ expectedSource =
+ "void bar() {\n" +
+ "}";
+ super.setUp();
+ }
+
+ public static Test suite() {
+ return new ReplaceReplacedNodeTest();
+ }
+
+ @Override
+ protected ASTVisitor createModificator(final ASTModificationStore modStore) {
+ return new ASTVisitor() {
+ {
+ shouldVisitNames = true;
+ }
+
+ @Override
+ public int visit(IASTName name) {
+ INodeFactory factory = name.getTranslationUnit().getASTNodeFactory();
+
+ IASTName intermediateName = factory.newName("intermediate".toCharArray());
+ ASTModification replaceMod = new ASTModification(REPLACE, name, intermediateName, null);
+ modStore.storeModification(null, replaceMod);
+
+ IASTName finalName = factory.newName("bar".toCharArray());
+ ASTModification replaceReplacementMod = new ASTModification(REPLACE, intermediateName, finalName, null);
+ modStore.storeModification(replaceMod, replaceReplacementMod);
+
+ return PROCESS_ABORT;
+ }
+ };
+ }
+}
diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/replace/ReplaceTestSuite.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/replace/ReplaceTestSuite.java
index 970876bf935..a5c6da18c0b 100644
--- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/replace/ReplaceTestSuite.java
+++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/replace/ReplaceTestSuite.java
@@ -38,6 +38,7 @@ public class ReplaceTestSuite {
suite.addTest(PointerInParameterTest.suite());
suite.addTest(ReplaceForLoopBodyTest.suite());
suite.addTest(ReplaceInsertStatementTest.suite());
+ suite.addTest(ReplaceReplacedNodeTest.suite());
suite.addTest(SameNameTest.suite());
suite.addTest(StatementTest.suite());
suite.addTest(WhitespaceHandlingTest.suite());
diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/rewrite/ASTRewrite.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/rewrite/ASTRewrite.java
index d785fd422e2..a508ddd4672 100644
--- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/rewrite/ASTRewrite.java
+++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/rewrite/ASTRewrite.java
@@ -169,9 +169,7 @@ public final class ASTRewrite {
*/
public final ASTRewrite insertBefore(IASTNode parent, IASTNode insertionPoint, IASTNode newNode,
TextEditGroup editGroup) {
- if (parent != fRoot) {
- checkBelongsToAST(parent);
- }
+ checkBelongsToAST(parent);
if (newNode == null) {
throw new IllegalArgumentException();
}
@@ -217,10 +215,10 @@ public final class ASTRewrite {
private void checkBelongsToAST(IASTNode node) {
while (node != null) {
- node= node.getParent();
if (node == fRoot) {
return;
}
+ node = node.getParent();
}
throw new IllegalArgumentException();
}

Back to the top