Download
Getting Started
Members
Projects
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
More
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
Toggle navigation
Bugzilla – Attachment 164583 Details for
Bug 308886
CompilationUnit.rewrite produces wrong result/
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Requests
|
Help
|
Log In
[x]
|
Terms of Use
|
Copyright Agent
Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read
this important communication.
JUnit Test to reproduce the bug
IfElseFailureTest.java (text/plain), 4.04 KB, created by
David Schuler
on 2010-04-12 12:22:57 EDT
(
hide
)
Description:
JUnit Test to reproduce the bug
Filename:
MIME Type:
Creator:
David Schuler
Created:
2010-04-12 12:22:57 EDT
Size:
4.04 KB
patch
obsolete
>package test; > >import static org.junit.Assert.*; > >import java.util.ArrayList; >import java.util.List; > >import org.eclipse.core.runtime.IProgressMonitor; >import org.eclipse.jdt.core.dom.AST; >import org.eclipse.jdt.core.dom.ASTNode; >import org.eclipse.jdt.core.dom.ASTParser; >import org.eclipse.jdt.core.dom.ASTVisitor; >import org.eclipse.jdt.core.dom.Block; >import org.eclipse.jdt.core.dom.CompilationUnit; >import org.eclipse.jdt.core.dom.ExpressionStatement; >import org.eclipse.jdt.core.dom.IfStatement; >import org.eclipse.jdt.core.dom.MethodInvocation; >import org.eclipse.jdt.core.dom.QualifiedName; >import org.eclipse.jdt.core.dom.Statement; >import org.eclipse.jdt.core.dom.StringLiteral; >import org.eclipse.jface.text.BadLocationException; >import org.eclipse.jface.text.Document; >import org.eclipse.text.edits.MalformedTreeException; >import org.eclipse.text.edits.TextEdit; >import org.junit.Test; > >public class IfElseFailureTest { > > public static class MyASTVisitor extends ASTVisitor { > > @Override > public boolean visit(IfStatement node) { > Statement thenStatement = node.getThenStatement(); > Statement elseStatement = node.getElseStatement(); > if (elseStatement != null) { > Block transformToBlock = transformToBlock(node, elseStatement); > node.setElseStatement(transformToBlock); > } > if (!(thenStatement instanceof Block)) { > Block block = transformToBlock(node, thenStatement); > node.setThenStatement(block); > } > return super.visit(node); > } > > @SuppressWarnings("unchecked") > private Block transformToBlock(Statement parent, Statement child) { > AST ast = parent.getAST(); > Block block = ast.newBlock(); > ASTNode copySubtree = ASTNode.copySubtree(block.getAST(), child); > block.statements().add(copySubtree); > return block; > } > > @Override > public boolean visit(Block node) { > addCoverageCalls(node.statements(), node); > return true; > } > > @SuppressWarnings("unchecked") > private void addCoverageCalls(List statements, ASTNode node) { > List<Statement> statementsCopy = new ArrayList<Statement>( > statements); > int count = 0; > for (Statement o : statementsCopy) { > ExpressionStatement coverageCalls = getCoverageCalls(node); > statements.add(count, coverageCalls); > count++; > count++; > } > } > > @SuppressWarnings("unchecked") > private ExpressionStatement getCoverageCalls(ASTNode node) { > AST ast = node.getAST(); > final MethodInvocation methodInvocation = ast.newMethodInvocation(); > QualifiedName name = ast.newQualifiedName(ast > .newSimpleName("System"), ast.newSimpleName("out")); > methodInvocation.setExpression(name); > methodInvocation.setName(ast.newSimpleName("println")); > StringLiteral classNameLiteral = ast.newStringLiteral(); > classNameLiteral.setLiteralValue("TEST"); > methodInvocation.arguments().add(classNameLiteral); > return ast.newExpressionStatement(methodInvocation); > } > > } > > @Test > public void test1() throws MalformedTreeException, BadLocationException { > ASTParser parser = ASTParser.newParser(AST.JLS3); > parser.setKind(ASTParser.K_COMPILATION_UNIT); > String source = getSource(); > parser.setSource(source.toCharArray()); > parser.setResolveBindings(false); > final CompilationUnit compU = (CompilationUnit) parser > .createAST((IProgressMonitor) null); > final Document doc = new Document(source); > ASTVisitor visitor = new MyASTVisitor(); > compU.recordModifications(); > compU.accept(visitor); > TextEdit rewrite = compU.rewrite(doc, null); > rewrite.apply(doc); > String transformed = doc.get(); > String rewriteString = transformed.toString(); > int rewriteCount = rewriteString.split("TEST").length - 1; > // Expecting 5 occurrences of TEST in the resulting source code. > assertEquals(5, rewriteCount); > String compilationUnitString = compU.toString(); > int compilationUnitCount = compilationUnitString.split("TEST").length - 1; > assertEquals(5, compilationUnitCount); > } > > private String getSource() { > > return "public class ElseIfNoBrace {" > + "public int method1(int x) {" > + " if (x > 5)" > + " x *= 3;" > + " else if (x < 0)" > + " x += x;" > + " return x;" > + "}"; > } >}
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Raw
Actions:
View
Attachments on
bug 308886
: 164583