/// <summary> /// Set the access flags of the created field. /// </summary> protected virtual void SetAccessFlags(DexLib.FieldDefinition dfield, FieldDefinition field) { if (field.IsPrivate) { if (field.DeclaringType.HasNestedTypes) { dfield.IsProtected = true; } else { dfield.IsPrivate = true; } } else if (field.IsFamily) { dfield.IsProtected = true; } else { dfield.IsPublic = true; } if (field.IsInitOnly) { dfield.IsFinal = true; } if (field.IsStatic) { dfield.IsStatic = true; } }
/// <summary> /// Set the value of the given dex field. /// </summary> protected virtual void SetFieldValue(DexLib.FieldDefinition dfield, FieldDefinition field) { var constant = field.Constant; if (constant != null) { var fieldType = field.FieldType; if (fieldType.IsByte()) { constant = XConvert.ToByte(constant); } else if (fieldType.IsUInt16()) { constant = XConvert.ToShort(constant); } else if (fieldType.IsUInt32()) { constant = XConvert.ToInt(constant); } else if (fieldType.IsUInt64()) { constant = XConvert.ToLong(constant); } } dfield.Value = constant; }
/// <summary> /// Set the access flags of the created field. /// </summary> protected virtual void SetAccessFlags(DexLib.FieldDefinition dfield, FieldDefinition field) { // subclass accesses have already been fixed on an actual use basis. if (field.IsPrivate) { dfield.IsPrivate = true; } else if (field.IsFamily || field.IsFamilyOrAssembly) { dfield.IsProtected = true; } else { dfield.IsPublic = true; } if (field.IsInitOnly) { dfield.IsFinal = true; } if (field.IsStatic) { dfield.IsStatic = true; } if (field.IsCompilerGenerated()) { dfield.IsSynthetic = true; } dfield.IsVolatile = field.IsUsedInInterlocked || IsVolatile(field);; }
public override void Create(ClassDefinition declaringClass, XTypeDefinition declaringType, DexTargetPackage targetPackage) { if (_baseFieldBuilder.dfield == null) return; // can't create udater for static fields. if (field.IsStatic) return; var updaterType = GetAtomicFieldUpdaterType(field.FieldType); if (updaterType == null) return; var fullUpdateTypeName = "Java.Util.Concurrent.Atomic." + updaterType; // create matching xField. Note: this seems to be a hack. what to do? var objType = new ObjectTypeReference(fullUpdateTypeName, new TypeArgument[0]); var javaTypeReference = new XBuilder.JavaTypeReference(Compiler.Module, objType, objType.ClassName); var basexField = _baseFieldBuilder.xField; var basedField = _baseFieldBuilder.dfield; var fieldName = basedField.Name + NameConstants.Atomic.FieldUpdaterPostfix; var xflags = XSyntheticFieldFlags.Static | XSyntheticFieldFlags.ReadOnly; if (basedField.IsProtected) xflags |= XSyntheticFieldFlags.Protected; if (basedField.IsPrivate) xflags |= XSyntheticFieldFlags.Private; var xAtomicField = XSyntheticFieldDefinition.Create(basexField.DeclaringType, xflags, fieldName, javaTypeReference); xField = xAtomicField; // create dfield. dfield = new DexLib.FieldDefinition { Name = fieldName, IsStatic = true, IsFinal = true, IsSynthetic = true, // same access as the original field. IsPublic = basedField.IsPublic, IsPrivate = basedField.IsPrivate, IsProtected = basedField.IsProtected, }; AddFieldToDeclaringClass(declaringClass, dfield, targetPackage); targetPackage.NameConverter.Record(xField, dfield); }
/// <summary> /// Ensure that the name of the field is unique. /// </summary> private static void UpdateName(DexLib.FieldDefinition field, ClassDefinition declaringClass) { var baseName = field.Name; var name = field.Name; var postfix = 0; while (true) { if (declaringClass.Fields.All(x => x.Name != name)) { field.Name = name; return; } name = baseName + ++postfix; } }
public override void Create(ClassDefinition declaringClass, XTypeDefinition declaringType, DexTargetPackage targetPackage) { if (_baseFieldBuilder.dfield == null) { return; } // can't create udater for static fields. if (field.IsStatic) { return; } var updaterType = GetAtomicFieldUpdaterType(field.FieldType); if (updaterType == null) { return; } var fullUpdateTypeName = "Java.Util.Concurrent.Atomic." + updaterType; // create matching xField. Note: this seems to be a hack. what to do? var objType = new ObjectTypeReference(fullUpdateTypeName, new TypeArgument[0]); var javaTypeReference = new XBuilder.JavaTypeReference(Compiler.Module, objType, objType.ClassName); var basexField = _baseFieldBuilder.xField; var basedField = _baseFieldBuilder.dfield; var fieldName = basedField.Name + NameConstants.Atomic.FieldUpdaterPostfix; var xflags = XSyntheticFieldFlags.Static | XSyntheticFieldFlags.ReadOnly; if (basedField.IsProtected) { xflags |= XSyntheticFieldFlags.Protected; } if (basedField.IsPrivate) { xflags |= XSyntheticFieldFlags.Private; } var xAtomicField = XSyntheticFieldDefinition.Create(basexField.DeclaringType, xflags, fieldName, javaTypeReference); xField = xAtomicField; // create dfield. dfield = new DexLib.FieldDefinition { Name = fieldName, IsStatic = true, IsFinal = true, IsSynthetic = true, // same access as the original field. IsPublic = basedField.IsPublic, IsPrivate = basedField.IsPrivate, IsProtected = basedField.IsProtected, }; AddFieldToDeclaringClass(declaringClass, dfield, targetPackage); targetPackage.NameConverter.Record(xField, dfield); }
/// <summary> /// Set the value of the given dex field. /// </summary> protected override void SetFieldValue(DexLib.FieldDefinition dfield, FieldDefinition field) { // Do nothing }
/// <summary> /// Set the field type of the given dex field. /// </summary> protected override void SetFieldType(DexLib.FieldDefinition dfield, FieldDefinition field, DexTargetPackage targetPackage) { dfield.Type = dfield.Owner; }
/// <summary> /// Set the access flags of the created field. /// </summary> protected override void SetAccessFlags(DexLib.FieldDefinition dfield, FieldDefinition field) { base.SetAccessFlags(dfield, field); dfield.IsFinal = true; }
/// <summary> /// Add the given field to its declaring class. /// </summary> protected override void AddFieldToDeclaringClass(ClassDefinition declaringClass, DexLib.FieldDefinition dfield, DexTargetPackage targetPackage) { var generatedCodeClass = targetPackage.GetOrCreateGeneratedCodeClass(); UpdateName(dfield, generatedCodeClass); dfield.Owner = generatedCodeClass; generatedCodeClass.Fields.Add(dfield); }
/// <summary> /// Add the given method to its declaring class. /// </summary> protected virtual void AddFieldToDeclaringClass(ClassDefinition declaringClass, DexLib.FieldDefinition dmethod, DexTargetPackage targetPackage) { dmethod.Owner = declaringClass; declaringClass.Fields.Add(dmethod); }
/// <summary> /// Set the field type of the given dex field. /// </summary> protected virtual void SetFieldType(DexLib.FieldDefinition dfield, FieldDefinition field, DexTargetPackage targetPackage) { dfield.Type = field.FieldType.GetReference(targetPackage, compiler.Module); }