Skip to content

Commit

Permalink
apache#3526: Retain traling / for non-existing directories.
Browse files Browse the repository at this point in the history
  • Loading branch information
sdedic committed Jan 30, 2022
1 parent 987c489 commit 95208b0
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
Expand Up @@ -26,6 +26,7 @@
import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
Expand Down Expand Up @@ -64,6 +65,8 @@
import org.apache.maven.project.ProjectBuildingRequest;
import org.netbeans.api.annotations.common.CheckForNull;
import org.netbeans.api.annotations.common.NonNull;
import org.netbeans.api.annotations.common.NullAllowed;
import org.netbeans.api.annotations.common.NullUnknown;
import org.netbeans.api.java.project.classpath.ProjectClassPathModifier;
import org.netbeans.api.project.Project;
import org.netbeans.api.queries.VisibilityQuery;
Expand Down Expand Up @@ -550,10 +553,39 @@ public MavenEmbedder getEmbedder() {
return auxprops;
}

/**
* The method will migrate to regular FileUtilities after NB13 release. The issue is that the result of
* {@link FileUtilities#convertStringToUri(java.lang.String)} result depends on whether the directory
* identified by the string exists or not. If it exists, the URI ends with a "/". For non-existent directories
* the URI lacks the trailing "/". This can break URI keys in a Map (if the directory gets created) and prevents
* from creating a ClassPath from such URLs (/ is checked). But FileUtilities is API and this behaviour is there for
* ages, so the correction should be added with a parameter.
*/
public static @NullUnknown URI convertStringToUri(@NullAllowed String str, boolean slashIfNotExist) {
if (str != null) {
File fil = new File(str);
fil = FileUtil.normalizeFile(fil);
// this conversion returns URIs that end with "/" if fil is an existing directory, but returns
// without the slash if the directory just does not exist yet.
URI uri = Utilities.toURI(fil);
String s = uri.toString();
if (slashIfNotExist && !s.endsWith("/") && (fil.isDirectory() || !fil.exists())) { // NOI18N
try {
return new URI(s + "/"); // NOI18N
} catch (URISyntaxException ex) {
throw new IllegalArgumentException(str);
}
} else {
return uri;
}
}
return null;
}

public URI[] getSourceRoots(boolean test) {
List<URI> uris = new ArrayList<URI>();
for (String root : test ? getOriginalMavenProject().getTestCompileSourceRoots() : getOriginalMavenProject().getCompileSourceRoots()) {
uris.add(FileUtilities.convertStringToUri(root));
uris.add(convertStringToUri(root, true));
}
for (JavaLikeRootProvider rp : getLookup().lookupAll(JavaLikeRootProvider.class)) {
// XXX for a few purposes (listening) it is desirable to list these even before they exist, but usually it is just noise (cf. #196414 comment #2)
Expand Down
Expand Up @@ -27,7 +27,6 @@
import org.apache.maven.model.Build;
import org.apache.maven.project.MavenProject;
import org.netbeans.modules.maven.NbMavenProjectImpl;
import org.netbeans.modules.maven.api.FileUtilities;
import org.openide.util.Utilities;

/**
Expand Down Expand Up @@ -57,7 +56,7 @@ public static List<URI> createPath(MavenProject prj) {
if (build != null) {
String outputDirectory = build.getOutputDirectory();
if (outputDirectory != null) {
lst.add(FileUtilities.convertStringToUri(outputDirectory));
lst.add(NbMavenProjectImpl.convertStringToUri(outputDirectory, true));
}
}
List<Artifact> arts = prj.getRuntimeArtifacts();
Expand Down
Expand Up @@ -27,7 +27,6 @@
import org.apache.maven.model.Build;
import org.apache.maven.project.MavenProject;
import org.netbeans.modules.maven.NbMavenProjectImpl;
import org.netbeans.modules.maven.api.FileUtilities;
import org.openide.util.Utilities;


Expand Down Expand Up @@ -66,11 +65,11 @@ URI[] createPath() {
if (build != null) {
String testOutputDirectory = build.getTestOutputDirectory();
if (testOutputDirectory != null) {
lst.add(FileUtilities.convertStringToUri(testOutputDirectory));
lst.add(NbMavenProjectImpl.convertStringToUri(testOutputDirectory, true));
}
String outputDirectory = build.getOutputDirectory();
if (outputDirectory != null) {
lst.add(FileUtilities.convertStringToUri(outputDirectory));
lst.add(NbMavenProjectImpl.convertStringToUri(outputDirectory, true));
}
}
List<Artifact> arts = prj.getTestArtifacts();
Expand Down

0 comments on commit 95208b0

Please sign in to comment.