public short AddConstantMethodref(Method method)
        {
            if (method == null) return 0;

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

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

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

                ConstantPool.Add(methodConst);
            }

            return methodConst.PoolIndex;
        }
示例#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;
        }