Skip to content

Commit

Permalink
Merge pull request #175 from akochnev/issue_151
Browse files Browse the repository at this point in the history
Fixed issue #151 - dealing with an exception in SourcePathProviderImpl
  • Loading branch information
dcaoyuan authored Oct 16, 2017
2 parents 59f1dce + fcaebb6 commit efff234
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 24 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,14 @@
<scala.console.version>1.8.2.0</scala.console.version>
<scala.core.version>1.8.2.0</scala.core.version>
<scala.debugger.version>1.8.2.0</scala.debugger.version>
<scala.debugger.projects.version>1.8.2.0</scala.debugger.projects.version>
<scala.debugger.projects.version>1.8.2.1</scala.debugger.projects.version>
<scala.editor.version>1.8.2.0</scala.editor.version>
<scala.fontscolors.version>1.8.2.0</scala.fontscolors.version>
<scala.maven.version>1.8.2.0</scala.maven.version>
<scala.platform.version>1.8.2.0</scala.platform.version>
<scala.project.version>1.8.2.0</scala.project.version>
<scala.refactoring.version>1.8.2.0</scala.refactoring.version>
<scala.sbt.version>1.8.2.0</scala.sbt.version>
<scala.sbt.version>1.8.2.1</scala.sbt.version>
<scala.sbt.project.version>1.8.2.0</scala.sbt.project.version>
<scala.stdplatform.version>1.8.2.0</scala.stdplatform.version>
</properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 ().
Expand All @@ -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)
Expand All @@ -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<FileObject> froots) {
List<PathResourceImplementation> pris = new ArrayList<PathResourceImplementation> ();
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
Expand Down
1 change: 1 addition & 0 deletions scala.sbt/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@
<artifactId>org-netbeans-libs-sbt</artifactId>
<scope>runtime</scope>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
}
}
80 changes: 80 additions & 0 deletions scala.sbt/src/test/scala/unittests/ClassPathTransformerTests.scala
Original file line number Diff line number Diff line change
@@ -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")
}

}

0 comments on commit efff234

Please sign in to comment.