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

Fix NPE when fetching the classpath of the project #3115

Merged
merged 1 commit into from
Mar 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,11 @@ public static Map<String, Object> getProjectSettings(String uri, List<String> se
continue;
} else if (entryKind == IClasspathEntry.CPE_LIBRARY) {
if (!path.toFile().exists()) {
// the case when lib path is relative
path = project.getFile(path.makeRelativeTo(project.getFullPath())).getLocation();
}
if (!path.toFile().exists()) {
continue;
// check if it's a project based full path
IPath filePath = project.getFile(path.makeRelativeTo(project.getFullPath())).getLocation();
if (filePath != null && filePath.toFile().exists()) {
path = filePath;
}
}
}
Map<String, String> attributes = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,27 @@ public void testGetClasspathEntries() throws Exception {
}));
}

@Test
public void testGetClasspathEntriesWithNonExistLib() throws Exception {
importProjects("maven/salut2");
IProject project = WorkspaceHelper.getProject("salut2");
IJavaProject javaProject = JavaCore.create(project);
List<IClasspathEntry> classpathEntries = Arrays.stream(javaProject.getRawClasspath())
.collect(Collectors.toList());
classpathEntries.add(JavaCore.newLibraryEntry(new Path("/foo/bar/a.jar"), null, null));
javaProject.setRawClasspath(classpathEntries.toArray(IClasspathEntry[]::new), monitor);

String uriString = project.getFile("src/main/java/foo/Bar.java").getLocationURI().toString();
List<String> settingKeys = Arrays.asList(ProjectCommand.CLASSPATH_ENTRIES);
Map<String, Object> options = ProjectCommand.getProjectSettings(uriString, settingKeys);
List<ProjectClasspathEntry> entries = (List) options.get(ProjectCommand.CLASSPATH_ENTRIES);
assertNotNull(options.get(ProjectCommand.CLASSPATH_ENTRIES));
assertTrue(entries.size() > 0);
assertTrue(entries.stream().anyMatch(entry -> {
return entry.getKind() == IClasspathEntry.CPE_LIBRARY && entry.getPath().equals("/foo/bar/a.jar");
}));
}

@Test
public void testUpdateClasspathEntries() throws Exception {
importProjects("maven/salut2");
Expand Down
Loading