Skip to content

Instantly share code, notes, and snippets.

@thedmitriyk
Last active November 11, 2015 13:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thedmitriyk/5a32188056414d561f49 to your computer and use it in GitHub Desktop.
Save thedmitriyk/5a32188056414d561f49 to your computer and use it in GitHub Desktop.
Parse JVM-style method parameter strings (e.g. "I[[BLjava/lang/String;") into separate components (e.g. Seq("I", "[[B", "Ljava/lang/String;"))
object JVMParamParser {
def parse(sig: String) = {
def parseRec(acc: Seq[String], sig: List[Char]): Seq[String] = {
val drop = sig.dropWhile(_ == '[')
val fill = Seq.fill(sig.length - drop.length)('[').mkString
drop match {
case head :: tail if jvmTypeLookup.contains(head) =>
parseRec(acc :+ s"$fill${head.toString}", tail)
case head :: tail if head == 'L' =>
val split = drop.mkString.split(';')
parseRec(acc :+ s"$fill${split.head};", split.tail.mkString(";").toList)
case head :: tail =>
parseRec(acc :+ s"$fill?", tail)
case _ =>
acc
}
}
parseRec(Seq(), sig.toList)
}
val jvmTypeLookup = Map(
'B' -> "byte",
'C' -> "char",
'D' -> "double",
'F' -> "float",
'I' -> "int",
'J' -> "long",
'S' -> "short",
'V' -> "void",
'Z' -> "boolean"
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment