Last active
November 11, 2015 13:46
-
-
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;"))
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
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