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
        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.º 3
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.º 4
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.º 5
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);
                }
            }
        }