コード例 #1
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);
        }