5using System.Collections.Immutable;
11 [DiagnosticAnalyzer(LanguageNames.CSharp)]
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(
24 ImmutableArray.Create(objectCreatedRule);
28 context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics);
29 context.EnableConcurrentExecution();
30 context.RegisterSyntaxNodeAction(AnalyzeObjectCreationExpression, SyntaxKind.ObjectCreationExpression);
33 private static void AnalyzeObjectCreationExpression(SyntaxNodeAnalysisContext context)
35 var constructorNode = (ObjectCreationExpressionSyntax)context.Node;
36 var constructorSymbol = context.SemanticModel.GetSymbolInfo(constructorNode).Symbol as IMethodSymbol;
37 var containingSymbol = constructorSymbol?.ContainingType;
39 if(containingSymbol.IsStereotype())
41 context.CancellationToken.ThrowIfCancellationRequested();
42 var callerClassNode = constructorNode.FindParent<ClassDeclarationSyntax>();
44 if(callerClassNode !=
null)
46 var callerClassSymbol = context.SemanticModel.GetDeclaredSymbol(callerClassNode) as ITypeSymbol;
48 if(!callerClassSymbol.IsObjectFactory())
50 context.ReportDiagnostic(Diagnostic.Create(objectCreatedRule, constructorNode.GetLocation()));
override ImmutableArray< DiagnosticDescriptor > SupportedDiagnostics
override void Initialize(AnalysisContext context)