KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > dom > StatementRewrite


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.internal.corext.dom;
12
13 import org.eclipse.text.edits.TextEditGroup;
14
15 import org.eclipse.jdt.core.dom.AST;
16 import org.eclipse.jdt.core.dom.ASTNode;
17 import org.eclipse.jdt.core.dom.Block;
18 import org.eclipse.jdt.core.dom.ChildListPropertyDescriptor;
19 import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
20 import org.eclipse.jdt.core.dom.rewrite.ListRewrite;
21
22
23 public class StatementRewrite extends ReplaceRewrite {
24
25     public StatementRewrite(ASTRewrite rewrite, ASTNode[] nodes) {
26         super(rewrite, nodes);
27     }
28     
29     protected void handleOneMany(ASTNode[] replacements, TextEditGroup description) {
30         AST ast= fToReplace[0].getAST();
31         // to replace == 1, but more than one replacement. Have to check if we
32
// need to insert a block to not change structure
33
if (ASTNodes.isControlStatementBody(fDescriptor)) {
34             Block block= ast.newBlock();
35             ListRewrite statements= fRewrite.getListRewrite(block, Block.STATEMENTS_PROPERTY);
36             for (int i= 0; i < replacements.length; i++) {
37                 statements.insertLast(replacements[i], description);
38             }
39             fRewrite.replace(fToReplace[0], block, description);
40         } else {
41             ListRewrite container= fRewrite.getListRewrite(fToReplace[0].getParent(), (ChildListPropertyDescriptor)fDescriptor);
42             container.replace(fToReplace[0], replacements[0], description);
43             for (int i= 1; i < replacements.length; i++) {
44                 container.insertAfter(replacements[i], replacements[i - 1], description);
45             }
46         }
47     }
48 }
49
Popular Tags