Created
August 18, 2011 20:46
-
-
Save bmc/1155154 to your computer and use it in GitHub Desktop.
Ugly, but damned useful, structural type example
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
class ClassDumper | |
{ | |
type Meth = | |
{ | |
def getParameterTypes: Array[Class[_]] | |
def getName: String | |
} | |
def methodToString(m: Meth): String = | |
{ | |
m.getName + | |
"(" + m.getParameterTypes.map(c => c.getCanonicalName).mkString(", ") + | |
")" | |
} | |
def methodStrings(l: Array[Meth]): List[String] = | |
l.map {m => methodToString(m)}.toList.sortWith((a, b) => a < b) | |
def classMethodSignatures(c: Class[_]): List[String] = | |
{ | |
// Must cast, because getConstructors returns Array[Constructor[_]], | |
// but getMethods returns Array[Method]. | |
methodStrings(c.getConstructors.asInstanceOf[Array[Meth]]) ++ | |
methodStrings(c.getMethods.asInstanceOf[Array[Meth]]) | |
} | |
} | |
object Test | |
{ | |
def main(args: Array[String]) | |
{ | |
val d = new ClassDumper | |
for (a <- args) | |
{ | |
println("Class: " + a) | |
println("-" * 30) | |
println(d.classMethodSignatures(Class.forName(a)).mkString("\n")) | |
} | |
} | |
} |
Cool! Might make a nice sbt command to fill the void left by javap's disappearance.
Hmm. Good point. In fact, that's almost exactly why I wrote it.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample run: