Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import scala.tools.nsc.{Interpreter, Settings, Global}
- import scala.tools.nsc.util.{Position,NoPosition, ShowPickled}
- import scala.reflect.generic.{PickleFormat, PickleBuffer}
- import scala.tools.nsc.symtab.classfile.{ClassfileParser}
- import scala.tools.nsc.io.AbstractFile
- import java.io.{OutputStream, PrintStream, ByteArrayOutputStream}
- object DumpGenericsSignature {
- class MyParser extends ClassfileParser{
- val global:Global=new Interpreter(new Settings).compiler
- override def parseClass(){
- in.nextChar // access_flags
- in.nextChar // this_class
- in.nextChar // super_class
- in.skip(2*((in.nextChar)&0xffff)) // interfaces
- }
- def dumpFieldSignature(p:OutputStream)=Console.withOut(p){
- for(_ <- 0 until in.nextChar){
- in.nextChar // access_flags
- val name=pool.getName(in.nextChar) // name_index
- print("Field: "+name+" | ");
- val desc = pool.getExternalName(in.nextChar) // descriptor_index
- print(desc);
- for(_ <- 0 until in.nextChar){
- val attribute_name=pool.getName(in.nextChar)
- attribute_name match{
- case global.nme.SignatureATTR => {
- in.nextInt // 2
- val sig = pool.getExternalName(in.nextChar)
- print(" | "+sig);
- }
- case _ => in.skip(in.nextInt)
- }
- }
- println();
- }
- }
- def dumpMethodSignature(p:OutputStream)=Console.withOut(p){
- for(_ <- 0 until in.nextChar){
- in.nextChar // access_flags
- val name=pool.getName(in.nextChar) // name_index
- print("Method: "+name+" | ");
- val sig = pool.getExternalName(in.nextChar) // signature_index
- print(sig);
- for(_ <- 0 until in.nextChar){
- val attribute_name=pool.getName(in.nextChar)
- attribute_name match{
- case global.nme.SignatureATTR => {
- in.nextInt // 2
- val sig = pool.getExternalName(in.nextChar)
- print(" | "+sig);
- }
- case _ => in.skip(in.nextInt)
- }
- }
- println();
- }
- }
- def dumpSignature(p:OutputStream)=Console.withOut(p){
- for(_ <- 0 until in.nextChar){
- val attrName = pool.getName(in.nextChar)
- val attrLen = in.nextInt
- print("Attribute: "+attrName+" | ")
- attrName match {
- case global.nme.SignatureATTR => {
- print(pool.getExternalName(in.nextChar))
- in.skip(attrLen-2)
- }
- case global.nme.ScalaSignatureATTR => {
- println()
- val buf=in.nextBytes(attrLen)
- val pickle = new PickleBuffer(buf, 0, buf.length)
- ShowPickled.printFile(pickle, new PrintStream(p))
- }
- case global.nme.SourceFileATTR => {
- print(pool.getName(in.nextChar))
- }
- case global.nme.SyntheticATTR => { in.skip(attrLen) }
- case global.nme.BridgeATTR => { in.skip(attrLen) }
- case global.nme.DeprecatedATTR => { in.skip(attrLen) }
- case global.nme.ConstantValueATTR => { in.skip(attrLen) }
- case global.nme.JacoMetaATTR => { in.skip(attrLen) }
- case global.nme.AnnotationDefaultATTR => { in.skip(attrLen) }
- case global.nme.RuntimeAnnotationATTR => { in.skip(attrLen) }
- case _ => { in.skip(attrLen) }
- }
- println()
- }
- }
- def parse(clssFile:String):String={
- import global._
- this.parse(AbstractFile.getFile(clssFile),
- NoSymbol.newClass(scala.tools.nsc.util.NoPosition, newTermName(clssFile)))
- val b=new ByteArrayOutputStream
- dumpFieldSignature(b)
- dumpMethodSignature(b)
- dumpSignature(b)
- b.toString
- }
- }
- def main(arg:Array[String]){
- val p=new MyParser()
- println(p.parse(arg(0)))
- }
- }
Add Comment
Please, Sign In to add comment