3 // Sebastien Pouliot <sebastien@xamarin.com>
5 // Copyright 2013 Xamarin Inc. http://www.xamarin.com
7 // Permission is hereby granted, free of charge, to any person obtaining
8 // a copy of this software and associated documentation files (the
9 // "Software"), to deal in the Software without restriction, including
10 // without limitation the rights to use, copy, modify, merge, publish,
11 // distribute, sublicense, and/or sell copies of the Software, and to
12 // permit persons to whom the Software is furnished to do so, subject to
13 // the following conditions:
15 // The above copyright notice and this permission notice shall be
16 // included in all copies or substantial portions of the Software.
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 using System.Collections.Generic;
29 using System.Reflection;
31 using System.Xml.Linq;
33 namespace Xamarin.ApiDiff {
35 public class ConstructorComparer : MemberComparer {
37 public override string GroupName {
38 get { return "constructors"; }
41 public override string ElementName {
42 get { return "constructor"; }
45 public override bool Find (XElement e)
47 return (e.Attribute ("name").Value == Source.Attribute ("name").Value);
50 public override string GetDescription (XElement e)
52 StringBuilder sb = GetObsoleteMessage (e);
53 bool obsolete = sb.Length > 0;
55 var attribs = e.Attribute ("attrib");
56 if (attribs != null) {
57 var attr = (MethodAttributes) Int32.Parse (attribs.Value);
58 if ((attr & MethodAttributes.Public) != MethodAttributes.Public) {
59 sb.Append ("protected ");
61 sb.Append ("public ");
64 if ((attr & MethodAttributes.Static) != 0) {
65 sb.Append ("static ");
66 } else if ((attr & MethodAttributes.Virtual) != 0) {
67 if ((attr & MethodAttributes.VtableLayoutMask) == 0)
68 sb.Append ("override ");
70 sb.Append ("virtual ");
74 string name = e.GetAttribute ("name");
76 var r = e.GetTypeName ("returntype");
78 // ctor dont' have a return type
79 sb.Append (r).Append (' ');
81 // show the constructor as it would be defined in C#
82 name = name.Replace (".ctor", State.Type);
85 // the XML file `name` does not contain parameter names, so we must process them ourselves
86 // which gives us the opportunity to simplify type names
87 sb.Append (name.Substring (0, name.IndexOf ('(')));
89 var genericp = e.Element ("generic-parameters");
90 if (genericp != null) {
91 var list = new List<string> ();
92 foreach (var p in genericp.Elements ("generic-parameter")) {
93 list.Add (p.GetTypeName ("name"));
95 sb.Append ("<").Append (String.Join (", ", list)).Append (">");
99 var parameters = e.Element ("parameters");
100 if (parameters != null) {
101 var list = new List<string> ();
102 foreach (var p in parameters.Elements ("parameter")) {
103 list.Add (p.GetTypeName ("type") + " " + p.GetAttribute ("name"));
105 sb.Append (String.Join (", ", list));
110 sb.AppendLine (); // more readable output
111 return sb.ToString ();