/// <summary> /// Create the current type as class definition. /// </summary> protected virtual void CreateClassDefinition(DexTargetPackage targetPackage, ClassDefinition parent, TypeDefinition parentType, XTypeDefinition parentXType) { // Create classdef var nsConverter = targetPackage.NameConverter; classDef = new ClassDefinition(); classDef.MapFileId = compiler.GetNextMapFileId(); classDef.Namespace = nsConverter.GetConvertedNamespace(XType); var name = CreateClassName(XType); if ((parentType != null) && parentType.HasDexImportAttribute()) { var fullName = nsConverter.GetConvertedFullName(parentXType) + "_" + name; var index = fullName.LastIndexOf('.'); classDef.Name = (index < 0) ? fullName : fullName.Substring(index + 1); } else { classDef.Name = (parent != null) ? parent.Name + "$" + name : name; } // Set access flags //if (typeDef.IsPublic) classDef.IsPublic = true; //else classDef.IsPrivate = true; classDef.IsPublic = true; if (typeDef.IsSealed) { classDef.IsFinal = true; } if (typeDef.IsInterface) { classDef.IsInterface = true; classDef.IsAbstract = true; } else if (typeDef.IsAbstract && !classDef.IsFinal) // apparently Android 6.x verifier does not allow 'final abstact' classes. { classDef.IsAbstract = true; } if ((parent != null) && (!parentType.HasDexImportAttribute())) { // Add to parent if this is a nested type classDef.Owner = parent; parent.AddInnerClass(classDef); } else { // Add to dex if it is a root class // TODO: here we could simplify the names, e.g. remove the scope, as long as no // clashing does occur. targetPackage.DexFile.AddClass(classDef); } }
/// <summary> /// Create the current type as class definition. /// </summary> protected virtual void CreateClassDefinition(Dex target, NameConverter nsConverter, ClassDefinition parent, ClassFile parentClass) { // Create classdef classDef = new ClassDefinition(); classDef.MapFileId = compiler.GetNextMapFileId(); classDef.Namespace = nsConverter.GetConvertedNamespace(typeDef); var name = NameConverter.GetConvertedName(typeDef); classDef.Name = (parent != null) ? parent.Name + "$" + name : name; // Set access flags //if (typeDef.IsPublic) classDef.IsPublic = true; //else classDef.IsPrivate = true; classDef.IsPublic = true; if (typeDef.IsFinal) { classDef.IsFinal = true; } if (typeDef.IsInterface) { classDef.IsInterface = true; classDef.IsAbstract = true; } else if (typeDef.IsAbstract) { classDef.IsAbstract = true; } if (typeDef.Interfaces.Any(x => x.ClassName == "java/lang/annotation/Annotation")) { classDef.IsAnnotation = true; } classDef.IsEnum = typeDef.IsEnum; if (parent != null) { // Add to parent if this is a nested type classDef.Owner = parent; parent.AddInnerClass(classDef); } else { // Add to dex if it is a root class target.AddClass(classDef); } }
/// <summary> /// Create the current type as class definition. /// </summary> public void Create(ClassDefinition declaringClass, DexTargetPackage targetPackage) { // Find xMethod xMethod = XBuilder.AsMethodDefinition(compiler.Module, method); // Create method definition dmethod = new DexLib.MethodDefinition(); dmethod.Name = GetMethodName(method, targetPackage); dmethod.MapFileId = compiler.GetNextMapFileId(); AddMethodToDeclaringClass(declaringClass, dmethod, targetPackage); targetPackage.Record(xMethod, dmethod); // Set access flags SetAccessFlags(dmethod, method); // Create prototype dmethod.Prototype = PrototypeBuilder.BuildPrototype(compiler, targetPackage, declaringClass, xMethod); }
public DelegateInstanceType Create() { instanceField = null; // actually at the momennt, we are not called multiple times... genericMethodTypeFields.Clear(); genericInstanceTypeFields.Clear(); // Prepare called method var target = targetPackage.DexFile; var owner = target.GetClass(calledMethod.DeclaringType.GetClassReference(targetPackage).Fullname) ?? targetPackage.GetOrCreateGeneratedCodeClass(); var calledMethodPrototype = PrototypeBuilder.BuildPrototype(compiler, targetPackage, owner, calledMethod); var calledMethodRef = calledMethod.GetReference(targetPackage); if (calledMethod.DeclaringType.HasDexImportAttribute()) { // Delegate method is a Dex import method } else { // Delegate method is a .NET method var calledDexMethod = owner.Methods.Single(x => (x.Name == calledMethodRef.Name) && (x.Prototype.Equals(calledMethodRef.Prototype))); if (calledDexMethod.IsPrivate) { calledDexMethod.IsPrivate = false; calledDexMethod.IsProtected = true; } } var @class = new ClassDefinition { Name = CreateInstanceTypeName(owner), Namespace = owner.Namespace, AccessFlags = AccessFlags.Public | AccessFlags.Final, MapFileId = compiler.GetNextMapFileId(), }; owner.AddInnerClass(@class); // Set super class @class.SuperClass = delegateClass; // Implement delegate interface //@class.Interfaces.Add(delegateInterface); // Get type of instance XTypeDefinition instanceType = calledMethod.DeclaringType; TypeReference instanceTypeRef = instanceType.GetReference(targetPackage); // Add ctor var ctor = new MethodDefinition { Owner = @class, Name = "<init>", AccessFlags = AccessFlags.Public | AccessFlags.Constructor, Prototype = new Prototype(PrimitiveType.Void), }; ctor.Prototype.Unfreeze(); if (!calledMethod.IsStatic) { ctor.Prototype.Parameters.Add(new Parameter(instanceTypeRef, "this")); } PrototypeBuilder.AddGenericParameters(compiler, targetPackage, calledMethod, ctor.Prototype); ctor.Prototype.Freeze(); @class.Methods.Add(ctor); // Add methodInfo field methodInfoField = new FieldDefinition(); methodInfoField.Name = "methodInfo"; methodInfoField.Owner = @class; methodInfoField.Type = compiler.GetDot42InternalType("System.Reflection", "MethodInfo").GetReference(targetPackage); methodInfoField.AccessFlags = AccessFlags.Private | AccessFlags.Final | AccessFlags.Static; @class.Fields.Add(methodInfoField); // Add instance field & getTargetImpl method if (!calledMethod.IsStatic) { instanceField = new FieldDefinition(); instanceField.Name = "instance"; instanceField.Owner = @class; instanceField.Type = instanceTypeRef; instanceField.AccessFlags = AccessFlags.Private | AccessFlags.Final; @class.Fields.Add(instanceField); AddMethod(@class, "GetTargetImpl", new Prototype(FrameworkReferences.Object), AccessFlags.Protected, CreateGetTargetImplBody()); } // Add generic instance type and method fields var gtpa = compiler.GetDot42InternalType(InternalConstants.GenericTypeParameterAnnotation).GetClassReference(targetPackage); var gmpa = compiler.GetDot42InternalType(InternalConstants.GenericMethodParameterAnnotation).GetClassReference(targetPackage); foreach (var parameter in ctor.Prototype.Parameters) { bool isGtpa = parameter.Annotations.Any(a => a.Type.Equals(gtpa)); bool isGmpa = parameter.Annotations.Any(a => a.Type.Equals(gmpa)); if (isGmpa || isGtpa) { var list = isGtpa ? genericInstanceTypeFields : genericMethodTypeFields; var field = new FieldDefinition(); field.Name = isGtpa ? "$git" : "$gmt"; if (parameter.Type.Equals(FrameworkReferences.Class)) { field.Name += list.Count + 1; } field.Owner = @class; field.Type = parameter.Type; field.AccessFlags = AccessFlags.Private | AccessFlags.Final; @class.Fields.Add(field); list.Add(field); } } // Create ctor body var ctorBody = CreateCtorBody(); targetPackage.Record(new CompiledMethod() { DexMethod = ctor, RLBody = ctorBody }); // add class static ctor AddMethod(@class, "<clinit>", new Prototype(PrimitiveType.Void), AccessFlags.Public | AccessFlags.Constructor | AccessFlags.Static, CreateCctorBody()); // Add Invoke method AddMethod(@class, "Invoke", invokePrototype, AccessFlags.Public, CreateInvokeBody(calledMethodPrototype)); // Add Equals method var typeOnlyEqualsSuffices = calledMethod.IsStatic && !calledMethod.NeedsGenericInstanceTypeParameter && !calledMethod.NeedsGenericInstanceMethodParameter; var equalsBody = typeOnlyEqualsSuffices ? CreateEqualsCheckTypeOnlyBody(@class) : CreateEqualsBody(@class); var equalsPrototype = new Prototype(PrimitiveType.Boolean, new Parameter(multicastDelegateClass, "other")); AddMethod(@class, "EqualsWithoutInvocationList", equalsPrototype, AccessFlags.Protected, equalsBody); if (!typeOnlyEqualsSuffices) { var hashCodePrototype = new Prototype(PrimitiveType.Int); AddMethod(@class, "HashCodeWithoutInvocationList", hashCodePrototype, AccessFlags.Protected, CreateHashCodeBody(@class)); } var clonePrototype = new Prototype(multicastDelegateClass, new Parameter(new ArrayType(multicastDelegateClass), "invocationList"), new Parameter(PrimitiveType.Int, "invocationListLength")); AddMethod(@class, "CloneWithNewInvocationList", clonePrototype, AccessFlags.Protected, CreateCloneBody(ctor, @class)); AddMethod(@class, "GetMethodInfoImpl", new Prototype(methodInfoField.Type), AccessFlags.Protected, CreateGetMethodInfoImplBody()); return(new DelegateInstanceType(calledMethod, @class, ctor)); }