Exemplo n.º 1
0
 public override void ModifyMethodName(NetMethodDefinition method, MethodRenamer renamer)
 {
     if(method.Name == "GetTitle")
         renamer.Rename(method, "JavaGetTitle");
     else
         base.ModifyMethodName(method, renamer);
 }
Exemplo n.º 2
0
 /// <summary>
 /// Modify the name of the given method to another name.
 /// By calling renamer.Rename, all methods in the same group are also updated.
 /// </summary>
 public override void ModifyMethodName(NetMethodDefinition method, MethodRenamer renamer)
 {
     base.ModifyMethodName(method, renamer);
     if (method.OriginalJavaName == "getMessage")
     {
         method.EditorBrowsableState = EditorBrowsableState.Never;
     }
 }
            /// <summary>
            /// Is the given method a property get method?
            /// </summary>
            protected override bool IsGetter(NetMethodDefinition method)
            {
                var name = method.OriginalJavaName;

                if (name == "getAndDecrement") return false;
                if (name == "getAndIncrement") return false;

                return base.IsGetter(method);
            }
Exemplo n.º 4
0
 public override void ModifyMethodName(NetMethodDefinition method, MethodRenamer renamer)
 {
     if (method.OriginalJavaName == "getLayoutParams")
         renamer.Rename(method, "GetLayoutParameters");
     else if (method.OriginalJavaName == "setLayoutParams")
         renamer.Rename(method, "SetLayoutParameters");
     else
         base.ModifyMethodName(method, renamer);
 }
Exemplo n.º 5
0
 /// <summary>
 /// Create a property name from the given getter.
 /// </summary>
 protected override string GetPropertyName(NetMethodDefinition getter)
 {
     switch (getter.Name)
     {
         case "GetPath":
             return "AbsolutePath";
         default:
             return base.GetPropertyName(getter);
     }
 }
Exemplo n.º 6
0
 /// <summary>
 /// Create a property name from the given getter.
 /// </summary>
 protected override string GetPropertyName(NetMethodDefinition getter)
 {
     switch (getter.Name)
     {
         case "GetCause":
             return "InnerException";
         case "GetStackTrace":
             return "JavaStackTrace";
         default:
             return base.GetPropertyName(getter);
     }
 }
        /// <summary>
        /// Is the given method a property get method?
        /// </summary>
        protected override bool IsSetter(NetMethodDefinition method)
        {
            var name = method.OriginalJavaName;
            if (_skipOriginalJavaProperties != null && _skipOriginalJavaProperties.Contains(name))
                return false;
            if (_isPropertyPredicate != null)
            {
                var ret = _isPropertyPredicate(method);
                if (ret != null) return ret.Value;
            }

            return base.IsSetter(method);
        }
Exemplo n.º 8
0
 /// <summary>
 /// Mark this method as an explicit interface implementation.
 /// </summary>
 public void SetExplicitImplementation(NetMethodDefinition iMethod, NetTypeReference iType)
 {
     if (iMethod == null)
     {
         throw new ArgumentNullException("iMethod");
     }
     if (iType == null)
     {
         throw new ArgumentNullException("iType");
     }
     InterfaceMethod = iMethod;
     InterfaceType   = iType;
 }
Exemplo n.º 9
0
 /// <summary>
 /// Rename the all methods in the method group of the given method to the given new name.
 /// </summary>
 public void Rename(NetMethodDefinition method, string newName)
 {
     if (method.Name == newName)
         return;
     var group = method.MethodGroup;
     if (group == null)
     {
         if (method.IsStatic)
         {
             method.SetName(newName);
             return;
         }
         if (method.IsConstructor)
         {
             throw new InvalidOperationException("Constructor cannot be renamed");
         }
         throw new InvalidOperationException("Method has no group");
     }
     foreach (var m in group)
     {
         m.SetName(newName);
     }
 }
Exemplo n.º 10
0
 /// <summary>
 /// Add the given mapping
 /// </summary>
 public void Add(MethodDefinition javaMethod, NetMethodDefinition netMethod)
 {
     map.Add(javaMethod, netMethod);
 }
Exemplo n.º 11
0
        /// <summary>
        /// Is the given method a property set method?
        /// </summary>
        protected virtual bool IsSetter(NetMethodDefinition method)
        {
            if (method.Parameters.Count != 1)
                return false;
            if (!method.ReturnType.IsVoid())
                return false;
            var name = method.Name;

            var prefix = GetNamePrefix(name);

            return prefix == "Set" || prefix == "set_";
        }
Exemplo n.º 12
0
        /// <summary>
        /// Is the given method a property get method?
        /// </summary>
        protected virtual bool IsGetter(NetMethodDefinition method)
        {
            if (method.Parameters.Count > 0)
                return false;
            if (method.ReturnType.IsVoid())
                return false;
            var name = method.Name;
            if (name == "GetHashCode")
                return false;

            var prefix = GetNamePrefix(name);
            return prefix == "Get" || prefix == "Has" || prefix == "Can" || prefix == "Is" || prefix == "get_";
        }
Exemplo n.º 13
0
 /// <summary>
 /// Mark this method as an explicit interface implementation.
 /// </summary>
 public void SetExplicitImplementation(NetMethodDefinition iMethod, NetTypeReference iType)
 {
     if (iMethod == null)
         throw new ArgumentNullException("iMethod");
     if (iType == null)
         throw new ArgumentNullException("iType");
     InterfaceMethod = iMethod;
     InterfaceType = iType;
 }
Exemplo n.º 14
0
 /// <summary>
 /// Are the two methods equal wrt name, parameters and type parameter count AND return type?
 /// </summary>
 internal static bool IsExactDuplicate(this NetMethodDefinition method1, NetMethodDefinition method2)
 {
     return method1.IsDuplicate(method2) && (method1.ReturnType.AreSame(method2.ReturnType));
 }
Exemplo n.º 15
0
 /// <summary>
 /// Is the given method a property get method?
 /// </summary>
 protected override bool IsGetter(NetMethodDefinition method)
 {
     return false;
 }
Exemplo n.º 16
0
            protected override bool IsGetter(NetMethodDefinition method)
            {
                if (method.Name == "GetMessage") return false;

                return base.IsGetter(method);
            }
Exemplo n.º 17
0
 /// <summary>
 /// Is the given method a property get method?
 /// </summary>
 protected override bool IsGetter(NetMethodDefinition method)
 {
     if (method.Parameters.Count == 0 && method.Name.StartsWith("Is"))
     {
         return true;
     }
     return false;
 }
Exemplo n.º 18
0
        /// <summary>
        /// Create the method in the given type
        /// </summary>
        public void Create(NetTypeDefinition declaringType, TargetFramework target)
        {
            try
            {
                // Do not add private methods
                if (javaMethod.IsPrivate)
                {
                    if (javaMethod.Name != "<init>")
                    {
                        return;
                    }
                }

                // Do not add useless bridges methods
                if (javaMethod.IsBridge)
                {
                    var targetMethod = javaMethod.DeclaringClass.Methods.FirstOrDefault(x => javaMethod.IsBridgeFor(x));
                    /*if (javaMethod.DeclaringClass.Methods.Any(x =>
                            (x != javaMethod) && (x.Name == javaMethod.Name) &&
                            (x.Parameters.Count == javaMethod.Parameters.Count) &&
                            (x.Descriptor != javaMethod.Descriptor)))*/
                    if (targetMethod != null)
                    {
                        if (!(targetMethod.IsAbstract && !javaMethod.IsAbstract))
                        {
                            return;
                        }
                    }
                }

                // We're using a dummy return type first.
                // Otherwise we cannot resolve generic return types.
                var signature = javaMethod.Signature;
                var nameInfo = declaringTypeBuilder.GetMethodName(javaMethod);

                var name = nameInfo.Name;
                if (nameInfo.IsConstructor)
                {
                    method = new NetMethodDefinition(".ctor", javaMethod, declaringType, target, convertSignedBytes, "MethodBuilder.Create")
                    {
                        AccessFlags = (int) javaMethod.AccessFlags,
                        EditorBrowsableState = nameInfo.EditorBrowsableState
                    };
                }
                else if (nameInfo.IsDeconstructor)
                {
                    method = new NetMethodDefinition("Finalize", javaMethod, declaringType, target, convertSignedBytes, "MethodBuilder.Create")
                    {
                        AccessFlags = (int) javaMethod.AccessFlags,
                        EditorBrowsableState = EditorBrowsableState.Always,
                        IsDeconstructor = true
                    };
                }
                else
                {
                    method = new NetMethodDefinition(name, javaMethod, declaringType, target, convertSignedBytes, "MethodBuilder.Create")
                    {
                        AccessFlags = (int) javaMethod.AccessFlags,
                        EditorBrowsableState = nameInfo.EditorBrowsableState
                    };
                    foreach (var typeParam in signature.TypeParameters)
                    {
                        method.GenericParameters.Add(
                            new NetGenericParameter(
                                TypeBuilder.GetMethodGenericParameterName(declaringType, typeParam.Name),
                                typeParam.Name, method));
                    }
                    var javaReturnType = signature.ReturnType;
                    NetTypeReference returnType;
                    if (!javaReturnType.TryResolve(target, this, convertSignedBytes, out returnType))
                    {
                        method = null;
                        return;
                    }
                    method.ReturnType = returnType;
                }
                method.OriginalJavaName = javaMethod.Name;

                // Find documentation
                var docClass = declaringTypeBuilder.Documentation;
                if (docClass != null)
                {
                    // Look for matches by name and parameter count first.
                    // If there is more then 1 match, we look to the parameter types.
                    var model = target.XmlModel;
                    var matches = docClass.Methods.Where(x => Matches(x, false, model)).ToList();
                    if (matches.Count == 1)
                    {
                        docMethod = matches[0];
                    }
                    else if (matches.Count > 0)
                    {
                        docMethod = matches.FirstOrDefault(x => Matches(x, true, model));
                    }
                }

                method.Attributes = declaringTypeBuilder.GetMethodAttributes(javaMethod, GetAttributes(declaringType, method, javaMethod, name, target.TypeNameMap));
                var paramIndex = 0;
                foreach (var iterator in signature.Parameters)
                {
                    var paramType = iterator;
                    if (paramType.IsJavaLangVoid())
                    {
                        paramType = new ObjectTypeReference("java/lang/Object", null);
                    }
                    NetTypeReference resolvedParamType;
                    if (!paramType.TryResolve(target, this, convertSignedBytes, out resolvedParamType))
                    {
                        method = null;
                        return; // Sometimes public methods used parameters with internal types
                    }
                    var docParam = (docMethod != null)
                                       ? docMethod.Parameters.ElementAtOrDefault(method.Parameters.Count)
                                       : null;
                    var paramName = MakeParameterNameUnique(CreateParameterName(resolvedParamType, docParam, target),
                                                            method);
                    var isParams = javaMethod.IsVarArgs && (paramIndex == signature.Parameters.Count - 1);
                    method.Parameters.Add(new NetParameterDefinition(paramName, resolvedParamType, isParams));
                    paramIndex++;
                }
                method.Description = (docMethod != null) ? docMethod.Description : null;
                declaringType.Methods.Add(method);
                if (!convertSignedBytes)
                {
                    target.MethodMap.Add(javaMethod, method);
                }
            }
            catch (ClassNotFoundException ex)
            {
                Console.WriteLine("Class {0} not found in {1}", ex.ClassName, javaMethod.Descriptor);
                method = null;
            }
        }
Exemplo n.º 19
0
 /// <summary>
 /// Add a postfix to the given parameter name to make it unique.
 /// </summary>
 private static string MakeParameterNameUnique(string name, NetMethodDefinition method)
 {
     var baseName = name;
     if (Keywords.IsKeyword(name))
         name = "@" + name;
     if (method.Parameters.All(x => x.Name != name))
         return name;
     var index = 1;
     while (true)
     {
         var extName = baseName + index;
         if (method.Parameters.All(x => x.Name != extName))
             return extName;
         index++;
     }
 }
Exemplo n.º 20
0
        /// <summary>
        /// Create type attributes
        /// </summary>
        private static MethodAttributes GetAttributes(NetTypeDefinition declaringType, NetMethodDefinition method, MethodDefinition javaMethod, string methodName, TypeNameMap typeNameMap)
        {
            var result = (MethodAttributes) 0;
            var isStatic = javaMethod.IsStatic;

            if (javaMethod.IsPublic) result |= MethodAttributes.Public;
            else if (javaMethod.IsProtected) result |= MethodAttributes.FamORAssem;
            else if (javaMethod.IsPrivate) result |= MethodAttributes.Private;
            else if (javaMethod.IsPackagePrivate) result |= MethodAttributes.Assembly;

            if (isStatic) result |= MethodAttributes.Static;
            if (javaMethod.IsAbstract) result |= MethodAttributes.Abstract;
            if (declaringType.IsInterface) result |= MethodAttributes.Abstract;

            if ((!javaMethod.IsFinal) && !isStatic && (methodName != ".ctor") && (!declaringType.IsStruct))
            {
                result |= MethodAttributes.Virtual;
            }
            else
            {
                result |= MethodAttributes.Final;
            }

            if (methodName == ".cctor")
            {
                result |= MethodAttributes.Static;
            }

            if (declaringType.IsSealed)
            {
                result &= ~MethodAttributes.Virtual;
            }

            return result;
        }
Exemplo n.º 21
0
 /// <summary>
 /// Is the given method a property set method?
 /// </summary>
 protected virtual bool IsSetter(NetMethodDefinition method)
 {
     if (method.Parameters.Count != 1)
         return false;
     if (!method.ReturnType.IsVoid())
         return false;
     var name = method.Name;
     if (!name.StartsWith("Set"))
         return false;
     if (name.Length < 4)
         return false;
     return true;
 }
Exemplo n.º 22
0
 /// <summary>
 /// Find the first matching set method.
 /// </summary>
 protected virtual NetMethodDefinition FindSetter(NetMethodDefinition getMethod, IEnumerable<NetMethodDefinition> setters)
 {
     var name = "Set" + getMethod.Name.Substring(3);
     var type = getMethod.ReturnType;
     return setters.FirstOrDefault(x => x.Name == name && x.Parameters[0].ParameterType.AreSame(type));
 }
Exemplo n.º 23
0
 /// <summary>
 /// Gets a mapping.
 /// Throws an error if not found.
 /// </summary>
 public bool TryGet(MethodDefinition javaMethod, out NetMethodDefinition  result)
 {
     return map.TryGetValue(javaMethod, out result);
 }
Exemplo n.º 24
0
 /// <summary>
 /// Customize the custom attributes collection of the given method.
 /// </summary>
 protected virtual void AddCustomAttributes(NetMethodDefinition method, List<NetCustomAttribute> customAttributes)
 {
     //Nothing to do here
 }
Exemplo n.º 25
0
        /// <summary>
        /// Returns true if the given boolean property should be prefixed by "Is"
        /// </summary>
        protected virtual bool AddIsPrefixToBoolProperty(string name, NetMethodDefinition method)
        {
            // We can't really tell if the "Is" Prefix was omitted because of
            // careless naming or because the chosen name is more to the point.
            // http://docstore.mik.ua/orelly/java-ent/jnut/ch06_02.htm
            // http://stackoverflow.com/questions/5322648/for-a-boolean-field-what-is-the-naming-convention-for-its-getter-setter
            // http://stackoverflow.com/questions/11941485/java-naming-convention-for-boolean-variable-names-writerenabled-vs-writerisenab
            // http://stackoverflow.com/questions/4851337/setter-for-a-boolean-variable-named-like-isactive
            //
            // Also, not adding a prefix improves source code compatibility with Xamarin.Android.

            return false;
            //var excludedPrefixes = new[] { "Is", "Can", "Has", "Use" };
            //var namePrefix = GetNamePrefix(name);
            //return !excludedPrefixes.Contains(namePrefix);
        }
Exemplo n.º 26
0
 public void RenameMethodOnly(NetMethodDefinition method, string newName)
 {
     method.SetName(newName);
 }
Exemplo n.º 27
0
        /// <summary>
        /// Find the first matching set method.
        /// </summary>
        protected virtual NetMethodDefinition FindSetter(NetMethodDefinition getMethod, IEnumerable<NetMethodDefinition> setters, bool findOverriden=false)
        {
            var getName = getMethod.Name;
            var getPrefix = GetNamePrefix(getName);

            if (getPrefix == "Has" || getPrefix == "Can")
                return null;

            if(getPrefix != null)
                getName = getName.Substring(getPrefix.Length);

            var possibleSetterNames = new List<string>
            {
                "Set" + getName,
                "set_" + getName
            };

            if (findOverriden && getPrefix == "get_")
            {
                // We need special handling for previously imported getters
                // to handle everything that might have been done to them in
                // "GetPropertyName". E.g. for get_IsEnabled, we want to find
                // "SetEnabled" as well. This might be a hack.
                if(getName.StartsWith("Is"))
                    possibleSetterNames.Add("Set" + getName.Substring(2));
                else if (getName.StartsWith("_"))
                    possibleSetterNames.Add("Set" + getName.Substring(1));
            }

            var type = getMethod.ReturnType;

            var possibleMatch = setters.Where(x => possibleSetterNames.Contains(x.Name)
                                                && x.Parameters[0].ParameterType.AreSame(type)
                                                && x.InterfaceType.AreSame(getMethod.InterfaceType)
                                                && x.HasSameVisibility(getMethod))
                                       .ToList();

            if (possibleMatch.Count > 1)
            {
                // try matching by create reason.
                var singleForSameReason = possibleMatch.SingleOrDefault(s => s.CreateReason == getMethod.CreateReason);
                if (singleForSameReason != null)
                    return singleForSameReason;

                Console.Error.WriteLine("Warning: more than one possible setter matches property {0}::{1}. Not generating setter.", typeDef.FullName, getMethod.Name);
                return null;
            }

            return possibleMatch.FirstOrDefault();
        }
Exemplo n.º 28
0
 /// <summary>
 /// Are the two methods equal wrt name, parameters and type parameter count?
 /// </summary>
 internal static bool IsDuplicate(this NetMethodDefinition method1, NetMethodDefinition method2)
 {
     if (method1.Name != method2.Name)
         return false;
     if (method1.IsSignConverted != method2.IsSignConverted)
         return false;
     var count = method1.Parameters.Count;
     if (count != method2.Parameters.Count)
         return false;
     if (method1.GenericParameters.Count != method2.GenericParameters.Count)
         return false;
     for (var i = 0; i < count; i++)
     {
         if (!method1.Parameters[i].ParameterType.AreSame(method2.Parameters[i].ParameterType))
             return false;
     }
     return true;
 }
Exemplo n.º 29
0
        /// <summary>
        /// Create a property name from the given getter.
        /// </summary>
        protected virtual string GetPropertyName(NetMethodDefinition method)
        {
            var name = method.Name;

            bool isSetter = method.Name.StartsWith("Set") || method.Name.StartsWith("set_");

            name = name.StartsWith("get_") ? name.Substring(4)
                 : name.StartsWith("Get")  ? name.Substring(3)
                 : name.StartsWith("set_") ? name.Substring(4)
                 : name.StartsWith("Set")  ? name.Substring(3)
                 : name;

            bool isBool = (isSetter ? method.Parameters[0].ParameterType : method.ReturnType)
                         .IsBoolean();

            if (isBool && AddIsPrefixToBoolProperty(name, method))
            {
                name = "Is" + name;
            }

            if (!(char.IsLetter(name[0]) || (name[0] == '_')))
                name = "_" + name;

            return name;
        }
            /// <summary>
            /// Is the given method a property get method?
            /// </summary>
            protected override bool IsGetter(NetMethodDefinition method)
            {
                var name = method.Name;
                if ((name == "Capacity") && (method.Parameters.Count == 0))
                    return true;

                return base.IsGetter(method);
            }
Exemplo n.º 31
0
 /// <summary>
 /// Create a property name from the given getter.
 /// </summary>
 protected virtual string GetPropertyName(NetMethodDefinition getter)
 {
     var name = getter.Name;
     name = name.StartsWith("Get") ? name.Substring(3) : name;
     if (!(char.IsLetter(name[0]) || (name[0] == '_')))
         name = "_" + name;
     if (getter.ReturnType.IsBoolean() && !name.StartsWith("Is"))
         name = "Is" + name;
     return name;
 }