Skip to content

Commit dffa110

Browse files
committed
🐛 use maven local repo path to accurately exclude directories
Signed-off-by: Pranav Gaikwad <[email protected]>
1 parent f6a6eb3 commit dffa110

File tree

5 files changed

+57
-17
lines changed

5 files changed

+57
-17
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ target/
33
.idea/*
44
*.iml
55
.DS_Store
6+
.gradle/

java-analyzer-bundle.core/src/main/java/io/konveyor/tackle/core/internal/RuleEntryParams.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ public class RuleEntryParams {
1717
private final String analysisMode;
1818
private final ArrayList<String> includedPaths;
1919
private final boolean includeOpenSourceLibraries;
20+
private final String mavenLocalRepoPath;
21+
private final String mavenIndexPath;
2022

2123
public RuleEntryParams(final String commandId, final List<Object> arguments) {
2224
@SuppressWarnings("unchecked")
@@ -27,11 +29,14 @@ public RuleEntryParams(final String commandId, final List<Object> arguments) {
2729
this.projectName = (String) obj.get("project");
2830
this.query = (String) obj.get("query");
2931
this.location = Integer.parseInt((String) obj.get("location"));
30-
this.annotationQuery = AnnotationQuery.fromMap(this.query, (Map<String, Object>) obj.get("annotationQuery"), location);
32+
this.annotationQuery = AnnotationQuery.fromMap(this.query, (Map<String, Object>) obj.get("annotationQuery"),
33+
location);
3134
this.analysisMode = (String) obj.get("analysisMode");
3235
this.includedPaths = (ArrayList<String>) obj.get("includedPaths");
3336
Boolean includeOpenSourceLibs = (Boolean) obj.get("includeOpenSourceLibraries");
3437
this.includeOpenSourceLibraries = (includeOpenSourceLibs != null) ? includeOpenSourceLibs : false;
38+
this.mavenLocalRepoPath = (String) obj.get("mavenLocalRepo");
39+
this.mavenIndexPath = (String) obj.get("mavenIndexPath");
3540
}
3641

3742
public String getProjectName() {
@@ -61,4 +66,12 @@ public ArrayList<String> getIncludedPaths() {
6166
public Boolean getIncludeOpenSourceLibraries() {
6267
return includeOpenSourceLibraries;
6368
}
69+
70+
public String getMavenLocalRepoPath() {
71+
return mavenLocalRepoPath;
72+
}
73+
74+
public String getMavenIndexPath() {
75+
return mavenIndexPath;
76+
}
6477
}

java-analyzer-bundle.core/src/main/java/io/konveyor/tackle/core/internal/SampleDelegateCommandHandler.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
import io.konveyor.tackle.core.internal.query.AnnotationQuery;
3131
import io.konveyor.tackle.core.internal.util.OpenSourceFilteredSearchScope;
32+
import io.konveyor.tackle.core.internal.util.OpenSourceLibraryExclusionManager;
3233

3334
public class SampleDelegateCommandHandler implements IDelegateCommandHandler {
3435

@@ -37,19 +38,19 @@ public class SampleDelegateCommandHandler implements IDelegateCommandHandler {
3738

3839
private static final String FullAnalysisMode = "full";
3940
private static final String sourceOnlyAnalysisMode = "source-only";
40-
4141

4242
@Override
4343
public Object executeCommand(String commandId, List<Object> arguments, IProgressMonitor progress) throws Exception {
4444
switch (commandId) {
4545
case COMMAND_ID:
4646
return "Hello World";
4747
case RULE_ENTRY_COMMAND_ID:
48-
logInfo("Here we get the arguments for rule entry: "+arguments);
48+
logInfo("Here we get the arguments for rule entry: " + arguments);
4949
RuleEntryParams params = new RuleEntryParams(commandId, arguments);
5050
return search(params.getProjectName(), params.getIncludedPaths(), params.getQuery(),
51-
params.getAnnotationQuery(), params.getLocation(), params.getAnalysisMode(),
52-
params.getIncludeOpenSourceLibraries(), progress);
51+
params.getAnnotationQuery(), params.getLocation(), params.getAnalysisMode(),
52+
params.getIncludeOpenSourceLibraries(), params.getMavenLocalRepoPath(),
53+
params.getMavenIndexPath(), progress);
5354
default:
5455
throw new UnsupportedOperationException(format("Unsupported command '%s'!", commandId));
5556
}
@@ -59,7 +60,7 @@ private static void waitForJavaSourceDownloads() {
5960
JobHelpers.waitForInitializeJobs();
6061
JobHelpers.waitForBuildJobs(JobHelpers.MAX_TIME_MILLIS);
6162
JobHelpers.waitForDownloadSourcesJobs(JobHelpers.MAX_TIME_MILLIS);
62-
63+
6364
}
6465

6566
// mapLocationToSearchPatternLocation will create the correct search pattern or throw an error if one can not be built.
@@ -189,7 +190,8 @@ private static SearchPattern getPatternSingleQuery(int location, String query) t
189190
throw new Exception("unable to create search pattern");
190191
}
191192

192-
private static List<SymbolInformation> search(String projectName, ArrayList<String> includedPaths, String query, AnnotationQuery annotationQuery, int location, String analsysisMode, boolean includeOpenSourceLibraries, IProgressMonitor monitor) throws Exception {
193+
private static List<SymbolInformation> search(String projectName, ArrayList<String> includedPaths, String query, AnnotationQuery annotationQuery, int location, String analysisMode,
194+
boolean includeOpenSourceLibraries, String mavenLocalRepoPath, String mavenIndexPath, IProgressMonitor monitor) throws Exception {
193195
IJavaProject[] targetProjects;
194196
IJavaProject project = ProjectUtils.getJavaProject(projectName);
195197
if (project != null) {
@@ -202,7 +204,7 @@ private static List<SymbolInformation> search(String projectName, ArrayList<Stri
202204

203205
// For Partial results, we are going to filter out based on a list in the engine
204206
int s = IJavaSearchScope.SOURCES | IJavaSearchScope.REFERENCED_PROJECTS | IJavaSearchScope.APPLICATION_LIBRARIES;
205-
if (analsysisMode.equals(sourceOnlyAnalysisMode)) {
207+
if (analysisMode.equals(sourceOnlyAnalysisMode)) {
206208
logInfo("KONVEYOR_LOG: source-only analysis mode only scoping to Sources");
207209
s = IJavaSearchScope.SOURCES;
208210
} else {
@@ -293,7 +295,8 @@ private static List<SymbolInformation> search(String projectName, ArrayList<Stri
293295

294296
// Use a filtered scope when open source libraries are not included
295297
if (!includeOpenSourceLibraries) {
296-
scope = new OpenSourceFilteredSearchScope(scope);
298+
scope = new OpenSourceFilteredSearchScope(scope,
299+
OpenSourceLibraryExclusionManager.getInstance(mavenLocalRepoPath, mavenIndexPath));
297300
}
298301
logInfo("scope: " + scope);
299302

@@ -320,7 +323,7 @@ private static List<SymbolInformation> search(String projectName, ArrayList<Stri
320323
try {
321324
searchEngine.search(pattern, participents, scope, requestor, monitor);
322325
} catch (Exception e) {
323-
//TODO: handle exception
326+
// TODO: handle exception
324327
logInfo("KONVEYOR_LOG: unable to get search " + e.toString().replace("\n", " "));
325328
}
326329

java-analyzer-bundle.core/src/main/java/io/konveyor/tackle/core/internal/util/OpenSourceFilteredSearchScope.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.eclipse.jdt.core.IJavaElement;
55
import org.eclipse.jdt.core.IPackageFragmentRoot;
66
import org.eclipse.jdt.core.search.IJavaSearchScope;
7+
import static org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin.logInfo;
78

89
import java.util.Arrays;
910

@@ -14,9 +15,9 @@ public class OpenSourceFilteredSearchScope implements IJavaSearchScope {
1415
private final IJavaSearchScope scope;
1516
private final OpenSourceLibraryExclusionManager exclusionManager;
1617

17-
public OpenSourceFilteredSearchScope(IJavaSearchScope scope) {
18+
public OpenSourceFilteredSearchScope(IJavaSearchScope scope, OpenSourceLibraryExclusionManager instance) {
1819
this.scope = scope;
19-
this.exclusionManager = OpenSourceLibraryExclusionManager.getInstance();
20+
this.exclusionManager = instance;
2021
}
2122

2223
@Override

java-analyzer-bundle.core/src/main/java/io/konveyor/tackle/core/internal/util/OpenSourceLibraryExclusionManager.java

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,31 +20,53 @@ public class OpenSourceLibraryExclusionManager {
2020

2121
private final List<Pattern> exclusionPatterns = new ArrayList<>();
2222
private final ConcurrentHashMap<String, Boolean> exclusionCache = new ConcurrentHashMap<>();
23+
private final String mavenLocalRepoPath;
24+
private final String mavenIndexPath;
2325

24-
private OpenSourceLibraryExclusionManager() {
26+
private OpenSourceLibraryExclusionManager(String mavenLocalRepoPath, String mavenIndexPath) {
27+
this.mavenLocalRepoPath = mavenLocalRepoPath;
28+
this.mavenIndexPath = mavenIndexPath;
2529
loadExclusionPatterns();
2630
}
2731

28-
public static OpenSourceLibraryExclusionManager getInstance() {
32+
public static OpenSourceLibraryExclusionManager getInstance(String mavenLocalRepoPath, String mavenIndexPath) {
2933
if (instance == null) {
3034
synchronized (OpenSourceLibraryExclusionManager.class) {
3135
if (instance == null) {
32-
instance = new OpenSourceLibraryExclusionManager();
36+
instance = new OpenSourceLibraryExclusionManager(mavenLocalRepoPath, mavenIndexPath);
3337
}
3438
}
3539
}
3640
return instance;
3741
}
3842

43+
private String normalizePath(String path) {
44+
if (path == null) {
45+
return "";
46+
}
47+
String normalized = path.replace('\\', '/');
48+
normalized = normalized.replaceAll("^[A-Za-z]:/", "");
49+
normalized = normalized.replace('/', '.').replace(':', '.');
50+
normalized = normalized.replaceAll("^\\.*", ".");
51+
return normalized;
52+
}
53+
3954
private void loadExclusionPatterns() {
40-
try (BufferedReader reader = new BufferedReader(new FileReader(MAVEN_INDEX_FILE, StandardCharsets.UTF_8))) {
55+
String normalizedRepoPath = this.normalizePath(this.mavenLocalRepoPath);
56+
String mavenIndexPath = MAVEN_INDEX_FILE;
57+
if (this.mavenIndexPath != null) {
58+
mavenIndexPath = this.mavenIndexPath;
59+
}
60+
logInfo("OpenSourceLibraryExclusionManager: using maven index path " + mavenIndexPath);
61+
logInfo("OpenSourceLibraryExclusionManager: using base query pattern " + normalizedRepoPath + ".*");
62+
try (BufferedReader reader = new BufferedReader(new FileReader(mavenIndexPath, StandardCharsets.UTF_8))) {
4163
List<String> patterns = reader.lines()
4264
.filter(line -> !line.trim().isEmpty())
4365
.collect(Collectors.toList());
4466

4567
for (String patternStr : patterns) {
4668
try {
47-
String prefix = ".*";
69+
String prefix = normalizedRepoPath + ".*";
4870
exclusionPatterns.add(Pattern.compile(prefix + patternStr));
4971
} catch (Exception e) {
5072
logInfo("Invalid exclusion pattern: " + patternStr);

0 commit comments

Comments
 (0)