Skip to content

Commit

Permalink
Fix NPE when fetching the classpath of the project (eclipse-jdtls#3115)
Browse files Browse the repository at this point in the history
Signed-off-by: Sheng Chen <[email protected]>
  • Loading branch information
jdneo authored Mar 31, 2024
1 parent c7322be commit ae10481
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
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

0 comments on commit ae10481

Please sign in to comment.