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 158154
Collapse All | Expand All

(-)a/java.source/src/org/netbeans/modules/java/source/save/CasualDiff.java (-3 / +11 lines)
Lines 2926-2931 Link Here
2926
        if (!matcher.match()) {
2926
        if (!matcher.match()) {
2927
            return localPointer;
2927
            return localPointer;
2928
        }
2928
        }
2929
        Queue<JCTree> deletedItems = new LinkedList<JCTree>(); // deleted items
2929
        JCTree lastdel = null; // last deleted element
2930
        JCTree lastdel = null; // last deleted element
2930
        ResultItem<JCTree>[] result = matcher.getResult();
2931
        ResultItem<JCTree>[] result = matcher.getResult();
2931
2932
Lines 3019-3024 Link Here
3019
                            }
3020
                            }
3020
                        }
3021
                        }
3021
                    }
3022
                    }
3023
                    JCTree ld = null;
3024
                    if (deletedItems != null && !deletedItems.isEmpty()) {
3025
                        ld = deletedItems.poll();
3026
                    }
3022
                    if (!found) {
3027
                    if (!found) {
3023
                        if (lastdel != null) {
3028
                        if (lastdel != null) {
3024
                            boolean wasInFieldGroup = false;
3029
                            boolean wasInFieldGroup = false;
Lines 3045-3051 Link Here
3045
                                printer.print(this.printer.toString());
3050
                                printer.print(this.printer.toString());
3046
                                this.printer = oldPrinter;
3051
                                this.printer = oldPrinter;
3047
                                this.printer.undent(old);
3052
                                this.printer.undent(old);
3048
                                lastdel = null;
3053
                                if (treesMatch(ld, lastdel)) {
3054
                                    lastdel = null;
3055
                                }
3049
                                break;
3056
                                break;
3050
                            }
3057
                            }
3051
                        }
3058
                        }
Lines 3061-3069 Link Here
3061
                        copyTo(localPointer, pos[0], printer);
3068
                        copyTo(localPointer, pos[0], printer);
3062
                    }
3069
                    }
3063
                    lastdel = oldList.get(i);
3070
                    lastdel = oldList.get(i);
3071
                    deletedItems.add( lastdel );
3072
                    CommentSet ch = comments.getComments(oldList.get(i));
3073
                    localPointer = Math.max(pos[1], Math.max(commentEnd(ch, CommentSet.RelativePosition.INLINE), commentEnd(ch, CommentSet.RelativePosition.TRAILING)));
3064
                    ++i;
3074
                    ++i;
3065
                    CommentSet ch = comments.getComments(lastdel);
3066
                    localPointer = Math.max(pos[1], Math.max(commentEnd(ch, CommentSet.RelativePosition.INLINE), commentEnd(ch, CommentSet.RelativePosition.TRAILING)));
3067
                    break;
3075
                    break;
3068
                }
3076
                }
3069
                case NOCHANGE: {
3077
                case NOCHANGE: {
(-)a/java.source/test/unit/src/org/netbeans/api/java/source/gen/IfTest.java (+89 lines)
Lines 85-90 Link Here
85
//        suite.addTest(new IfTest("testModifyingIf"));
85
//        suite.addTest(new IfTest("testModifyingIf"));
86
//        suite.addTest(new IfTest("test158463a"));
86
//        suite.addTest(new IfTest("test158463a"));
87
//        suite.addTest(new IfTest("test158463b"));
87
//        suite.addTest(new IfTest("test158463b"));
88
//        suite.addTest(new IfTest("test158154OneIf"));
89
//        suite.addTest(new IfTest("test158154TwoIfs"));
88
        return suite;
90
        return suite;
89
    }
91
    }
90
92
Lines 433-438 Link Here
433
        assertEquals(golden, res);
435
        assertEquals(golden, res);
434
    }
436
    }
435
437
438
    public void test158154OneIf() throws Exception {
439
        String source = "class Test {\n"
440
                + "    void m1(boolean b) {\n"
441
                + "        if (b) ; else System.out.println(\"hi\");\n"
442
                + "    }\n"
443
                + "}";
444
        String golden = "class Test {\n"
445
                + "    void m1(boolean b) {\n"
446
                + "        if (!(b)) System.out.println(\"hi\");\n"
447
                + "    }\n"
448
                + "}";
449
        testFile = new File(getWorkDir(), "Test.java");
450
451
        TestUtilities.copyStringToFile(testFile, source);
452
        JavaSource src = getJavaSource(testFile);
453
        Task<WorkingCopy> task = new Task<WorkingCopy>() {
454
            public void run(WorkingCopy copy) throws Exception {
455
                if (copy.toPhase(Phase.RESOLVED).compareTo(Phase.RESOLVED) < 0) {
456
                    return;
457
                }
458
459
                TreeMaker make = copy.getTreeMaker();
460
                ClassTree clazz = (ClassTree) copy.getCompilationUnit().getTypeDecls().get(0);
461
                MethodTree method = (MethodTree) clazz.getMembers().get(1);
462
                BlockTree block = method.getBody();
463
                IfTree original = (IfTree) block.getStatements().get(0);
464
465
                IfTree modified = make.If(
466
                        make.Parenthesized(
467
                        make.Unary(Kind.LOGICAL_COMPLEMENT, original.getCondition())),
468
                        original.getElseStatement(), null);
469
                copy.rewrite(original, modified);
470
            }
471
        };
472
473
        src.runModificationTask(task).commit();
474
        String res = TestUtilities.copyFileToString(testFile);
475
        System.out.println(res);
476
        assertEquals(golden, res);
477
    }
478
479
    public void test158154TwoIfs() throws Exception {
480
        String source = "class Test {\n"
481
                + "    void m1(boolean b) {\n"
482
                + "        if (b) ; else System.out.println(\"first hi\");\n"
483
                + "        if (b) ; else System.out.println(\"second hi\");\n"
484
                + "    }\n"
485
                + "}";
486
        String golden = "class Test {\n"
487
                + "    void m1(boolean b) {\n"
488
                + "        if (!(b)) System.out.println(\"first hi\");\n"
489
                + "        if (!(b)) System.out.println(\"second hi\");\n"
490
                + "    }\n"
491
                + "}";
492
        testFile = new File(getWorkDir(), "Test.java");
493
        TestUtilities.copyStringToFile(testFile, source);
494
        JavaSource src = getJavaSource(testFile);
495
        Task<WorkingCopy> task = new Task<WorkingCopy>() {
496
            public void run(WorkingCopy copy) throws Exception {
497
                if (copy.toPhase(Phase.RESOLVED).compareTo(Phase.RESOLVED) < 0) {
498
                    return;
499
                }
500
501
                TreeMaker make = copy.getTreeMaker();
502
                ClassTree clazz = (ClassTree) copy.getCompilationUnit().getTypeDecls().get(0);
503
                MethodTree method = (MethodTree) clazz.getMembers().get(1);
504
                BlockTree block = method.getBody();
505
                IfTree originalA = (IfTree) block.getStatements().get(0);
506
                IfTree originalB = (IfTree) block.getStatements().get(1);
507
                IfTree modifiedA = make.If(
508
                        make.Parenthesized(
509
                        make.Unary(Kind.LOGICAL_COMPLEMENT, originalA.getCondition())),
510
                        originalA.getElseStatement(), null);
511
                copy.rewrite(originalA, modifiedA);
512
                IfTree modifiedB = make.If(
513
                        make.Parenthesized(
514
                        make.Unary(Kind.LOGICAL_COMPLEMENT, originalB.getCondition())),
515
                        originalB.getElseStatement(), null);
516
                copy.rewrite(originalB, modifiedB);
517
            }
518
        };
519
        src.runModificationTask(task).commit();
520
        String res = TestUtilities.copyFileToString(testFile);
521
        System.out.println(res);
522
        assertEquals(golden, res);
523
    }
524
436
    String getGoldenPckg() {
525
    String getGoldenPckg() {
437
        return "";
526
        return "";
438
    }
527
    }

Return to bug 158154