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

Refaster block expression template fails to match a single statement that is not wrapped in a block #4283

Open
trebele-tibco opened this issue Feb 9, 2024 · 0 comments

Comments

@trebele-tibco
Copy link

I'm using error_prone_refaster-2.24.1.jar and error_prone_core-2.24.1-with-dependencies.jar to refactor code. I noticed that the block do not get applied in some cases. I'll explain with an example. Here the class that I want to refactor:

import java.util.*;
public class RefasterExample {
	public void method1() {
		if (true) System.out.println("bad");
		if (true) { System.out.println("good"); }
	}

	public void method2() {
		if (true) Arrays.asList("bad");
		if (true) { Arrays.asList("good"); }
	}
}

I tried the following template:

	static class ExampleRule1 {
		@BeforeTemplate void match(String o) {
			System.out.println(o);
		}

		@AfterTemplate void optimizedMethod(String o) {
			System.out.println("debug: " + o);
		}
	}

However, it only matches the "good" line of method1:

@@ -3,5 +3,5 @@
 	public void method1() {
 		if (true) System.out.println("bad");
-		if (true) { System.out.println("good"); }
+		if (true) { System.out.println("debug: " + "good"); }
 	}

When adding a return to the match, the template fails to match altogether. Adding a return to optimizedMethod, but not to match leads to an exception (not sure whether it's worth investigating, as adding the return there does not make much sense).


However, an expression template matches single statements without braces, e.g.,

	static class ExampleRule2 {
		@BeforeTemplate List<?> match(String o) {
			return Arrays.asList(o);
		}

		@AfterTemplate List<?> optimizedMethod(String o) {
			return Arrays.asList("debug: " + o);
		}
	}

leads to the expected result:

@@ -7,6 +8,6 @@
 
 	public void method2() {
-		if (true) Arrays.asList("bad");
-		if (true) { Arrays.asList("good"); }
+		if (true) Arrays.asList("debug: " + "bad");
+		if (true) { Arrays.asList("debug: " + "good"); }
 	}
 }
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