/// <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); } if (!name.StartsWith("Get")) { return(false); } if (name.Length < 4) { return(false); } return(true); }
/// <summary> /// Add a default ctor if there is none, but there are other ctors. /// </summary> private void AddDefaultConstructor(NetTypeDefinition typeDef, TargetFramework target) { if (typeDef.IsStruct || typeDef.IsInterface || (this is IInterfaceConstantsTypeBuilder)) { return; } var ctors = typeDef.Methods.Where(x => x.IsConstructor).ToList(); if (ctors.Count == 0) { return; } // Is there a default ctor? if (ctors.Any(x => x.Parameters.Count == 0)) { return; } // Add default ctor var ctor = new NetMethodDefinition(".ctor", null, typeDef, target, SignedByteMode.None, "TypeBuilder.AddDefaultConstructor") { Attributes = MethodAttributes.FamORAssem, AccessFlags = (int)MethodAccessFlags.Protected, EditorBrowsableState = EditorBrowsableState.Never }; typeDef.Methods.Add(ctor); }
/// <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))); }
/// <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> /// 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); }
/// <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()); }
/// <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); } }
/// <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); }
/// <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; } }
protected override bool IsGetter(NetMethodDefinition method) { if (method.Name == "GetMessage") { return(false); } return(base.IsGetter(method)); }
/// <summary> /// Add a private default constructor. /// </summary> private static void AddPrivateDefaultCtor(NetTypeDefinition typeDef, TargetFramework target) { var ctor = new NetMethodDefinition(".ctor", null, typeDef, target, SignedByteMode.None, "TypeBuilder.AddPrivateDefaultCtor") { Attributes = MethodAttributes.Private, AccessFlags = (int)MethodAccessFlags.Private }; typeDef.Methods.Add(ctor); }
/// <summary> /// Is the given method a property set method? /// </summary> protected override bool IsSetter(NetMethodDefinition method) { var name = method.Name; if ((name == "SetLength") && (method.Parameters.Count == 1)) { return(true); } return(base.IsSetter(method)); }
/// <summary> /// Customize the custom attributes collection of the given method. /// </summary> protected override void AddCustomAttributes(NetMethodDefinition method, List <NetCustomAttribute> customAttributes) { if (method.OriginalJavaName == "charAt") { customAttributes.Add(new NetCustomAttribute(new NetTypeDefinition(ClassFile.Empty, method.Target, AttributeConstants.Dot42Scope) { Namespace = "System.Runtime.CompilerServices", Name = "IndexerName" }, "Chars")); } }
/// <summary> /// Is the given method a property get method? /// </summary> protected override bool IsGetter(NetMethodDefinition method) { var name = method.OriginalJavaName; if (name == "getBytes") { return(false); } return(base.IsGetter(method)); }
/// <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)); }
/// <summary> /// Create a property name from the given getter. /// </summary> protected override string GetPropertyName(NetMethodDefinition getter) { switch (getter.Name) { case "CharAt": return("Chars"); default: return(base.GetPropertyName(getter)); } }
/// <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)); } }
public override void ModifyMethodName(NetMethodDefinition method, MethodRenamer renamer) { if (method.Name == "GetTitle") { renamer.Rename(method, "JavaGetTitle"); } else { base.ModifyMethodName(method, renamer); } }
/// <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); }
/// <summary> /// Create a property name from the given getter. /// </summary> protected override string GetPropertyName(NetMethodDefinition getter) { switch (getter.Name) { // don't rename property that gets overridden //case "GetCause": // return "InnerException"; case "GetStackTrace": return("JavaStackTrace"); default: return(base.GetPropertyName(getter)); } }
/// <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)); } }
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); } }
/// <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); }
/// <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_"); }
/// <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); }
/// <summary> /// Is the given method a property get method? /// </summary> protected override bool IsGetter(NetMethodDefinition method) { var name = method.OriginalJavaName; if ((name == "length") && (method.Parameters.Count == 0)) { return(true); } if ((name == "charAt") && (method.Parameters.Count == 1)) { return(true); } if (name == "getBytes") { return(false); } return(base.IsGetter(method)); }
/// <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 virtual void ModifyMethodName(NetMethodDefinition method, MethodRenamer renamer) { var name = method.Name; var newName = name; if (name.IndexOf('$') >= 0) { newName = name.Replace('$', '_'); } var typeDef = method.DeclaringType; if ((typeDef != null) && ((name == typeDef.Name) || typeDef.GenericParameters.Any(x => x.Name == name))) { // Name conflict with generic parameter newName = "Java" + name; } if (newName != name) { renamer.Rename(method, newName); } }
/// <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_"); }
private void AddToProperty(NetTypeDefinition typeDef, NetMethodDefinition method, MethodDefinition ilMethod) { var ilProp = ilMethod.DeclaringType.Properties.First(p => p.SetMethod == ilMethod || p.GetMethod == ilMethod); if (ilProp.Parameters.Count > 0) //no need to handle indexers for now { return; } var propType = ilMethod.IsGetter ? method.ReturnType : method.Parameters[0].ParameterType; var prop = typeDef.Properties.FirstOrDefault(p => p.Name == ilProp.Name //&& p.Parameters.Count == ilProp.Parameters.Count && p.PropertyType == propType && p.MainMethod.InterfaceType.AreSame(method.InterfaceType)); if (prop == null) { prop = new NetPropertyDefinition(); prop.Name = ilProp.Name; prop.Description = method.Description; prop.DeclaringType = method.DeclaringType; prop.EditorBrowsableState = method.EditorBrowsableState; prop.CustomAttributes.AddRange(method.CustomAttributes); typeDef.Properties.Add(prop); } if (ilMethod.IsGetter) { prop.Getter = method; } else { prop.Setter = method; } }
/// <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++; } }