-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCSharpMemberAnalyzer.cs
283 lines (254 loc) · 11.6 KB
/
CSharpMemberAnalyzer.cs
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
namespace CSharpMemberAnalyzerLibrary
{
public class MemberInfo
{
public string Name { get; set; }
public string Kind { get; set; }
public List<string> AccessModifiers { get; set; } = new List<string>();
public List<string> OptionalModifiers { get; set; } = new List<string>();
public string ReturnType { get; set; }
public List<ParameterInfo> Parameters { get; set; } = new List<ParameterInfo>();
public List<string> Attributes { get; set; } = new List<string>();
public string DocumentationComment { get; set; }
public string VisibilityScope { get; set; }
public string DefaultValue { get; set; }
public bool IsExplicitInterfaceImplementation { get; set; }
public string EventDelegateType { get; set; }
public string InitializerValue { get; set; }
public PropertyAccessorInfo PropertyAccessors { get; set; }
public bool IsOverloaded { get; set; }
public string InheritanceInfo { get; set; }
public List<GenericTypeParameterInfo> GenericTypeParameters { get; set; } = new List<GenericTypeParameterInfo>();
public string Text { get; set; }
}
public class ParameterInfo
{
public string Name { get; set; }
public string Type { get; set; }
public List<string> Modifiers { get; set; } = new List<string>();
public string DefaultValue { get; set; }
}
public class PropertyAccessorInfo
{
public bool HasGetter { get; set; }
public bool HasSetter { get; set; }
public string GetterAccessibility { get; set; }
public string SetterAccessibility { get; set; }
}
public class GenericTypeParameterInfo
{
public string Name { get; set; }
public List<string> Constraints { get; set; } = new List<string>();
}
public class CSharpMemberAnalyzer
{
public List<MemberInfo> AnalyzeType(CSharpTypeAnalyzerLibrary.TypeInfo typeInfo)
{
var members = new List<MemberInfo>();
var tree = CSharpSyntaxTree.ParseText(typeInfo.Text);
var root = tree.GetCompilationUnitRoot();
var typeDeclaration = root.DescendantNodes().OfType<TypeDeclarationSyntax>().FirstOrDefault();
if (typeDeclaration == null) return members;
foreach (var member in typeDeclaration.Members)
{
var memberInfo = AnalyzeMember(member, typeInfo.TypeKeyword);
if (memberInfo != null)
{
members.Add(memberInfo);
}
}
return members;
}
private MemberInfo AnalyzeMember(MemberDeclarationSyntax member, string parentTypeKeyword)
{
var memberInfo = new MemberInfo
{
Name = GetMemberName(member),
Kind = member.Kind().ToString(),
VisibilityScope = parentTypeKeyword
};
SetAccessModifiers(member, memberInfo);
SetOptionalModifiers(member, memberInfo);
SetReturnType(member, memberInfo);
SetParameters(member, memberInfo);
SetAttributes(member, memberInfo);
SetDocumentationComment(member, memberInfo);
SetDefaultValue(member, memberInfo);
SetExplicitInterfaceImplementation(member, memberInfo);
SetEventDelegateType(member, memberInfo);
SetInitializerValue(member, memberInfo);
SetPropertyAccessors(member, memberInfo);
SetInheritanceInfo(member, memberInfo);
SetGenericTypeParameters(member, memberInfo);
return memberInfo;
}
private string GetMemberName(MemberDeclarationSyntax member)
{
return member switch
{
MethodDeclarationSyntax m => m.Identifier.Text,
PropertyDeclarationSyntax p => p.Identifier.Text,
FieldDeclarationSyntax f => f.Declaration.Variables.First().Identifier.Text,
EventFieldDeclarationSyntax e => e.Declaration.Variables.First().Identifier.Text,
ConstructorDeclarationSyntax c => c.Identifier.Text,
DestructorDeclarationSyntax d => d.Identifier.Text,
_ => "Unknown"
};
}
private void SetAccessModifiers(MemberDeclarationSyntax member, MemberInfo memberInfo)
{
if (member is BaseMethodDeclarationSyntax || member is BasePropertyDeclarationSyntax || member is BaseFieldDeclarationSyntax)
{
var modifiers = member.Modifiers;
memberInfo.AccessModifiers = modifiers
.Where(m => m.IsKind(SyntaxKind.PublicKeyword) || m.IsKind(SyntaxKind.PrivateKeyword) ||
m.IsKind(SyntaxKind.ProtectedKeyword) || m.IsKind(SyntaxKind.InternalKeyword))
.Select(m => m.Text)
.ToList();
}
}
private void SetOptionalModifiers(MemberDeclarationSyntax member, MemberInfo memberInfo)
{
if (member is BaseMethodDeclarationSyntax || member is BasePropertyDeclarationSyntax || member is BaseFieldDeclarationSyntax)
{
var modifiers = member.Modifiers;
memberInfo.OptionalModifiers = modifiers
.Where(m => !m.IsKind(SyntaxKind.PublicKeyword) && !m.IsKind(SyntaxKind.PrivateKeyword) &&
!m.IsKind(SyntaxKind.ProtectedKeyword) && !m.IsKind(SyntaxKind.InternalKeyword))
.Select(m => m.Text)
.ToList();
}
}
private void SetReturnType(MemberDeclarationSyntax member, MemberInfo memberInfo)
{
memberInfo.ReturnType = member switch
{
MethodDeclarationSyntax m => m.ReturnType.ToString(),
PropertyDeclarationSyntax p => p.Type.ToString(),
FieldDeclarationSyntax f => f.Declaration.Type.ToString(),
_ => null
};
}
private void SetParameters(MemberDeclarationSyntax member, MemberInfo memberInfo)
{
if (member is BaseMethodDeclarationSyntax methodBase)
{
foreach (var parameter in methodBase.ParameterList.Parameters)
{
memberInfo.Parameters.Add(new ParameterInfo
{
Name = parameter.Identifier.Text,
Type = parameter.Type?.ToString(),
Modifiers = parameter.Modifiers.Select(m => m.Text).ToList(),
DefaultValue = parameter.Default?.Value.ToString()
});
}
}
}
private void SetAttributes(MemberDeclarationSyntax member, MemberInfo memberInfo)
{
memberInfo.Attributes = member.AttributeLists
.SelectMany(al => al.Attributes)
.Select(a => a.Name.ToString())
.ToList();
}
private void SetDocumentationComment(MemberDeclarationSyntax member, MemberInfo memberInfo)
{
var trivia = member.GetLeadingTrivia()
.Select(t => t.GetStructure())
.OfType<DocumentationCommentTriviaSyntax>()
.FirstOrDefault();
memberInfo.DocumentationComment = trivia?.ToString();
}
private void SetDefaultValue(MemberDeclarationSyntax member, MemberInfo memberInfo)
{
if (member is FieldDeclarationSyntax field)
{
memberInfo.DefaultValue = field.Declaration.Variables.FirstOrDefault()?.Initializer?.Value.ToString();
}
}
private void SetExplicitInterfaceImplementation(MemberDeclarationSyntax member, MemberInfo memberInfo)
{
if (member is MethodDeclarationSyntax method)
{
memberInfo.IsExplicitInterfaceImplementation = method.ExplicitInterfaceSpecifier != null;
}
else if (member is PropertyDeclarationSyntax property)
{
memberInfo.IsExplicitInterfaceImplementation = property.ExplicitInterfaceSpecifier != null;
}
}
private void SetEventDelegateType(MemberDeclarationSyntax member, MemberInfo memberInfo)
{
if (member is EventFieldDeclarationSyntax eventField)
{
memberInfo.EventDelegateType = eventField.Declaration.Type.ToString();
}
}
private void SetInitializerValue(MemberDeclarationSyntax member, MemberInfo memberInfo)
{
if (member is PropertyDeclarationSyntax property)
{
memberInfo.InitializerValue = property.Initializer?.Value.ToString();
}
}
private void SetPropertyAccessors(MemberDeclarationSyntax member, MemberInfo memberInfo)
{
if (member is PropertyDeclarationSyntax property)
{
memberInfo.PropertyAccessors = new PropertyAccessorInfo
{
HasGetter = property.AccessorList?.Accessors.Any(a => a.IsKind(SyntaxKind.GetAccessorDeclaration)) ?? false,
HasSetter = property.AccessorList?.Accessors.Any(a => a.IsKind(SyntaxKind.SetAccessorDeclaration)) ?? false,
GetterAccessibility = property.AccessorList?.Accessors
.FirstOrDefault(a => a.IsKind(SyntaxKind.GetAccessorDeclaration))
?.Modifiers.ToString() ?? "default",
SetterAccessibility = property.AccessorList?.Accessors
.FirstOrDefault(a => a.IsKind(SyntaxKind.SetAccessorDeclaration))
?.Modifiers.ToString() ?? "default"
};
}
}
private void SetInheritanceInfo(MemberDeclarationSyntax member, MemberInfo memberInfo)
{
if (member is MethodDeclarationSyntax method)
{
if (method.Modifiers.Any(SyntaxKind.OverrideKeyword))
{
memberInfo.InheritanceInfo = "Overridden from base class";
}
else if (method.Modifiers.Any(SyntaxKind.VirtualKeyword))
{
memberInfo.InheritanceInfo = "Virtual method";
}
}
}
private void SetGenericTypeParameters(MemberDeclarationSyntax member, MemberInfo memberInfo)
{
if (member is MethodDeclarationSyntax method && method.TypeParameterList != null)
{
foreach (var typeParameter in method.TypeParameterList.Parameters)
{
var genericTypeParameter = new GenericTypeParameterInfo
{
Name = typeParameter.Identifier.Text,
Constraints = new List<string>()
};
// Find the corresponding type parameter constraints
var constraints = method.ConstraintClauses
.Where(cc => cc.Name.Identifier.Text == typeParameter.Identifier.Text)
.SelectMany(cc => cc.Constraints)
.Select(c => c.ToString());
genericTypeParameter.Constraints.AddRange(constraints);
memberInfo.GenericTypeParameters.Add(genericTypeParameter);
}
}
}
}
}