Skip to content

Commit

Permalink
Merge pull request apache#7509 from matthiasblaesing/multi-source-roo…
Browse files Browse the repository at this point in the history
…t-provider-guard

Guard MultiSourceRootProvider to supply classpath only for java source on file system
  • Loading branch information
matthiasblaesing authored Jun 25, 2024
2 parents 5e8b62d + 1830a39 commit 1851691
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,18 @@ public class MultiSourceRootProvider implements ClassPathProvider {
private Map<FileObject, ClassPath> file2ClassPath = new WeakHashMap<>();
private Map<FileObject, ClassPath> file2ModulePath = new WeakHashMap<>();

static boolean isSupportedFile(FileObject file) {
return SingleSourceFileUtil.isSingleSourceFile(file)
// MultiSourceRootProvider assumes it can convert FileObject to
// java.io.File, so filter here
&& Objects.equals("file", file.toURI().getScheme());
}

@Override
public ClassPath findClassPath(FileObject file, String type) {
if (! isSupportedFile(file)) {
return null;
}
switch (type) {
case ClassPath.SOURCE: return getSourcePath(file);
case ClassPath.COMPILE:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,38 @@ public void testIsSupportedFile() throws IOException {
}
}
}

@Test
public void testSsSingleSourceFile() throws IOException {
File vcsDemoDir = null;
File supportedFile = null;
File unsupportedFile = null;
File unsupportedFile2 = null;
try {
vcsDemoDir = Files.createTempDirectory("vcs-dummy").toFile();
supportedFile = Files.createTempFile("dummy", ".java").toFile();
unsupportedFile = new File(vcsDemoDir, "dummy.java");
unsupportedFile2 = Files.createTempFile("dummy", ".non-java").toFile();;
FileUtil.createData(unsupportedFile);

assertTrue(SingleSourceFileUtil.isSingleSourceFile(FileUtil.createData(supportedFile)));
assertFalse(SingleSourceFileUtil.isSingleSourceFile(FileUtil.createData(unsupportedFile)));
assertFalse(SingleSourceFileUtil.isSingleSourceFile(FileUtil.createData(unsupportedFile2)));

} finally {
if(supportedFile != null && supportedFile.exists()) {
supportedFile.delete();
}
if(unsupportedFile != null && unsupportedFile.exists()) {
unsupportedFile.delete();
}
if(unsupportedFile2 != null && unsupportedFile2.exists()) {
unsupportedFile2.delete();
}
if(vcsDemoDir != null && vcsDemoDir.exists()) {
vcsDemoDir.delete();
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
*/
package org.netbeans.modules.java.file.launcher.queries;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.HashSet;
import java.util.concurrent.atomic.AtomicInteger;
Expand Down Expand Up @@ -245,6 +248,22 @@ public void testBrokenOptions() throws Exception {
provider.findClassPath(test, JavaClassPathConstants.MODULE_COMPILE_PATH);
}

public void testMultiSourceRootProviderOnlySupportedForLocalFiles() throws IOException {
File supportedFile = null;
try {
supportedFile = Files.createTempFile("dummy", ".java").toFile();
FileObject realFileSource = FileUtil.createData(supportedFile);
FileObject inMemorySource = FileUtil.createMemoryFileSystem().getRoot().createData("Ahoj.java");

assertFalse(MultiSourceRootProvider.isSupportedFile(inMemorySource));
assertTrue(MultiSourceRootProvider.isSupportedFile(realFileSource));
} finally {
if(supportedFile != null && supportedFile.exists()) {
supportedFile.delete();
}
}
}

@Override
protected void setUp() throws Exception {
super.setUp();
Expand Down

0 comments on commit 1851691

Please sign in to comment.