diff --git a/java/java.file.launcher/src/org/netbeans/modules/java/file/launcher/queries/MultiSourceRootProvider.java b/java/java.file.launcher/src/org/netbeans/modules/java/file/launcher/queries/MultiSourceRootProvider.java index 7793d0663d79..0bfc6bfbc23d 100644 --- a/java/java.file.launcher/src/org/netbeans/modules/java/file/launcher/queries/MultiSourceRootProvider.java +++ b/java/java.file.launcher/src/org/netbeans/modules/java/file/launcher/queries/MultiSourceRootProvider.java @@ -94,8 +94,18 @@ public class MultiSourceRootProvider implements ClassPathProvider { private Map file2ClassPath = new WeakHashMap<>(); private Map 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: diff --git a/java/java.file.launcher/test/unit/src/org/netbeans/modules/java/file/launcher/SingleSourceFileUtilTest.java b/java/java.file.launcher/test/unit/src/org/netbeans/modules/java/file/launcher/SingleSourceFileUtilTest.java index 5ad96c09c670..9bbd0d592ae7 100644 --- a/java/java.file.launcher/test/unit/src/org/netbeans/modules/java/file/launcher/SingleSourceFileUtilTest.java +++ b/java/java.file.launcher/test/unit/src/org/netbeans/modules/java/file/launcher/SingleSourceFileUtilTest.java @@ -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(); + } + } + } + } diff --git a/java/java.file.launcher/test/unit/src/org/netbeans/modules/java/file/launcher/queries/MultiSourceRootProviderTest.java b/java/java.file.launcher/test/unit/src/org/netbeans/modules/java/file/launcher/queries/MultiSourceRootProviderTest.java index 61694bb7845a..ff7245d3af48 100644 --- a/java/java.file.launcher/test/unit/src/org/netbeans/modules/java/file/launcher/queries/MultiSourceRootProviderTest.java +++ b/java/java.file.launcher/test/unit/src/org/netbeans/modules/java/file/launcher/queries/MultiSourceRootProviderTest.java @@ -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; @@ -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();