Please use the Apache issue tracking system for new NetBeans issues (https://issues.apache.org/jira/projects/NETBEANS0/issues) !!
View | Details | Raw Unified | Return to bug 158154 | Differences between
and this patch

Collapse All | Expand All

(-)a/java.source/src/org/netbeans/modules/java/source/save/CasualDiff.java (-22 / +22 lines)
 Lines 170-176    Link Here 
170
        //was:
170
        //was:
171
//        int ln = td.oldTopLevel.lineMap.getLineNumber(start);
171
//        int ln = td.oldTopLevel.lineMap.getLineNumber(start);
172
//        int lineStart = td.oldTopLevel.lineMap.getStartPosition(ln);
172
//        int lineStart = td.oldTopLevel.lineMap.getStartPosition(ln);
173
        
173
174
        td.printer.setInitialOffset(lineStart);
174
        td.printer.setInitialOffset(lineStart);
175
175
176
        Tree current = oldTree;
176
        Tree current = oldTree;
 Lines 288-294    Link Here 
288
        localPointer = diffList(oldT.getTypeDecls(), newT.getTypeDecls(), localPointer, est, Measure.MEMBER, printer);
288
        localPointer = diffList(oldT.getTypeDecls(), newT.getTypeDecls(), localPointer, est, Measure.MEMBER, printer);
289
        printer.print(origText.substring(localPointer));
289
        printer.print(origText.substring(localPointer));
290
    }
290
    }
291
    
291
292
    private static int getOldIndent(DiffContext diffContext, Tree t) {
292
    private static int getOldIndent(DiffContext diffContext, Tree t) {
293
        if (diffContext.doc != null) {
293
        if (diffContext.doc != null) {
294
            try {
294
            try {
 Lines 534-540    Link Here 
534
        }
534
        }
535
        return bounds[1];
535
        return bounds[1];
536
    }
536
    }
537
        
537
538
    private boolean isEnum(Tree tree) {
538
    private boolean isEnum(Tree tree) {
539
        if (tree instanceof FieldGroupTree) return ((FieldGroupTree) tree).isEnum();
539
        if (tree instanceof FieldGroupTree) return ((FieldGroupTree) tree).isEnum();
540
        if (tree instanceof VariableTree) return (((JCVariableDecl) tree).getModifiers().flags & Flags.ENUM) != 0;
540
        if (tree instanceof VariableTree) return (((JCVariableDecl) tree).getModifiers().flags & Flags.ENUM) != 0;
 Lines 622-628    Link Here 
622
        }
622
        }
623
        if (oldT.params.isEmpty()) {
623
        if (oldT.params.isEmpty()) {
624
            // compute the position. Find the parameters closing ')', its
624
            // compute the position. Find the parameters closing ')', its
625
            // start position is important for us. This is used when 
625
            // start position is important for us. This is used when
626
            // there was not any parameter in original tree.
626
            // there was not any parameter in original tree.
627
            int startOffset = oldT.pos;
627
            int startOffset = oldT.pos;
628
628
 Lines 876-883    Link Here 
876
            JCTree tree = oldstats.get(oldstats.size() - 1);
876
            JCTree tree = oldstats.get(oldstats.size() - 1);
877
            localPointer = adjustLocalPointer(localPointer, comments.getComments(oldT), CommentSet.RelativePosition.INNER);
877
            localPointer = adjustLocalPointer(localPointer, comments.getComments(oldT), CommentSet.RelativePosition.INNER);
878
            CommentSet cs = comments.getComments(tree);
878
            CommentSet cs = comments.getComments(tree);
879
            localPointer = adjustLocalPointer(localPointer, cs, CommentSet.RelativePosition.INLINE);            
879
            localPointer = adjustLocalPointer(localPointer, cs, CommentSet.RelativePosition.INLINE);
880
            localPointer = adjustLocalPointer(localPointer, cs, CommentSet.RelativePosition.TRAILING);            
880
            localPointer = adjustLocalPointer(localPointer, cs, CommentSet.RelativePosition.TRAILING);
881
*/
881
*/
882
            copyTo(localPointer, localPointer = endPos(oldT));
882
            copyTo(localPointer, localPointer = endPos(oldT));
883
        }
883
        }
 Lines 906-912    Link Here 
906
                return false;
906
                return false;
907
        }
907
        }
908
    }
908
    }
909
    
909
910
910
911
    protected int diffDoLoop(JCDoWhileLoop oldT, JCDoWhileLoop newT, int[] bounds) {
911
    protected int diffDoLoop(JCDoWhileLoop oldT, JCDoWhileLoop newT, int[] bounds) {
912
        int localPointer = bounds[0];
912
        int localPointer = bounds[0];
 Lines 1135-1141    Link Here 
1135
                    for (Tree t = l.head; t!= null; l = l.tail, t = l.head) {
1135
                    for (Tree t = l.head; t!= null; l = l.tail, t = l.head) {
1136
                        printer.oldTrees.remove(t);
1136
                        printer.oldTrees.remove(t);
1137
                    }
1137
                    }
1138
                }  
1138
                }
1139
                localPointer = diffParameterList(oldT.resources,
1139
                localPointer = diffParameterList(oldT.resources,
1140
                        newT.resources,
1140
                        newT.resources,
1141
                        parens ? new JavaTokenId[] { JavaTokenId.LPAREN, JavaTokenId.RPAREN } : null,
1141
                        parens ? new JavaTokenId[] { JavaTokenId.LPAREN, JavaTokenId.RPAREN } : null,
 Lines 1152-1158    Link Here 
1152
                }
1152
                }
1153
            }
1153
            }
1154
        }
1154
        }
1155
        
1155
1156
        copyTo(localPointer, bodyPos[0]);
1156
        copyTo(localPointer, bodyPos[0]);
1157
        localPointer = diffTree(oldT.body, newT.body, bodyPos);
1157
        localPointer = diffTree(oldT.body, newT.body, bodyPos);
1158
        copyTo(localPointer, localPointer = bodyPos[1]);
1158
        copyTo(localPointer, localPointer = bodyPos[1]);
 Lines 1503-1509    Link Here 
1503
            }
1503
            }
1504
        }
1504
        }
1505
        //#174552 end
1505
        //#174552 end
1506
        
1506
1507
        // rhs
1507
        // rhs
1508
        copyTo(localPointer, rhsBounds[0]);
1508
        copyTo(localPointer, rhsBounds[0]);
1509
        localPointer = diffTree(oldT.rhs, newT.rhs, rhsBounds);
1509
        localPointer = diffTree(oldT.rhs, newT.rhs, rhsBounds);
 Lines 1858-1864    Link Here 
1858
                localPointer = tokenSequence.offset();
1858
                localPointer = tokenSequence.offset();
1859
            }
1859
            }
1860
        }
1860
        }
1861
        
1861
1862
        localPointer = diffAnnotationsLists(oldT.getAnnotations(), newT.getAnnotations(), startPos, localPointer);
1862
        localPointer = diffAnnotationsLists(oldT.getAnnotations(), newT.getAnnotations(), startPos, localPointer);
1863
1863
1864
        if ((oldT.flags & Flags.ANNOTATION) != 0) {
1864
        if ((oldT.flags & Flags.ANNOTATION) != 0) {
 Lines 1904-1910    Link Here 
1904
1904
1905
    private int diffAnnotationsLists(com.sun.tools.javac.util.List<JCAnnotation> oldAnnotations, com.sun.tools.javac.util.List<JCAnnotation> newAnnotations, int startPos, int localPointer) {
1905
    private int diffAnnotationsLists(com.sun.tools.javac.util.List<JCAnnotation> oldAnnotations, com.sun.tools.javac.util.List<JCAnnotation> newAnnotations, int startPos, int localPointer) {
1906
        int annotationsEnd = oldAnnotations.nonEmpty() ? endPos(oldAnnotations) : localPointer;
1906
        int annotationsEnd = oldAnnotations.nonEmpty() ? endPos(oldAnnotations) : localPointer;
1907
        
1907
1908
        if (listsMatch(oldAnnotations, newAnnotations)) {
1908
        if (listsMatch(oldAnnotations, newAnnotations)) {
1909
            copyTo(localPointer, localPointer = (annotationsEnd != localPointer ? annotationsEnd : startPos));
1909
            copyTo(localPointer, localPointer = (annotationsEnd != localPointer ? annotationsEnd : startPos));
1910
        } else {
1910
        } else {
 Lines 2344-2350    Link Here 
2344
    {
2344
    {
2345
        return diffParameterList(oldList, newList, null, makeAround, pos, measure, spaceBefore, spaceAfter, isEnum, separator);
2345
        return diffParameterList(oldList, newList, null, makeAround, pos, measure, spaceBefore, spaceAfter, isEnum, separator);
2346
    }
2346
    }
2347
    
2347
2348
    private int diffParameterList(
2348
    private int diffParameterList(
2349
            List<? extends JCTree> oldList,
2349
            List<? extends JCTree> oldList,
2350
            List<? extends JCTree> newList,
2350
            List<? extends JCTree> newList,
 Lines 2583-2589    Link Here 
2583
                }
2583
                }
2584
                case INSERT: {
2584
                case INSERT: {
2585
                    JCVariableDecl decl = (JCVariableDecl) item.element;
2585
                    JCVariableDecl decl = (JCVariableDecl) item.element;
2586
                    
2586
2587
                    if (oldIndex == 0) {
2587
                    if (oldIndex == 0) {
2588
                        int oldPrec = printer.setPrec(TreeInfo.noPrec);
2588
                        int oldPrec = printer.setPrec(TreeInfo.noPrec);
2589
                        printer.visitVarDef(decl);
2589
                        printer.visitVarDef(decl);
 Lines 2890-2896    Link Here 
2890
                                printer.print(this.printer.toString());
2890
                                printer.print(this.printer.toString());
2891
                                this.printer = oldPrinter;
2891
                                this.printer = oldPrinter;
2892
                                this.printer.undent(old);
2892
                                this.printer.undent(old);
2893
                                lastdel = null;
2893
                                //lastdel = null;
2894
                                break;
2894
                                break;
2895
                            }
2895
                            }
2896
                        }
2896
                        }
 Lines 2938-2944    Link Here 
2938
    protected int diffPrecedingComments(JCTree oldT, JCTree newT, int localPointer) {
2938
    protected int diffPrecedingComments(JCTree oldT, JCTree newT, int localPointer) {
2939
        return diffPrecedingComments(oldT, newT, getOldPos(oldT), localPointer);
2939
        return diffPrecedingComments(oldT, newT, getOldPos(oldT), localPointer);
2940
    }
2940
    }
2941
    
2941
2942
    protected int diffPrecedingComments(JCTree oldT, JCTree newT, int oldTreeStartPos, int localPointer) {
2942
    protected int diffPrecedingComments(JCTree oldT, JCTree newT, int oldTreeStartPos, int localPointer) {
2943
        CommentSet cs = comments.getComments(newT);
2943
        CommentSet cs = comments.getComments(newT);
2944
        CommentSet old = comments.getComments(oldT);
2944
        CommentSet old = comments.getComments(oldT);
 Lines 2964-2970    Link Here 
2964
            while (printer.out.isWhitespaceLine())
2964
            while (printer.out.isWhitespaceLine())
2965
                printer.eatChars(1);
2965
                printer.eatChars(1);
2966
        }
2966
        }
2967
        
2967
2968
        localPointer = diffCommentLists(getOldPos(oldT), oldInlineComments, newInlineComments, false, false, localPointer);
2968
        localPointer = diffCommentLists(getOldPos(oldT), oldInlineComments, newInlineComments, false, false, localPointer);
2969
2969
2970
        boolean containedEmbeddedNewLine = false;
2970
        boolean containedEmbeddedNewLine = false;
 Lines 2973-2979    Link Here 
2973
        for (Comment oldComment : oldInlineComments) {
2973
        for (Comment oldComment : oldInlineComments) {
2974
            if (oldComment.style() == Style.LINE) containedEmbeddedNewLine = true;
2974
            if (oldComment.style() == Style.LINE) containedEmbeddedNewLine = true;
2975
        }
2975
        }
2976
        
2976
2977
        for (Comment nueComment : newInlineComments) {
2977
        for (Comment nueComment : newInlineComments) {
2978
            if (nueComment.style() == Style.LINE) containsEmbeddedNewLine = true;
2978
            if (nueComment.style() == Style.LINE) containsEmbeddedNewLine = true;
2979
        }
2979
        }
 Lines 2990-2996    Link Here 
2990
        Iterator<Comment> newIter = newList.iterator();
2990
        Iterator<Comment> newIter = newList.iterator();
2991
        Comment oldC = safeNext(oldIter);
2991
        Comment oldC = safeNext(oldIter);
2992
        Comment newC = safeNext(newIter);
2992
        Comment newC = safeNext(newIter);
2993
        
2993
2994
        while (oldC != null && newC != null) {
2994
        while (oldC != null && newC != null) {
2995
            if (!commentsMatch(oldC, newC)) return false;
2995
            if (!commentsMatch(oldC, newC)) return false;
2996
            oldC = safeNext(oldIter);
2996
            oldC = safeNext(oldIter);
 Lines 2999-3005    Link Here 
2999
2999
3000
        return !((oldC == null) ^ (newC == null));
3000
        return !((oldC == null) ^ (newC == null));
3001
    }
3001
    }
3002
    
3002
3003
    // refactor it! make it better
3003
    // refactor it! make it better
3004
    private int diffCommentLists(int oldTreeStartPos, List<Comment>oldList,
3004
    private int diffCommentLists(int oldTreeStartPos, List<Comment>oldList,
3005
                                  List<Comment>newList, boolean trailing, boolean preceding, int localPointer) {
3005
                                  List<Comment>newList, boolean trailing, boolean preceding, int localPointer) {
 Lines 3058-3064    Link Here 
3058
        }
3058
        }
3059
        while (newC != null) {
3059
        while (newC != null) {
3060
            if (Style.WHITESPACE != newC.style()) {
3060
            if (Style.WHITESPACE != newC.style()) {
3061
//                printer.print(newC.getText());                
3061
//                printer.print(newC.getText());
3062
                if (!firstNewCommentPrinted && preceding) {
3062
                if (!firstNewCommentPrinted && preceding) {
3063
                    copyTo(localPointer, localPointer = oldTreeStartPos);
3063
                    copyTo(localPointer, localPointer = oldTreeStartPos);
3064
                }
3064
                }
 Lines 3204-3210    Link Here 
3204
                return oldBounds[1];
3204
                return oldBounds[1];
3205
            }
3205
            }
3206
        }
3206
        }
3207
        
3207
3208
        int commentsStart = Math.min(commentStart(comments.getComments(oldT), CommentSet.RelativePosition.INLINE), commentStart(comments.getComments(oldT), CommentSet.RelativePosition.TRAILING));
3208
        int commentsStart = Math.min(commentStart(comments.getComments(oldT), CommentSet.RelativePosition.INLINE), commentStart(comments.getComments(oldT), CommentSet.RelativePosition.TRAILING));
3209
        if (commentsStart < elementBounds[1]) {
3209
        if (commentsStart < elementBounds[1]) {
3210
            int lastIndex;
3210
            int lastIndex;
(-)a/java.source/test/unit/src/org/netbeans/api/java/source/gen/IfTest.java (-17 / +160 lines)
 Lines 59-64    Link Here 
59
import java.io.IOException;
59
import java.io.IOException;
60
import java.util.Arrays;
60
import java.util.Arrays;
61
import java.util.Collections;
61
import java.util.Collections;
62
import org.junit.Ignore;
62
import org.netbeans.junit.NbTestSuite;
63
import org.netbeans.junit.NbTestSuite;
63
import org.netbeans.api.java.source.JavaSource;
64
import org.netbeans.api.java.source.JavaSource;
64
import org.netbeans.api.java.source.JavaSource.Phase;
65
import org.netbeans.api.java.source.JavaSource.Phase;
 Lines 75-81    Link Here 
75
    public IfTest(String name) {
76
    public IfTest(String name) {
76
        super(name);
77
        super(name);
77
    }
78
    }
78
    
79
79
    public static NbTestSuite suite() {
80
    public static NbTestSuite suite() {
80
        NbTestSuite suite = new NbTestSuite();
81
        NbTestSuite suite = new NbTestSuite();
81
        suite.addTestSuite(IfTest.class);
82
        suite.addTestSuite(IfTest.class);
 Lines 92-99    Link Here 
92
     * Test replacing then statement with empty block.
93
     * Test replacing then statement with empty block.
93
     */
94
     */
94
    public void testEmptyThenBlock() throws Exception {
95
    public void testEmptyThenBlock() throws Exception {
95
        testFile = new File(getWorkDir(), "IfTest.java");        
96
        testFile = new File(getWorkDir(), "IfTest.java");
96
        TestUtilities.copyStringToFile(testFile, 
97
        TestUtilities.copyStringToFile(testFile,
97
            "package foo.bar;\n" +
98
            "package foo.bar;\n" +
98
            "\n" +
99
            "\n" +
99
            "public class IfTest {\n" +
100
            "public class IfTest {\n" +
 Lines 113-119    Link Here 
113
            "    }\n" +
114
            "    }\n" +
114
            "}\n";
115
            "}\n";
115
        JavaSource src = getJavaSource(testFile);
116
        JavaSource src = getJavaSource(testFile);
116
        
117
117
        Task<WorkingCopy> task = new Task<WorkingCopy>() {
118
        Task<WorkingCopy> task = new Task<WorkingCopy>() {
118
119
119
            public void run(WorkingCopy workingCopy) throws IOException {
120
            public void run(WorkingCopy workingCopy) throws IOException {
 Lines 136-145    Link Here 
136
        System.err.println(res);
137
        System.err.println(res);
137
        assertEquals(golden, res);
138
        assertEquals(golden, res);
138
    }
139
    }
139
    
140
140
    public void testEmptyElseBlock() throws Exception {
141
    public void testEmptyElseBlock() throws Exception {
141
        testFile = new File(getWorkDir(), "IfTest.java");        
142
        testFile = new File(getWorkDir(), "IfTest.java");
142
        TestUtilities.copyStringToFile(testFile, 
143
        TestUtilities.copyStringToFile(testFile,
143
            "package foo.bar;\n" +
144
            "package foo.bar;\n" +
144
            "\n" +
145
            "\n" +
145
            "public class IfTest {\n" +
146
            "public class IfTest {\n" +
 Lines 161-167    Link Here 
161
            "    }\n" +
162
            "    }\n" +
162
            "}\n";
163
            "}\n";
163
        JavaSource src = getJavaSource(testFile);
164
        JavaSource src = getJavaSource(testFile);
164
        
165
165
        Task<WorkingCopy> task = new Task<WorkingCopy>() {
166
        Task<WorkingCopy> task = new Task<WorkingCopy>() {
166
167
167
            public void run(WorkingCopy workingCopy) throws IOException {
168
            public void run(WorkingCopy workingCopy) throws IOException {
 Lines 184-193    Link Here 
184
        System.err.println(res);
185
        System.err.println(res);
185
        assertEquals(golden, res);
186
        assertEquals(golden, res);
186
    }
187
    }
187
    
188
188
    public void testReplaceCondition() throws Exception {
189
    public void testReplaceCondition() throws Exception {
189
        testFile = new File(getWorkDir(), "IfTest.java");        
190
        testFile = new File(getWorkDir(), "IfTest.java");
190
        TestUtilities.copyStringToFile(testFile, 
191
        TestUtilities.copyStringToFile(testFile,
191
            "package foo.bar;\n" +
192
            "package foo.bar;\n" +
192
            "\n" +
193
            "\n" +
193
            "public class IfTest {\n" +
194
            "public class IfTest {\n" +
 Lines 207-213    Link Here 
207
            "    }\n" +
208
            "    }\n" +
208
            "}\n";
209
            "}\n";
209
        JavaSource src = getJavaSource(testFile);
210
        JavaSource src = getJavaSource(testFile);
210
        
211
211
        Task<WorkingCopy> task = new Task<WorkingCopy>() {
212
        Task<WorkingCopy> task = new Task<WorkingCopy>() {
212
213
213
            public void run(WorkingCopy workingCopy) throws IOException {
214
            public void run(WorkingCopy workingCopy) throws IOException {
 Lines 233-239    Link Here 
233
234
234
    public void testModifyingIf() throws Exception {
235
    public void testModifyingIf() throws Exception {
235
        testFile = new File(getWorkDir(), "Test.java");
236
        testFile = new File(getWorkDir(), "Test.java");
236
        TestUtilities.copyStringToFile(testFile, 
237
        TestUtilities.copyStringToFile(testFile,
237
            "package personal;\n" +
238
            "package personal;\n" +
238
            "\n" +
239
            "\n" +
239
            "public class Test {\n" +
240
            "public class Test {\n" +
 Lines 245-257    Link Here 
245
            "        return y == 8;\n" +
246
            "        return y == 8;\n" +
246
            "    }\n" +
247
            "    }\n" +
247
            "}\n");
248
            "}\n");
248
         String golden = 
249
         String golden =
249
            "package personal;\n" +
250
            "package personal;\n" +
250
            "\n" +
251
            "\n" +
251
            "public class Test {\n" +
252
            "public class Test {\n" +
252
            "    public boolean method(int i) {\n" +
253
            "    public boolean method(int i) {\n" +
253
            "        int y = 0;\n" +
254
            "        int y = 0;\n" +
254
            "        if (method(null)) {\n" + 
255
            "        if (method(null)) {\n" +
255
            "            return true;\n" +
256
            "            return true;\n" +
256
            "        }\n" +
257
            "        }\n" +
257
            "        return y == 8;\n" +
258
            "        return y == 8;\n" +
 Lines 281-287    Link Here 
281
                );
282
                );
282
                workingCopy.rewrite(mit, nue);
283
                workingCopy.rewrite(mit, nue);
283
            }
284
            }
284
            
285
285
            public void cancel() {
286
            public void cancel() {
286
            }
287
            }
287
        };
288
        };
 Lines 398-404    Link Here 
398
        System.err.println(res);
399
        System.err.println(res);
399
        assertEquals(golden, res);
400
        assertEquals(golden, res);
400
    }
401
    }
401
    
402
402
    public void test159940() throws Exception {
403
    public void test159940() throws Exception {
403
        String test =
404
        String test =
404
                "class Test {\n" +
405
                "class Test {\n" +
 Lines 433-438    Link Here 
433
        assertEquals(golden, res);
434
        assertEquals(golden, res);
434
    }
435
    }
435
436
437
    public void test158154OneIf() throws Exception {
438
        String source = "class Test {\n"
439
                + "    void m1(boolean b) {\n"
440
                + "        if (b) ; else System.out.println(\"hi\");\n"
441
                + "    }\n"
442
                + "}";
443
        String golden = "class Test {\n"
444
                + "    void m1(boolean b) {\n"
445
                + "        if (!(b)) System.out.println(\"hi\");\n"
446
                + "    }\n"
447
                + "}";
448
        testFile = new File(getWorkDir(), "Test.java");
449
450
        TestUtilities.copyStringToFile(testFile, source);
451
        JavaSource src = getJavaSource(testFile);
452
        Task<WorkingCopy> task = new Task<WorkingCopy>() {
453
            public void run(WorkingCopy copy) throws Exception {
454
                if (copy.toPhase(Phase.RESOLVED).compareTo(Phase.RESOLVED) < 0) {
455
                    return;
456
                }
457
458
                TreeMaker make = copy.getTreeMaker();
459
                ClassTree clazz = (ClassTree) copy.getCompilationUnit().getTypeDecls().get(0);
460
                MethodTree method = (MethodTree) clazz.getMembers().get(1);
461
                BlockTree block = method.getBody();
462
                IfTree original = (IfTree) block.getStatements().get(0);
463
464
                IfTree modified = make.If(
465
                        make.Parenthesized(
466
                        make.Unary(Kind.LOGICAL_COMPLEMENT, original.getCondition())),
467
                        original.getElseStatement(), null);
468
                copy.rewrite(original, modified);
469
            }
470
        };
471
472
        src.runModificationTask(task).commit();
473
        String res = TestUtilities.copyFileToString(testFile);
474
        System.out.println(res);
475
        assertEquals(golden, res);
476
    }
477
478
    public void test158154TwoIfs() throws Exception {
479
        String source = "class Test {\n"
480
                + "    void m1(boolean b) {\n"
481
                + "        if (b) ; else System.out.println(\"first hi\");\n"
482
                + "        if (b) ; else System.out.println(\"second hi\");\n"
483
                + "    }\n"
484
                + "}";
485
        String golden = "class Test {\n"
486
                + "    void m1(boolean b) {\n"
487
                + "        if (!(b)) System.out.println(\"first hi\");\n"
488
                + "        if (!(b)) System.out.println(\"second hi\");\n"
489
                + "    }\n"
490
                + "}";
491
        testFile = new File(getWorkDir(), "Test.java");
492
        TestUtilities.copyStringToFile(testFile, source);
493
        JavaSource src = getJavaSource(testFile);
494
        Task<WorkingCopy> task = new Task<WorkingCopy>() {
495
            public void run(WorkingCopy copy) throws Exception {
496
                if (copy.toPhase(Phase.RESOLVED).compareTo(Phase.RESOLVED) < 0) {
497
                    return;
498
                }
499
500
                TreeMaker make = copy.getTreeMaker();
501
                ClassTree clazz = (ClassTree) copy.getCompilationUnit().getTypeDecls().get(0);
502
                MethodTree method = (MethodTree) clazz.getMembers().get(1);
503
                BlockTree block = method.getBody();
504
                IfTree originalA = (IfTree) block.getStatements().get(0);
505
                IfTree originalB = (IfTree) block.getStatements().get(1);
506
                IfTree modifiedA = make.If(
507
                        make.Parenthesized(
508
                        make.Unary(Kind.LOGICAL_COMPLEMENT, originalA.getCondition())),
509
                        originalA.getElseStatement(), null);
510
                copy.rewrite(originalA, modifiedA);
511
                IfTree modifiedB = make.If(
512
                        make.Parenthesized(
513
                        make.Unary(Kind.LOGICAL_COMPLEMENT, originalB.getCondition())),
514
                        originalB.getElseStatement(), null);
515
                copy.rewrite(originalB, modifiedB);
516
            }
517
        };
518
        src.runModificationTask(task).commit();
519
        String res = TestUtilities.copyFileToString(testFile);
520
        System.out.println(res);
521
        assertEquals(golden, res);
522
    }
523
524
    public void test158154SeveralIfs() throws Exception {
525
        String source = "class Test {\n"
526
                + "    void m1(boolean b) {\n"
527
                + "        if (b) ; else System.out.println(\"first hi\");\n"
528
                + "        if (b) ; else System.out.println(\"second hi\");\n"
529
                + "        if (b) ; else System.out.println(\"third hi\");\n"
530
                + "        if (b) ; else System.out.println(\"fourth hi\");\n"
531
                + "    }\n"
532
                + "}";
533
        String golden = "class Test {\n"
534
                + "    void m1(boolean b) {\n"
535
                + "        if (!(b)) System.out.println(\"first hi\");\n"
536
                + "        if (!(b)) System.out.println(\"second hi\");\n"
537
                + "        if (!(b)) System.out.println(\"third hi\");\n"
538
                + "        if (!(b)) System.out.println(\"fourth hi\");\n"
539
                + "    }\n"
540
                + "}";
541
        testFile = new File(getWorkDir(), "Test.java");
542
        TestUtilities.copyStringToFile(testFile, source);
543
        JavaSource src = getJavaSource(testFile);
544
        Task<WorkingCopy> task = new Task<WorkingCopy>() {
545
            public void run(WorkingCopy copy) throws Exception {
546
                if (copy.toPhase(Phase.RESOLVED).compareTo(Phase.RESOLVED) < 0) {
547
                    return;
548
                }
549
550
                TreeMaker make = copy.getTreeMaker();
551
                ClassTree clazz = (ClassTree) copy.getCompilationUnit().getTypeDecls().get(0);
552
                MethodTree method = (MethodTree) clazz.getMembers().get(1);
553
                BlockTree block = method.getBody();
554
555
                IfTree [] ifStatements = new IfTree[block.getStatements().size()];
556
                for (int i = 0; i < block.getStatements().size(); i++) {
557
                    ifStatements[i] = (IfTree) block.getStatements().get(i);
558
                }
559
560
                IfTree [] modifiedIfs = new IfTree[block.getStatements().size()];
561
                for (int i = 0; i < block.getStatements().size(); i++) {
562
                    modifiedIfs[i] = make.If(
563
                        make.Parenthesized(
564
                        make.Unary(Kind.LOGICAL_COMPLEMENT, ifStatements[i].getCondition())),
565
                        ifStatements[i].getElseStatement(), null);
566
                }
567
568
                for (int i = 0; i < block.getStatements().size(); i++) {
569
                    copy.rewrite(ifStatements[i], modifiedIfs[i]);
570
                }
571
            }
572
        };
573
        src.runModificationTask(task).commit();
574
        String res = TestUtilities.copyFileToString(testFile);
575
        System.out.println(res);
576
        assertEquals(golden, res);
577
    }
578
436
    String getGoldenPckg() {
579
    String getGoldenPckg() {
437
        return "";
580
        return "";
438
    }
581
    }

Return to bug 158154
By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2014, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo