Permalink
Browse files

[nomaster] SI-7652 REPL extended quest for tools

Javap tries harder and fails louder when
looking for tools.jar

This is a backport of the method as it
was first coded for partest.

If JAVA_HOME is wrong, it will either
fail with a message or succeed after
rooting about.

```
apm@mara:~/tmp/q$ JAVA_HOME=$JAVA7_HOME JAVACMD='/usr/lib/jvm/java-6-openjdk-amd64/bin/java' /home/apm/projects/snytt/build/pack/bin/scala
Welcome to Scala version 2.10.3-20130829-123337-59d6568daa (OpenJDK 64-Bit Server VM, Java 1.6.0_27).
Type in expressions to have them evaluated.
Type :help for more information.

scala> :javap
:javap [-lcsvp] [path1 path2 ...]

scala> :javap java.lang.Object
Failed: Could not load javap tool. Check that JAVA_HOME is correct.

apm@mara:~/tmp/q$ JAVA_HOME=/usr/lib/jvm/java-6-openjdk-amd64/jre JAVACMD='/usr/lib/jvm/java-6-openjdk-amd64/bin/java' /home/apm/projects/snytt/build/pack/bin/scala
Welcome to Scala version 2.10.3-20130829-123337-59d6568daa (OpenJDK 64-Bit Server VM, Java 1.6.0_27).
Type in expressions to have them evaluated.
Type :help for more information.

scala>

scala> :javap
:javap [-lcsvp] [path1 path2 ...]

scala> :javap java.lang.Object
Compiled from "Object.java"
```
  • Loading branch information...
som-snytt committed Aug 29, 2013
1 parent 8b10daf commit 7804ceca432c1d7d6ce3a669828ac0d3f6ffeac4
Showing with 23 additions and 12 deletions.
  1. +23 −12 src/compiler/scala/tools/nsc/interpreter/ILoop.scala
@@ -11,14 +11,14 @@ import java.io.{ BufferedReader, FileReader }
import java.util.concurrent.locks.ReentrantLock
import scala.sys.process.Process
import session._
-import scala.util.Properties.{ jdkHome, javaVersion }
+import scala.util.Properties.{ envOrNone, javaHome, jdkHome, javaVersion }
import scala.tools.util.{ Javap }
import scala.annotation.tailrec
import scala.collection.mutable.ListBuffer
import scala.concurrent.ops
import util.{ ClassPath, Exceptional, stringFromWriter, stringFromStream }
import interpreter._
-import io.{ File, Directory }
+import io.{ File, Directory, Path }
import scala.reflect.NameTransformer._
import util.ScalaClassLoader
import ScalaClassLoader._
@@ -373,18 +373,29 @@ class ILoop(in0: Option[BufferedReader], protected val out: JPrintWriter)
}
}
- private def findToolsJar() = {
- val jdkPath = Directory(jdkHome)
- val jar = jdkPath / "lib" / "tools.jar" toFile;
+ private[this] lazy val platformTools: Option[File] = {
+ val jarName = "tools.jar"
+ def jarPath(path: Path) = (path / "lib" / jarName).toFile
+ def jarAt(path: Path) = {
+ val f = jarPath(path)
+ if (f.isFile) Some(f) else None
+ }
+ val jdkDir = {
+ val d = Directory(jdkHome)
+ if (d.isDirectory) Some(d) else None
+ }
+ def deeply(dir: Directory) = dir.deepFiles find (_.name == jarName)
- if (jar isFile)
- Some(jar)
- else if (jdkPath.isDirectory)
- jdkPath.deepFiles find (_.name == "tools.jar")
- else None
- }
+ val home = envOrNone("JDK_HOME") orElse envOrNone("JAVA_HOME") map (p => Path(p))
+ val install = Some(Path(javaHome))
+
+ (home flatMap jarAt) orElse
+ (install flatMap jarAt) orElse
+ (install map (_.parent) flatMap jarAt) orElse
+ (jdkDir flatMap deeply)
+ }
private def addToolsJarToLoader() = {
- val cl = findToolsJar match {
+ val cl = platformTools match {
case Some(tools) => ScalaClassLoader.fromURLs(Seq(tools.toURL), intp.classLoader)
case _ => intp.classLoader
}

0 comments on commit 7804cec

Please sign in to comment.