1
1
/*******************************************************************************
2
- * Copyright (c) 2020, 2023 Red Hat Inc. and others.
2
+ * Copyright (c) 2020, 2025 Red Hat Inc. and others.
3
3
*
4
4
* This program and the accompanying materials
5
5
* are made available under the terms of the Eclipse Public License 2.0
@@ -326,6 +326,7 @@ public void rewriteAST(CompilationUnitRewrite cuRewrite, LinkedProposalModelCore
326
326
}
327
327
328
328
// build switch expression
329
+ boolean defaultFallThrough = false ;
329
330
for (Map .Entry <SwitchCase , List <Statement >> entry : caseMap .entrySet ()) {
330
331
SwitchCase oldSwitchCase = entry .getKey ();
331
332
List <Statement > oldStatements = entry .getValue ();
@@ -335,15 +336,23 @@ public void rewriteAST(CompilationUnitRewrite cuRewrite, LinkedProposalModelCore
335
336
newSwitchExpression .statements ().add (newSwitchCase );
336
337
} else {
337
338
// fall-through, want all fall-through labels in single case
338
- if (lastSwitchCase == null ) {
339
- lastSwitchCase = ast .newSwitchCase ();
340
- lastSwitchCase .setSwitchLabeledRule (true );
341
- newSwitchExpression .statements ().add (lastSwitchCase );
339
+ if (oldSwitchCase .expressions ().isEmpty ()) {
340
+ // default falls through but we can't add default with other labels
341
+ // so defer until we have finished the fallthrough case and then
342
+ // duplicate statements for default case
343
+ defaultFallThrough = true ;
342
344
}
343
- for (Object obj : oldSwitchCase .expressions ()) {
344
- Expression oldExpression = (Expression )obj ;
345
- Expression newExpression = (Expression )rewrite .createCopyTarget (oldExpression );
346
- lastSwitchCase .expressions ().add (newExpression );
345
+ else {
346
+ if (lastSwitchCase == null ) {
347
+ lastSwitchCase = ast .newSwitchCase ();
348
+ lastSwitchCase .setSwitchLabeledRule (true );
349
+ newSwitchExpression .statements ().add (lastSwitchCase );
350
+ }
351
+ for (Object obj : oldSwitchCase .expressions ()) {
352
+ Expression oldExpression = (Expression )obj ;
353
+ Expression newExpression = (Expression )rewrite .createCopyTarget (oldExpression );
354
+ lastSwitchCase .expressions ().add (newExpression );
355
+ }
347
356
}
348
357
}
349
358
continue ;
@@ -385,21 +394,38 @@ public void rewriteAST(CompilationUnitRewrite cuRewrite, LinkedProposalModelCore
385
394
if (oldStatements .size () == 1 ) {
386
395
Statement oldStatement = oldStatements .get (0 );
387
396
Statement newStatement = null ;
397
+ Statement newStatement2 = null ;
388
398
if (oldStatement instanceof ThrowStatement ) {
389
399
ThrowStatement throwStatement = (ThrowStatement )oldStatement ;
390
400
newStatement = (Statement )rewrite .createCopyTarget (throwStatement );
401
+ if (defaultFallThrough ) {
402
+ newStatement2 = (Statement )rewrite .createCopyTarget (throwStatement );
403
+ }
391
404
} else if (oldStatement instanceof ReturnStatement && createReturnStatement ) {
392
405
if (forceOldStyle ) {
393
406
newStatement = getNewYieldStatementFromReturn (cuRewrite , rewrite , (ReturnStatement )oldStatement );
394
407
} else {
395
408
newStatement = getNewStatementFromReturn (cuRewrite , rewrite , (ReturnStatement )oldStatement );
409
+ if (defaultFallThrough ) {
410
+ newStatement2 = getNewStatementFromReturn (cuRewrite , rewrite , (ReturnStatement )oldStatement );
411
+ }
396
412
}
397
413
} else if (forceOldStyle ) {
398
414
newStatement = getNewYieldStatement (cuRewrite , rewrite , (ExpressionStatement )oldStatement );
399
415
} else {
400
416
newStatement = getNewStatementForCase (cuRewrite , rewrite , oldStatement );
417
+ if (defaultFallThrough ) {
418
+ newStatement2 = getNewStatementForCase (cuRewrite , rewrite , oldStatement );
419
+ }
401
420
}
402
421
newSwitchExpression .statements ().add (newStatement );
422
+ if (defaultFallThrough ) {
423
+ SwitchCase newSwitchCase = ast .newSwitchCase ();
424
+ newSwitchCase .setSwitchLabeledRule (true );
425
+ newSwitchExpression .statements ().add (newSwitchCase );
426
+ newSwitchExpression .statements ().add (newStatement2 );
427
+ defaultFallThrough = false ;
428
+ }
403
429
} else {
404
430
Block newBlock = ast .newBlock ();
405
431
int statementsLen = oldStatements .size ();
@@ -419,6 +445,13 @@ public void rewriteAST(CompilationUnitRewrite cuRewrite, LinkedProposalModelCore
419
445
}
420
446
newBlock .statements ().add (newStatement );
421
447
newSwitchExpression .statements ().add (newBlock );
448
+ if (defaultFallThrough ) {
449
+ SwitchCase newSwitchCase = ast .newSwitchCase ();
450
+ newSwitchCase .setSwitchLabeledRule (true );
451
+ newSwitchCase .expressions ().add (ast .newCaseDefaultExpression ());
452
+ newSwitchExpression .statements ().add (newSwitchCase );
453
+ newSwitchExpression .statements ().add (newBlock );
454
+ }
422
455
}
423
456
if (needDuplicateDefault ) {
424
457
needDuplicateDefault = false ;
0 commit comments