This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 245348
Collapse All | Expand All

(-)a/java.hints/src/org/netbeans/modules/java/hints/jdk/ConvertToStringSwitch.java (-7 / +26 lines)
Lines 368-373 Link Here
368
        private final List<CatchDescription<TreePathHandle>> literal2Statement;
368
        private final List<CatchDescription<TreePathHandle>> literal2Statement;
369
        private final TreePathHandle defaultStatement;
369
        private final TreePathHandle defaultStatement;
370
        private boolean varNotNull;
370
        private boolean varNotNull;
371
        
372
        private Set<Tree> ifSeen = new HashSet<Tree>();
371
373
372
        public ConvertToSwitch(CompilationInfo info, TreePath create, TreePathHandle value, List<CatchDescription<TreePathHandle>> literal2Statement, TreePathHandle defaultStatement, 
374
        public ConvertToSwitch(CompilationInfo info, TreePath create, TreePathHandle value, List<CatchDescription<TreePathHandle>> literal2Statement, TreePathHandle defaultStatement, 
373
                boolean varNotNull) {
375
                boolean varNotNull) {
Lines 482-493 Link Here
482
484
483
            return label;
485
            return label;
484
        }
486
        }
487
        
488
        private Tree findExpressionParentIf(TreePath p) {
489
            while (!StatementTree.class.isAssignableFrom(p.getLeaf().getKind().asInterface()) && p != null) {
490
                p = p.getParentPath();
491
            }
492
            return p == null ? null : p.getLeaf();
493
        }
485
494
486
        private boolean addCase(WorkingCopy copy, CatchDescription<TreePath> desc, List<CaseTree> cases, Map<TreePath, Set<Name>> catch2Declared, Map<TreePath, Set<Name>> catch2Used) {
495
        private boolean addCase(WorkingCopy copy, CatchDescription<TreePath> desc, List<CaseTree> cases, Map<TreePath, Set<Name>> catch2Declared, Map<TreePath, Set<Name>> catch2Used) {
487
            TreeMaker make = copy.getTreeMaker();
496
            TreeMaker make = copy.getTreeMaker();
488
            List<StatementTree> statements = new LinkedList<StatementTree>();
497
            List<StatementTree> statements = new LinkedList<StatementTree>();
489
            Tree then = desc.path.getLeaf();
498
            Tree then = desc.path.getLeaf();
490
499
500
            Tree replacedByCase = null;
491
            if (then.getKind() == Kind.BLOCK) {
501
            if (then.getKind() == Kind.BLOCK) {
492
                Set<Name> currentDeclared = catch2Declared.get(desc.path);
502
                Set<Name> currentDeclared = catch2Declared.get(desc.path);
493
                boolean keepBlock = false;
503
                boolean keepBlock = false;
Lines 517-531 Link Here
517
                }
527
                }
518
528
519
                BlockTree block = (BlockTree) then;
529
                BlockTree block = (BlockTree) then;
520
521
                if (keepBlock) {
530
                if (keepBlock) {
522
                    if (!exitsFromAllBranches) {
531
                    if (!exitsFromAllBranches) {
523
                        statements.add(make.addBlockStatement(block, make.Break(null)));
532
                        statements.add(
533
                                make.asReplacementOf(
534
                                    make.addBlockStatement(block, make.Break(null)), block, true));
524
                    } else {
535
                    } else {
525
                        statements.add(block);
536
                        statements.add(block);
526
                    }
537
                    }
527
                } else {
538
                } else {
528
                    statements.addAll(block.getStatements());
539
                    statements.addAll(block.getStatements());
540
                    replacedByCase = block;
529
                    if (!exitsFromAllBranches) {
541
                    if (!exitsFromAllBranches) {
530
                        statements.add(make.Break(null));
542
                        statements.add(make.Break(null));
531
                    }
543
                    }
Lines 536-544 Link Here
536
                    statements.add(make.Break(null));
548
                    statements.add(make.Break(null));
537
                }
549
                }
538
            }
550
            }
539
551
            
540
            if (desc.literals == null) {
552
            if (desc.literals == null) {
541
                cases.add(make.Case(null, statements));
553
                cases.add(make.asReplacementOf(make.Case(null, statements), replacedByCase, true));
542
554
543
                return false;
555
                return false;
544
            }
556
            }
Lines 551-562 Link Here
551
                    //XXX: log
563
                    //XXX: log
552
                    return true;
564
                    return true;
553
                }
565
                }
566
                Tree ifSt = findExpressionParentIf(lit);
567
                if (ifSt != null && !ifSeen.add(ifSt)) {
568
                    ifSt = null;
569
                }
554
570
555
                List<StatementTree> body = it.hasNext() ? Collections.<StatementTree>emptyList() : statements;
571
                List<StatementTree> body = it.hasNext() ? Collections.<StatementTree>emptyList() : statements;
556
572
                CaseTree nc = make.Case((ExpressionTree) lit.getLeaf(), body);
557
                cases.add(make.Case((ExpressionTree) lit.getLeaf(), body));
573
                if (ifSt != null) {
574
                    nc = make.asReplacementOf(nc, ifSt, true);
575
                }
576
                cases.add(nc);
558
            }
577
            }
559
578
                
560
            return false;
579
            return false;
561
        }
580
        }
562
581

Return to bug 245348