예제 #1
0
        public static MemberInfo[] ReadMembers(ClassReader reader, ConstantPool constantPool)
        {
            var count   = reader.ReadU2();
            var members = new MemberInfo[count];

            for (var i = 0; i < members.Length; i++)
            {
                members[i] = ReadMemberInfo(reader, constantPool);
            }
            return(members);
        }
예제 #2
0
        public static MemberInfo ReadMemberInfo(ClassReader reader, ConstantPool pool)
        {
            var member = new MemberInfo()
            {
                AccessFlags     = reader.ReadU2(),
                NameIndex       = reader.ReadU2(),
                DescriptorIndex = reader.ReadU2(),
                Attributes      = reader.ReadAttributes(pool)
            };

            member.Name       = pool.GetUtf8String(member.NameIndex);
            member.Descriptor = pool.GetUtf8String(member.DescriptorIndex);
            return(member);
        }
예제 #3
0
        public static ClassFile Parse(ClassReader reader)
        {
            var magic = reader.ReadU4();

            if (magic != Constants.Magic)
            {
                throw new Exception("java.lang.ClassFormatError:magic");
            }
            ushort minorVersion = reader.ReadU2(), majorVersion = reader.ReadU2();

            switch (majorVersion)
            {
            case Constants.JDK2_MajorVersion:
            case Constants.JDK3_MajorVersion:
            case Constants.JDK4_MajorVersion:
            case Constants.JDK5_MajorVersion:
            case Constants.JDK6_MajorVersion:
            case Constants.JDK7_MajorVersion:
            case Constants.JDK8_MajorVersion:
                if (minorVersion != 0)
                {
                    throw new Exception("java.lang.UnsupportedClassVersionError!");
                }
                break;

            default:
                throw new Exception("java.lang.UnsupportedClassVersionError!");
            }
            var pool      = new ConstantPool(reader);
            var classFile = new ClassFile
            {
                MinorVersion = minorVersion,
                MajorVersion = majorVersion,
                ConstantPool = pool,
                AccessFlags  = reader.ReadU2(),
                ThisClass    = reader.ReadU2(),
                SuperClass   = reader.ReadU2(),
                Interfaces   = reader.ReadU2S()
            };

            classFile.ClassName      = pool.GetClassName(classFile.ThisClass);
            classFile.SuperClassName = pool.GetClassName(classFile.SuperClass);
            classFile.Fields         = MemberInfo.ReadMembers(reader, classFile.ConstantPool);
            classFile.Methods        = MemberInfo.ReadMembers(reader, classFile.ConstantPool);
            classFile.Attributes     = reader.ReadAttributes(classFile.ConstantPool);
            classFile.InterfaceNames = classFile.Interfaces.Select(index => classFile.ConstantPool.GetClassName(index)).ToArray();
            return(classFile);
        }