Skip to content

Commit 946c118

Browse files
committed
Handle relative paths in JavaProjectConfigurator.getFolder and simplify
The TychoLifecycleMapping.EclipsePluginProjectConfigurator can call getFolder() with a relative path which only seems to cause trouble on Windows.
1 parent 2b2cfa2 commit 946c118

File tree

1 file changed

+28
-38
lines changed

1 file changed

+28
-38
lines changed

org.eclipse.m2e.jdt/src/org/eclipse/m2e/jdt/internal/AbstractJavaProjectConfigurator.java

Lines changed: 28 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.util.Optional;
2828
import java.util.Set;
2929
import java.util.function.Predicate;
30+
import java.util.stream.Stream;
3031

3132
import org.osgi.framework.Version;
3233
import org.slf4j.Logger;
@@ -600,42 +601,27 @@ private boolean overlapsWithSourceFolder(IPath path, IProject project, MavenProj
600601
IPath relPath = path.makeRelativeTo(project.getFullPath());
601602
List<String> compile = mavenProject.getCompileSourceRoots();
602603
List<String> test = mavenProject.getTestCompileSourceRoots();
603-
return isContained(relPath, project, getSourceFolders(project, compile))
604-
|| isContained(relPath, project, getSourceFolders(project, test));
604+
return isContained(relPath, getSourceFolders(project, compile))
605+
|| isContained(relPath, getSourceFolders(project, test));
605606
}
606607

607608
private boolean overlapsWithOtherResourceFolder(IPath path, IProject project, MavenProject mavenProject) {
608609
IPath relPath = path.makeRelativeTo(project.getFullPath());
609-
return isContained(relPath, project, getOtherResourceFolders(project, mavenProject.getResources(), relPath))
610-
|| isContained(relPath, project, getOtherResourceFolders(project, mavenProject.getTestResources(), relPath));
610+
return isContained(relPath, getOtherResourceFolders(project, mavenProject.getResources(), relPath))
611+
|| isContained(relPath, getOtherResourceFolders(project, mavenProject.getTestResources(), relPath));
611612
}
612613

613-
private IPath[] getSourceFolders(IProject project, List<String> sources) {
614-
List<IPath> paths = new ArrayList<>();
615-
for(String source : sources) {
616-
paths.add(getProjectRelativePath(project, source));
617-
}
618-
return paths.toArray(new IPath[paths.size()]);
614+
private Stream<IPath> getSourceFolders(IProject project, List<String> sources) {
615+
return sources.stream().map(Path::of).map(source -> getProjectRelativePath(project, source));
619616
}
620617

621-
private IPath[] getOtherResourceFolders(IProject project, List<Resource> resources, IPath curPath) {
622-
List<IPath> paths = new ArrayList<>();
623-
for(Resource res : resources) {
624-
IPath path = getProjectRelativePath(project, res.getDirectory());
625-
if(!path.equals(curPath)) {
626-
paths.add(path);
627-
}
628-
}
629-
return paths.toArray(new IPath[paths.size()]);
618+
private Stream<IPath> getOtherResourceFolders(IProject project, List<Resource> resources, IPath curPath) {
619+
return resources.stream().map(Resource::getDirectory).map(Path::of)//
620+
.map(res -> getProjectRelativePath(project, res)).filter(path -> !path.equals(curPath));
630621
}
631622

632-
private boolean isContained(IPath path, IProject project, IPath[] otherPaths) {
633-
for(IPath otherPath : otherPaths) {
634-
if(otherPath.isPrefixOf(path)) {
635-
return true;
636-
}
637-
}
638-
return false;
623+
private static boolean isContained(IPath path, Stream<IPath> otherPaths) {
624+
return otherPaths.anyMatch(p -> p.isPrefixOf(path));
639625
}
640626

641627
protected void addJavaProjectOptions(Map<String, String> options, ProjectConfigurationRequest request,
@@ -899,13 +885,19 @@ private void removeMavenClasspathContainer(IProject project) throws JavaModelExc
899885
}
900886
}
901887

902-
protected IContainer getFolder(IProject project, String absolutePath) throws CoreException {
888+
protected IContainer getFolder(IProject project, String path) throws CoreException {
903889
Path projectLocation = project.getLocation().toPath().toAbsolutePath();
904-
Path folderPath = Path.of(absolutePath);
890+
Path folderPath = Path.of(path);
905891
if(projectLocation.equals(folderPath)) {
906892
return project;
907893
}
908-
IPath relativePath = getProjectRelativePath(project, absolutePath);
894+
IPath relativePath;
895+
if(folderPath.isAbsolute()) {
896+
relativePath = getProjectRelativePath(project, folderPath);
897+
} else {
898+
folderPath = projectLocation.resolve(path);
899+
relativePath = IPath.fromOSString(path);
900+
}
909901
if(!project.exists(relativePath) && Files.exists(folderPath)
910902
&& !ResourcesPlugin.getWorkspace().getRoot().getLocation().toPath().equals(folderPath)) {
911903
String linkName = projectLocation.relativize(folderPath).toString().replace("/", "_");
@@ -917,17 +909,15 @@ protected IContainer getFolder(IProject project, String absolutePath) throws Cor
917909
return project.getFolder(relativePath);
918910
}
919911

920-
protected IPath getProjectRelativePath(IProject project, String absolutePath) {
921-
File basedir = project.getLocation().toFile();
922-
String relative;
923-
if(absolutePath.equals(basedir.getAbsolutePath())) {
924-
relative = "."; //$NON-NLS-1$
925-
} else if(absolutePath.startsWith(basedir.getAbsolutePath())) {
926-
relative = absolutePath.substring(basedir.getAbsolutePath().length() + 1);
912+
private static IPath getProjectRelativePath(IProject project, Path absolutePath) {
913+
Path basedir = project.getLocation().toPath().toAbsolutePath();
914+
if(absolutePath.equals(basedir)) {
915+
return IPath.fromOSString(".");
916+
} else if(absolutePath.startsWith(basedir)) {
917+
return IPath.fromPath(basedir.relativize(absolutePath));
927918
} else {
928-
relative = absolutePath;
919+
return IPath.fromPath(absolutePath);
929920
}
930-
return IPath.fromOSString(relative.replace('\\', '/'));
931921
}
932922

933923
/**

0 commit comments

Comments
 (0)