3 // Sebastien Pouliot <sebastien@xamarin.com>
5 // Copyright 2013-2014 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;
30 using System.Reflection;
32 using System.Xml.Linq;
34 namespace Xamarin.ApiDiff {
36 public class FieldComparer : MemberComparer {
38 public override string GroupName {
39 get { return "fields"; }
42 public override string ElementName {
43 get { return "field"; }
46 void RenderFieldAttributes (FieldAttributes source, FieldAttributes target, ApiChange change)
48 if (!State.IgnoreNonbreaking) {
49 var srcNotSerialized = (source & FieldAttributes.NotSerialized) == FieldAttributes.NotSerialized;
50 var tgtNotSerialized = (target & FieldAttributes.NotSerialized) == FieldAttributes.NotSerialized;
51 if (srcNotSerialized != tgtNotSerialized) {
52 // this is not a breaking change, so only render it if it changed.
53 if (srcNotSerialized) {
54 change.AppendRemoved ("[NonSerialized]\n");
56 change.AppendAdded ("[NonSerialized]\n");
61 // the visibility values are the same for MethodAttributes and FieldAttributes, so just use the same method.
62 RenderVisibility ((MethodAttributes) source, (MethodAttributes) target, change);
63 // same for the static flag
64 RenderStatic ((MethodAttributes) source, (MethodAttributes) target, change);
66 var srcLiteral = (source & FieldAttributes.Literal) != 0;
67 var tgtLiteral = (target & FieldAttributes.Literal) != 0;
71 change.Append ("const ");
73 change.AppendRemoved ("const", true).Append (" ");
75 } else if (tgtLiteral) {
76 change.AppendAdded ("const", true).Append (" ");
79 var srcInitOnly = (source & FieldAttributes.InitOnly) != 0;
80 var tgtInitOnly = (target & FieldAttributes.InitOnly) != 0;
83 change.Append ("readonly ");
85 change.AppendRemoved ("readonly", false).Append (" ");
87 } else if (tgtInitOnly) {
88 change.AppendAdded ("readonly", true).Append (" ");
92 public override bool Equals (XElement source, XElement target, ApiChanges changes)
94 if (base.Equals (source, target, changes))
97 var name = source.GetAttribute ("name");
98 var srcValue = source.GetAttribute ("value");
99 var tgtValue = target.GetAttribute ("value");
100 var change = new ApiChange ();
101 change.Header = "Modified " + GroupName;
103 if (State.BaseType == "System.Enum") {
104 change.Append (name).Append (" = ");
105 if (srcValue != tgtValue) {
106 change.AppendModified (srcValue, tgtValue, true);
108 change.Append (srcValue);
111 RenderFieldAttributes (source.GetFieldAttributes (), target.GetFieldAttributes (), change);
113 var srcType = source.GetTypeName ("fieldtype");
114 var tgtType = target.GetTypeName ("fieldtype");
116 if (srcType != tgtType) {
117 change.AppendModified (srcType, tgtType, true);
119 change.Append (srcType);
122 change.Append (name);
124 if (srcType == "string" && srcValue != null)
125 srcValue = "\"" + srcValue + "\"";
127 if (tgtType == "string" && tgtValue != null)
128 tgtValue = "\"" + tgtValue + "\"";
130 if (srcValue != tgtValue) {
131 change.Append (" = ");
132 if (srcValue == null)
134 if (tgtValue == null)
136 change.AppendModified (srcValue, tgtValue, true);
137 } else if (srcValue != null) {
138 change.Append (" = ");
139 change.Append (srcValue);
144 changes.Add (source, target, change);
149 public override string GetDescription (XElement e)
151 var sb = new StringBuilder ();
153 string name = e.GetAttribute ("name");
154 string value = e.GetAttribute ("value");
156 if (State.BaseType == "System.Enum") {
157 sb.Append (name).Append (" = ").Append (value).Append (',');
159 var attribs = e.Attribute ("attrib");
160 if (attribs != null) {
161 var attr = (FieldAttributes)Int32.Parse (attribs.Value);
162 if ((attr & FieldAttributes.Public) != FieldAttributes.Public) {
163 sb.Append ("protected ");
165 sb.Append ("public ");
168 if ((attr & FieldAttributes.Static) != 0)
169 sb.Append ("static ");
171 if ((attr & FieldAttributes.Literal) != 0)
172 sb.Append ("const ");
175 string ftype = e.GetTypeName ("fieldtype");
176 sb.Append (ftype).Append (' ');
178 if (ftype == "string" && e.Attribute ("value") != null) {
180 sb.Append (" = null");
182 sb.Append (" = \"").Append (value).Append ('"');
187 return sb.ToString ();
190 public override void BeforeAdding (IEnumerable<XElement> list)
193 if (State.BaseType == "System.Enum") {
194 Output.WriteLine ("<div>");
195 Output.WriteLine ("<p>Added value{0}:</p>", list.Count () > 1 ? "s" : String.Empty);
196 Output.WriteLine ("<pre class='added' data-is-non-breaking>");
198 base.BeforeAdding (list);
202 public override void BeforeRemoving (IEnumerable<XElement> list)
205 if (State.BaseType == "System.Enum") {
206 Output.WriteLine ("<p>Removed value{0}:</p>", list.Count () > 1 ? "s" : String.Empty);
207 Output.WriteLine ("<pre class='removed' data-is-breaking>");
209 base.BeforeRemoving (list);