Exemple #1
0
        public string AddFieldAccessor(FieldAccessor fld)
        {
            int index = fieldAccessors.IndexOf(fld);

            if (index < 0)
            {
                index = fieldAccessors.Count;
                fieldAccessors.Add(fld);
            }

            return(ClassNames.FieldAccessorPrefix + index.ToString());
        }
Exemple #2
0
        private void GenerateFieldAccessors(InterType type)
        {
            for (int i = 0; i < type.FieldAccessors.Count; i++)
            {
                FieldAccessor fld          = type.FieldAccessors[i];
                string        fldTypeDescr = GetFieldDescriptor(fld.Field.FieldType);
                if ((fld.Field.IsStatic) && (fld.Field.IsThreadLocal))
                {
                    fldTypeDescr = "L" + TypeNameToJava(ClassNames.JavaLangThreadLocal.ClassName) + ";";
                }

                JavaPrimitiveType javaFldType = JavaHelpers.InterTypeToJavaPrimitive(fld.Field.FieldType);
                FieldRef          fldRef      = new FieldRef(TypeNameToJava(type.Fullname), FieldNameToJava(fld.Field.Name), fldTypeDescr);

                Method result = new Method();
                result.AccessFlags = MethodAccessFlags.Public | MethodAccessFlags.Static;
                result.Name        = ClassNames.FieldAccessorPrefix + i.ToString();

                JavaBytecodeWriter codeWriter = new JavaBytecodeWriter();
                if (!fld.Field.IsStatic)
                {
                    codeWriter.Add(OpCodes.aload_0);
                }

                if (fld.Type == FieldAccessorType.Getter)
                {
                    result.Descriptor = "(" + (fld.Field.IsStatic ? "" : GetFieldDescriptor(type)) + ")" + fldTypeDescr;
                    codeWriter
                    .Add(fld.Field.IsStatic ? OpCodes.getstatic : OpCodes.getfield, fldRef)
                    .AddReturn(javaFldType);
                }
                else
                {
                    result.Descriptor = "(" + (fld.Field.IsStatic ? "" : GetFieldDescriptor(type)) + fldTypeDescr + ")V";
                    codeWriter
                    .AddLoad(javaFldType, 1)
                    .Add(fld.Field.IsStatic ? OpCodes.putstatic : OpCodes.putfield, fldRef)
                    .Add(OpCodes._return);
                }

                result.Attributes.Add(codeWriter.End(currentJavaClass.ConstantPool));
                currentJavaClass.Methods.Add(result);
            }
        }