diff -r b55daa2abcb0 java.source/test/unit/src/org/netbeans/api/java/source/gen/Method1Test.java --- a/java.source/test/unit/src/org/netbeans/api/java/source/gen/Method1Test.java Mon May 11 13:04:02 2009 +0200 +++ b/java.source/test/unit/src/org/netbeans/api/java/source/gen/Method1Test.java Sun May 17 01:27:24 2009 +0100 @@ -41,6 +41,8 @@ package org.netbeans.api.java.source.gen; import com.sun.source.tree.*; +import com.sun.source.tree.Tree.Kind; +import java.io.File; import java.util.*; import java.io.IOException; import javax.lang.model.element.Modifier; @@ -70,6 +72,8 @@ suite.addTest(new Method1Test("testMethodParameterChange")); suite.addTest(new Method1Test("testMethodThrows")); suite.addTest(new Method1Test("testMethodReturnType")); + suite.addTest(new Method1Test("test159944")); + suite.addTest(new Method1Test("test159944b")); // suite.addTest(new Method1Test("testMethodBody")); // suite.addTest(new Method1Test("testParameterizedMethod")); // suite.addTest(new Method1Test("testAddRemoveInOneTrans")); @@ -242,6 +246,110 @@ assertFiles("testMethodReturnType.pass"); } + public void test159944() throws Exception { + String test = + "class Test {\n" + + " int plus(int x, int y) {\n" + + " return x + y;\n" + + " }\n" + + " void m2() {\n" + + " plus(|1, plus(2, 3));\n" + + " }\n" + + "}"; + String golden = + "class Test {\n" + + " int plus(int x, int y) {\n" + + " return x + y;\n" + + " }\n" + + " void m2() {\n" + + " plus(plus(2, 3), 1);\n" + + " }\n" + + "}"; + testFile = new File(getWorkDir(), "Test.java"); + final int indexA = test.indexOf("|"); + assertTrue(indexA != -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 node = copy.getTreeUtilities().pathFor(indexA).getLeaf(); + assertEquals(Kind.METHOD_INVOCATION, node.getKind()); + TreeMaker make = copy.getTreeMaker(); + MethodInvocationTree original = (MethodInvocationTree) node; + List oldArgs = original.getArguments(); + List newArgs = new ArrayList(); + newArgs.add(oldArgs.get(1)); + newArgs.add(oldArgs.get(0)); + @SuppressWarnings("unchecked") + MethodInvocationTree modified = make.MethodInvocation( + (List) original.getTypeArguments(), + original.getMethodSelect(), newArgs); + System.out.println("original: " + node); + System.out.println("modified: " + modified); + copy.rewrite(node, modified); } + }; + src.runModificationTask(task).commit(); + String res = TestUtilities.copyFileToString(testFile); + assertEquals(golden, res); + } + + public void test159944b() throws Exception { + // XXX should also test annotations + // XXX expected whitespace might not be correct in golden + String test = + "class Test {\n" + + " int plus(int x, int y) {\n" + + " return x + y;\n" + + " }\n" + + " void m2() {\n" + + " plus(|/*foo*/ 1, /*bar*/ plus(2, 3));\n" + + " }\n" + + "}"; + String golden = + "class Test {\n" + + " int plus(int x, int y) {\n" + + " return x + y;\n" + + " }\n" + + " void m2() {\n" + + " plus(/*bar*/ plus(2, 3), /*foo*/ 1);\n" + + " }\n" + + "}"; + testFile = new File(getWorkDir(), "Test.java"); + final int indexA = test.indexOf("|"); + assertTrue(indexA != -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 node = copy.getTreeUtilities().pathFor(indexA).getLeaf(); + assertEquals(Kind.METHOD_INVOCATION, node.getKind()); + TreeMaker make = copy.getTreeMaker(); + MethodInvocationTree original = (MethodInvocationTree) node; + List oldArgs = original.getArguments(); + List newArgs = new ArrayList(); + newArgs.add(oldArgs.get(1)); + newArgs.add(oldArgs.get(0)); + @SuppressWarnings("unchecked") + MethodInvocationTree modified = make.MethodInvocation( + (List) original.getTypeArguments(), + original.getMethodSelect(), newArgs); + System.out.println("original: " + node); + System.out.println("modified: " + modified); + copy.rewrite(node, modified); } + }; + src.runModificationTask(task).commit(); + String res = TestUtilities.copyFileToString(testFile); + assertEquals(golden, res); + } + /** * Tests method body. */