Skip to content

Commit 9ab9ff8

Browse files
authored
modified the pattern so that only checks imports that at the start of the line (#5795)
1 parent c665d11 commit 9ab9ff8

File tree

3 files changed

+10
-2
lines changed

3 files changed

+10
-2
lines changed

codegen/src/main/java/software/amazon/awssdk/codegen/emitters/UnusedImportRemover.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
import java.util.regex.Pattern;
2727

2828
public class UnusedImportRemover implements CodeTransformer {
29-
private static Pattern IMPORT_PATTERN = Pattern.compile("import(?:\\s+)(?:static\\s+)?(.*)(?:\\s*);");
29+
private static final Pattern IMPORT_PATTERN = Pattern.compile("^import(?:\\s+)(?:static\\s+)?(.*)(?:\\s*);",
30+
Pattern.MULTILINE);
3031

3132
@Override
3233
public String apply(String content) {
@@ -52,7 +53,8 @@ private List<String> findImports(String content) {
5253
}
5354

5455
private String removeAllImports(String content) {
55-
return content.replaceAll(IMPORT_PATTERN.pattern(), "");
56+
Matcher matcher = IMPORT_PATTERN.matcher(content);
57+
return matcher.replaceAll("");
5658
}
5759

5860
private Predicate<String> isUnused(String content) {

codegen/src/test/resources/software/amazon/awssdk/codegen/emitters/content-after-unused-import-removal.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ import java.util.Set;
2525
import java.time.*;
2626
import java.time.format.*;
2727

28+
/*
29+
* import something in the doc;
30+
*/
2831
public class FileWithUnusedImports implements Predicate<Set<String> {
2932
private final List<Date> dates;
3033
public FileWithUnusedImports() {

codegen/src/test/resources/software/amazon/awssdk/codegen/emitters/content-before-unused-import-removal.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ import
3333
java.util.ArrayList
3434
;
3535

36+
/*
37+
* import something in the doc;
38+
*/
3639
public class FileWithUnusedImports implements Predicate<Set<String> {
3740
private final List<Date> dates;
3841
public FileWithUnusedImports() {

0 commit comments

Comments
 (0)