private DocMethod ToDocMethod(XmlNode node, DocType parent) { DocPrimitive dp = ToDocPrimitive(node, 2); var m = new DocMethod { Name = dp.Name, Summary = dp.Summary, Parent = parent }; XmlNodeList typeParams = node.SelectNodes("typeparam"); m.TypeParameters = typeParams.Count == 0 ? null : typeParams.Cast <XmlNode>().Select(n => ToDocPrimitive(n, useInnerTextAsSummary: true)).ToArray(); XmlNodeList methodParams = node.SelectNodes("param"); m.Parameters = methodParams.Count == 0 ? null : methodParams.Cast <XmlNode>().Select(n => ToDocPrimitive(n, useInnerTextAsSummary: true)).ToArray(); m.Returns = GetValue(node, "returns"); return(m); }
private void GenerateMethodShort(DocMethod m, StringBuilder output) { output.Append("|"); output.Append(m.GetBeautifulParameterString()); output.Append("|"); if (!string.IsNullOrEmpty(m.Summary)) { output.Append(m.Summary); } output.Append("|"); output.AppendLine(); }
private void GenerateMethod(DocMethod m, StringBuilder output) { //output.Append("**"); output.Append("|**"); output.Append(m.GetBeautifulParameterString()); output.Append("**|"); if (!string.IsNullOrEmpty(m.Summary)) { output.Append(m.Summary); } output.Append("|"); if (!string.IsNullOrEmpty(m.Returns)) { output.Append(m.Returns); } output.Append("|"); output.AppendLine(); output.AppendLine(); /*if (m.Parameters != null || m.TypeParameters != null) * { * if(m.TypeParameters != null) * { * foreach(DocPrimitive p in m.TypeParameters) * { * AppendListItem(output, p); * } * } * * if (m.Parameters != null) * { * foreach (DocPrimitive p in m.Parameters) * { * AppendListItem(output, p); * } * } * }*/ }
/// <summary> /// Does my method match the given documentation? /// </summary> private bool Matches(DocMethod arg, bool matchParamTypes, DocModel model) { if (javaMethod.Name != arg.Name) { if (method.IsConstructor) { if (!javaMethod.DeclaringClass.ClassName.EndsWith("/" + arg.Name)) { return(false); } } else { return(false); } } var pCount = javaMethod.Parameters.Count; if (pCount != arg.Parameters.Count()) { return(false); } if (!matchParamTypes) { // Param match and name matches, fine for now return(true); } // Match on param types var pIndex = 0; foreach (var docParam in arg.Parameters) { var p = javaMethod.Parameters[pIndex++]; var paramType = docParam.ParameterType.Resolve(model); if (paramType == null) { // Param type cannot be resolved, so no match return(false); } } return(true); }
/// <summary> /// Processes the public method overloads. /// </summary> /// <param name="exportedType">The <see cref="DocExportedType"/>.</param> private void ProcessMethods(DocExportedType exportedType) { var typePropertyMethods = exportedType.Type.GetProperties() .Select(p => new[] { p.GetMethod, p.SetMethod }) .SelectMany(p => p) .Where(p => p != null) .Distinct(); var typeMethods = exportedType.Type.GetMethods( BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance).Where(m => m.DeclaringType == exportedType.Type) .Except(typePropertyMethods); foreach (var methodInfo in typeMethods) { var method = exportedType.Methods.FirstOrDefault(m => m.Name == methodInfo.Name); if (method == null) { method = new DocMethod(exportedType) { Name = methodInfo.Name, MethodReturnType = TypeCache.Cache[methodInfo.ReturnType], }; exportedType.Methods.Add(method); } var methodOverload = new DocMethodOverload(methodInfo, method) { Name = MemberUtils.GenerateCodeFor(methodInfo), XPath = MemberUtils.GetSelector(methodInfo), }; LinkCache.Register(methodOverload); methodOverload.Code = methodOverload.Name; ProcessParameters(methodInfo.GetParameters(), methodOverload, methodOverload.Parameters); method.MethodOverloads.Add(methodOverload); } }
/// <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, signedByteMode, "MethodBuilder.Create") { AccessFlags = (int)javaMethod.AccessFlags, EditorBrowsableState = nameInfo.EditorBrowsableState }; } else if (nameInfo.IsDeconstructor) { method = new NetMethodDefinition("Finalize", javaMethod, declaringType, target, signedByteMode, "MethodBuilder.Create") { AccessFlags = (int)javaMethod.AccessFlags, EditorBrowsableState = EditorBrowsableState.Always, IsDeconstructor = true }; } else { method = new NetMethodDefinition(name, javaMethod, declaringType, target, signedByteMode, "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, method.IsSignConverted, out returnType)) { method = null; return; } method.ReturnType = returnType; //if (signedByteMode == SignedByteMode.HasUnsignedPartner || signedByteMode == SignedByteMode.HasUnsignedPartnerOnlyInReturnType) //{ // // clear virtual status if possible, as this will create problems with overrides. // // set to final. // method.IsVirtual = false; // method.IsFinal = true; //} } 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, method.IsSignConverted, 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 (!method.HasUnsignedPartner) { target.MethodMap.Add(javaMethod, method); } } catch (ClassNotFoundException ex) { Console.WriteLine("Class {0} not found in {1}", ex.ClassName, javaMethod.Descriptor); method = null; } }
/// <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; } }
/// <summary> /// Does my method match the given documentation? /// </summary> private bool Matches(DocMethod arg, bool matchParamTypes, DocModel model) { if (javaMethod.Name != arg.Name) { if (method.IsConstructor) { if (!javaMethod.DeclaringClass.ClassName.EndsWith("/" + arg.Name)) return false; } else { return false; } } var pCount = javaMethod.Parameters.Count; if (pCount != arg.Parameters.Count()) return false; if (!matchParamTypes) { // Param match and name matches, fine for now return true; } // Match on param types var pIndex = 0; foreach (var docParam in arg.Parameters) { var p = javaMethod.Parameters[pIndex++]; var paramType = docParam.ParameterType.Resolve(model); if (paramType == null) { // Param type cannot be resolved, so no match return false; } } return true; }
/// <summary> /// Load the given parameter /// </summary> private void LoadParameter(DocMethod method, XElement param, string @namespace) { var name = param.GetElementValue("declname"); var type = LoadType(param.Element("type"), @namespace); var p = new DocParameter(name) { ParameterType = type }; method.Parameters.Add(p); }
/// <summary> /// Load the given method /// </summary> private void LoadMethod(DocClass @class, XElement memberdef) { var name = memberdef.GetElementValue("name"); var xmlMethod = new DocMethod(name) { Description = GetDescription(memberdef) }; @class.Methods.Add(xmlMethod); // Load parameter var ns = @class.Namespace; foreach (var p in memberdef.Elements("param")) { LoadParameter(xmlMethod, p, ns); } }