Ejemplo n.º 1
0
            public override void Write(JavaWriter wtr)
            {
                wtr.Where.Push(tag);
                wtr.Write16(wtr.ConstUtf8(tag));
                wtr.Fork();
                wtr.Write16(maxStack);
                wtr.Write16(maxLocals);
                wtr.Write32((uint)code.Length);
                wtr.WriteBlock(code);

                if (exceptions == null)
                {
                    wtr.Write16(0);
                }
                else
                {
                    wtr.Write16(exceptions.Length);
                    for (int i = 0; i < exceptions.Length; i++)
                    {
                        wtr.Write16(exceptions[i].start);
                        wtr.Write16(exceptions[i].endPlus1);
                        wtr.Write16(exceptions[i].handler);
                        wtr.Write16((exceptions[i].catchType == null) ? 0 :
                                    wtr.ConstClass(exceptions[i].catchType));
                    }
                }

                attributes.Write(wtr);
                wtr.Join();
                wtr.Where.Pop();
            }
Ejemplo n.º 2
0
            public override void Write(JavaWriter wtr)
            {
                int length = sizeof(ushort) // classes.Length
                             + classes.Length * sizeof(ushort) * 4;

                Write(wtr, tag, length);
                wtr.Write16(classes.Length);
                for (int i = 0; i < classes.Length; i++)
                {
                    wtr.Write16(wtr.ConstClass(classes[i].InnerLongName));
                    ushort v = (classes[i].OuterLongName == null) ? (ushort)0
                             : wtr.ConstClass(classes[i].OuterLongName);
                    wtr.Write16(v);
                    v = (classes[i].InnerShortName == null) ? (ushort)0
                      : wtr.ConstUtf8(classes[i].InnerShortName);
                    wtr.Write16(v);
                    wtr.Write16((ushort)classes[i].Flags);
                }
            }
Ejemplo n.º 3
0
            public override void Write(JavaWriter wtr)
            {
                int length = sizeof(ushort) // classes.Length
                             + classes.Length * sizeof(ushort);

                Write(wtr, tag, length);
                wtr.Write16(classes.Length);
                for (int i = 0; i < classes.Length; i++)
                {
                    wtr.Write16(wtr.ConstClass(classes[i]));
                }
            }
Ejemplo n.º 4
0
        void FillInstruction_ConstLoad(JavaWriter wtr, Instruction inst)
        {
            int  constantIndex = -1;
            byte op            = 0;

            if (inst.Data is long vLong)
            {
                if (FillInstruction_ConstLoad_Long(inst, vLong))
                {
                    return;
                }
                constantIndex = wtr.ConstLong(vLong);
                op            = 0x14;
            }

            else if (inst.Data is double vDouble)
            {
                if (FillInstruction_ConstLoad_Double(inst, vDouble))
                {
                    return;
                }
                constantIndex = wtr.ConstDouble(vDouble);
                op            = 0x14;
            }

            else
            {
                if (inst.Class != null)
                {
                    if (inst.Data is JavaFieldRef vField)
                    {
                        constantIndex = wtr.ConstField(inst.Class, vField);
                    }

                    else if (inst.Data is JavaMethodRef vMethod)
                    {
                        constantIndex = wtr.ConstMethod(inst.Class, vMethod);
                    }

                    else if (inst.Data == null)
                    {
                        constantIndex = wtr.ConstClass(inst.Class);
                    }
                }

                else if (inst.Data is int vInteger)
                {
                    if (FillInstruction_ConstLoad_Integer(inst, vInteger))
                    {
                        return;
                    }
                    constantIndex = wtr.ConstInteger(vInteger);
                }

                else if (inst.Data is float vFloat)
                {
                    if (FillInstruction_ConstLoad_Float(inst, vFloat))
                    {
                        return;
                    }
                    constantIndex = wtr.ConstFloat(vFloat);
                }

                else if (inst.Data is string vString)
                {
                    constantIndex = wtr.ConstString(vString);
                }

                op          = (byte)((constantIndex <= 255) ? 0x12 : 0x13);
                inst.Opcode = op;
            }

            if (constantIndex == -1)
            {
                throw wtr.Where.Exception("invalid constant in ldc/ldc_w/ldc2_w instruction");
            }

            if (op == 0x12)
            {
                inst.Bytes    = new byte[2];
                inst.Bytes[1] = (byte)constantIndex;
            }
            else
            {
                inst.Bytes    = new byte[3];
                inst.Bytes[1] = (byte)(constantIndex >> 8);
                inst.Bytes[2] = (byte)constantIndex;
            }
            inst.Bytes[0] = op;
        }
Ejemplo n.º 5
0
        bool FillInstruction_Const(JavaWriter wtr, Instruction inst, byte op)
        {
            int length = 3;
            int count  = 0;

            if (op >= 0x12 && op <= 0x14)
            {
                FillInstruction_ConstLoad(wtr, inst);
                return(true);
            }

            int constantIndex = -1;

            if (op >= 0xB2 && op <= 0xB5)
            {
                // getstatic/putstatic/getfield/putfield
                if (inst.Data is JavaFieldRef vField)
                {
                    constantIndex = wtr.ConstField(inst.Class, vField);
                }
            }

            else if (op >= 0xB6 && op <= 0xB8)
            {
                // invokevirtual/invokespecial/invokestatic
                if (inst.Data is JavaMethodRef vMethod)
                {
                    constantIndex = wtr.ConstMethod(inst.Class, vMethod);
                }
            }

            else if (op == 0xB9)
            {
                // invokeinterface
                if (inst.Data is JavaMethodRef vMethod)
                {
                    constantIndex = wtr.ConstInterfaceMethod(inst.Class, vMethod);
                    length        = 5;
                    count         = 1; // 'this' argument
                    int numArgs = vMethod.Parameters.Count;
                    for (int i = 0; i < numArgs; i++)
                    {
                        count += vMethod.Parameters[i].Type.Category;
                    }
                }
            }

            else if (op == 0xBA)
            {
                // invokedynamic
                if (inst.Data is JavaCallSite vCallSite)
                {
                    constantIndex = wtr.ConstInvokeDynamic(vCallSite);
                    length        = 5;
                }
            }

            else if (op >= 0xBB)
            {
                // new/anewarray/checkcast/instanceof/multianewarray
                constantIndex = wtr.ConstClass(inst.Class);

                if (op == 0xC5)
                {
                    length++;
                    count = (int)inst.Data;
                }
            }

            if (constantIndex == -1)
            {
                return(false);
            }

            inst.Bytes    = new byte[length];
            inst.Bytes[0] = op;
            inst.Bytes[1] = (byte)(constantIndex >> 8);
            inst.Bytes[2] = (byte)constantIndex;

            if (op == 0xB9 || op == 0xC5)
            {
                inst.Bytes[3] = (byte)count;
            }

            return(true);
        }
Ejemplo n.º 6
0
        public void Write(JavaWriter wtr)
        {
            wtr.Where.Push($"writing class '{Name}'");

            if (Name == null)
            {
                throw wtr.Where.Exception("missing class name");
            }

            wtr.Write16((ushort)Flags);
            wtr.Write16(wtr.ConstClass(Name));
            wtr.Write16(wtr.ConstClass(Super));

            if (Interfaces == null)
            {
                wtr.Write16(0);
            }
            else
            {
                wtr.Write16(Interfaces.Count);
                for (int i = 0; i < Interfaces.Count; i++)
                {
                    wtr.Write16(wtr.ConstClass(Interfaces[i]));
                }
            }

            if (Fields == null)
            {
                wtr.Write16(0);
            }
            else
            {
                wtr.Write16(Fields.Count);
                for (int i = 0; i < Fields.Count; i++)
                {
                    Fields[i].Write(wtr);
                }
            }

            if (Methods == null)
            {
                wtr.Write16(0);
            }
            else
            {
                wtr.Write16(Methods.Count);
                for (int i = 0; i < Methods.Count; i++)
                {
                    Methods[i].Write(wtr);
                }
            }

            var attributes = new JavaAttributeSet();

            if (SourceFile != null)
            {
                attributes.Put(new JavaAttribute.SourceFile(SourceFile));
            }

            if (Signature != null)
            {
                attributes.Put(new JavaAttribute.Signature(Signature));
            }

            WriteInnerClasses(attributes);

            if (wtr.bootstrapMethods != null)
            {
                attributes.Put(wtr.bootstrapMethods);
            }

            attributes.Write(wtr);

            wtr.Where.Pop();
        }
Ejemplo n.º 7
0
            public override void Write(JavaWriter wtr)
            {
                wtr.Write16(wtr.ConstUtf8(tag));
                wtr.Fork();
                wtr.Write16(methods.Length);
                for (int i = 0; i < methods.Length; i++)
                {
                    wtr.Write16(wtr.ConstMethodHandle(methods[i].mh));
                    var args = methods[i].args;
                    wtr.Write16(args.Length);
                    for (int j = 0; j < args.Length; j++)
                    {
                        ushort constantIndex;
                        var    value = args[j];

                        if (value is int intValue)
                        {
                            constantIndex = wtr.ConstInteger(intValue);
                        }

                        else if (value is float floatValue)
                        {
                            constantIndex = wtr.ConstFloat(floatValue);
                        }

                        else if (value is long longValue)
                        {
                            constantIndex = wtr.ConstLong(longValue);
                        }

                        else if (value is double doubleValue)
                        {
                            constantIndex = wtr.ConstDouble(doubleValue);
                        }

                        else if (value is JavaType classValue)
                        {
                            constantIndex = wtr.ConstClass(classValue);
                        }

                        else if (value is string stringValue)
                        {
                            constantIndex = wtr.ConstString(stringValue);
                        }

                        else if (value is JavaMethodHandle methodHandleValue)
                        {
                            constantIndex = wtr.ConstMethodHandle(methodHandleValue);
                        }

                        else if (value is JavaMethodType methodTypeValue)
                        {
                            constantIndex = wtr.ConstMethodType(methodTypeValue);
                        }
                        else
                        {
                            throw wtr.Where.Exception($"invalid constant value");
                        }

                        wtr.Write16(constantIndex);
                    }
                }
                wtr.Join();
            }
Ejemplo n.º 8
0
 public override void Write(JavaWriter wtr)
 {
     Write(wtr, tag, sizeof(ushort) * 2);
     wtr.Write16(wtr.ConstClass(className));
     wtr.Write16(methodConst);
 }