google / error-prone

Catch common Java mistakes as compile-time errors

Home Page:https://errorprone.info

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Feature request] Add an annotation to allow ignoring comments for Refaster templates

trebele-tibco opened this issue · comments

I'm using error_prone_refaster-2.24.1.jar and error_prone_core-2.24.1-with-dependencies.jar to refactor code. Given the following class to refactor:

import java.util.*;
public class RefasterExample {
	public void method2() {
		Arrays.asList(//
				"bad");
		Arrays.asList(/**/
				"bad");
		Arrays.asList("good");
	}
}

and the following template:

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

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

then only the "good" line will be refactored:

@@ -6,5 +7,5 @@
 		Arrays.asList(/**/
 				"bad");
-		Arrays.asList("good");
+		Arrays.asList("debug: " + "good");
 	}
 }

It would be nice to have a possibility to ignore comments. Maybe with a parameter to configure which comments should be ignored (A regular expression? Predicate<String>?).

Alternatively, perhaps would-be-removed comments could be prepended to the replacement code. (This will in some cases yield misplaced/misleading/incorrect comments, but it would be handy i.c.w. patch mode.)

Thanks, I agree this is an area that could be improved. The current behaviour is supposed to be a safe default so we don't actually remove worthwhile comments, but I don't have objections to making that configurable, or to having a mode that keeps the comments and moves.

I went looking for the place where the current logic is implemented, and found a relevant TODO :)

boolean rejectMatchesWithComments() {
return true; // TODO(lowasser): worth making configurable?
}