class file

import java.io.*;

public class ClassAnalyzer {

    public static void main(String[] args) {
        DataInputStream input = null;
        try {
            input = new DataInputStream(new BufferedInputStream(new FileInputStream("C://jdk1.2//bin//Test.class")));
            analyze(input);
        }
        catch(Exception e) {
            System.out.println("Analyze failed!");
        }
        finally {
            try {
                input.close();
            }
            catch(Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    static void analyze(DataInputStream input) throws IOException {
        // read magic number:
        int java_magic = input.readInt();  //u4
        if(java_magic==0xCAFEBABE)
            System.out.println("magic number = 0xCAFEBABE");
        else
            throw new RuntimeException("Invalid magic number!or the input File isn't a classFile");
        // read minor version and major version:
        short minor_ver = input.readShort(); //00 03
        short major_ver = input.readShort(); //00 2D
        System.out.println(" MinorVersion = " + minor_ver + " MajorVersion=" + major_ver);
        // read constant pool:
        short const_pool_count = input.readShort();
        System.out.println("constant_pool_count = " + const_pool_count);
        // read each constant:
        for(int i=1; i<const_pool_count; i++) {
            analyzeConstant(input, i);
        }
    }

    public static void analyzeConstant(DataInputStream input, int index) throws IOException {
        byte flag = input.readByte(); //1B
        // for read:
        byte n8;    // 8bits
        short n16;  // 16bits
        int n32;    // 32bits
        long n64;   // 64bits
        float f;    // 32bits
        double d;   // 64bits
        byte[] buffer; //字节缓冲区
        System.out.println("/nconst index = " + index + ", flag = " + (int)flag);
        switch(flag) {
        case 1: // utf-8 string
            System.out.println(" const type = Utf8");
            n16 = input.readShort();
            System.out.println("     length = " + n16);
            buffer = new byte[n16];
            input.readFully(buffer);
            System.out.println("      value = " + new String(buffer));
            break;
        case 3: // integer
            System.out.println(" const type = Integer");
            n32 = input.readInt();
            System.out.println("      value = " + n32);
            break;
        case 4: // float
            System.out.println(" const type = Float");
            f = input.readFloat();
            System.out.println("      value = " + f);
            break;
        case 5: // long
            System.out.println(" const type = Long");
            n64 = input.readLong();
            System.out.println("      value = " + n64);
            break;
        case 6: // double
            System.out.println(" const type = Double");
            d = input.readDouble();
            System.out.println("      value = " + d);
            break;
        case 7: // class or interface reference
            System.out.println(" const type = Class");
            n16 = input.readShort();
            System.out.println("      index = " + n16 + " (where to find the class name)");
            break;
        case 8: // string
            System.out.println(" const type = String");
            n16 = input.readShort();
            System.out.println("      index = " + n16);
            break;
        case 9: // field reference
            System.out.println(" const type = Fieldreference");
            n16 = input.readShort();
            System.out.println("class index = " + n16 + " (where to find the class)");
            n16 = input.readShort();
            System.out.println("nameAndType = " + n16 + " (where to find the NameAndType)");
            break;
        case 10: // method reference
            System.out.println(" const type = Methodreference");
            n16 = input.readShort();
            System.out.println("class index = " + n16 + " (where to find the class)");
            n16 = input.readShort();
            System.out.println("nameAndType = " + n16 + " (where to find the NameAndType)");
            break;
        case 11: // interface method reference
            System.out.println(" const type = InterfaceMethodreference");
            n16 = input.readShort();
            System.out.println("class index = " + n16 + " (where to find the interface)");
            n16 = input.readShort();
            System.out.println("nameAndType = " + n16 + " (where to find the NameAndType)");
            break;
        case 12: // name and type reference
            System.out.println(" const type = NameAndType");
            n16 = input.readShort();
            System.out.println(" name index = " + n16 + " (where to find the name)");
            n16 = input.readShort();
            System.out.println(" des cripter = " + n16 + " (where to find the des criptor)");
            break;
        default:
            throw new RuntimeException("Invalid constant pool flag: " + flag);
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值