EntityFiledAccessAnalyzer.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. using System.Collections.Immutable;
  2. using System.Linq;
  3. using Microsoft.CodeAnalysis;
  4. using Microsoft.CodeAnalysis.CSharp;
  5. using Microsoft.CodeAnalysis.CSharp.Syntax;
  6. using Microsoft.CodeAnalysis.Diagnostics;
  7. namespace ET.Analyzer
  8. {
  9. [DiagnosticAnalyzer(LanguageNames.CSharp)]
  10. public class EntityFiledAccessAnalyzer: DiagnosticAnalyzer
  11. {
  12. private const string Title = "实体字段访问错误";
  13. private const string MessageFormat = "实体: {0} 字段: {1} 只能在实体类生命周期组件或友元类(含有FriendOfAttribute)中访问";
  14. private const string Description = "请使用实体类属性或方法访问其他实体字段.";
  15. private static readonly DiagnosticDescriptor Rule = new DiagnosticDescriptor(DiagnosticIds.EntityFiledAccessAnalyzerRuleId,
  16. Title,
  17. MessageFormat,
  18. DiagnosticCategories.Hotfix,
  19. DiagnosticSeverity.Error, true, Description);
  20. public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(Rule);
  21. public override void Initialize(AnalysisContext context)
  22. {
  23. if (!AnalyzerGlobalSetting.EnableAnalyzer)
  24. {
  25. return;
  26. }
  27. context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
  28. context.EnableConcurrentExecution();
  29. context.RegisterSyntaxNodeAction(this.AnalyzeMemberAccessExpression, SyntaxKind.SimpleMemberAccessExpression);
  30. }
  31. private void AnalyzeMemberAccessExpression(SyntaxNodeAnalysisContext context)
  32. {
  33. if (!AnalyzerHelper.IsAssemblyNeedAnalyze(context.Compilation.AssemblyName, AnalyzeAssembly.All))
  34. {
  35. return;
  36. }
  37. if (!(context.Node is MemberAccessExpressionSyntax memberAccessExpressionSyntax))
  38. {
  39. return;
  40. }
  41. // -----筛选出实体类的字段symbol-----
  42. ISymbol? filedSymbol = context.SemanticModel.GetSymbolInfo(memberAccessExpressionSyntax).Symbol;
  43. if (filedSymbol == null || !(filedSymbol is IFieldSymbol))
  44. {
  45. return;
  46. }
  47. if (filedSymbol.IsStatic)
  48. {
  49. return;
  50. }
  51. if (filedSymbol.ContainingType.BaseType?.ToString() != Definition.EntityType)
  52. {
  53. return;
  54. }
  55. // -----筛选出在实体类和实体System外部字段访问-----
  56. // 实体System包括awakeSystem updateSystem等生命周期类和 componentSystem静态方法类
  57. ClassDeclarationSyntax? accessFieldClassDeclaretion = memberAccessExpressionSyntax.GetParentClassDeclaration();
  58. if (accessFieldClassDeclaretion == null)
  59. {
  60. return;
  61. }
  62. INamedTypeSymbol? accessFieldClassSymbol = context.SemanticModel.GetDeclaredSymbol(accessFieldClassDeclaretion);
  63. if (accessFieldClassSymbol == null)
  64. {
  65. return;
  66. }
  67. // 实体基类忽略处理
  68. if (accessFieldClassSymbol.ToString() == Definition.EntityType)
  69. {
  70. return;
  71. }
  72. // 允许类内部访问字段
  73. if (accessFieldClassSymbol.ToString()== filedSymbol.ContainingType.ToString() )
  74. {
  75. return;
  76. }
  77. //判断是否在实体类生命周期System中
  78. if (this.CheckIsEntityLifecycleSystem(accessFieldClassSymbol, filedSymbol.ContainingType))
  79. {
  80. return;
  81. }
  82. //判断是否在实体类的友元类中
  83. if (this.CheckIsEntityFriendOf(accessFieldClassSymbol, filedSymbol.ContainingType))
  84. {
  85. return;
  86. }
  87. var builder = ImmutableDictionary.CreateBuilder<string, string?>();
  88. builder.Add("FriendOfType",filedSymbol.ContainingType.ToString());
  89. Diagnostic diagnostic = Diagnostic.Create(Rule, memberAccessExpressionSyntax.GetLocation(), builder.ToImmutable(),filedSymbol.ContainingType.ToString(),
  90. filedSymbol.Name);
  91. context.ReportDiagnostic(diagnostic);
  92. }
  93. private bool CheckIsEntityLifecycleSystem(INamedTypeSymbol accessFieldClassSymbol, INamedTypeSymbol entityTypeSymbol)
  94. {
  95. if (accessFieldClassSymbol.BaseType == null || !accessFieldClassSymbol.BaseType.IsGenericType)
  96. {
  97. return false;
  98. }
  99. // 判断是否含有 ObjectSystem Attribute 且继承了接口 ISystemType
  100. if (accessFieldClassSymbol.BaseType.HasAttribute(Definition.ObjectSystemAttribute) && accessFieldClassSymbol.HasInterface(Definition.ISystemType))
  101. {
  102. // 获取 accessFieldClassSymbol 父类的实体类型参数
  103. ITypeSymbol? entityTypeArgumentSymbol = accessFieldClassSymbol.BaseType.TypeArguments.FirstOrDefault();
  104. if (entityTypeArgumentSymbol == null)
  105. {
  106. return false;
  107. }
  108. // 判断 accessFieldClassSymbol 父类的实体类型参数是否为 entityTypeSymbol
  109. if (entityTypeArgumentSymbol.ToString() == entityTypeSymbol.ToString())
  110. {
  111. return true;
  112. }
  113. }
  114. return false;
  115. }
  116. private bool CheckIsEntityFriendOf(INamedTypeSymbol accessFieldTypeSymbol, INamedTypeSymbol entityTypeSymbol)
  117. {
  118. var attributes = accessFieldTypeSymbol.GetAttributes();
  119. foreach (AttributeData? attributeData in attributes)
  120. {
  121. if (attributeData.AttributeClass?.ToString() != Definition.FriendOfAttribute)
  122. {
  123. continue;
  124. }
  125. if (!(attributeData.ConstructorArguments[0].Value is INamedTypeSymbol namedTypeSymbol))
  126. {
  127. continue;
  128. }
  129. if (namedTypeSymbol.ToString() == entityTypeSymbol.ToString())
  130. {
  131. return true;
  132. }
  133. }
  134. return false;
  135. }
  136. }
  137. }