Lines 3060-3065
Link Here
|
3060 |
|
3060 |
|
3061 |
} |
3061 |
} |
3062 |
|
3062 |
|
|
|
3063 |
public void testIfStatementReplaceElse5() throws Exception { |
3064 |
IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null); |
3065 |
StringBuffer buf= new StringBuffer(); |
3066 |
buf.append("package test1;\n"); |
3067 |
buf.append("public class E {\n"); |
3068 |
buf.append(" public void foo(int i) {\n"); |
3069 |
buf.append(" if (i == 0) {\n"); |
3070 |
buf.append(" System.beep();\n"); |
3071 |
buf.append(" } else {\n"); |
3072 |
buf.append(" System.beep();\n"); |
3073 |
buf.append(" System.beep();\n"); |
3074 |
buf.append(" }\n"); |
3075 |
buf.append(" // comment\n"); |
3076 |
buf.append(" }\n"); |
3077 |
buf.append("}\n"); |
3078 |
ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null); |
3079 |
|
3080 |
CompilationUnit astRoot= createAST(cu); |
3081 |
AST ast= astRoot.getAST(); |
3082 |
ASTRewrite rewrite= ASTRewrite.create(ast); |
3083 |
|
3084 |
assertTrue("Parse errors", (astRoot.getFlags() & ASTNode.MALFORMED) == 0); |
3085 |
TypeDeclaration type= findTypeDeclaration(astRoot, "E"); |
3086 |
MethodDeclaration methodDecl= findMethodDeclaration(type, "foo"); |
3087 |
Block block= methodDecl.getBody(); |
3088 |
List statements= block.statements(); |
3089 |
assertTrue("Number of statements not 1", statements.size() == 1); |
3090 |
|
3091 |
{ |
3092 |
// replace then statement by block , replace else with if statement (block) |
3093 |
IfStatement ifStatement= (IfStatement) statements.get(0); |
3094 |
|
3095 |
Statement thenStatement= ifStatement.getThenStatement(); |
3096 |
Statement elseStatement= ifStatement.getElseStatement(); |
3097 |
|
3098 |
Statement newElseStatement= (Statement) rewrite.createMoveTarget(thenStatement); |
3099 |
Statement newThenStatement= (Statement) rewrite.createMoveTarget(elseStatement); |
3100 |
|
3101 |
rewrite.set(ifStatement, IfStatement.THEN_STATEMENT_PROPERTY, newThenStatement, null); |
3102 |
rewrite.set(ifStatement, IfStatement.ELSE_STATEMENT_PROPERTY, newElseStatement, null); |
3103 |
} |
3104 |
String preview= evaluateRewrite(cu, rewrite); |
3105 |
|
3106 |
buf= new StringBuffer(); |
3107 |
buf.append("package test1;\n"); |
3108 |
buf.append("public class E {\n"); |
3109 |
buf.append(" public void foo(int i) {\n"); |
3110 |
buf.append(" if (i == 0) {\n"); |
3111 |
buf.append(" System.beep();\n"); |
3112 |
buf.append(" System.beep();\n"); |
3113 |
buf.append(" }\n"); |
3114 |
buf.append(" // comment\n"); |
3115 |
buf.append(" else {\n"); |
3116 |
buf.append(" System.beep();\n"); |
3117 |
buf.append(" }\n"); |
3118 |
buf.append(" }\n"); |
3119 |
buf.append("}\n"); |
3120 |
assertEqualString(preview, buf.toString()); |
3121 |
|
3122 |
} |
3123 |
|
3063 |
public void testLabeledStatement() throws Exception { |
3124 |
public void testLabeledStatement() throws Exception { |
3064 |
IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null); |
3125 |
IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null); |
3065 |
StringBuffer buf= new StringBuffer(); |
3126 |
StringBuffer buf= new StringBuffer(); |