Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The sniper printer does not behave as expected when an invocation is replaced by another invocation. #3399

Closed
fermadeiral opened this issue Jun 8, 2020 · 1 comment

Comments

@fermadeiral
Copy link
Contributor

fermadeiral commented Jun 8, 2020

Hi all,

I have the following code:

String argStr = args.toString();

And I wish to transform it to:

String argStr = Arrays.toString(args);

However, with the Sniper printer (but not with the default one) I get:

String argStr = args.toString(args);

No exception is thrown, but the printed code is not the expected one.

I hope that the following failing test case helps to find the actual issue:

        @Test
	public void testPrintReplacementOfInvocation() {
		testSniper(InvocationReplacement.class.getName(), type -> {
			CtLocalVariable localVariable = (CtLocalVariable) type.getMethodsByName("main").get(0).getBody().getStatements().get(0);
			CtInvocation invocation = (CtInvocation) localVariable.getAssignment();
			CtExpression prevTarget = invocation.getTarget();
			CtCodeSnippetExpression newTarget = type.getFactory().Code().createCodeSnippetExpression("Arrays");
			CtType arraysClass = type.getFactory().Class().get(Arrays.class);
			CtMethod method = (CtMethod) arraysClass.getMethodsByName("toString").get(0);
			CtExecutableReference refToMethod = type.getFactory().Executable().createReference(method);
			CtInvocation newInvocation = type.getFactory().Code().createInvocation(newTarget, refToMethod, prevTarget);
			invocation.replace(newInvocation);
		}, (type, printed) -> {
			assertIsPrintedWithExpectedChanges(type, printed, "\\QString argStr = args.toString();", "String argStr = Arrays.toString(args);");
		});
	}

This is the content of the InvocationReplacement class:

package spoon.test.prettyprinter.testclasses;

public class InvocationReplacement {

	public static void main(String[] args) {
		String argStr = args.toString();
	}

}
@fermadeiral
Copy link
Contributor Author

I believe this issue was solved, thus I'm closing it.

nharrand added a commit that referenced this issue Jun 14, 2020
* test(SniperPrettyPrinter): test replacement of invocation (#3399) (#3400)

* test(SniperPrettyPrinter): modification on first statement of a block should not remove new line (#3401) (#3402)

* up

* up

* @monperrus at work

* Update src/main/java/spoon/support/DefaultCoreFactory.java

* @monperrus at work

* up

* @monperrus at work

* @monperrus at work

* @monperrus at work

* up

* up

* @monperrus at work

* @monperrus at work

* @monperrus at work

Co-authored-by: Fernanda Madeiral <fer.madeiral@gmail.com>
Co-authored-by: Nicolas Harrand <nicolas.harrand@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant