Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ClassComparer.cs « mono-api-html « corcompare « tools « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a3399fbef04e5b731cfeb8277176f2a0705752e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
// 
// Authors
//    Sebastien Pouliot  <sebastien@xamarin.com>
//
// Copyright 2013 Xamarin Inc. http://www.xamarin.com
// 
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml.Linq;

namespace Xamarin.ApiDiff {

	public class ClassComparer : Comparer {

		InterfaceComparer icomparer;
		ConstructorComparer ccomparer;
		FieldComparer fcomparer;
		PropertyComparer pcomparer;
		EventComparer ecomparer;
		MethodComparer mcomparer;
		ClassComparer kcomparer;

		public ClassComparer ()
		{
			icomparer = new InterfaceComparer ();
			ccomparer = new ConstructorComparer ();
			fcomparer = new FieldComparer ();
			pcomparer = new PropertyComparer ();
			ecomparer = new EventComparer ();
			mcomparer = new MethodComparer ();
		}

		public override void SetContext (XElement current)
		{
			State.Type = current.GetAttribute ("name");
			State.BaseType = current.GetAttribute ("base");
		}

		public void Compare (XElement source, XElement target)
		{
			var s = source.Element ("classes");
			var t = target.Element ("classes");
			if (XNode.DeepEquals (s, t))
				return;
			Compare (s.Elements ("class"), t.Elements ("class"));
		}

		public override void Added (XElement target, bool wasParentAdded)
		{
			string name = target.Attribute ("name").Value;
			if (State.IgnoreNew.Any (re => re.IsMatch (name)))
				return;
			Output.WriteLine ("<div> <!-- start type {0} -->", name);
			Output.WriteLine ("<h3>New Type {0}.{1}</h3>", State.Namespace, name);
			Output.WriteLine ("<pre class='added' data-is-non-breaking>");
			State.Indent = 0;
			AddedInner (target);
			Output.WriteLine ("</pre>");
			Output.WriteLine ("</div> <!-- end type {0} -->", name);
		}

		public void AddedInner (XElement target)
		{
			SetContext (target);
			if (target.IsTrue ("serializable"))
				Indent ().WriteLine ("[Serializable]");

			var type = target.Attribute ("type").Value;

			if (type == "enum") {
				// check if [Flags] is present
				var cattrs = target.Element ("attributes");
				if (cattrs != null) {
					foreach (var ca in cattrs.Elements ("attribute")) {
						if (ca.GetAttribute ("name") == "System.FlagsAttribute") {
							Indent ().WriteLine ("[Flags]");
							break;
						}
					}
				}
			}

			Indent ().Write ("public");

			if (type != "enum") {
				bool seal = target.IsTrue ("sealed");
				bool abst = target.IsTrue ("abstract");
				if (seal && abst)
					Output.Write (" static");
				else if (seal && type != "struct")
					Output.Write (" sealed");
				else if (abst && type != "interface")
					Output.Write (" abstract");
			}

			Output.Write (' ');
			Output.Write (type);
			Output.Write (' ');
			Output.Write (target.GetAttribute ("name"));

			var baseclass = target.GetAttribute ("base");
			if ((type != "enum") && (type != "struct")) {
				if (baseclass != null) {
					if (baseclass == "System.Object") {
						// while true we do not need to be reminded every time...
						baseclass = null;
					} else {
						Output.Write (" : ");
						Output.Write (baseclass);
					}
				}
			}

			// interfaces on enums are "standard" not user provided - so we do not want to show them
			if (type != "enum") {
				var i = target.Element ("interfaces");
				if (i != null) {
					var interfaces = new List<string> ();
					foreach (var iface in i.Elements ("interface"))
						interfaces.Add (icomparer.GetDescription (iface));
					Output.Write ((baseclass == null) ? " : " : ", ");
					Output.Write (String.Join (", ", interfaces));
				}
			}

			Output.WriteLine (" {");

			var t = target.Element ("constructors");
			if (t != null) {
				Indent ().WriteLine ("\t// constructors");
				foreach (var ctor in t.Elements ("constructor"))
					ccomparer.Added (ctor, true);
			}

			t = target.Element ("fields");
			if (t != null) {
				if (type != "enum")
					Indent ().WriteLine ("\t// fields");
				else
					SetContext (target);
				foreach (var field in t.Elements ("field"))
					fcomparer.Added (field, true);
			}

			t = target.Element ("properties");
			if (t != null) {
				Indent ().WriteLine ("\t// properties");
				foreach (var property in t.Elements ("property"))
					pcomparer.Added (property, true);
			}

			t = target.Element ("events");
			if (t != null) {
				Indent ().WriteLine ("\t// events");
				foreach (var evnt in t.Elements ("event"))
					ecomparer.Added (evnt, true);
			}

			t = target.Element ("methods");
			if (t != null) {
				Indent ().WriteLine ("\t// methods");
				foreach (var method in t.Elements ("method"))
					mcomparer.Added (method, true);
			}

			t = target.Element ("classes");
			if (t != null) {
				Output.WriteLine ();
				Indent ().WriteLine ("\t// inner types");
				kcomparer = new NestedClassComparer ();
				State.Indent++;
				foreach (var inner in t.Elements ("class"))
					kcomparer.AddedInner (inner);
				State.Indent--;
			}
			Indent ().WriteLine ("}");
		}

		public override void Modified (XElement source, XElement target, ApiChanges diff)
		{
			// hack - there could be changes that we're not monitoring (e.g. attributes properties)
			var output = Output;
			State.Output = new StringWriter ();

			var sb = source.GetAttribute ("base");
			var tb = target.GetAttribute ("base");
			if (sb != tb) {
				Output.Write ("Modified base type: ");
				Output.WriteLine (new ApiChange ().AppendModified (sb, tb, true).Member.ToString ());
			}

			ccomparer.Compare (source, target);
			icomparer.Compare (source, target);
			fcomparer.Compare (source, target);
			pcomparer.Compare (source, target);
			ecomparer.Compare (source, target);
			mcomparer.Compare (source, target);

			var si = source.Element ("classes");
			if (si != null) {
				var ti = target.Element ("classes");
				kcomparer = new NestedClassComparer ();
				kcomparer.Compare (si.Elements ("class"), ti == null ? null : ti.Elements ("class"));
			}

			var s = (Output as StringWriter).ToString ();
			State.Output = output;
			if (s.Length > 0) {
				var tn = GetTypeName (target);
				Output.WriteLine ("<!-- start type {0} --> <div>", tn);
				Output.WriteLine ("<h3>Type Changed: {0}.{1}</h3>", State.Namespace, GetTypeName (target));
				Output.WriteLine (s);
				Output.WriteLine ("</div> <!-- end type {0} -->", tn);
			}
		}

		public override void Removed (XElement source)
		{
			Output.Write ("<h3>Removed Type <span class='breaking' data-is-breaking>{0}.{1}</span></h3>", State.Namespace, GetTypeName (source));
		}

		public virtual string GetTypeName (XElement type)
		{
			return type.GetAttribute ("name");
		}
	}

	public class NestedClassComparer : ClassComparer {

		public override void SetContext (XElement current)
		{
		}

		public override string GetTypeName (XElement type)
		{
			return State.Type + "." + base.GetTypeName (type);
		}
	}
}