Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve handling of the java sources #825

Merged
merged 2 commits into from Jul 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -1462,9 +1462,14 @@ class MetalsLanguageServer(
// remove cached symbols from Jars
// that are not used
val usedJars = mutable.HashSet.empty[AbsolutePath]
JdkSources(userConfig.javaHome).foreach { zip =>
usedJars += zip
addSourceJarSymbols(zip)
JdkSources(userConfig.javaHome) match {
case Some(zip) =>
usedJars += zip
addSourceJarSymbols(zip)
case None =>
scribe.warn(
s"Could not find java sources in ${userConfig.javaHome}. Java symbols will not be available."
)
}
for {
item <- dependencySources.getItems.asScala
Expand Down
48 changes: 24 additions & 24 deletions mtags/src/main/scala/scala/meta/internal/metals/JdkSources.scala
@@ -1,16 +1,19 @@
package scala.meta.internal.metals

import java.nio.file.Files
import scala.meta.internal.jdk.CollectionConverters._
import java.nio.file.Path
import java.nio.file.Paths
import scala.meta.io.AbsolutePath

/**
* Locates zip file on disk that contains the source code for the JDK.
*/
object JdkSources {
private val sources = Paths.get("src.zip")
private val libSources = Paths.get("lib").resolve(sources)

def apply(userJavaHome: Option[String] = None): Option[AbsolutePath] = {
candidates(userJavaHome).find(_.isFile)
candidates(userJavaHome).headOption
}

def defaultJavaHome: Option[String] = {
Expand All @@ -19,36 +22,33 @@ object JdkSources {
)
}

def candidates(userJavaHome: Option[String]): List[AbsolutePath] = {
private def candidates(userJavaHome: Option[String]): List[AbsolutePath] = {
def isJdkCandidate(path: Path): Boolean = {
def containsJre = Files.exists(path.resolve("jre"))
val name = path.getFileName.toString
name.contains("jdk") || containsJre //e.g. jdk-8, java-openjdk-11
}

for {
javaHomeString <- userJavaHome.orElse(defaultJavaHome).toList
javaHome = Paths.get(javaHomeString)
jdkHome = {
if (javaHome.getFileName.toString.startsWith("jdk")) {
if (isJdkCandidate(javaHome)) {
Nil
} else {
// In case java.home points to the JRE instead of the JDK, try
// to pick a sibling directory that starts with jdk*.
val ls = Files.list(javaHome.getParent)
try {
ls.iterator()
.asScala
.filter(_.getFileName.toString.startsWith("jdk"))
.toArray
.sortBy(_.getFileName.toString)
.toList
} finally ls.close()
// In case java.home points to the JRE instead of the JDK,
// try to find jdk among its siblings
Files
.list(javaHome.getParent)
.filter(isJdkCandidate)
.toArray[Path](size => new Array(size))
.sortBy(_.getFileName)
.toList
}
}
src <- jdkHome ++ List(
javaHome.getParent,
javaHome
).flatMap { dir =>
List(
dir.resolve("src.zip"),
dir.resolve("lib").resolve("src.zip")
)
}
jdk <- jdkHome ++ List(javaHome.getParent, javaHome)
src <- List(sources, libSources).map(jdk.resolve)
if Files.isRegularFile(src)
} yield AbsolutePath(src)
}

Expand Down