Exemplo n.º 1
0
        /*
         * JavaFieldReader
         */



        public JavaField(JavaReader rdr)
        {
            Class = rdr.Class;

            Flags = (JavaAccessFlags)rdr.Read16();

            Name = rdr.ConstUtf8(rdr.Read16());

            rdr.Where.Push($"field '{Name}'");

            Type = new JavaType(rdr.ConstUtf8(rdr.Read16()), rdr.Where);

            var attributes = new JavaAttributeSet(rdr);

            if ((Flags & JavaAccessFlags.ACC_STATIC) != 0)
            {
                var constAttr = attributes.GetAttr <JavaAttribute.ConstantValue>();
                if (constAttr != null)
                {
                    InitConstant(constAttr.value, rdr.Where);
                }
            }

            rdr.Where.Pop();
        }
Exemplo n.º 2
0
 void WriteInnerClasses(JavaAttributeSet attributes)
 {
     if (OuterAndInnerClasses != null)
     {
         int n = 0;
         for (int i = 0; i < OuterAndInnerClasses.Length; i++)
         {
             if (OuterAndInnerClasses[i] != null)
             {
                 n++;
             }
         }
         if (n != 0)
         {
             var innerClasses = new JavaClass.InnerClass[n];
             n = 0;
             for (int i = 0; i < OuterAndInnerClasses.Length; i++)
             {
                 if (OuterAndInnerClasses[i] != null)
                 {
                     innerClasses[n++] = OuterAndInnerClasses[i];
                 }
             }
             attributes.Put(new JavaAttribute.InnerClasses(innerClasses));
         }
     }
 }
Exemplo n.º 3
0
        /*
         * JavaFieldWriter
         */



        public void Write(JavaWriter wtr)
        {
            wtr.Where.Push($"field '{Name}'");

            wtr.Write16((ushort)Flags);
            wtr.Write16(wtr.ConstUtf8(Name));
            wtr.Write16(wtr.ConstUtf8(Type.ToDescriptor()));

            if (Constant != null)
            {
                var attributes = new JavaAttributeSet();
                attributes.Put(new JavaAttribute.ConstantValue(Constant));
                attributes.Write(wtr);
            }
            else
            {
                wtr.Write16(0); // attributes
            }
            wtr.Where.Pop();
        }
Exemplo n.º 4
0
        public JavaMethod(JavaReader rdr, bool withCode = true)
        {
            Class = rdr.Class;

            Flags = (JavaAccessFlags)rdr.Read16();

            Name = rdr.ConstUtf8(rdr.Read16());

            rdr.Where.Push($"method '{Name}'");

            var tmpMethod = new JavaMethodRef(rdr.ConstUtf8(rdr.Read16()), rdr.Where);

            ReturnType = tmpMethod.ReturnType;
            Parameters = tmpMethod.Parameters;

            var attributes = new JavaAttributeSet(rdr, withCode);

            Exceptions = attributes.GetAttr <JavaAttribute.Exceptions>()?.classes;

            var codeAttr = attributes.GetAttr <JavaAttribute.Code>();

            if (withCode && codeAttr != null)
            {
                #if !DEBUG
                try
                {
                #endif
                Code = new JavaCode(rdr, this, codeAttr);
                #if !DEBUG
            }
            catch (IndexOutOfRangeException)
            {
                throw rdr.Where.Exception("unexpected end of code");
            }
                #endif
            }

            InitParameterNames(attributes, codeAttr);

            rdr.Where.Pop();
        }
Exemplo n.º 5
0
        public void Write(JavaWriter wtr)
        {
            wtr.Where.Push($"method '{Name}'");

            wtr.Write16((ushort)Flags);
            wtr.Write16(wtr.ConstUtf8(Name));
            wtr.Write16(wtr.ConstUtf8(ToDescriptor()));

            var attributes = new JavaAttributeSet();

            if (Code != null)
            {
                var codeAttr = new JavaAttribute.Code();
                Code.Write(wtr, codeAttr);
                attributes.Put(codeAttr);
            }

            attributes.Write(wtr);

            wtr.Where.Pop();
        }
Exemplo n.º 6
0
            public Code(JavaReader rdr)
            {
                rdr.Where.Push(tag);
                (maxStack, maxLocals) = (rdr.Read16(), rdr.Read16());
                var codeLength = ReadLength(rdr, "code");

                code = rdr.ReadBlock(codeLength);

                exceptions = new Exception[rdr.Read16()];
                for (int i = 0; i < exceptions.Length; i++)
                {
                    exceptions[i].start    = rdr.Read16();
                    exceptions[i].endPlus1 = rdr.Read16();
                    exceptions[i].handler  = rdr.Read16();
                    ushort catchType = rdr.Read16();
                    exceptions[i].catchType =
                        (catchType == 0 ? null : rdr.ConstClass(catchType).ClassName);
                }

                attributes = new JavaAttributeSet(rdr);
                rdr.Where.Pop();
            }
Exemplo n.º 7
0
        void ReadCallSites(JavaReader rdr, JavaAttributeSet attributes)
        {
            if (rdr.callSites != null)
            {
                var attr = attributes.GetAttr <JavaAttribute.BootstrapMethods>();
                if (attr == null)
                {
                    throw rdr.Where.Exception($"missing bootstrap methods attribute");
                }

                var methods = attr.methods;
                foreach (var callSite in rdr.callSites)
                {
                    int i = callSite.BootstrapMethodIndex;
                    if (i >= methods.Length)
                    {
                        throw rdr.Where.Exception($"invalid bootstrap method index");
                    }

                    callSite.BootstrapMethod = methods[i].mh;
                    callSite.BootstrapArgs   = methods[i].args;
                }
            }
        }
Exemplo n.º 8
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();
        }
Exemplo n.º 9
0
 public Code()
 {
     attributes = new JavaAttributeSet();
 }
Exemplo n.º 10
0
        public JavaClass(JavaReader rdr, int majorVersion, int minorVersion, bool withCode = true)
        {
            rdr.Class = this;

            Flags = (JavaAccessFlags)rdr.Read16();

            Name = rdr.ConstClass(rdr.Read16()).ClassName;

            rdr.Where.Push($"reading class '{Name}' version {majorVersion}.{minorVersion}");

            ushort superConstIndex = rdr.Read16();

            if (!(superConstIndex == 0 && Name == "java.lang.Object"))
            {
                Super = rdr.ConstClass(superConstIndex).ClassName;
            }

            var interfaceCount = rdr.Read16();

            if (interfaceCount != 0)
            {
                Interfaces = new List <string>(interfaceCount);
                for (int i = 0; i < interfaceCount; i++)
                {
                    Interfaces.Add(rdr.ConstClass(rdr.Read16()).ClassName);
                }
            }

            var fieldCount = rdr.Read16();

            if (fieldCount != 0)
            {
                Fields = new List <JavaField>(fieldCount);
                for (int i = 0; i < fieldCount; i++)
                {
                    Fields.Add(new JavaField(rdr));
                }
            }

            var methodCount = rdr.Read16();

            if (methodCount != 0)
            {
                Methods = new List <JavaMethod>(methodCount);
                for (int i = 0; i < methodCount; i++)
                {
                    Methods.Add(new JavaMethod(rdr, withCode));
                }
            }

            var attributes = new JavaAttributeSet(rdr, withCode);

            var sourceAttr = attributes.GetAttr <JavaAttribute.SourceFile>();

            SourceFile = sourceAttr?.fileName;

            var signatureAttr = attributes.GetAttr <JavaAttribute.Signature>();

            Signature = signatureAttr?.descriptor;

            var innerAttr = attributes.GetAttr <JavaAttribute.InnerClasses>();

            if (innerAttr != null)
            {
                var enclosingClass =
                    attributes.GetAttr <JavaAttribute.EnclosingMethod>()
                    ?.className;
                ParseInnerClasses(innerAttr.classes, enclosingClass);
            }

            ReadCallSites(rdr, attributes);

            if ((Flags & JavaAccessFlags.ACC_ENUM) != 0)
            {
                bool superOk = (Super == "java.lang.Enum");
                if (IsInnerClass() && Super == OuterAndInnerClasses[0].OuterLongName)
                {
                    superOk = true;
                }
                if (!superOk)
                {
                    throw rdr.Where.Exception($"bad super class '{Super}' for enum");
                }
            }

            rdr.Where.Pop();
        }
Exemplo n.º 11
0
        void InitParameterNames(JavaAttributeSet attributes, JavaAttribute.Code codeAttr)
        {
            int numArgs = Parameters.Count;

            var parmsAttr = attributes.GetAttr <JavaAttribute.MethodParameters>();

            if (parmsAttr != null)
            {
                //
                // MethodParameters attributes lists parameter names one by one
                //

                int numParms = parmsAttr.parms.Length;
                if (numParms > numArgs)
                {
                    numParms = numArgs;
                }

                for (int i = 0; i < numParms; i++)
                {
                    Parameters[i].Name = parmsAttr.parms[i].name;
                }
            }

            else if (codeAttr != null)
            {
                var localsAttrs =
                    codeAttr.attributes.GetAttrs <JavaAttribute.LocalVariableTable>();

                if (localsAttrs != null)
                {
                    //
                    // LocalVariableTables specify local variables by index,
                    // so we also need to keep track of the index of each parameter
                    //

                    int index = 0;
                    if ((Flags & JavaAccessFlags.ACC_STATIC) == 0)
                    {
                        index++;
                    }

                    for (int i = 0; i < numArgs; i++)
                    {
                        foreach (var localsAttr in localsAttrs)
                        {
                            for (int j = 0; j < localsAttr.vars.Length; j++)
                            {
                                if (localsAttr.vars[j].index == index)
                                {
                                    Parameters[i].Name = localsAttr.vars[j].nameAndType.Name;
                                }
                            }
                        }

                        index += Parameters[i].Type.Category;
                    }
                }
            }

            int i0 = ((Flags & JavaAccessFlags.ACC_STATIC) != 0) ? 0 : 1;

            for (int i = 0; i < numArgs; i++)
            {
                if (Parameters[i].Name == null)
                {
                    Parameters[i].Name = "$arg" + (i + i0);
                }
            }
        }