Skip to content

Instantly share code, notes, and snippets.

@bmc
Created August 18, 2011 20:46
Show Gist options
  • Save bmc/1155154 to your computer and use it in GitHub Desktop.
Save bmc/1155154 to your computer and use it in GitHub Desktop.
Ugly, but damned useful, structural type example
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"))
}
}
}
@bmc
Copy link
Author

bmc commented Aug 18, 2011

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