From f558f531502e7ea1ce8590a581fdb237b5b3ada9 Mon Sep 17 00:00:00 2001 From: Alex Kochnev Date: Thu, 8 Dec 2016 00:08:27 -0500 Subject: [PATCH 1/2] Fix for issue#151 : added exception handler for IllegalArgumentException --- pom.xml | 4 +- .../projects/SourcePathProviderImpl.java | 37 +++++++-- scala.sbt/pom.xml | 15 ++++ .../scala/sbt/classpath/SBTClassPath.scala | 43 ++++++---- .../unittests/ClassPathTransformerTests.scala | 80 +++++++++++++++++++ 5 files changed, 155 insertions(+), 24 deletions(-) create mode 100644 scala.sbt/src/test/scala/unittests/ClassPathTransformerTests.scala diff --git a/pom.xml b/pom.xml index 61dddee..bb53271 100644 --- a/pom.xml +++ b/pom.xml @@ -72,14 +72,14 @@ 1.8.2.0 1.8.2.0 1.8.2.0 - 1.8.2.0 + 1.8.2.1 1.8.2.0 1.8.2.0 1.8.2.0 1.8.2.0 1.8.2.0 1.8.2.0 - 1.8.2.0 + 1.8.2.1 1.8.2.0 1.8.2.0 diff --git a/scala.debugger.projects/src/main/java/org/netbeans/modules/scala/debugger/projects/SourcePathProviderImpl.java b/scala.debugger.projects/src/main/java/org/netbeans/modules/scala/debugger/projects/SourcePathProviderImpl.java index d5b2f33..f5b4016 100644 --- a/scala.debugger.projects/src/main/java/org/netbeans/modules/scala/debugger/projects/SourcePathProviderImpl.java +++ b/scala.debugger.projects/src/main/java/org/netbeans/modules/scala/debugger/projects/SourcePathProviderImpl.java @@ -73,6 +73,7 @@ import org.netbeans.api.project.Sources; import org.netbeans.api.project.ui.OpenProjects; import org.netbeans.spi.java.classpath.ClassPathProvider; +import org.netbeans.spi.java.classpath.PathResourceImplementation; import org.netbeans.spi.java.classpath.support.ClassPathSupport; import org.openide.filesystems.FileObject; @@ -202,9 +203,8 @@ public SourcePathProviderImpl (ContextProvider contextProvider) { allSourceRoots1.add(fo); } } - originalSourcePath = ClassPathSupport.createClassPath ( - allSourceRoots1.toArray(new FileObject [allSourceRoots1.size()]) - ); + originalSourcePath = createClassPath(allSourceRoots1); + projectSourceRoots = getSourceRoots(originalSourcePath); JavaPlatform[] platforms = JavaPlatformManager.getDefault (). @@ -217,10 +217,7 @@ public SourcePathProviderImpl (ContextProvider contextProvider) { for (j = 0; j < jj; j++) allSourceRoots1.remove (roots [j]); } - smartSteppingSourcePath = ClassPathSupport.createClassPath ( - allSourceRoots1.toArray - (new FileObject [allSourceRoots1.size()]) - ); + smartSteppingSourcePath = createClassPath(allSourceRoots1); } if (verbose) @@ -231,6 +228,32 @@ public SourcePathProviderImpl (ContextProvider contextProvider) { "SPPI: init smartSteppingSourcePath " + smartSteppingSourcePath ); } + + /** + * Creates a ClassPath, while guarding for an IllegalArgumentException, + * as exemplified in org.netbeans.modules.debugger.jpda.projects.SourcePathProviderImpl + * @param froots file object list from which to create the classpath + * @return + */ + private ClassPath createClassPath(List froots) { + List pris = new ArrayList (); + for (FileObject fo : froots) { + if (fo != null && fo.canRead()) { + try { + URL url = fo.toURL(); + pris.add(ClassPathSupport.createResource(url)); + } catch (IllegalArgumentException iaex) { + // Can be thrown from ClassPathSupport.createResource() + // Ignore - bad source root + //logger.log(Level.INFO, "Invalid source root = "+fo, iaex); + logger.warning(iaex.getLocalizedMessage()); + } + } + } + return ClassPathSupport.createClassPath(pris); + } + + /** * Translates a relative path ("java/lang/Thread.java") to url diff --git a/scala.sbt/pom.xml b/scala.sbt/pom.xml index 13b146f..81cc952 100644 --- a/scala.sbt/pom.xml +++ b/scala.sbt/pom.xml @@ -186,6 +186,21 @@ org-netbeans-libs-sbt runtime + + diff --git a/scala.sbt/src/main/scala/org/netbeans/modules/scala/sbt/classpath/SBTClassPath.scala b/scala.sbt/src/main/scala/org/netbeans/modules/scala/sbt/classpath/SBTClassPath.scala index c41b779..76b1514 100644 --- a/scala.sbt/src/main/scala/org/netbeans/modules/scala/sbt/classpath/SBTClassPath.scala +++ b/scala.sbt/src/main/scala/org/netbeans/modules/scala/sbt/classpath/SBTClassPath.scala @@ -56,23 +56,10 @@ final class SBTClassPath(project: Project, tpe: String, isTest: Boolean) extends private def getResolvedClassPath(resolver: SBTResolver, isTest: Boolean, result: java.util.ArrayList[PathResourceImplementation]) { for { file <- resolver.getResolvedClassPath(tpe, isTest) - fo = FileUtil.toFileObject(file) + } { try { - val rootUrl = if (fo != null) { - if (FileUtil.isArchiveFile(fo)) { - FileOwnerQuery.markExternalOwner(fo, project, FileOwnerQuery.EXTERNAL_ALGORITHM_TRANSIENT) - FileUtil.getArchiveRoot(fo).toURL - } else { - file.toURI.toURL - } - } else { // fo is null, file does not exist - // file is a classes/srcs *folder* and may not exist yet, we must add a slash at the end. - // to tell ClassPathSupport that it's a folder instead of a general file - // @Note should avoid url string ends with doubled "/", i.e. "//" - val uriStr = file.toURI.toString - URI.create(if (uriStr.endsWith("/")) uriStr else uriStr + "/").toURL - } + val rootUrl = ProjectFileUrlConverter.convert(project, file) result.add(ClassPathSupport.createResource(rootUrl)) } catch { case ex: FileStateInvalidException => Exceptions.printStackTrace(ex) @@ -126,3 +113,29 @@ final class SBTClassPath(project: Project, tpe: String, isTest: Boolean) extends pcs.addPropertyChangeListener(listener) } } + +object ProjectFileUrlConverter { + import org.openide.filesystems.FileObject + def convert(project: Project, file: java.io.File): java.net.URL = { + val fo = FileUtil.toFileObject(file) + if (fo != null) { + if (FileUtil.isArchiveFile(fo)) { + FileOwnerQuery.markExternalOwner(fo, project, FileOwnerQuery.EXTERNAL_ALGORITHM_TRANSIENT) + FileUtil.getArchiveRoot(fo).toURL + } else { + + if (file.isDirectory) { + FileUtil.urlForArchiveOrDir(file) + } else { + file.toURI.toURL + } + } + } else { // fo is null, file does not exist + // file is a classes/srcs *folder* and may not exist yet, we must add a slash at the end. + // to tell ClassPathSupport that it's a folder instead of a general file + // @Note should avoid url string ends with doubled "/", i.e. "//" + val uriStr = file.toURI.toString + URI.create(if (uriStr.endsWith("/")) uriStr else uriStr + "/").toURL + } + } +} diff --git a/scala.sbt/src/test/scala/unittests/ClassPathTransformerTests.scala b/scala.sbt/src/test/scala/unittests/ClassPathTransformerTests.scala new file mode 100644 index 0000000..69bccce --- /dev/null +++ b/scala.sbt/src/test/scala/unittests/ClassPathTransformerTests.scala @@ -0,0 +1,80 @@ + +package unittests + +import java.io.File +import org.junit.Test +import org.netbeans.api.project.Project +import org.netbeans.modules.scala.sbt.classpath.ProjectFileUrlConverter +import org.netbeans.modules.scala.sbt.classpath.ProjectFileUrlConverter +import org.netbeans.modules.scala.sbt.classpath.ProjectFileUrlConverter +import org.netbeans.modules.scala.sbt.classpath.ProjectFileUrlConverter +import org.openide.filesystems.FileObject +import org.openide.filesystems.FileUtil +import org.junit.Assert +import org.openide.util.Lookup + +class ClassPathTransformerTests { + + /** + * @param args the command line arguments + */ + @Test + def testFileExists(): Unit = { + val f1 = new File("/bin/bash") + val fo1 = FileUtil.toFileObject(f1) + + Assert.assertTrue(f1.isFile) + Assert.assertTrue(f1.exists()) + Assert.assertTrue(fo1 != null) + + Assert.assertFalse(f1.toURI.toURL.toString.endsWith("/")) + } + + @Test + def testDirectoryConverts(): Unit = { + val f1 = new File("/bin/") + val fo1 = FileUtil.toFileObject(f1) + + Assert.assertTrue(f1.isDirectory) + Assert.assertTrue(f1.exists()) + Assert.assertTrue(fo1 != null) + + Assert.assertTrue(f1.toURI.toURL.toString.endsWith("/")) + } + + @Test + def testWithExistingFile() = { + val file = new File("/bin/bash") + val dummyProj = new Project() { + def getLookup(): Lookup = Lookup.EMPTY + def getProjectDirectory(): FileObject = FileUtil.toFileObject(new File(".")) + } + val foundUrl = ProjectFileUrlConverter.convert(dummyProj, file) + + assert(!foundUrl.toString.endsWith("/"), s"an existing file in $foundUrl must not end with a slash") + } + + @Test + def testWithExistingDirectory() = { + val file = new File("/bin") + val dummyProj = new Project() { + def getLookup(): Lookup = Lookup.EMPTY + def getProjectDirectory(): FileObject = FileUtil.toFileObject(new File(".")) + } + val foundUrl = ProjectFileUrlConverter.convert(dummyProj, file) + + assert(foundUrl.toString.endsWith("/"), s"an existing directory in $foundUrl must end with a slash") + } + @Test + def testWithNonExistingDirectory() = { + val file = new File("/foobar") + val dummyProj = new Project() { + def getLookup(): Lookup = Lookup.EMPTY + def getProjectDirectory(): FileObject = FileUtil.toFileObject(new File(".")) + } + val foundUrl = ProjectFileUrlConverter.convert(dummyProj, file) + + assert(foundUrl.toString.endsWith("/"), s"an non-existing directory in $foundUrl must end with a slash") + } + +} From fcaebb6bf997c54bf4a3b26a943a0fbaade910e9 Mon Sep 17 00:00:00 2001 From: Alex Kochnev Date: Thu, 8 Dec 2016 15:05:19 -0500 Subject: [PATCH 2/2] Removed commented out scalatest dependency --- scala.sbt/pom.xml | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/scala.sbt/pom.xml b/scala.sbt/pom.xml index 81cc952..fcf8323 100644 --- a/scala.sbt/pom.xml +++ b/scala.sbt/pom.xml @@ -187,20 +187,6 @@ runtime -