diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/replace/ReplaceInsertStatementTest.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/replace/ReplaceInsertStatementTest.java new file mode 100644 index 00000000000..a5c85e6926b --- /dev/null +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/replace/ReplaceInsertStatementTest.java @@ -0,0 +1,101 @@ +/******************************************************************************* + * Copyright (c) 2011 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: + * Institute for Software - 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.IASTBinaryExpression; +import org.eclipse.cdt.core.dom.ast.IASTCompoundStatement; +import org.eclipse.cdt.core.dom.ast.IASTExpressionStatement; +import org.eclipse.cdt.core.dom.ast.IASTIdExpression; +import org.eclipse.cdt.core.dom.ast.IASTLiteralExpression; +import org.eclipse.cdt.core.dom.ast.IASTStatement; +import org.eclipse.cdt.core.dom.ast.IASTUnaryExpression; +import org.eclipse.cdt.core.dom.ast.cpp.CPPASTVisitor; +import org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.ChangeGeneratorTest; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTBinaryExpression; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTExpressionStatement; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTIdExpression; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTLiteralExpression; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTName; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTUnaryExpression; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModificationStore; + +public class ReplaceInsertStatementTest extends ChangeGeneratorTest { + + public ReplaceInsertStatementTest() { + super("Replace Insert Statement Test"); + } + + @Override + protected void setUp() throws Exception { + source = "void main(){\r\n int i = 0;\r\n ++i;\r\n}"; //$NON-NLS-1$ + expectedSource = "void main(){\r\n int i = 0;\r\n i = 42;\r\n i++;\r\n}"; //$NON-NLS-1$ + super.setUp(); + } + + public static Test suite() { + return new ReplaceInsertStatementTest(); + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.ChangeGeneratorTest#createModificator(org.eclipse.cdt.internal.core.dom.rewrite.ASTModificationStore) + */ + @Override + protected CPPASTVisitor createModificator( + final ASTModificationStore modStore) { + return new CPPASTVisitor() { + { + shouldVisitStatements = true; + } + + + @Override + public int visit(IASTStatement statement) { + if (statement instanceof IASTCompoundStatement) { + IASTCompoundStatement compStmt = (IASTCompoundStatement) statement; + IASTStatement stmt = compStmt.getStatements()[1]; + + IASTIdExpression id = new CPPASTIdExpression( + new CPPASTName("i".toCharArray())); + IASTLiteralExpression value = new CPPASTLiteralExpression( + IASTLiteralExpression.lk_integer_constant, "42"); + IASTExpressionStatement insertStmt = new CPPASTExpressionStatement( + new CPPASTBinaryExpression( + IASTBinaryExpression.op_assign, id, value)); + + IASTIdExpression incId = new CPPASTIdExpression( + new CPPASTName("i".toCharArray())); + IASTUnaryExpression incExp = new CPPASTUnaryExpression( + IASTUnaryExpression.op_postFixIncr, incId); + IASTExpressionStatement replaceStatement = new CPPASTExpressionStatement( + incExp); + + ASTModification replaceMod = new ASTModification( + ASTModification.ModificationKind.REPLACE, stmt, + replaceStatement, null); + modStore.storeModification(null, replaceMod); + + ASTModification insertMod = new ASTModification( + ASTModification.ModificationKind.INSERT_BEFORE, + stmt, insertStmt, null); + modStore.storeModification(null, insertMod); + + } + + return PROCESS_CONTINUE; + } + }; + } + +} 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 6c4f52b4d4e..a2a46caf25e 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 @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik + * Copyright (c) 2008, 2011 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 @@ -36,6 +36,7 @@ public class ReplaceTestSuite{ suite.addTest(ArraySizeExpressionTest.suite()); suite.addTest(NewInitializerExpressionTest.suite()); suite.addTest(StatementTest.suite()); + suite.addTest(ReplaceInsertStatementTest.suite()); return suite; } } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/changegenerator/ASTModificationHelper.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/changegenerator/ASTModificationHelper.java index 568c4c68448..8879b1442e4 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/changegenerator/ASTModificationHelper.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/changegenerator/ASTModificationHelper.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik + * Copyright (c) 2008, 2011 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 @@ -35,26 +35,6 @@ public class ASTModificationHelper { public T[] createModifiedChildArray(IASTNode parent, T[] unmodifiedChildren, Class clazz){ ArrayList modifiedChildren = new ArrayList(Arrays.asList(unmodifiedChildren)); - for(T currentChild : unmodifiedChildren){ - for(ASTModification childModification : modificationsForNode(currentChild)){ - try{ - final T newNode = cast(childModification.getNewNode(), clazz); - switch(childModification.getKind()){ - case REPLACE: - if (newNode != null) { - modifiedChildren.add(modifiedChildren.indexOf(childModification.getTargetNode()), newNode); - } - modifiedChildren.remove(childModification.getTargetNode()); - break; - case INSERT_BEFORE: - case APPEND_CHILD: - throw new UnhandledASTModificationException(childModification); - } - }catch(ClassCastException e){ - throw new UnhandledASTModificationException(childModification); - } - } - } for(ASTModification parentModification : modificationsForNode(parent)){ switch(parentModification.getKind()){ @@ -91,6 +71,28 @@ public class ASTModificationHelper { } } + for (T currentChild : unmodifiedChildren) { + for (ASTModification childModification : modificationsForNode(currentChild)) { + try { + final T newNode = cast(childModification.getNewNode(), clazz); + switch (childModification.getKind()) { + case REPLACE: + if (newNode != null) { + modifiedChildren.add( + modifiedChildren.indexOf(childModification.getTargetNode()), + newNode); + } + modifiedChildren.remove(childModification.getTargetNode()); + break; + case INSERT_BEFORE: + case APPEND_CHILD: + throw new UnhandledASTModificationException(childModification); + } + } catch (ClassCastException e) { + throw new UnhandledASTModificationException(childModification); + } + } + } return modifiedChildren.toArray(newArrayInstance(clazz, modifiedChildren.size())); }