Created
July 27, 2012 13:47
-
-
Save Sciss/3188126 to your computer and use it in GitHub Desktop.
Disassemble.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// to be called from the class output folder as cwd, traverses that folder | |
// for class files, calls javap on each, and writes the results to text | |
// files relative to a base directory given as parameter -d <outputDir> | |
// | |
// e.g. | |
// cd scala-2.10.0-M5 | |
// mkdir ~/Desktop/m5 | |
// scala /path/to/Disassemble -d ~/Desktop/m5 | |
// ../scala-2.10.0-M6 | |
// mkdir ~/Desktop/m6 | |
// scala /path/to/Disassemble -d ~/Desktop/m6 | |
// opendiff ~/Desktop/m5 ~/Desktop/m6 | |
import sys.process._ | |
import java.io.File | |
def recurse( inBase: File, outBase: File )( in: File = inBase, name: String = "" ) { | |
val arr = in.listFiles | |
if( arr == null ) return | |
arr.foreach { inChild => | |
val f = inChild.getName | |
val name1 = if( name == "" ) name else name + "." | |
if( inChild.isDirectory ) { | |
recurse( inBase, outBase )( new File( in, f ), name1 + f ) | |
} else if( inChild.isFile ) { | |
if( f.endsWith( ".class" )) { | |
val outName = name1 + f.substring( 0, f.length - 6 ) | |
disassemble( outName, outBase ) | |
} | |
} | |
} | |
} | |
def disassemble( name: String, outBase: File ) { | |
val cmd = Seq( "javap", "-c", name ) | |
println( cmd.mkString( " " )) | |
val code = (cmd #> new File( outBase, name + ".txt" )).! | |
if( code != 0 ) println( "... failed with code " + code ) | |
} | |
args.toSeq match { | |
case Seq( "-d", outPath ) => | |
val outDir = new File( outPath ) | |
require( outDir.isDirectory, outPath + " must be an existing directory" ) | |
recurse( new File( "" ).getAbsoluteFile, outDir )() | |
case _ => println( "Usage: scala Disassemble -d <outputDir>" ) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment