Example #1
0
        /// <summary>
        /// Gets a class reference for the given type reference.
        /// </summary>
        internal static ClassReference GetClassReference(this Mono.Cecil.TypeReference type, DexTargetPackage targetPackage, XModule module)
        {
            var classRef = type.GetReference(targetPackage, module) as ClassReference;

            if (classRef == null)
            {
                throw new ArgumentException(string.Format("type {0} is not a class reference", type.FullName));
            }
            return(classRef);
        }
Example #2
0
        /// <summary>
        /// Gets a class reference for the given type reference.
        /// </summary>
        internal static TypeReference GetReference(this Mono.Cecil.TypeReference type, DexTargetPackage targetPackage, XModule module)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

            var xType = XBuilder.AsTypeReference(module, type);

            return(xType.GetReference(targetPackage));
        }
Example #3
0
        /// <summary>
        /// Create an annotation with default values.
        /// </summary>
        private static Annotation CreateDefaultAnnotation(AttributeAnnotationInterface mapping)
        {
            // Create annotation
            Annotation annotation = new Annotation {
                Visibility = AnnotationVisibility.Runtime
            };

            annotation.Type = mapping.AnnotationInterfaceClass;

            // Add field default values
            foreach (KeyValuePair <Mono.Cecil.FieldDefinition, MethodDefinition> entry in mapping.FieldToGetMethodMap)
            {
                string name = entry.Value.Name;
                Mono.Cecil.TypeReference type = entry.Key.FieldType;
                annotation.Arguments.Add(new AnnotationArgument(name, GetDefaultValue(type)));
            }

            // Add property default values
            foreach (KeyValuePair <PropertyDefinition, MethodDefinition> entry in mapping.PropertyToGetMethodMap)
            {
                string name = entry.Value.Name;
                Mono.Cecil.TypeReference type = entry.Key.PropertyType;
                annotation.Arguments.Add(new AnnotationArgument(name, GetDefaultValue(type)));
            }

            // Add ctor argument default values
            foreach (KeyValuePair <Mono.Cecil.MethodDefinition, AttributeCtorMapping> entry in mapping.CtorMap)
            {
                Mono.Cecil.MethodDefinition ctor = entry.Key;
                for (int i = 0; i < ctor.Parameters.Count; i++)
                {
                    string name = entry.Value.ArgumentGetters[i].Name;
                    Mono.Cecil.TypeReference type = entry.Key.Parameters[i].ParameterType;
                    annotation.Arguments.Add(new AnnotationArgument(name, GetDefaultValue(type)));
                }
            }

            // Wrap it in a dalvik.annotation.AnnotationDefault
            Annotation defAnnotation = new Annotation {
                Visibility = AnnotationVisibility.System
            };

            defAnnotation.Type = new ClassReference("dalvik.annotation.AnnotationDefault");
            defAnnotation.Arguments.Add(new AnnotationArgument("value", annotation));
            return(defAnnotation);
        }
Example #4
0
 /// <summary>
 /// Gets the default value for any value of the given type.
 /// </summary>
 private static object GetDefaultValue(Mono.Cecil.TypeReference type)
 {
     if (type.IsByte())
     {
         return((byte)0);
     }
     if (type.IsSByte())
     {
         return((sbyte)0);
     }
     if (type.IsBoolean())
     {
         return(false);
     }
     if (type.IsChar())
     {
         return('\0');
     }
     if (type.IsInt16())
     {
         return((short)0);
     }
     if (type.IsInt32())
     {
         return(0);
     }
     if (type.IsInt64())
     {
         return(0L);
     }
     if (type.IsFloat())
     {
         return(0.0F);
     }
     if (type.IsDouble())
     {
         return(0.0);
     }
     return(null);
 }