Guest User

Untitled

a guest
Feb 20th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.06 KB | None | 0 0
  1. import scala.tools.nsc.{Interpreter, Settings, Global}
  2. import scala.tools.nsc.util.{Position,NoPosition, ShowPickled}
  3. import scala.reflect.generic.{PickleFormat, PickleBuffer}
  4. import scala.tools.nsc.symtab.classfile.{ClassfileParser}
  5. import scala.tools.nsc.io.AbstractFile
  6. import java.io.{OutputStream, PrintStream, ByteArrayOutputStream}
  7.  
  8. object DumpGenericsSignature {
  9. class MyParser extends ClassfileParser{
  10.  
  11. val global:Global=new Interpreter(new Settings).compiler
  12.  
  13. override def parseClass(){
  14. in.nextChar // access_flags
  15. in.nextChar // this_class
  16. in.nextChar // super_class
  17. in.skip(2*((in.nextChar)&0xffff)) // interfaces
  18. }
  19.  
  20. def dumpFieldSignature(p:OutputStream)=Console.withOut(p){
  21. for(_ <- 0 until in.nextChar){
  22. in.nextChar // access_flags
  23. val name=pool.getName(in.nextChar) // name_index
  24. print("Field: "+name+" | ");
  25. val desc = pool.getExternalName(in.nextChar) // descriptor_index
  26. print(desc);
  27. for(_ <- 0 until in.nextChar){
  28. val attribute_name=pool.getName(in.nextChar)
  29. attribute_name match{
  30. case global.nme.SignatureATTR => {
  31. in.nextInt // 2
  32. val sig = pool.getExternalName(in.nextChar)
  33. print(" | "+sig);
  34. }
  35. case _ => in.skip(in.nextInt)
  36. }
  37. }
  38. println();
  39. }
  40. }
  41.  
  42. def dumpMethodSignature(p:OutputStream)=Console.withOut(p){
  43. for(_ <- 0 until in.nextChar){
  44. in.nextChar // access_flags
  45. val name=pool.getName(in.nextChar) // name_index
  46. print("Method: "+name+" | ");
  47. val sig = pool.getExternalName(in.nextChar) // signature_index
  48. print(sig);
  49. for(_ <- 0 until in.nextChar){
  50. val attribute_name=pool.getName(in.nextChar)
  51. attribute_name match{
  52. case global.nme.SignatureATTR => {
  53. in.nextInt // 2
  54. val sig = pool.getExternalName(in.nextChar)
  55. print(" | "+sig);
  56. }
  57. case _ => in.skip(in.nextInt)
  58. }
  59. }
  60. println();
  61. }
  62. }
  63.  
  64. def dumpSignature(p:OutputStream)=Console.withOut(p){
  65. for(_ <- 0 until in.nextChar){
  66. val attrName = pool.getName(in.nextChar)
  67. val attrLen = in.nextInt
  68.  
  69. print("Attribute: "+attrName+" | ")
  70.  
  71. attrName match {
  72. case global.nme.SignatureATTR => {
  73. print(pool.getExternalName(in.nextChar))
  74. in.skip(attrLen-2)
  75. }
  76. case global.nme.ScalaSignatureATTR => {
  77. println()
  78. val buf=in.nextBytes(attrLen)
  79. val pickle = new PickleBuffer(buf, 0, buf.length)
  80. ShowPickled.printFile(pickle, new PrintStream(p))
  81. }
  82. case global.nme.SourceFileATTR => {
  83. print(pool.getName(in.nextChar))
  84. }
  85. case global.nme.SyntheticATTR => { in.skip(attrLen) }
  86. case global.nme.BridgeATTR => { in.skip(attrLen) }
  87. case global.nme.DeprecatedATTR => { in.skip(attrLen) }
  88. case global.nme.ConstantValueATTR => { in.skip(attrLen) }
  89. case global.nme.JacoMetaATTR => { in.skip(attrLen) }
  90. case global.nme.AnnotationDefaultATTR => { in.skip(attrLen) }
  91. case global.nme.RuntimeAnnotationATTR => { in.skip(attrLen) }
  92. case _ => { in.skip(attrLen) }
  93. }
  94. println()
  95. }
  96. }
  97.  
  98. def parse(clssFile:String):String={
  99. import global._
  100. this.parse(AbstractFile.getFile(clssFile),
  101. NoSymbol.newClass(scala.tools.nsc.util.NoPosition, newTermName(clssFile)))
  102. val b=new ByteArrayOutputStream
  103. dumpFieldSignature(b)
  104. dumpMethodSignature(b)
  105. dumpSignature(b)
  106. b.toString
  107. }
  108. }
  109.  
  110. def main(arg:Array[String]){
  111. val p=new MyParser()
  112. println(p.parse(arg(0)))
  113. }
  114. }
Add Comment
Please, Sign In to add comment