/// <summary> /// Implement the class now that all classes have been created /// </summary> protected override void CreateMembers(DexTargetPackage targetPackage) { base.CreateMembers(targetPackage); // Build default ctor XTypeSystem typeSystem = Compiler.Module.TypeSystem; XSyntheticMethodDefinition ctor = XSyntheticMethodDefinition.Create(XType, XSyntheticMethodFlags.Constructor, "<init>", null, typeSystem.Void); ctor.Body = CreateCtorBody(); Class.Methods.Add(ctor.GetDexMethod(Class, targetPackage)); // Build Invoke method. XMethodDefinition sourceMethod = XType.Methods.Single(x => x.EqualsName("Invoke")); Prototype prototype = PrototypeBuilder.BuildPrototype(Compiler, targetPackage, Class, sourceMethod); MethodDefinition method = new MethodDefinition(Class, sourceMethod.Name, prototype) { AccessFlags = AccessFlags.Public | AccessFlags.Abstract, MapFileId = Compiler.GetNextMapFileId() }; Class.Methods.Add(method); // Find xSource method targetPackage.NameConverter.Record(sourceMethod, method); // If void() delegate, implement java.lang.Runnable if (sourceMethod.ReturnType.IsVoid() && (sourceMethod.Parameters.Count == 0)) { // Implement interface Class.Interfaces.Add(FrameworkReferences.Runnable); // Build run method var run = new MethodDefinition(Class, "run", new Prototype(PrimitiveType.Void)) { AccessFlags = AccessFlags.Public | AccessFlags.Final }; Class.Methods.Add(run); run.Body = new DexLib.Instructions.MethodBody(run, 1) { IncomingArguments = 1, OutgoingArguments = 1 }; var insList = run.Body.Instructions; var rThis = run.Body.Registers[0]; insList.Add(new DexLib.Instructions.Instruction(OpCodes.Invoke_virtual, method, rThis)); insList.Add(new DexLib.Instructions.Instruction(OpCodes.Return_void)); } }
/// <summary> /// Implement the class now that all classes have been created /// </summary> protected override void CreateMembers(DexTargetPackage targetPackage) { // Build value ctor var module = Compiler.Module; var isWide = Type.GetEnumUnderlyingType().Resolve().IsWide(); var enumType = Compiler.GetDot42InternalType("Enum"); var valueType = isWide ? module.TypeSystem.Long : module.TypeSystem.Int; // Build default ctor defaultCtor = XSyntheticMethodDefinition.Create(XType, XSyntheticMethodFlags.Constructor, "<init>", module.TypeSystem.Void); Class.Methods.Add(defaultCtor.GetDexMethod(Class, targetPackage)); // Build Create method create = XSyntheticMethodDefinition.Create(XType, XSyntheticMethodFlags.Protected, "Create", enumType, XParameter.Create("value", valueType)); Class.Methods.Add(create.GetDexMethod(Class, targetPackage)); }
/// <summary> /// Implement the class now that all classes have been created /// </summary> protected override void CreateMembers(DexTargetPackage targetPackage) { // Build value ctor var module = Compiler.Module; var isWide = Type.GetEnumUnderlyingType().Resolve().IsWide(); var enumType = Compiler.GetDot42InternalType("Enum"); var valueType = isWide ? module.TypeSystem.Long : module.TypeSystem.Int; // Build default ctor defaultCtor = XSyntheticMethodDefinition.Create(XType, XSyntheticMethodFlags.Constructor, "<init>", null, module.TypeSystem.Void); Class.Methods.Add(defaultCtor.GetDexMethod(Class, targetPackage)); // Build Create method create = XSyntheticMethodDefinition.Create(XType, XSyntheticMethodFlags.Protected, "Create", null, enumType, XParameter.Create("value", valueType)); Class.Methods.Add(create.GetDexMethod(Class, targetPackage)); }
/// <summary> /// Implement the class now that all classes have been created /// </summary> protected override void CreateMembers(DexTargetPackage targetPackage) { // Build value__ field var module = Compiler.Module; var underlyingEnumType = Type.GetEnumUnderlyingType(); var isWide = underlyingEnumType.IsWide(); var xValueType = isWide ? module.TypeSystem.Long : module.TypeSystem.Int; var valueField = XSyntheticFieldDefinition.Create(XType, XSyntheticFieldFlags.Protected, NameConstants.Enum.ValueFieldName, xValueType); Class.Fields.Add(valueField.GetDexField(Class, targetPackage)); // Create normal members base.CreateMembers(targetPackage); // Build value ctor ctor = XSyntheticMethodDefinition.Create(XType, XSyntheticMethodFlags.Constructor | XSyntheticMethodFlags.Protected, "<init>", module.TypeSystem.Void, XParameter.Create("name", module.TypeSystem.String), XParameter.Create("ordinal", module.TypeSystem.Int), XParameter.Create("value", xValueType)); ctor.Body = CreateCtorBody(ctor); Class.Methods.Add(ctor.GetDexMethod(Class, targetPackage)); // Build enumInfo field var internalEnumInfoType = Compiler.GetDot42InternalType("EnumInfo"); var enumInfoField = XSyntheticFieldDefinition.Create(XType, XSyntheticFieldFlags.Static, NameConstants.Enum.InfoFieldName, internalEnumInfoType /* enumInfoClassBuilder.Class*/); Class.Fields.Add(enumInfoField.GetDexField(Class, targetPackage)); // Build default__ field var defaultField = XSyntheticFieldDefinition.Create(XType, XSyntheticFieldFlags.Static, NameConstants.Enum.DefaultFieldName, XType); Class.Fields.Add(defaultField.GetDexField(Class, targetPackage)); // Build class ctor var classCtor = XSyntheticMethodDefinition.Create(XType, XSyntheticMethodFlags.Static | XSyntheticMethodFlags.Constructor | XSyntheticMethodFlags.Private, "<clinit>", module.TypeSystem.Void); classCtor.Body = CreateClassCtorBody(isWide, enumInfoField, defaultField, enumInfoClassBuilder.DefaultCtor, xValueType, module.TypeSystem); Class.Methods.Add(classCtor.GetDexMethod(Class, targetPackage)); if (!isWide) { // Build IntValue method var intValue = XSyntheticMethodDefinition.Create(XType, XSyntheticMethodFlags.Virtual, "IntValue", module.TypeSystem.Int); intValue.Body = CreateIntOrLongValueBody(); Class.Methods.Add(intValue.GetDexMethod(Class, targetPackage)); } else { // Build LongValue method var longValue = XSyntheticMethodDefinition.Create(XType, XSyntheticMethodFlags.Virtual, "LongValue", module.TypeSystem.Long); longValue.Body = CreateIntOrLongValueBody(); Class.Methods.Add(longValue.GetDexMethod(Class, targetPackage)); } // Build values() method var valuesMethod = XSyntheticMethodDefinition.Create(XType, XSyntheticMethodFlags.Static, NameConstants.Enum.ValuesMethodName, new XArrayType(XType)); valuesMethod.Body = CreateValuesBody(); Class.Methods.Add(valuesMethod.GetDexMethod(Class, targetPackage)); // Build valueOf(string) method var valueOfMethod = XSyntheticMethodDefinition.Create(XType, XSyntheticMethodFlags.Static, NameConstants.Enum.ValueOfMethodName, XType, XParameter.Create("name", module.TypeSystem.String)); valueOfMethod.Body = CreateValueOfBody(valueOfMethod, module.TypeSystem); Class.Methods.Add(valueOfMethod.GetDexMethod(Class, targetPackage)); // Build Unbox(object) method unboxMethod = XSyntheticMethodDefinition.Create(XType, XSyntheticMethodFlags.Static, NameConstants.Enum.UnboxMethodName, XType, XParameter.Create("value", Compiler.Module.TypeSystem.Object)); unboxMethod.Body = CreateUnboxBody(unboxMethod, isWide, Compiler); Class.Methods.Add(unboxMethod.GetDexMethod(Class, targetPackage)); }
/// <summary> /// Implement the class now that all classes have been created /// </summary> protected override void CreateMembers(DexTargetPackage targetPackage) { // Build value__ field var module = Compiler.Module; var underlyingEnumType = Type.GetEnumUnderlyingType(); var isWide = underlyingEnumType.IsWide(); var xValueType = isWide ? module.TypeSystem.Long : module.TypeSystem.Int; var valueField = XSyntheticFieldDefinition.Create(XType, XSyntheticFieldFlags.Protected, NameConstants.Enum.ValueFieldName, xValueType); Class.Fields.Add(valueField.GetDexField(Class, targetPackage)); // Create normal members base.CreateMembers(targetPackage); // Build value ctor ctor = XSyntheticMethodDefinition.Create(XType, XSyntheticMethodFlags.Constructor | XSyntheticMethodFlags.Protected, "<init>", module.TypeSystem.Void, XParameter.Create("name", module.TypeSystem.String), XParameter.Create("ordinal", module.TypeSystem.Int), XParameter.Create("value", xValueType)); ctor.Body = CreateCtorBody(ctor); Class.Methods.Add(ctor.GetDexMethod(Class, targetPackage)); // Build enumInfo field var internalEnumInfoType = Compiler.GetDot42InternalType("EnumInfo"); var enumInfoField = XSyntheticFieldDefinition.Create(XType, XSyntheticFieldFlags.Static, NameConstants.Enum.InfoFieldName, internalEnumInfoType/* enumInfoClassBuilder.Class*/); Class.Fields.Add(enumInfoField.GetDexField(Class, targetPackage)); // Build default__ field var defaultField = XSyntheticFieldDefinition.Create(XType, XSyntheticFieldFlags.Static, NameConstants.Enum.DefaultFieldName, XType); Class.Fields.Add(defaultField.GetDexField(Class, targetPackage)); // Build class ctor var classCtor = XSyntheticMethodDefinition.Create(XType, XSyntheticMethodFlags.Static | XSyntheticMethodFlags.Constructor | XSyntheticMethodFlags.Private, "<clinit>", module.TypeSystem.Void); classCtor.Body = CreateClassCtorBody(isWide, enumInfoField, defaultField, enumInfoClassBuilder.DefaultCtor, xValueType, module.TypeSystem); Class.Methods.Add(classCtor.GetDexMethod(Class, targetPackage)); if (!isWide) { // Build IntValue method var intValue = XSyntheticMethodDefinition.Create(XType, XSyntheticMethodFlags.Virtual, "IntValue", module.TypeSystem.Int); intValue.Body = CreateIntOrLongValueBody(); Class.Methods.Add(intValue.GetDexMethod(Class, targetPackage)); } else { // Build LongValue method var longValue = XSyntheticMethodDefinition.Create(XType, XSyntheticMethodFlags.Virtual, "LongValue", module.TypeSystem.Long); longValue.Body = CreateIntOrLongValueBody(); Class.Methods.Add(longValue.GetDexMethod(Class, targetPackage)); } // Build values() method var valuesMethod = XSyntheticMethodDefinition.Create(XType, XSyntheticMethodFlags.Static, NameConstants.Enum.ValuesMethodName, new XArrayType(XType)); valuesMethod.Body = CreateValuesBody(); Class.Methods.Add(valuesMethod.GetDexMethod(Class, targetPackage)); // Build valueOf(string) method var valueOfMethod = XSyntheticMethodDefinition.Create(XType, XSyntheticMethodFlags.Static, NameConstants.Enum.ValueOfMethodName, XType, XParameter.Create("name", module.TypeSystem.String)); valueOfMethod.Body = CreateValueOfBody(valueOfMethod, module.TypeSystem); Class.Methods.Add(valueOfMethod.GetDexMethod(Class, targetPackage)); // Build Unbox(object) method unboxMethod = XSyntheticMethodDefinition.Create(XType, XSyntheticMethodFlags.Static, NameConstants.Enum.UnboxMethodName, XType, XParameter.Create("value", Compiler.Module.TypeSystem.Object)); unboxMethod.Body = CreateUnboxBody(unboxMethod, isWide, Compiler); Class.Methods.Add(unboxMethod.GetDexMethod(Class, targetPackage)); }