mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-06-07 09:46:02 +02:00
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>
This commit is contained in:
parent
05d86fc1d1
commit
ec2fe61a04
3 changed files with 72 additions and 4 deletions
|
@ -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;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
|
@ -38,6 +38,7 @@ public class ReplaceTestSuite {
|
||||||
suite.addTest(PointerInParameterTest.suite());
|
suite.addTest(PointerInParameterTest.suite());
|
||||||
suite.addTest(ReplaceForLoopBodyTest.suite());
|
suite.addTest(ReplaceForLoopBodyTest.suite());
|
||||||
suite.addTest(ReplaceInsertStatementTest.suite());
|
suite.addTest(ReplaceInsertStatementTest.suite());
|
||||||
|
suite.addTest(ReplaceReplacedNodeTest.suite());
|
||||||
suite.addTest(SameNameTest.suite());
|
suite.addTest(SameNameTest.suite());
|
||||||
suite.addTest(StatementTest.suite());
|
suite.addTest(StatementTest.suite());
|
||||||
suite.addTest(WhitespaceHandlingTest.suite());
|
suite.addTest(WhitespaceHandlingTest.suite());
|
||||||
|
|
|
@ -169,9 +169,7 @@ public final class ASTRewrite {
|
||||||
*/
|
*/
|
||||||
public final ASTRewrite insertBefore(IASTNode parent, IASTNode insertionPoint, IASTNode newNode,
|
public final ASTRewrite insertBefore(IASTNode parent, IASTNode insertionPoint, IASTNode newNode,
|
||||||
TextEditGroup editGroup) {
|
TextEditGroup editGroup) {
|
||||||
if (parent != fRoot) {
|
checkBelongsToAST(parent);
|
||||||
checkBelongsToAST(parent);
|
|
||||||
}
|
|
||||||
if (newNode == null) {
|
if (newNode == null) {
|
||||||
throw new IllegalArgumentException();
|
throw new IllegalArgumentException();
|
||||||
}
|
}
|
||||||
|
@ -217,10 +215,10 @@ public final class ASTRewrite {
|
||||||
|
|
||||||
private void checkBelongsToAST(IASTNode node) {
|
private void checkBelongsToAST(IASTNode node) {
|
||||||
while (node != null) {
|
while (node != null) {
|
||||||
node= node.getParent();
|
|
||||||
if (node == fRoot) {
|
if (node == fRoot) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
node = node.getParent();
|
||||||
}
|
}
|
||||||
throw new IllegalArgumentException();
|
throw new IllegalArgumentException();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue