public TConstantPool(BinaryReader Reader) { FReader = Reader; FMaxItems = Common.ReadWord(FReader) - 1; FItems = new ArrayList(); int count = 0; // goes from 1 -> constantpoolcount - 1 while (count < FMaxItems) { byte tag = Common.ReadByte(FReader); switch (tag) { case (byte)ConstantPoolInfoTag.ConstantClass: { ConstantClassInfo cc = new ConstantClassInfo(); cc.Read(tag, FReader); FItems.Add(cc); break; } case (byte)ConstantPoolInfoTag.ConstantString: { ConstantStringInfo cc = new ConstantStringInfo(); cc.Read(tag, FReader); FItems.Add(cc); break; } case (byte)ConstantPoolInfoTag.ConstantFieldref: { ConstantFieldrefInfo cc = new ConstantFieldrefInfo(); cc.Read(tag, FReader); FItems.Add(cc); break; } case (byte)ConstantPoolInfoTag.ConstantMethodref: { ConstantMethodrefInfo cc = new ConstantMethodrefInfo(); cc.Read(tag, FReader); FItems.Add(cc); break; } case (byte)ConstantPoolInfoTag.ConstantInterfaceMethodref: { ConstantInterfaceMethodrefInfo cc = new ConstantInterfaceMethodrefInfo(); cc.Read(tag, FReader); FItems.Add(cc); break; } case (byte)ConstantPoolInfoTag.ConstantInteger: { ConstantIntegerInfo cc = new ConstantIntegerInfo(); cc.Read(tag, FReader); FItems.Add(cc); break; } case (byte)ConstantPoolInfoTag.ConstantFloat: { ConstantFloatInfo cc = new ConstantFloatInfo(); cc.Read(tag, FReader); FItems.Add(cc); break; } case (byte)ConstantPoolInfoTag.ConstantLong: { ConstantLongInfo cc = new ConstantLongInfo(); cc.Read(tag, FReader); FItems.Add(cc); // longs take up two entries in the pool table count++; FItems.Add(cc); break; } case (byte)ConstantPoolInfoTag.ConstantDouble: { ConstantDoubleInfo cc = new ConstantDoubleInfo(); cc.Read(tag, FReader); FItems.Add(cc); // so do doubles count++; FItems.Add(cc); break; } case (byte)ConstantPoolInfoTag.ConstantNameAndType: { ConstantNameAndTypeInfo cc = new ConstantNameAndTypeInfo(); cc.Read(tag, FReader); FItems.Add(cc); break; } case (byte)ConstantPoolInfoTag.ConstantUtf8: { ConstantUtf8Info cc = new ConstantUtf8Info(); cc.Read(tag, FReader); FItems.Add(cc); break; } default: // fail safe ? count++; break; } count++; } foreach (ConstantPoolInfo cc in FItems) { cc.Resolve(FItems); } }
public void Write(BinaryWriter Writer) { // i am assuming we have a valid constant pool list... // i dont do any error checking here except bare minimum! // write the number of constant pool entries Common.WriteWord(Writer, FMaxItems + 1); int count = 0; // goes from 1 -> constantpoolcount - 1 while (count < FMaxItems) { ConstantPoolInfo Item = (ConstantPoolInfo)FItems[count]; switch (Item.Tag) { case (byte)ConstantPoolInfoTag.ConstantClass: { ConstantClassInfo cc = (ConstantClassInfo)Item; cc.Write(Writer); break; } case (byte)ConstantPoolInfoTag.ConstantString: { ConstantStringInfo cc = (ConstantStringInfo)Item; cc.Write(Writer); break; } case (byte)ConstantPoolInfoTag.ConstantFieldref: { ConstantFieldrefInfo cc = (ConstantFieldrefInfo)Item; cc.Write(Writer); break; } case (byte)ConstantPoolInfoTag.ConstantMethodref: { ConstantMethodrefInfo cc = (ConstantMethodrefInfo)Item; cc.Write(Writer); break; } case (byte)ConstantPoolInfoTag.ConstantInterfaceMethodref: { ConstantInterfaceMethodrefInfo cc = (ConstantInterfaceMethodrefInfo)Item; cc.Write(Writer); break; } case (byte)ConstantPoolInfoTag.ConstantInteger: { ConstantIntegerInfo cc = (ConstantIntegerInfo)Item; cc.Write(Writer); break; } case (byte)ConstantPoolInfoTag.ConstantFloat: { ConstantFloatInfo cc = (ConstantFloatInfo)Item; cc.Write(Writer); break; } case (byte)ConstantPoolInfoTag.ConstantLong: { ConstantLongInfo cc = (ConstantLongInfo)Item; cc.Write(Writer); // longs take up two entries in the pool table count++; break; } case (byte)ConstantPoolInfoTag.ConstantDouble: { ConstantDoubleInfo cc = (ConstantDoubleInfo)Item; cc.Write(Writer); // so do doubles count++; break; } case (byte)ConstantPoolInfoTag.ConstantNameAndType: { ConstantNameAndTypeInfo cc = (ConstantNameAndTypeInfo)Item; cc.Write(Writer); break; } case (byte)ConstantPoolInfoTag.ConstantUtf8: { ConstantUtf8Info cc = (ConstantUtf8Info)Item; cc.Write(Writer); break; } default: // fail safe ? // BADDDDDDDDDDDDDDDDDDDDD, prolly should check/fix this count++; break; } count++; } }
public TChangeRecord ChangeFieldName(int FieldNumber, string NewName) { FieldInfo Field = (FieldInfo)FFields.Items[FieldNumber]; //FieldInfo OriginalFieldInfo = Field.Clone(); //FieldInfo NewField = null; TChangeRecord Result = null; ConstantFieldrefInfo FieldRef = null; ushort NewNameIndex; // first we need to loop through the constant pool for method // references that match our new method name for (int i = 0; i < FConstantPool.MaxItems(); i++) { if (FConstantPool.Item(i).Tag == (byte)ConstantPoolInfoTag.ConstantFieldref) { FieldRef = (ConstantFieldrefInfo)FConstantPool.Item(i); if (FieldRef.ParentClass.Name == FThisClassName && FieldRef.NameAndType.Name == Field.Name.Value && FieldRef.NameAndType.Descriptor == Field.Descriptor) { // jackpot, we found the reference! // there should be only one, so we will break and fix it up after we generate the new name break; } } FieldRef = null; } Field.Name.References--; // add a new string constant to the pool ConstantUtf8Info NewUtf = new ConstantUtf8Info(NewName); NewNameIndex = ConstantPool.Add(NewUtf); // set the method its new name Field.SetName(NewNameIndex, ConstantPool); Field.Name.References = 1; //NewField = Field.Clone(); if (FieldRef == null) return Result; if (FieldRef.NameAndType.References <= 1) { // if this is the only reference to the name/type descriptor // we can overwrite the value FieldRef.NameAndType.SetName(NewNameIndex, FConstantPool); } else { // we have to make a new one ! FieldRef.NameAndType.References--; // add a new string constant to the pool ConstantNameAndTypeInfo NewNaT = new ConstantNameAndTypeInfo(NewNameIndex, FieldRef.NameAndType.TypeIndex, FConstantPool); ushort NewIndex = ConstantPool.Add(NewNaT); // set the method its new name FieldRef.SetNameAndType(NewIndex, ConstantPool); FieldRef.NameAndType.References = 1; } return Result; }