Lines 19-24
Link Here
|
19 |
import org.eclipse.jdt.core.dom.*; |
19 |
import org.eclipse.jdt.core.dom.*; |
20 |
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; |
20 |
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; |
21 |
import org.eclipse.jdt.core.dom.rewrite.ListRewrite; |
21 |
import org.eclipse.jdt.core.dom.rewrite.ListRewrite; |
|
|
22 |
import org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants; |
22 |
|
23 |
|
23 |
public class ASTRewritingStatementsTest extends ASTRewritingTest { |
24 |
public class ASTRewritingStatementsTest extends ASTRewritingTest { |
24 |
|
25 |
|
Lines 2973-2978
Link Here
|
2973 |
assertEqualString(preview, buf.toString()); |
2974 |
assertEqualString(preview, buf.toString()); |
2974 |
|
2975 |
|
2975 |
} |
2976 |
} |
|
|
2977 |
|
2978 |
public void testSwitchStatement2() throws Exception { |
2979 |
String previousValue = null; |
2980 |
try { |
2981 |
previousValue = this.project1.getOption(DefaultCodeFormatterConstants.FORMATTER_INDENT_SWITCHSTATEMENTS_COMPARE_TO_SWITCH, false); |
2982 |
|
2983 |
this.project1.setOption(DefaultCodeFormatterConstants.FORMATTER_INDENT_SWITCHSTATEMENTS_COMPARE_TO_SWITCH, DefaultCodeFormatterConstants.FALSE); |
2984 |
|
2985 |
IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null); |
2986 |
StringBuffer buf= new StringBuffer(); |
2987 |
buf.append("package test1;\n"); |
2988 |
buf.append("public class E {\n"); |
2989 |
buf.append(" public void foo(int i) {\n"); |
2990 |
buf.append(" switch (i) {\n"); |
2991 |
buf.append(" }\n"); |
2992 |
buf.append(" switch (i) {\n"); |
2993 |
buf.append(" case 1:\n"); |
2994 |
buf.append(" i= 1;\n"); |
2995 |
buf.append(" break;\n"); |
2996 |
buf.append(" case 2:\n"); |
2997 |
buf.append(" i= 2;\n"); |
2998 |
buf.append(" break;\n"); |
2999 |
buf.append(" default:\n"); |
3000 |
buf.append(" i= 3;\n"); |
3001 |
buf.append(" }\n"); |
3002 |
buf.append(" }\n"); |
3003 |
buf.append("}\n"); |
3004 |
ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null); |
3005 |
|
3006 |
CompilationUnit astRoot= createAST(cu); |
3007 |
ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST()); |
3008 |
|
3009 |
AST ast= astRoot.getAST(); |
3010 |
|
3011 |
assertTrue("Parse errors", (astRoot.getFlags() & ASTNode.MALFORMED) == 0); |
3012 |
TypeDeclaration type= findTypeDeclaration(astRoot, "E"); |
3013 |
MethodDeclaration methodDecl= findMethodDeclaration(type, "foo"); |
3014 |
Block block= methodDecl.getBody(); |
3015 |
List blockStatements= block.statements(); |
3016 |
assertTrue("Number of statements not 2", blockStatements.size() == 2); |
3017 |
{ // insert statements, replace expression |
3018 |
SwitchStatement switchStatement= (SwitchStatement) blockStatements.get(0); |
3019 |
|
3020 |
ASTNode expression= switchStatement.getExpression(); |
3021 |
SimpleName newExpression= ast.newSimpleName("x"); |
3022 |
rewrite.replace(expression, newExpression, null); |
3023 |
|
3024 |
List statements= switchStatement.statements(); |
3025 |
assertTrue("Number of statements not 0", statements.size() == 0); |
3026 |
|
3027 |
SwitchCase caseStatement1= ast.newSwitchCase(); |
3028 |
caseStatement1.setExpression(ast.newNumberLiteral("1")); |
3029 |
|
3030 |
Statement statement1= ast.newReturnStatement(); |
3031 |
|
3032 |
SwitchCase caseStatement2= ast.newSwitchCase(); // default |
3033 |
caseStatement2.setExpression(null); |
3034 |
|
3035 |
ListRewrite listRewrite= rewrite.getListRewrite(switchStatement, SwitchStatement.STATEMENTS_PROPERTY); |
3036 |
listRewrite.insertLast(caseStatement1, null); |
3037 |
listRewrite.insertLast(statement1, null); |
3038 |
listRewrite.insertLast(caseStatement2, null); |
3039 |
} |
3040 |
|
3041 |
{ // insert, remove, replace statements, change case statements |
3042 |
SwitchStatement switchStatement= (SwitchStatement) blockStatements.get(1); |
3043 |
|
3044 |
List statements= switchStatement.statements(); |
3045 |
assertTrue("Number of statements not 8", statements.size() == 8); |
3046 |
|
3047 |
// remove statements |
3048 |
|
3049 |
rewrite.remove((ASTNode) statements.get(0), null); |
3050 |
rewrite.remove((ASTNode) statements.get(1), null); |
3051 |
rewrite.remove((ASTNode) statements.get(2), null); |
3052 |
|
3053 |
// change case statement |
3054 |
SwitchCase caseStatement= (SwitchCase) statements.get(3); |
3055 |
Expression newCaseExpression= ast.newNumberLiteral("10"); |
3056 |
rewrite.replace(caseStatement.getExpression(), newCaseExpression, null); |
3057 |
|
3058 |
ListRewrite listRewrite= rewrite.getListRewrite(switchStatement, SwitchStatement.STATEMENTS_PROPERTY); |
3059 |
|
3060 |
{ |
3061 |
// insert case statement |
3062 |
SwitchCase caseStatement2= ast.newSwitchCase(); |
3063 |
caseStatement2.setExpression(ast.newNumberLiteral("11")); |
3064 |
listRewrite.insertFirst(caseStatement2, null); |
3065 |
|
3066 |
// insert statement |
3067 |
Statement statement1= ast.newReturnStatement(); |
3068 |
listRewrite.insertAfter(statement1, caseStatement2, null); |
3069 |
} |
3070 |
|
3071 |
{ |
3072 |
// insert case statement |
3073 |
SwitchCase caseStatement2= ast.newSwitchCase(); |
3074 |
caseStatement2.setExpression(ast.newNumberLiteral("12")); |
3075 |
listRewrite.insertLast(caseStatement2, null); |
3076 |
|
3077 |
// insert statement |
3078 |
Statement statement1= ast.newReturnStatement(); |
3079 |
listRewrite.insertAfter(statement1, caseStatement2, null); |
3080 |
} |
3081 |
|
3082 |
|
3083 |
} |
3084 |
|
3085 |
String preview= evaluateRewrite(cu, rewrite); |
3086 |
|
3087 |
buf= new StringBuffer(); |
3088 |
buf.append("package test1;\n"); |
3089 |
buf.append("public class E {\n"); |
3090 |
buf.append(" public void foo(int i) {\n"); |
3091 |
buf.append(" switch (x) {\n"); |
3092 |
buf.append(" case 1:\n"); |
3093 |
buf.append(" return;\n"); |
3094 |
buf.append(" default:\n"); |
3095 |
buf.append(" }\n"); |
3096 |
buf.append(" switch (i) {\n"); |
3097 |
buf.append(" case 11:\n"); |
3098 |
buf.append(" return;\n"); |
3099 |
buf.append(" case 10:\n"); |
3100 |
buf.append(" i= 2;\n"); |
3101 |
buf.append(" break;\n"); |
3102 |
buf.append(" default:\n"); |
3103 |
buf.append(" i= 3;\n"); |
3104 |
buf.append(" case 12:\n"); |
3105 |
buf.append(" return;\n"); |
3106 |
buf.append(" }\n"); |
3107 |
buf.append(" }\n"); |
3108 |
buf.append("}\n"); |
3109 |
assertEqualString(preview, buf.toString()); |
3110 |
} finally { |
3111 |
if (previousValue != null) { |
3112 |
this.project1.setOption(DefaultCodeFormatterConstants.FORMATTER_INDENT_SWITCHSTATEMENTS_COMPARE_TO_CASES, previousValue); |
3113 |
} |
3114 |
} |
3115 |
} |
3116 |
|
3117 |
public void testSwitchStatement3() throws Exception { |
3118 |
String previousValue = null; |
3119 |
try { |
3120 |
previousValue = this.project1.getOption(DefaultCodeFormatterConstants.FORMATTER_INDENT_SWITCHSTATEMENTS_COMPARE_TO_SWITCH, false); |
3121 |
|
3122 |
this.project1.setOption(DefaultCodeFormatterConstants.FORMATTER_INDENT_SWITCHSTATEMENTS_COMPARE_TO_SWITCH, DefaultCodeFormatterConstants.FALSE); |
3123 |
|
3124 |
IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null); |
3125 |
StringBuffer buf= new StringBuffer(); |
3126 |
buf.append("package test1;\n"); |
3127 |
buf.append("public class E {\n"); |
3128 |
buf.append(" public void foo(int i) {\n"); |
3129 |
buf.append(" switch (i) {\n"); |
3130 |
buf.append(" case 1:\n"); |
3131 |
buf.append(" i= 1;\n"); |
3132 |
buf.append(" case 2:\n"); |
3133 |
buf.append(" i= 2;\n"); |
3134 |
buf.append(" break;\n"); |
3135 |
buf.append(" }\n"); |
3136 |
buf.append(" }\n"); |
3137 |
buf.append("}\n"); |
3138 |
ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null); |
3139 |
|
3140 |
CompilationUnit astRoot= createAST(cu); |
3141 |
ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST()); |
3142 |
|
3143 |
AST ast= astRoot.getAST(); |
3144 |
|
3145 |
assertTrue("Parse errors", (astRoot.getFlags() & ASTNode.MALFORMED) == 0); |
3146 |
TypeDeclaration type= findTypeDeclaration(astRoot, "E"); |
3147 |
MethodDeclaration methodDecl= findMethodDeclaration(type, "foo"); |
3148 |
Block block= methodDecl.getBody(); |
3149 |
List blockStatements= block.statements(); |
3150 |
assertTrue("Number of statements not 2", blockStatements.size() == 1); |
3151 |
{ // insert statements, replace expression |
3152 |
SwitchStatement switchStatement= (SwitchStatement) blockStatements.get(0); |
3153 |
|
3154 |
List statements= switchStatement.statements(); |
3155 |
assertTrue("Number of statements not 0", statements.size() == 5); |
3156 |
|
3157 |
SwitchCase caseStatement = (SwitchCase)statements.get(2); |
3158 |
|
3159 |
BreakStatement breakStatement= ast.newBreakStatement(); |
3160 |
|
3161 |
ListRewrite listRewrite= rewrite.getListRewrite(switchStatement, SwitchStatement.STATEMENTS_PROPERTY); |
3162 |
listRewrite.insertBefore(breakStatement, caseStatement, null); |
3163 |
} |
3164 |
|
3165 |
String preview= evaluateRewrite(cu, rewrite); |
3166 |
|
3167 |
buf= new StringBuffer(); |
3168 |
buf.append("package test1;\n"); |
3169 |
buf.append("public class E {\n"); |
3170 |
buf.append(" public void foo(int i) {\n"); |
3171 |
buf.append(" switch (i) {\n"); |
3172 |
buf.append(" case 1:\n"); |
3173 |
buf.append(" i= 1;\n"); |
3174 |
buf.append(" break;\n"); |
3175 |
buf.append(" case 2:\n"); |
3176 |
buf.append(" i= 2;\n"); |
3177 |
buf.append(" break;\n"); |
3178 |
buf.append(" }\n"); |
3179 |
buf.append(" }\n"); |
3180 |
buf.append("}\n"); |
3181 |
assertEqualString(preview, buf.toString()); |
3182 |
} finally { |
3183 |
if (previousValue != null) { |
3184 |
this.project1.setOption(DefaultCodeFormatterConstants.FORMATTER_INDENT_SWITCHSTATEMENTS_COMPARE_TO_CASES, previousValue); |
3185 |
} |
3186 |
} |
3187 |
} |
3188 |
|
3189 |
/* |
3190 |
* https://bugs.eclipse.org/bugs/show_bug.cgi?id=246627 |
3191 |
* Insert a statement before an unchanged statement (and preceded by an unchanged statement) |
3192 |
*/ |
3193 |
public void testSwitchStatement5() throws Exception { |
3194 |
String previousValue = null; |
3195 |
try { |
3196 |
previousValue = this.project1.getOption(DefaultCodeFormatterConstants.FORMATTER_INDENT_SWITCHSTATEMENTS_COMPARE_TO_SWITCH, false); |
3197 |
|
3198 |
this.project1.setOption(DefaultCodeFormatterConstants.FORMATTER_INDENT_SWITCHSTATEMENTS_COMPARE_TO_SWITCH, DefaultCodeFormatterConstants.FALSE); |
3199 |
|
3200 |
IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null); |
3201 |
StringBuffer buf= new StringBuffer(); |
3202 |
buf.append("package test1;\n"); |
3203 |
buf.append("public class E {\n"); |
3204 |
buf.append(" public void foo(int i) {\n"); |
3205 |
buf.append(" switch (i) {\n"); |
3206 |
buf.append(" case 1:\n"); |
3207 |
buf.append(" i= 1;\n"); |
3208 |
buf.append(" case 2:\n"); |
3209 |
buf.append(" break;\n"); |
3210 |
buf.append(" }\n"); |
3211 |
buf.append(" }\n"); |
3212 |
buf.append("}\n"); |
3213 |
ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null); |
3214 |
|
3215 |
CompilationUnit astRoot= createAST(cu); |
3216 |
ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST()); |
3217 |
|
3218 |
AST ast= astRoot.getAST(); |
3219 |
|
3220 |
assertTrue("Parse errors", (astRoot.getFlags() & ASTNode.MALFORMED) == 0); |
3221 |
TypeDeclaration type= findTypeDeclaration(astRoot, "E"); |
3222 |
MethodDeclaration methodDecl= findMethodDeclaration(type, "foo"); |
3223 |
Block block= methodDecl.getBody(); |
3224 |
List blockStatements= block.statements(); |
3225 |
assertTrue("Number of statements not 1", blockStatements.size() == 1); |
3226 |
{ // insert statements, replace expression |
3227 |
SwitchStatement switchStatement= (SwitchStatement) blockStatements.get(0); |
3228 |
|
3229 |
List statements= switchStatement.statements(); |
3230 |
assertTrue("Number of statements not 4", statements.size() == 4); |
3231 |
|
3232 |
SwitchCase caseStatement = (SwitchCase)statements.get(2); // case 2: |
3233 |
|
3234 |
ListRewrite listRewrite= rewrite.getListRewrite(switchStatement, SwitchStatement.STATEMENTS_PROPERTY); |
3235 |
listRewrite.insertBefore(ast.newBreakStatement(), caseStatement, null); |
3236 |
} |
3237 |
|
3238 |
String preview= evaluateRewrite(cu, rewrite); |
3239 |
|
3240 |
buf= new StringBuffer(); |
3241 |
buf.append("package test1;\n"); |
3242 |
buf.append("public class E {\n"); |
3243 |
buf.append(" public void foo(int i) {\n"); |
3244 |
buf.append(" switch (i) {\n"); |
3245 |
buf.append(" case 1:\n"); |
3246 |
buf.append(" i= 1;\n"); |
3247 |
buf.append(" break;\n"); |
3248 |
buf.append(" case 2:\n"); |
3249 |
buf.append(" break;\n"); |
3250 |
buf.append(" }\n"); |
3251 |
buf.append(" }\n"); |
3252 |
buf.append("}\n"); |
3253 |
assertEqualString(preview, buf.toString()); |
3254 |
} finally { |
3255 |
if (previousValue != null) { |
3256 |
this.project1.setOption(DefaultCodeFormatterConstants.FORMATTER_INDENT_SWITCHSTATEMENTS_COMPARE_TO_CASES, previousValue); |
3257 |
} |
3258 |
} |
3259 |
} |
3260 |
|
3261 |
/* |
3262 |
* https://bugs.eclipse.org/bugs/show_bug.cgi?id=246627 |
3263 |
* Insert a statement after an unchanged statement (and preceded by an unchanged statement) |
3264 |
*/ |
3265 |
public void testSwitchStatement6() throws Exception { |
3266 |
String previousValue = null; |
3267 |
try { |
3268 |
previousValue = this.project1.getOption(DefaultCodeFormatterConstants.FORMATTER_INDENT_SWITCHSTATEMENTS_COMPARE_TO_SWITCH, false); |
3269 |
|
3270 |
this.project1.setOption(DefaultCodeFormatterConstants.FORMATTER_INDENT_SWITCHSTATEMENTS_COMPARE_TO_SWITCH, DefaultCodeFormatterConstants.FALSE); |
3271 |
|
3272 |
IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null); |
3273 |
StringBuffer buf= new StringBuffer(); |
3274 |
buf.append("package test1;\n"); |
3275 |
buf.append("public class E {\n"); |
3276 |
buf.append(" public void foo(int i) {\n"); |
3277 |
buf.append(" switch (i) {\n"); |
3278 |
buf.append(" case 1:\n"); |
3279 |
buf.append(" i= 1;\n"); |
3280 |
buf.append(" case 2:\n"); |
3281 |
buf.append(" break;\n"); |
3282 |
buf.append(" }\n"); |
3283 |
buf.append(" }\n"); |
3284 |
buf.append("}\n"); |
3285 |
ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null); |
3286 |
|
3287 |
CompilationUnit astRoot= createAST(cu); |
3288 |
ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST()); |
3289 |
|
3290 |
AST ast= astRoot.getAST(); |
3291 |
|
3292 |
assertTrue("Parse errors", (astRoot.getFlags() & ASTNode.MALFORMED) == 0); |
3293 |
TypeDeclaration type= findTypeDeclaration(astRoot, "E"); |
3294 |
MethodDeclaration methodDecl= findMethodDeclaration(type, "foo"); |
3295 |
Block block= methodDecl.getBody(); |
3296 |
List blockStatements= block.statements(); |
3297 |
assertTrue("Number of statements not 1", blockStatements.size() == 1); |
3298 |
{ // insert statements, replace expression |
3299 |
SwitchStatement switchStatement= (SwitchStatement) blockStatements.get(0); |
3300 |
|
3301 |
List statements= switchStatement.statements(); |
3302 |
assertTrue("Number of statements not 4", statements.size() == 4); |
3303 |
|
3304 |
ExpressionStatement assignment = (ExpressionStatement)statements.get(1); // i= 1; |
3305 |
|
3306 |
ListRewrite listRewrite= rewrite.getListRewrite(switchStatement, SwitchStatement.STATEMENTS_PROPERTY); |
3307 |
listRewrite.insertAfter(ast.newBreakStatement(), assignment, null); |
3308 |
} |
3309 |
|
3310 |
String preview= evaluateRewrite(cu, rewrite); |
3311 |
|
3312 |
buf= new StringBuffer(); |
3313 |
buf.append("package test1;\n"); |
3314 |
buf.append("public class E {\n"); |
3315 |
buf.append(" public void foo(int i) {\n"); |
3316 |
buf.append(" switch (i) {\n"); |
3317 |
buf.append(" case 1:\n"); |
3318 |
buf.append(" i= 1;\n"); |
3319 |
buf.append(" break;\n"); |
3320 |
buf.append(" case 2:\n"); |
3321 |
buf.append(" break;\n"); |
3322 |
buf.append(" }\n"); |
3323 |
buf.append(" }\n"); |
3324 |
buf.append("}\n"); |
3325 |
assertEqualString(preview, buf.toString()); |
3326 |
} finally { |
3327 |
if (previousValue != null) { |
3328 |
this.project1.setOption(DefaultCodeFormatterConstants.FORMATTER_INDENT_SWITCHSTATEMENTS_COMPARE_TO_CASES, previousValue); |
3329 |
} |
3330 |
} |
3331 |
} |
3332 |
|
3333 |
/* |
3334 |
* https://bugs.eclipse.org/bugs/show_bug.cgi?id=246627 |
3335 |
* Replace a statement preceded by an unchanged statement) |
3336 |
*/ |
3337 |
public void testSwitchStatement7() throws Exception { |
3338 |
String previousValue = null; |
3339 |
try { |
3340 |
previousValue = this.project1.getOption(DefaultCodeFormatterConstants.FORMATTER_INDENT_SWITCHSTATEMENTS_COMPARE_TO_SWITCH, false); |
3341 |
|
3342 |
this.project1.setOption(DefaultCodeFormatterConstants.FORMATTER_INDENT_SWITCHSTATEMENTS_COMPARE_TO_SWITCH, DefaultCodeFormatterConstants.FALSE); |
3343 |
|
3344 |
IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null); |
3345 |
StringBuffer buf= new StringBuffer(); |
3346 |
buf.append("package test1;\n"); |
3347 |
buf.append("public class E {\n"); |
3348 |
buf.append(" public void foo(int i) {\n"); |
3349 |
buf.append(" switch (i) {\n"); |
3350 |
buf.append(" case 1:\n"); |
3351 |
buf.append(" i= 1;\n"); |
3352 |
buf.append(" case 3:\n"); |
3353 |
buf.append(" break;\n"); |
3354 |
buf.append(" }\n"); |
3355 |
buf.append(" }\n"); |
3356 |
buf.append("}\n"); |
3357 |
ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null); |
3358 |
|
3359 |
CompilationUnit astRoot= createAST(cu); |
3360 |
ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST()); |
3361 |
|
3362 |
AST ast= astRoot.getAST(); |
3363 |
|
3364 |
assertTrue("Parse errors", (astRoot.getFlags() & ASTNode.MALFORMED) == 0); |
3365 |
TypeDeclaration type= findTypeDeclaration(astRoot, "E"); |
3366 |
MethodDeclaration methodDecl= findMethodDeclaration(type, "foo"); |
3367 |
Block block= methodDecl.getBody(); |
3368 |
List blockStatements= block.statements(); |
3369 |
assertTrue("Number of statements not 1", blockStatements.size() == 1); |
3370 |
{ // insert statements, replace expression |
3371 |
SwitchStatement switchStatement= (SwitchStatement) blockStatements.get(0); |
3372 |
|
3373 |
List statements= switchStatement.statements(); |
3374 |
assertTrue("Number of statements not 4", statements.size() == 4); |
3375 |
|
3376 |
ExpressionStatement assignment = (ExpressionStatement)statements.get(1); // i= 1;: |
3377 |
|
3378 |
SwitchCase switchCase = ast.newSwitchCase(); |
3379 |
switchCase.setExpression(ast.newNumberLiteral("2")); |
3380 |
|
3381 |
ListRewrite listRewrite= rewrite.getListRewrite(switchStatement, SwitchStatement.STATEMENTS_PROPERTY); |
3382 |
listRewrite.replace(assignment, switchCase, null); |
3383 |
} |
3384 |
|
3385 |
String preview= evaluateRewrite(cu, rewrite); |
3386 |
|
3387 |
buf= new StringBuffer(); |
3388 |
buf.append("package test1;\n"); |
3389 |
buf.append("public class E {\n"); |
3390 |
buf.append(" public void foo(int i) {\n"); |
3391 |
buf.append(" switch (i) {\n"); |
3392 |
buf.append(" case 1:\n"); |
3393 |
buf.append(" case 2:\n"); |
3394 |
buf.append(" case 3:\n"); |
3395 |
buf.append(" break;\n"); |
3396 |
buf.append(" }\n"); |
3397 |
buf.append(" }\n"); |
3398 |
buf.append("}\n"); |
3399 |
assertEqualString(preview, buf.toString()); |
3400 |
} finally { |
3401 |
if (previousValue != null) { |
3402 |
this.project1.setOption(DefaultCodeFormatterConstants.FORMATTER_INDENT_SWITCHSTATEMENTS_COMPARE_TO_CASES, previousValue); |
3403 |
} |
3404 |
} |
3405 |
} |
3406 |
|
3407 |
/* |
3408 |
* https://bugs.eclipse.org/bugs/show_bug.cgi?id=246627 |
3409 |
* Remove a statement preceded by an unchanged statement) |
3410 |
*/ |
3411 |
public void testSwitchStatement8() throws Exception { |
3412 |
String previousValue = null; |
3413 |
try { |
3414 |
previousValue = this.project1.getOption(DefaultCodeFormatterConstants.FORMATTER_INDENT_SWITCHSTATEMENTS_COMPARE_TO_SWITCH, false); |
3415 |
|
3416 |
this.project1.setOption(DefaultCodeFormatterConstants.FORMATTER_INDENT_SWITCHSTATEMENTS_COMPARE_TO_SWITCH, DefaultCodeFormatterConstants.FALSE); |
3417 |
|
3418 |
IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null); |
3419 |
StringBuffer buf= new StringBuffer(); |
3420 |
buf.append("package test1;\n"); |
3421 |
buf.append("public class E {\n"); |
3422 |
buf.append(" public void foo(int i) {\n"); |
3423 |
buf.append(" switch (i) {\n"); |
3424 |
buf.append(" case 1:\n"); |
3425 |
buf.append(" i= 1;\n"); |
3426 |
buf.append(" case 2:\n"); |
3427 |
buf.append(" break;\n"); |
3428 |
buf.append(" }\n"); |
3429 |
buf.append(" }\n"); |
3430 |
buf.append("}\n"); |
3431 |
ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null); |
3432 |
|
3433 |
CompilationUnit astRoot= createAST(cu); |
3434 |
ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST()); |
3435 |
|
3436 |
assertTrue("Parse errors", (astRoot.getFlags() & ASTNode.MALFORMED) == 0); |
3437 |
TypeDeclaration type= findTypeDeclaration(astRoot, "E"); |
3438 |
MethodDeclaration methodDecl= findMethodDeclaration(type, "foo"); |
3439 |
Block block= methodDecl.getBody(); |
3440 |
List blockStatements= block.statements(); |
3441 |
assertTrue("Number of statements not 1", blockStatements.size() == 1); |
3442 |
{ // insert statements, replace expression |
3443 |
SwitchStatement switchStatement= (SwitchStatement) blockStatements.get(0); |
3444 |
|
3445 |
List statements= switchStatement.statements(); |
3446 |
assertTrue("Number of statements not 4", statements.size() == 4); |
3447 |
|
3448 |
ExpressionStatement assignment = (ExpressionStatement)statements.get(1); // i= 1;: |
3449 |
|
3450 |
ListRewrite listRewrite= rewrite.getListRewrite(switchStatement, SwitchStatement.STATEMENTS_PROPERTY); |
3451 |
listRewrite.remove(assignment, null); |
3452 |
} |
3453 |
|
3454 |
String preview= evaluateRewrite(cu, rewrite); |
3455 |
|
3456 |
buf= new StringBuffer(); |
3457 |
buf.append("package test1;\n"); |
3458 |
buf.append("public class E {\n"); |
3459 |
buf.append(" public void foo(int i) {\n"); |
3460 |
buf.append(" switch (i) {\n"); |
3461 |
buf.append(" case 1:\n"); |
3462 |
buf.append(" case 2:\n"); |
3463 |
buf.append(" break;\n"); |
3464 |
buf.append(" }\n"); |
3465 |
buf.append(" }\n"); |
3466 |
buf.append("}\n"); |
3467 |
assertEqualString(preview, buf.toString()); |
3468 |
} finally { |
3469 |
if (previousValue != null) { |
3470 |
this.project1.setOption(DefaultCodeFormatterConstants.FORMATTER_INDENT_SWITCHSTATEMENTS_COMPARE_TO_CASES, previousValue); |
3471 |
} |
3472 |
} |
3473 |
} |
3474 |
|
3475 |
/* |
3476 |
* https://bugs.eclipse.org/bugs/show_bug.cgi?id=246627 |
3477 |
* Remove a statement followed by an inserted statement) |
3478 |
*/ |
3479 |
public void testSwitchStatement9() throws Exception { |
3480 |
String previousValue = null; |
3481 |
try { |
3482 |
previousValue = this.project1.getOption(DefaultCodeFormatterConstants.FORMATTER_INDENT_SWITCHSTATEMENTS_COMPARE_TO_SWITCH, false); |
3483 |
|
3484 |
this.project1.setOption(DefaultCodeFormatterConstants.FORMATTER_INDENT_SWITCHSTATEMENTS_COMPARE_TO_SWITCH, DefaultCodeFormatterConstants.FALSE); |
3485 |
|
3486 |
IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null); |
3487 |
StringBuffer buf= new StringBuffer(); |
3488 |
buf.append("package test1;\n"); |
3489 |
buf.append("public class E {\n"); |
3490 |
buf.append(" public void foo(int i) {\n"); |
3491 |
buf.append(" switch (i) {\n"); |
3492 |
buf.append(" case 1:\n"); |
3493 |
buf.append(" i= 1;\n"); |
3494 |
buf.append(" case 3:\n"); |
3495 |
buf.append(" break;\n"); |
3496 |
buf.append(" }\n"); |
3497 |
buf.append(" }\n"); |
3498 |
buf.append("}\n"); |
3499 |
ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null); |
3500 |
|
3501 |
CompilationUnit astRoot= createAST(cu); |
3502 |
ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST()); |
3503 |
|
3504 |
AST ast= astRoot.getAST(); |
3505 |
|
3506 |
assertTrue("Parse errors", (astRoot.getFlags() & ASTNode.MALFORMED) == 0); |
3507 |
TypeDeclaration type= findTypeDeclaration(astRoot, "E"); |
3508 |
MethodDeclaration methodDecl= findMethodDeclaration(type, "foo"); |
3509 |
Block block= methodDecl.getBody(); |
3510 |
List blockStatements= block.statements(); |
3511 |
assertTrue("Number of statements not 1", blockStatements.size() == 1); |
3512 |
{ // insert statements, replace expression |
3513 |
SwitchStatement switchStatement= (SwitchStatement) blockStatements.get(0); |
3514 |
|
3515 |
List statements= switchStatement.statements(); |
3516 |
assertTrue("Number of statements not 4", statements.size() == 4); |
3517 |
|
3518 |
ExpressionStatement assignment = (ExpressionStatement)statements.get(1); // i= 1; |
3519 |
|
3520 |
SwitchCase switchCase = ast.newSwitchCase(); |
3521 |
switchCase.setExpression(ast.newNumberLiteral("2")); |
3522 |
|
3523 |
ListRewrite listRewrite= rewrite.getListRewrite(switchStatement, SwitchStatement.STATEMENTS_PROPERTY); |
3524 |
listRewrite.remove(assignment, null); |
3525 |
listRewrite.insertAfter(switchCase, assignment, null); |
3526 |
} |
3527 |
|
3528 |
String preview= evaluateRewrite(cu, rewrite); |
3529 |
|
3530 |
buf= new StringBuffer(); |
3531 |
buf.append("package test1;\n"); |
3532 |
buf.append("public class E {\n"); |
3533 |
buf.append(" public void foo(int i) {\n"); |
3534 |
buf.append(" switch (i) {\n"); |
3535 |
buf.append(" case 1:\n"); |
3536 |
buf.append(" case 2:\n"); |
3537 |
buf.append(" case 3:\n"); |
3538 |
buf.append(" break;\n"); |
3539 |
buf.append(" }\n"); |
3540 |
buf.append(" }\n"); |
3541 |
buf.append("}\n"); |
3542 |
assertEqualString(preview, buf.toString()); |
3543 |
} finally { |
3544 |
if (previousValue != null) { |
3545 |
this.project1.setOption(DefaultCodeFormatterConstants.FORMATTER_INDENT_SWITCHSTATEMENTS_COMPARE_TO_CASES, previousValue); |
3546 |
} |
3547 |
} |
3548 |
} |
3549 |
|
3550 |
/* |
3551 |
* https://bugs.eclipse.org/bugs/show_bug.cgi?id=246627 |
3552 |
* Remove a statement preceded by an inserted statement) |
3553 |
*/ |
3554 |
public void testSwitchStatement10() throws Exception { |
3555 |
String previousValue = null; |
3556 |
try { |
3557 |
previousValue = this.project1.getOption(DefaultCodeFormatterConstants.FORMATTER_INDENT_SWITCHSTATEMENTS_COMPARE_TO_SWITCH, false); |
3558 |
|
3559 |
this.project1.setOption(DefaultCodeFormatterConstants.FORMATTER_INDENT_SWITCHSTATEMENTS_COMPARE_TO_SWITCH, DefaultCodeFormatterConstants.FALSE); |
3560 |
|
3561 |
IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null); |
3562 |
StringBuffer buf= new StringBuffer(); |
3563 |
buf.append("package test1;\n"); |
3564 |
buf.append("public class E {\n"); |
3565 |
buf.append(" public void foo(int i) {\n"); |
3566 |
buf.append(" switch (i) {\n"); |
3567 |
buf.append(" case 1:\n"); |
3568 |
buf.append(" i= 1;\n"); |
3569 |
buf.append(" case 2:\n"); |
3570 |
buf.append(" break;\n"); |
3571 |
buf.append(" }\n"); |
3572 |
buf.append(" }\n"); |
3573 |
buf.append("}\n"); |
3574 |
ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null); |
3575 |
|
3576 |
CompilationUnit astRoot= createAST(cu); |
3577 |
ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST()); |
3578 |
|
3579 |
AST ast= astRoot.getAST(); |
3580 |
|
3581 |
assertTrue("Parse errors", (astRoot.getFlags() & ASTNode.MALFORMED) == 0); |
3582 |
TypeDeclaration type= findTypeDeclaration(astRoot, "E"); |
3583 |
MethodDeclaration methodDecl= findMethodDeclaration(type, "foo"); |
3584 |
Block block= methodDecl.getBody(); |
3585 |
List blockStatements= block.statements(); |
3586 |
assertTrue("Number of statements not 1", blockStatements.size() == 1); |
3587 |
{ // insert statements, replace expression |
3588 |
SwitchStatement switchStatement= (SwitchStatement) blockStatements.get(0); |
3589 |
|
3590 |
List statements= switchStatement.statements(); |
3591 |
assertTrue("Number of statements not 4", statements.size() == 4); |
3592 |
|
3593 |
ExpressionStatement assignment = (ExpressionStatement)statements.get(1); // i= 1;: |
3594 |
|
3595 |
ListRewrite listRewrite= rewrite.getListRewrite(switchStatement, SwitchStatement.STATEMENTS_PROPERTY); |
3596 |
listRewrite.insertBefore(ast.newBreakStatement(), assignment, null); |
3597 |
listRewrite.remove(assignment, null); |
3598 |
} |
3599 |
|
3600 |
String preview= evaluateRewrite(cu, rewrite); |
3601 |
|
3602 |
buf= new StringBuffer(); |
3603 |
buf.append("package test1;\n"); |
3604 |
buf.append("public class E {\n"); |
3605 |
buf.append(" public void foo(int i) {\n"); |
3606 |
buf.append(" switch (i) {\n"); |
3607 |
buf.append(" case 1:\n"); |
3608 |
buf.append(" break;\n"); |
3609 |
buf.append(" case 2:\n"); |
3610 |
buf.append(" break;\n"); |
3611 |
buf.append(" }\n"); |
3612 |
buf.append(" }\n"); |
3613 |
buf.append("}\n"); |
3614 |
assertEqualString(preview, buf.toString()); |
3615 |
} finally { |
3616 |
if (previousValue != null) { |
3617 |
this.project1.setOption(DefaultCodeFormatterConstants.FORMATTER_INDENT_SWITCHSTATEMENTS_COMPARE_TO_CASES, previousValue); |
3618 |
} |
3619 |
} |
3620 |
} |
2976 |
|
3621 |
|
2977 |
public void testSynchronizedStatement() throws Exception { |
3622 |
public void testSynchronizedStatement() throws Exception { |
2978 |
IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null); |
3623 |
IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null); |