EntityFiledAccessAnalyzer.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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.AllModelHotfix))
  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 && filedSymbol.ContainingType.BaseType?.ToString() != Definition.LSEntityType)
  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() is Definition.EntityType or Definition.LSEntityType)
  69. {
  70. return;
  71. }
  72. // 允许类内部访问字段
  73. if (accessFieldClassSymbol.ToString()== filedSymbol.ContainingType.ToString() )
  74. {
  75. return;
  76. }
  77. //判断是否在实体类生命周期System中, 这里做了修改,周期System也不允许
  78. //判断是否在实体类的友元类中
  79. if (this.CheckIsEntityFriendOf(accessFieldClassSymbol, filedSymbol.ContainingType))
  80. {
  81. return;
  82. }
  83. var builder = ImmutableDictionary.CreateBuilder<string, string?>();
  84. builder.Add("FriendOfType",filedSymbol.ContainingType.ToString());
  85. Diagnostic diagnostic = Diagnostic.Create(Rule, memberAccessExpressionSyntax.GetLocation(), builder.ToImmutable(),filedSymbol.ContainingType.ToString(),
  86. filedSymbol.Name);
  87. context.ReportDiagnostic(diagnostic);
  88. }
  89. private bool CheckIsEntityFriendOf(INamedTypeSymbol accessFieldTypeSymbol, INamedTypeSymbol entityTypeSymbol)
  90. {
  91. var attributes = accessFieldTypeSymbol.GetAttributes();
  92. foreach (AttributeData? attributeData in attributes)
  93. {
  94. if (attributeData.AttributeClass?.ToString() != Definition.FriendOfAttribute)
  95. {
  96. continue;
  97. }
  98. if (!(attributeData.ConstructorArguments[0].Value is INamedTypeSymbol namedTypeSymbol))
  99. {
  100. continue;
  101. }
  102. if (namedTypeSymbol.ToString() == entityTypeSymbol.ToString())
  103. {
  104. return true;
  105. }
  106. }
  107. return false;
  108. }
  109. }
  110. }