public short AddConstantFieldref(Field field)
        {
            if (field == null) return 0;

            short classIndex = AddConstantClass(field.DeclaringType);
            short nameAndTypeIndex = AddConstantNameAndType(field.Name, field);

            CompileConstantFieldref fieldConst =
                ConstantPool.OfType<CompileConstantFieldref>().FirstOrDefault(
                    x => x.ClassIndex == classIndex && x.NameAndTypeIndex == nameAndTypeIndex);

            if (fieldConst == null)
            {
                fieldConst = new CompileConstantFieldref
                                 {
                                     PoolIndex = nextConstantIndex++,
                                     ClassIndex = classIndex,
                                     NameAndTypeIndex = nameAndTypeIndex
                                 };

                ConstantPool.Add(fieldConst);
            }

            return fieldConst.PoolIndex;
        }
Example #2
0
        private static CompileConstant[] ReadConstants(EndianBinaryReader reader)
        {
            short constantCount = reader.ReadInt16();
            var constants = new CompileConstant[constantCount];

            for (int i = 1; i < constantCount; i++)
            {
                var tag = (CompileConstants)reader.ReadByte();
                switch (tag)
                {
                    case CompileConstants.Class:
                        constants[i] = new CompileConstantClass().Read(reader);
                        break;
                    case CompileConstants.Fieldref:
                        constants[i] = new CompileConstantFieldref().Read(reader);
                        break;
                    case CompileConstants.Methodref:
                        constants[i] = new CompileConstantMethodref().Read(reader);
                        break;
                    case CompileConstants.InterfaceMethodref:
                        constants[i] = new CompileConstantInterfaceMethodref().Read(reader);
                        break;
                    case CompileConstants.String:
                        constants[i] = new CompileConstantString().Read(reader);
                        break;
                    case CompileConstants.Integer:
                        constants[i] = new CompileConstantInteger().Read(reader);
                        break;
                    case CompileConstants.Float:
                        constants[i] = new CompileConstantFloat().Read(reader);
                        break;
                    case CompileConstants.Long:
                        constants[i] = new CompileConstantLong().Read(reader);
                        i++; // The next slot is invalid
                        break;
                    case CompileConstants.Double:
                        constants[i] = new CompileConstantDouble().Read(reader);
                        i++; // The next slot is invalid
                        break;
                    case CompileConstants.NameAndType:
                        constants[i] = new CompileConstantNameAndType().Read(reader);
                        break;
                    case CompileConstants.Utf8:
                        constants[i] = new CompileConstantUtf8().Read(reader);
                        break;
                    case CompileConstants.MethodHandle:
                        constants[i] = new CompileConstantMethodHandle().Read(reader);
                        break;
                    case CompileConstants.MethodType:
                        constants[i] = new CompileConstantMethodType().Read(reader);
                        break;
                    case CompileConstants.InvokeDynamic:
                        constants[i] = new CompileConstantInvokeDynamic().Read(reader);
                        break;
                    default:
                        throw new NotImplementedException();
                }
            }

            return constants;
        }