--- a/java.source/src/org/netbeans/modules/java/source/save/CasualDiff.java +++ a/java.source/src/org/netbeans/modules/java/source/save/CasualDiff.java @@ -170,7 +170,7 @@ //was: // int ln = td.oldTopLevel.lineMap.getLineNumber(start); // int lineStart = td.oldTopLevel.lineMap.getStartPosition(ln); - + td.printer.setInitialOffset(lineStart); Tree current = oldTree; @@ -288,7 +288,7 @@ localPointer = diffList(oldT.getTypeDecls(), newT.getTypeDecls(), localPointer, est, Measure.MEMBER, printer); printer.print(origText.substring(localPointer)); } - + private static int getOldIndent(DiffContext diffContext, Tree t) { if (diffContext.doc != null) { try { @@ -534,7 +534,7 @@ } return bounds[1]; } - + private boolean isEnum(Tree tree) { if (tree instanceof FieldGroupTree) return ((FieldGroupTree) tree).isEnum(); if (tree instanceof VariableTree) return (((JCVariableDecl) tree).getModifiers().flags & Flags.ENUM) != 0; @@ -622,7 +622,7 @@ } if (oldT.params.isEmpty()) { // compute the position. Find the parameters closing ')', its - // start position is important for us. This is used when + // start position is important for us. This is used when // there was not any parameter in original tree. int startOffset = oldT.pos; @@ -876,8 +876,8 @@ JCTree tree = oldstats.get(oldstats.size() - 1); localPointer = adjustLocalPointer(localPointer, comments.getComments(oldT), CommentSet.RelativePosition.INNER); CommentSet cs = comments.getComments(tree); - localPointer = adjustLocalPointer(localPointer, cs, CommentSet.RelativePosition.INLINE); - localPointer = adjustLocalPointer(localPointer, cs, CommentSet.RelativePosition.TRAILING); + localPointer = adjustLocalPointer(localPointer, cs, CommentSet.RelativePosition.INLINE); + localPointer = adjustLocalPointer(localPointer, cs, CommentSet.RelativePosition.TRAILING); */ copyTo(localPointer, localPointer = endPos(oldT)); } @@ -906,7 +906,7 @@ return false; } } - + protected int diffDoLoop(JCDoWhileLoop oldT, JCDoWhileLoop newT, int[] bounds) { int localPointer = bounds[0]; @@ -1135,7 +1135,7 @@ for (Tree t = l.head; t!= null; l = l.tail, t = l.head) { printer.oldTrees.remove(t); } - } + } localPointer = diffParameterList(oldT.resources, newT.resources, parens ? new JavaTokenId[] { JavaTokenId.LPAREN, JavaTokenId.RPAREN } : null, @@ -1152,7 +1152,7 @@ } } } - + copyTo(localPointer, bodyPos[0]); localPointer = diffTree(oldT.body, newT.body, bodyPos); copyTo(localPointer, localPointer = bodyPos[1]); @@ -1503,7 +1503,7 @@ } } //#174552 end - + // rhs copyTo(localPointer, rhsBounds[0]); localPointer = diffTree(oldT.rhs, newT.rhs, rhsBounds); @@ -1858,7 +1858,7 @@ localPointer = tokenSequence.offset(); } } - + localPointer = diffAnnotationsLists(oldT.getAnnotations(), newT.getAnnotations(), startPos, localPointer); if ((oldT.flags & Flags.ANNOTATION) != 0) { @@ -1904,7 +1904,7 @@ private int diffAnnotationsLists(com.sun.tools.javac.util.List oldAnnotations, com.sun.tools.javac.util.List newAnnotations, int startPos, int localPointer) { int annotationsEnd = oldAnnotations.nonEmpty() ? endPos(oldAnnotations) : localPointer; - + if (listsMatch(oldAnnotations, newAnnotations)) { copyTo(localPointer, localPointer = (annotationsEnd != localPointer ? annotationsEnd : startPos)); } else { @@ -2344,7 +2344,7 @@ { return diffParameterList(oldList, newList, null, makeAround, pos, measure, spaceBefore, spaceAfter, isEnum, separator); } - + private int diffParameterList( List oldList, List newList, @@ -2583,7 +2583,7 @@ } case INSERT: { JCVariableDecl decl = (JCVariableDecl) item.element; - + if (oldIndex == 0) { int oldPrec = printer.setPrec(TreeInfo.noPrec); printer.visitVarDef(decl); @@ -2890,7 +2890,7 @@ printer.print(this.printer.toString()); this.printer = oldPrinter; this.printer.undent(old); - lastdel = null; + //lastdel = null; break; } } @@ -2938,7 +2938,7 @@ protected int diffPrecedingComments(JCTree oldT, JCTree newT, int localPointer) { return diffPrecedingComments(oldT, newT, getOldPos(oldT), localPointer); } - + protected int diffPrecedingComments(JCTree oldT, JCTree newT, int oldTreeStartPos, int localPointer) { CommentSet cs = comments.getComments(newT); CommentSet old = comments.getComments(oldT); @@ -2964,7 +2964,7 @@ while (printer.out.isWhitespaceLine()) printer.eatChars(1); } - + localPointer = diffCommentLists(getOldPos(oldT), oldInlineComments, newInlineComments, false, false, localPointer); boolean containedEmbeddedNewLine = false; @@ -2973,7 +2973,7 @@ for (Comment oldComment : oldInlineComments) { if (oldComment.style() == Style.LINE) containedEmbeddedNewLine = true; } - + for (Comment nueComment : newInlineComments) { if (nueComment.style() == Style.LINE) containsEmbeddedNewLine = true; } @@ -2990,7 +2990,7 @@ Iterator newIter = newList.iterator(); Comment oldC = safeNext(oldIter); Comment newC = safeNext(newIter); - + while (oldC != null && newC != null) { if (!commentsMatch(oldC, newC)) return false; oldC = safeNext(oldIter); @@ -2999,7 +2999,7 @@ return !((oldC == null) ^ (newC == null)); } - + // refactor it! make it better private int diffCommentLists(int oldTreeStartPos, ListoldList, ListnewList, boolean trailing, boolean preceding, int localPointer) { @@ -3058,7 +3058,7 @@ } while (newC != null) { if (Style.WHITESPACE != newC.style()) { -// printer.print(newC.getText()); +// printer.print(newC.getText()); if (!firstNewCommentPrinted && preceding) { copyTo(localPointer, localPointer = oldTreeStartPos); } @@ -3204,7 +3204,7 @@ return oldBounds[1]; } } - + int commentsStart = Math.min(commentStart(comments.getComments(oldT), CommentSet.RelativePosition.INLINE), commentStart(comments.getComments(oldT), CommentSet.RelativePosition.TRAILING)); if (commentsStart < elementBounds[1]) { int lastIndex; --- a/java.source/test/unit/src/org/netbeans/api/java/source/gen/IfTest.java +++ a/java.source/test/unit/src/org/netbeans/api/java/source/gen/IfTest.java @@ -59,6 +59,7 @@ import java.io.IOException; import java.util.Arrays; import java.util.Collections; +import org.junit.Ignore; import org.netbeans.junit.NbTestSuite; import org.netbeans.api.java.source.JavaSource; import org.netbeans.api.java.source.JavaSource.Phase; @@ -75,7 +76,7 @@ public IfTest(String name) { super(name); } - + public static NbTestSuite suite() { NbTestSuite suite = new NbTestSuite(); suite.addTestSuite(IfTest.class); @@ -92,8 +93,8 @@ * Test replacing then statement with empty block. */ public void testEmptyThenBlock() throws Exception { - testFile = new File(getWorkDir(), "IfTest.java"); - TestUtilities.copyStringToFile(testFile, + testFile = new File(getWorkDir(), "IfTest.java"); + TestUtilities.copyStringToFile(testFile, "package foo.bar;\n" + "\n" + "public class IfTest {\n" + @@ -113,7 +114,7 @@ " }\n" + "}\n"; JavaSource src = getJavaSource(testFile); - + Task task = new Task() { public void run(WorkingCopy workingCopy) throws IOException { @@ -136,10 +137,10 @@ System.err.println(res); assertEquals(golden, res); } - + public void testEmptyElseBlock() throws Exception { - testFile = new File(getWorkDir(), "IfTest.java"); - TestUtilities.copyStringToFile(testFile, + testFile = new File(getWorkDir(), "IfTest.java"); + TestUtilities.copyStringToFile(testFile, "package foo.bar;\n" + "\n" + "public class IfTest {\n" + @@ -161,7 +162,7 @@ " }\n" + "}\n"; JavaSource src = getJavaSource(testFile); - + Task task = new Task() { public void run(WorkingCopy workingCopy) throws IOException { @@ -184,10 +185,10 @@ System.err.println(res); assertEquals(golden, res); } - + public void testReplaceCondition() throws Exception { - testFile = new File(getWorkDir(), "IfTest.java"); - TestUtilities.copyStringToFile(testFile, + testFile = new File(getWorkDir(), "IfTest.java"); + TestUtilities.copyStringToFile(testFile, "package foo.bar;\n" + "\n" + "public class IfTest {\n" + @@ -207,7 +208,7 @@ " }\n" + "}\n"; JavaSource src = getJavaSource(testFile); - + Task task = new Task() { public void run(WorkingCopy workingCopy) throws IOException { @@ -233,7 +234,7 @@ public void testModifyingIf() throws Exception { testFile = new File(getWorkDir(), "Test.java"); - TestUtilities.copyStringToFile(testFile, + TestUtilities.copyStringToFile(testFile, "package personal;\n" + "\n" + "public class Test {\n" + @@ -245,13 +246,13 @@ " return y == 8;\n" + " }\n" + "}\n"); - String golden = + String golden = "package personal;\n" + "\n" + "public class Test {\n" + " public boolean method(int i) {\n" + " int y = 0;\n" + - " if (method(null)) {\n" + + " if (method(null)) {\n" + " return true;\n" + " }\n" + " return y == 8;\n" + @@ -281,7 +282,7 @@ ); workingCopy.rewrite(mit, nue); } - + public void cancel() { } }; @@ -398,7 +399,7 @@ System.err.println(res); assertEquals(golden, res); } - + public void test159940() throws Exception { String test = "class Test {\n" + @@ -433,6 +434,148 @@ assertEquals(golden, res); } + public void test158154OneIf() throws Exception { + String source = "class Test {\n" + + " void m1(boolean b) {\n" + + " if (b) ; else System.out.println(\"hi\");\n" + + " }\n" + + "}"; + String golden = "class Test {\n" + + " void m1(boolean b) {\n" + + " if (!(b)) System.out.println(\"hi\");\n" + + " }\n" + + "}"; + testFile = new File(getWorkDir(), "Test.java"); + + TestUtilities.copyStringToFile(testFile, source); + JavaSource src = getJavaSource(testFile); + Task task = new Task() { + public void run(WorkingCopy copy) throws Exception { + if (copy.toPhase(Phase.RESOLVED).compareTo(Phase.RESOLVED) < 0) { + return; + } + + TreeMaker make = copy.getTreeMaker(); + ClassTree clazz = (ClassTree) copy.getCompilationUnit().getTypeDecls().get(0); + MethodTree method = (MethodTree) clazz.getMembers().get(1); + BlockTree block = method.getBody(); + IfTree original = (IfTree) block.getStatements().get(0); + + IfTree modified = make.If( + make.Parenthesized( + make.Unary(Kind.LOGICAL_COMPLEMENT, original.getCondition())), + original.getElseStatement(), null); + copy.rewrite(original, modified); + } + }; + + src.runModificationTask(task).commit(); + String res = TestUtilities.copyFileToString(testFile); + System.out.println(res); + assertEquals(golden, res); + } + + public void test158154TwoIfs() throws Exception { + String source = "class Test {\n" + + " void m1(boolean b) {\n" + + " if (b) ; else System.out.println(\"first hi\");\n" + + " if (b) ; else System.out.println(\"second hi\");\n" + + " }\n" + + "}"; + String golden = "class Test {\n" + + " void m1(boolean b) {\n" + + " if (!(b)) System.out.println(\"first hi\");\n" + + " if (!(b)) System.out.println(\"second hi\");\n" + + " }\n" + + "}"; + testFile = new File(getWorkDir(), "Test.java"); + TestUtilities.copyStringToFile(testFile, source); + JavaSource src = getJavaSource(testFile); + Task task = new Task() { + public void run(WorkingCopy copy) throws Exception { + if (copy.toPhase(Phase.RESOLVED).compareTo(Phase.RESOLVED) < 0) { + return; + } + + TreeMaker make = copy.getTreeMaker(); + ClassTree clazz = (ClassTree) copy.getCompilationUnit().getTypeDecls().get(0); + MethodTree method = (MethodTree) clazz.getMembers().get(1); + BlockTree block = method.getBody(); + IfTree originalA = (IfTree) block.getStatements().get(0); + IfTree originalB = (IfTree) block.getStatements().get(1); + IfTree modifiedA = make.If( + make.Parenthesized( + make.Unary(Kind.LOGICAL_COMPLEMENT, originalA.getCondition())), + originalA.getElseStatement(), null); + copy.rewrite(originalA, modifiedA); + IfTree modifiedB = make.If( + make.Parenthesized( + make.Unary(Kind.LOGICAL_COMPLEMENT, originalB.getCondition())), + originalB.getElseStatement(), null); + copy.rewrite(originalB, modifiedB); + } + }; + src.runModificationTask(task).commit(); + String res = TestUtilities.copyFileToString(testFile); + System.out.println(res); + assertEquals(golden, res); + } + + public void test158154SeveralIfs() throws Exception { + String source = "class Test {\n" + + " void m1(boolean b) {\n" + + " if (b) ; else System.out.println(\"first hi\");\n" + + " if (b) ; else System.out.println(\"second hi\");\n" + + " if (b) ; else System.out.println(\"third hi\");\n" + + " if (b) ; else System.out.println(\"fourth hi\");\n" + + " }\n" + + "}"; + String golden = "class Test {\n" + + " void m1(boolean b) {\n" + + " if (!(b)) System.out.println(\"first hi\");\n" + + " if (!(b)) System.out.println(\"second hi\");\n" + + " if (!(b)) System.out.println(\"third hi\");\n" + + " if (!(b)) System.out.println(\"fourth hi\");\n" + + " }\n" + + "}"; + testFile = new File(getWorkDir(), "Test.java"); + TestUtilities.copyStringToFile(testFile, source); + JavaSource src = getJavaSource(testFile); + Task task = new Task() { + public void run(WorkingCopy copy) throws Exception { + if (copy.toPhase(Phase.RESOLVED).compareTo(Phase.RESOLVED) < 0) { + return; + } + + TreeMaker make = copy.getTreeMaker(); + ClassTree clazz = (ClassTree) copy.getCompilationUnit().getTypeDecls().get(0); + MethodTree method = (MethodTree) clazz.getMembers().get(1); + BlockTree block = method.getBody(); + + IfTree [] ifStatements = new IfTree[block.getStatements().size()]; + for (int i = 0; i < block.getStatements().size(); i++) { + ifStatements[i] = (IfTree) block.getStatements().get(i); + } + + IfTree [] modifiedIfs = new IfTree[block.getStatements().size()]; + for (int i = 0; i < block.getStatements().size(); i++) { + modifiedIfs[i] = make.If( + make.Parenthesized( + make.Unary(Kind.LOGICAL_COMPLEMENT, ifStatements[i].getCondition())), + ifStatements[i].getElseStatement(), null); + } + + for (int i = 0; i < block.getStatements().size(); i++) { + copy.rewrite(ifStatements[i], modifiedIfs[i]); + } + } + }; + src.runModificationTask(task).commit(); + String res = TestUtilities.copyFileToString(testFile); + System.out.println(res); + assertEquals(golden, res); + } + String getGoldenPckg() { return ""; }