-
Notifications
You must be signed in to change notification settings - Fork 796
/
UnusedImportRemover.java
77 lines (64 loc) · 2.91 KB
/
UnusedImportRemover.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
* Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package software.amazon.awssdk.codegen.lite.emitters;
import static java.util.stream.Collectors.toSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class UnusedImportRemover implements CodeTransformer {
private static Pattern IMPORT_PATTERN = Pattern.compile("import(?:\\s+)(?:static\\s+)?(.*)(?:\\s*);");
@Override
public String apply(String content) {
return findUnusedImports(content).stream().map(this::removeImportFunction).reduce(Function.identity(),
Function::andThen).apply(content);
}
private Function<String, String> removeImportFunction(String importToRemove) {
return c -> c.replaceFirst(findSpecificImportRegex(importToRemove), "");
}
private Set<String> findUnusedImports(String content) {
return findImports(content).stream().filter(isUnused(content)).collect(toSet());
}
private List<String> findImports(String content) {
Matcher m = IMPORT_PATTERN.matcher(content);
List<String> imports = new ArrayList<>();
while (m.find()) {
imports.add(m.group(1));
}
return imports;
}
private String removeAllImports(String content) {
return content.replaceAll(IMPORT_PATTERN.pattern(), "");
}
private Predicate<String> isUnused(String content) {
String contentWithoutImports = removeAllImports(content);
return importToCheck -> !importToCheck.contains("*")
&& (isNotReferenced(contentWithoutImports, importToCheck) || isDuplicate(content, importToCheck));
}
private boolean isNotReferenced(String contentWithoutImports, String importToCheck) {
String symbol = importToCheck.substring(importToCheck.lastIndexOf(".") + 1);
return !Pattern.compile(String.format("\\b%s\\b", symbol)).matcher(contentWithoutImports).find();
}
private boolean isDuplicate(String content, String importToCheck) {
Matcher m = Pattern.compile(findSpecificImportRegex(importToCheck)).matcher(content);
return m.find() && m.find();
}
private String findSpecificImportRegex(String specificImport) {
return String.format("import(?:\\s+)(?:static )?(?:%s)(?:\\s*);", specificImport);
}
}