| Why the code didn't be modified? [message #254300] | 
Fri, 27 June 2008 07:48   | 
 
Eclipse User  | 
 | 
 | 
   | 
 
Originally posted by: upupsky.gmail.com 
 
Hi, all, 
I want to insert a statement into every IfStatement, For example, the  
original code is like: 
 
public class Test{  
 public void test{  
  if(true){ 
    String str=""; 
   }  
 }  
} 
 
 
After insertion, the modified code is like: 
public class Test{  
 public void test{  
  if(true){ 
    System.out.println("Hello"+" world"); 
    String str=""; 
   }  
 }  
} 
 
I want to know why the code is not modified after calling modify(Block); 
and what should I do to save the modification to file? 
 
Below is my code: 
--------------------------------------------------- 
package change; 
 
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.jface.text.*; 
import org.eclipse.text.edits.MalformedTreeException; 
import org.eclipse.text.edits.TextEdit; 
import org.eclipse.text.edits.UndoEdit; 
 
/** 
 * This class insert a statement into the source code. For example, the  
original 
 * code is like: 
 * <p> 
 * public class Test{ public void test{ if(true){String str="";} } } 
 * </p> 
 *  
 * After insertion, the modified code is like: 
 * <p> 
 * public class Test{ public void test{ System.out.println("Hello"+"  
world"); 
 * if(true){String str="";} } } 
 * </p> 
 *  
 * @author Zhang Yu 
 */ 
public class Inserter { 
	private ASTParser parser; 
	private Document doc = null; 
	private CompilationUnit unit = null; 
 
	/** 
	 * Creates a new Inserter instance. 
	 * <p> 
	 *  
	 * </p> 
	 *  
	 * @param content 
	 *            the source code to handle 
	 */ 
	public Inserter(String content) { 
		parser = ASTParser.newParser(AST.JLS3); 
		parser.setSource(content.toCharArray()); 
		parser.setKind(ASTParser.K_COMPILATION_UNIT); 
		doc = new Document(content); 
		unit = (CompilationUnit) parser.createAST(null); 
		unit.recordModifications(); 
	} 
 
	/** 
	 * Visits the CompilationUnit. 
	 * <p> 
	 * Find every IfStatement and then change it 
	 * </p> 
	 */ 
	public void visit() { 
		unit.accept(new ASTVisitor() { 
			public boolean visit(IfStatement node) { 
				Statement s = node.getThenStatement(); 
				if (s instanceof Block) 
					modify((Block) s); 
				return false; 
			} 
		}); 
	} 
 
	/** 
	 * Creates a new expression statement node owned by this AST 
	 *  
	 * @param ast 
	 * @return ExpressionStatement System.out.println("Hello"+ "world"). 
	 */ 
	public Statement createStmt(AST ast) { 
		MethodInvocation methodInvocation = ast.newMethodInvocation(); 
		QualifiedName name = ast.newQualifiedName(ast.newSimpleName("System"), 
				ast.newSimpleName("out")); 
		methodInvocation.setExpression(name); 
		methodInvocation.setName(ast.newSimpleName("println")); 
		InfixExpression infixExpression = ast.newInfixExpression(); 
		infixExpression.setOperator(InfixExpression.Operator.PLUS); 
		StringLiteral literal = ast.newStringLiteral(); 
		literal.setLiteralValue("Hello"); 
		infixExpression.setLeftOperand(literal); 
		literal = ast.newStringLiteral(); 
		literal.setLiteralValue(" world"); 
		infixExpression.setRightOperand(literal); 
		methodInvocation.arguments().add(infixExpression); 
		ExpressionStatement expressionStatement = ast 
				.newExpressionStatement(methodInvocation); 
		return expressionStatement; 
	} 
 
	/** 
	 * Change the block by inserting an Statement 
	 *  
	 * @param block 
	 *            the block to modify 
	 */ 
	public void modify(Block block) { 
		AST ast = block.getAST(); 
		ASTRewrite rewrite = ASTRewrite.create(unit.getAST()); 
		Statement newStmt = createStmt(ast); 
		ListRewrite statementsListRewrite = rewrite.getListRewrite(block, 
				Block.STATEMENTS_PROPERTY); 
		int index = 0; 
		statementsListRewrite.insertAt(newStmt, index, null); 
		String code = applyModify(); 
		System.out.println("Code after modification:\n" + code); 
	} 
 
	/** 
	 * Apply the change and return the new source code 
	 *  
	 * @return the modified source code 
	 */ 
	public String applyModify() { 
		// Apply the rewrite, and get the output 
		TextEdit edits = unit.rewrite(doc, null); 
		try { 
			UndoEdit undo = edits.apply(doc); 
		} catch (MalformedTreeException e) { 
			e.printStackTrace(); 
		} catch (BadLocationException e) { 
			e.printStackTrace(); 
		} 
		String code = doc.get(); 
		save(); 
		return code; 
	} 
 
	/** 
	 * Save the modified source code 
	 *  
	 * <p> 
	 * What should I do to save the modification to file 
	 * </p> 
	 */ 
	public void save() { 
	} 
 
	public static void main(String[] args) { 
		String CODE_START = "public class Test {\n public void Test(){\n"; 
		String CODE_END = "\n }\n}"; 
		String CODE = "    if(true){String str=\"\";}"; 
		String content = CODE_START + CODE + CODE_END; 
		System.out.println("The original code:\n" + content); 
		Inserter jp = new Inserter(content); 
		jp.visit(); 
	} 
}
 |  
 |  
  | 
| Re: Why the code didn't be modified? [message #254443 is a reply to message #254300] | 
Tue, 01 July 2008 09:18   | 
 
Eclipse User  | 
 | 
 | 
   | 
 
Originally posted by: upupsky.gmail.com 
 
Let me reply to myself. :) 
 
I found out that if the applyModify() is like below, then the code would  
be modified: 
 
TextEdit textEdits = rewrite.rewriteAST(doc, null); 
try { 
	textEdits.apply(doc); 
} catch (MalformedTreeException e) { 
	e.printStackTrace(); 
} catch (BadLocationException e) { 
	e.printStackTrace(); 
} 
 
String code = doc.get(); 
System.out.println("Code after modification:\n" + code); 
 
Although I can't see the difference, but it works.
 |  
 |  
  | 
Powered by 
FUDForum. Page generated in 0.04901 seconds