diff options
-rw-r--r-- | src/scalap/scala/tools/scalap/Main.scala | 169 | ||||
-rw-r--r-- | test/files/run/t6669.scala | 7 |
2 files changed, 89 insertions, 87 deletions
diff --git a/src/scalap/scala/tools/scalap/Main.scala b/src/scalap/scala/tools/scalap/Main.scala index edad3bfc09..dc3bf64060 100644 --- a/src/scalap/scala/tools/scalap/Main.scala +++ b/src/scalap/scala/tools/scalap/Main.scala @@ -10,11 +10,12 @@ package tools.scalap import java.io.{ PrintStream, OutputStreamWriter, ByteArrayOutputStream } import scala.reflect.NameTransformer -import scalax.rules.scalasig._ -import scala.tools.nsc.util.{ ClassFileLookup, ClassPath, JavaClassPath } -import scala.tools.util.PathResolver -import ClassPath.DefaultJavaContext import scala.tools.nsc.io.AbstractFile +import scala.tools.nsc.util.ClassFileLookup +import scala.tools.nsc.util.JavaClassPath +import scala.tools.nsc.util.ClassPath.DefaultJavaContext +import scala.tools.util.PathResolver +import scalax.rules.scalasig._ /**The main object used to execute scalap on the command-line. * @@ -42,12 +43,12 @@ class Main { * * @param clazz the class file to be processed. */ - def processJavaClassFile(clazz: Classfile) { + def processJavaClassFile(clazz: Classfile): Unit = { // construct a new output stream writer val out = new OutputStreamWriter(Console.out) val writer = new JavaWriter(clazz, out) // print the class - writer.printClass + writer.printClass() out.flush() } @@ -60,21 +61,20 @@ class Main { syms.head.parent match { // Partial match - case Some(p) if (p.name != "<empty>") => { + case Some(p) if p.name != "<empty>" => val path = p.path if (!isPackageObject) { - stream.print("package "); - stream.print(path); + stream.print("package ") + stream.print(path) stream.print("\n") } else { val i = path.lastIndexOf(".") if (i > 0) { - stream.print("package "); + stream.print("package ") stream.print(path.substring(0, i)) stream.print("\n") } } - } case _ => } // Print classes @@ -106,92 +106,91 @@ class Main { // we can afford allocations because this is not a performance critical code classname.split('.').map(NameTransformer.encode).mkString(".") } - val cls = path.findClass(encName) - if (cls.isDefined && cls.get.binary.isDefined) { - val cfile = cls.get.binary.get - if (verbose) { - Console.println(Console.BOLD + "FILENAME" + Console.RESET + " = " + cfile.path) - } - val bytes = cfile.toByteArray - if (isScalaFile(bytes)) { - Console.println(decompileScala(bytes, isPackageObjectFile(encName))) - } else { - // construct a reader for the classfile content - val reader = new ByteArrayReader(cfile.toByteArray) - // parse the classfile - val clazz = new Classfile(reader) - processJavaClassFile(clazz) - } - // if the class corresponds to the artificial class scala.Any. - // (see member list in class scala.tool.nsc.symtab.Definitions) - } - else - Console.println("class/object " + classname + " not found.") - } - object EmptyClasspath extends ClassPath[AbstractFile] { - /** - * The short name of the package (without prefix) - */ - def name = "" - def asURLs = Nil - def asClassPathString = "" - - val context = DefaultJavaContext - val classes = IndexedSeq() - val packages = IndexedSeq() - val sourcepaths = IndexedSeq() + path.findClassFile(encName) match { + case Some(classFile) => + if (verbose) { + Console.println(Console.BOLD + "FILENAME" + Console.RESET + " = " + classFile.path) + } + val bytes = classFile.toByteArray + if (isScalaFile(bytes)) { + Console.println(decompileScala(bytes, isPackageObjectFile(encName))) + } else { + // construct a reader for the classfile content + val reader = new ByteArrayReader(classFile.toByteArray) + // parse the classfile + val clazz = new Classfile(reader) + processJavaClassFile(clazz) + } + // if the class corresponds to the artificial class scala.Any. + // (see member list in class scala.tool.nsc.symtab.Definitions) + case _ => + Console.println(s"class/object $classname not found.") + } } } object Main extends Main { + + private object opts { + val cp = "-cp" + val help = "-help" + val classpath = "-classpath" + val showPrivateDefs = "-private" + val verbose = "-verbose" + val version = "-version" + } + /** Prints usage information for scalap. */ - def usage() { - Console println """ + def usage(): Unit = { + Console println s""" |Usage: scalap {<option>} <name> |where <name> is fully-qualified class name or <package_name>.package for package objects |and <option> is - | -private print private definitions - | -verbose print out additional information - | -version print out the version number of scalap - | -help display this usage message - | -classpath <path> specify where to find user class files - | -cp <path> specify where to find user class files + | ${opts.showPrivateDefs} print private definitions + | ${opts.verbose} print out additional information + | ${opts.version} print out the version number of scalap + | ${opts.help} display this usage message + | ${opts.classpath} <path> specify where to find user class files + | ${opts.cp} <path> specify where to find user class files """.stripMargin.trim } - def main(args: Array[String]) { - // print usage information if there is no command-line argument - if (args.isEmpty) - return usage() - - val arguments = Arguments.Parser('-') - .withOption("-private") - .withOption("-verbose") - .withOption("-version") - .withOption("-help") - .withOptionalArg("-classpath") - .withOptionalArg("-cp") - .parse(args); - - if (arguments contains "-version") - Console.println(versionMsg) - if (arguments contains "-help") - usage() - - verbose = arguments contains "-verbose" - printPrivates = arguments contains "-private" - // construct a custom class path - val cparg = List("-classpath", "-cp") map (arguments getArgument _) reduceLeft (_ orElse _) - val path = cparg match { - case Some(cp) => new JavaClassPath(DefaultJavaContext.classesInExpandedPath(cp), DefaultJavaContext) - case _ => PathResolver.fromPathString(".") // include '.' in the default classpath SI-6669 + def main(args: Array[String]): Unit = + // print usage information if there is no command-line argument + if (args.isEmpty) usage() + else { + val arguments = parseArguments(args) + + if (arguments contains opts.version) + Console.println(versionMsg) + if (arguments contains opts.help) + usage() + + verbose = arguments contains opts.verbose + printPrivates = arguments contains opts.showPrivateDefs + // construct a custom class path + val cpArg = List(opts.classpath, opts.cp) map (arguments getArgument _) reduceLeft (_ orElse _) + val path = cpArg match { + case Some(cp) => new JavaClassPath(DefaultJavaContext.classesInExpandedPath(cp), DefaultJavaContext) + case _ => PathResolver.fromPathString(".") // include '.' in the default classpath SI-6669 + } + + // print the classpath if output is verbose + if (verbose) + Console.println(Console.BOLD + "CLASSPATH" + Console.RESET + " = " + path.asClassPathString) + + // process all given classes + arguments.getOthers foreach process(arguments, path) } - // print the classpath if output is verbose - if (verbose) - Console.println(Console.BOLD + "CLASSPATH" + Console.RESET + " = " + path) - // process all given classes - arguments.getOthers foreach process(arguments, path) - } + private def parseArguments(args: Array[String]) = + Arguments.Parser('-') + .withOption(opts.showPrivateDefs) + .withOption(opts.verbose) + .withOption(opts.version) + .withOption(opts.help) + .withOptionalArg(opts.classpath) + .withOptionalArg(opts.cp) + .parse(args) } diff --git a/test/files/run/t6669.scala b/test/files/run/t6669.scala index e18f2514a9..27c4970d60 100644 --- a/test/files/run/t6669.scala +++ b/test/files/run/t6669.scala @@ -1,4 +1,5 @@ import java.io.{ByteArrayOutputStream, PrintStream} +import scala.reflect.io.File object Test extends App { val baos = new ByteArrayOutputStream() @@ -9,9 +10,11 @@ object Test extends App { scala.tools.scalap.Main.main(Array("-verbose", "java.lang.Object")) } + val currentLocationCpFragment = File.pathSeparator + "." + // now make sure we saw the '.' in the classpath val msg1 = baos.toString() - assert(msg1 contains "directory classpath: .", s"Did not see '.' in the default class path. Full results were:\n$msg1") + assert(msg1 contains currentLocationCpFragment, s"Did not see '.' in the default class path. Full results were:\n$msg1") // then test again with a user specified classpath baos.reset @@ -22,5 +25,5 @@ object Test extends App { // now make sure we did not see the '.' in the classpath val msg2 = baos.toString() - assert(!(msg2 contains "directory classpath: ."), s"Did saw '.' in the user specified class path. Full results were:\n$msg2") + assert(!(msg2 contains currentLocationCpFragment), s"Did saw '.' in the user specified class path. Full results were:\n$msg2") } |