diff -r 06ad65d6fab5 java.source/src/org/netbeans/modules/java/source/save/Measure.java --- a/java.source/src/org/netbeans/modules/java/source/save/Measure.java Sat May 16 19:07:21 2009 +0200 +++ b/java.source/src/org/netbeans/modules/java/source/save/Measure.java Sat May 23 16:56:52 2009 +0100 @@ -96,8 +96,8 @@ public int compare(JCTree t1, JCTree t2) { int distance = DEFAULT.compare(t1, t2); if (distance == INFINITE_DISTANCE) { - if (t1.getKind() == t2.getKind() && t1.pos == t2.pos) { - return ALMOST_THE_SAME; + if (t1.getKind() == t2.getKind()) { + return t1.pos == t2.pos ? ALMOST_THE_SAME : THE_SAME_KIND; } } return distance; diff -r 06ad65d6fab5 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 Sat May 16 19:07:21 2009 +0200 +++ b/java.source/test/unit/src/org/netbeans/api/java/source/gen/IfTest.java Sat May 23 16:56:52 2009 +0100 @@ -50,6 +50,7 @@ import com.sun.source.tree.MethodTree; import com.sun.source.tree.ParenthesizedTree; import com.sun.source.tree.StatementTree; +import com.sun.source.tree.Tree; import com.sun.source.tree.Tree.Kind; import java.io.File; import java.io.IOException; @@ -395,6 +396,58 @@ assertEquals(golden, res); } + public void test158154() throws Exception { + String test = "class Test {\n" + + " void m2(boolean b) {\n" + + " i|f (b); else \n" + + " System.out.println(\"hi\");\n" + + " \n" + + " i|f (b); else \n" + + " System.out.println(\"hi\");\n" + + " \n" + + " }\n" + + "}"; + String golden = "class Test {\n" + + " void m2(boolean b) {\n" + + " if (!(b))System.out.println(\"hi\");\n" + + " \n" + + " if (!(b))System.out.println(\"hi\");\n" + + " \n" + + " }\n" + + "}"; + testFile = new File(getWorkDir(), "Test.java"); + final int indexA = test.indexOf("|"); + final int indexB = test.lastIndexOf("|") - 1; + assertTrue(indexA != -1); + assertTrue(indexB != -1); + TestUtilities.copyStringToFile(testFile, test.replace("|", "")); + 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; + } + Tree nodeA = copy.getTreeUtilities().pathFor(indexA).getLeaf(); + Tree nodeB = copy.getTreeUtilities().pathFor(indexB).getLeaf(); + for (Tree n : new Tree[]{nodeA, nodeB}) { + assertEquals(Kind.IF, n.getKind()); + TreeMaker make = copy.getTreeMaker(); + IfTree original = (IfTree) n; + IfTree modified = make.If( + make.Parenthesized( + make.Unary(Kind.LOGICAL_COMPLEMENT, original.getCondition())), + original.getElseStatement(), null); + copy.rewrite(n, modified); + } + } + }; + src.runModificationTask(task).commit(); + String res = TestUtilities.copyFileToString(testFile); + System.out.println(res); + assertEquals(golden, res); + } + String getGoldenPckg() { return ""; }