CSLA.NET 6.0.0
CSLA .NET is a software development framework that helps you build a reusable, maintainable object-oriented business layer for your app.
FindBusinessObjectCreationAnalyzer.cs
Go to the documentation of this file.
1using Microsoft.CodeAnalysis;
2using Microsoft.CodeAnalysis.CSharp;
3using Microsoft.CodeAnalysis.CSharp.Syntax;
4using Microsoft.CodeAnalysis.Diagnostics;
5using System.Collections.Immutable;
8
9namespace Csla.Analyzers
10{
11 [DiagnosticAnalyzer(LanguageNames.CSharp)]
13 : DiagnosticAnalyzer
14 {
15 private static readonly DiagnosticDescriptor objectCreatedRule =
16 new DiagnosticDescriptor(
17 Constants.AnalyzerIdentifiers.FindBusinessObjectCreation, FindBusinessObjectCreationConstants.Title,
18 FindBusinessObjectCreationConstants.Message, Constants.Categories.Usage,
19 DiagnosticSeverity.Error, true,
20 helpLinkUri: HelpUrlBuilder.Build(
21 Constants.AnalyzerIdentifiers.FindBusinessObjectCreation, nameof(FindBusinessObjectCreationAnalyzer)));
22
23 public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics =>
24 ImmutableArray.Create(objectCreatedRule);
25
26 public override void Initialize(AnalysisContext context)
27 {
28 context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics);
29 context.EnableConcurrentExecution();
30 context.RegisterSyntaxNodeAction(AnalyzeObjectCreationExpression, SyntaxKind.ObjectCreationExpression);
31 }
32
33 private static void AnalyzeObjectCreationExpression(SyntaxNodeAnalysisContext context)
34 {
35 var constructorNode = (ObjectCreationExpressionSyntax)context.Node;
36 var constructorSymbol = context.SemanticModel.GetSymbolInfo(constructorNode).Symbol as IMethodSymbol;
37 var containingSymbol = constructorSymbol?.ContainingType;
38
39 if(containingSymbol.IsStereotype())
40 {
41 context.CancellationToken.ThrowIfCancellationRequested();
42 var callerClassNode = constructorNode.FindParent<ClassDeclarationSyntax>();
43
44 if(callerClassNode != null)
45 {
46 var callerClassSymbol = context.SemanticModel.GetDeclaredSymbol(callerClassNode) as ITypeSymbol;
47
48 if(!callerClassSymbol.IsObjectFactory())
49 {
50 context.ReportDiagnostic(Diagnostic.Create(objectCreatedRule, constructorNode.GetLocation()));
51 }
52 }
53 }
54 }
55 }
56}
override ImmutableArray< DiagnosticDescriptor > SupportedDiagnostics