11using System.Collections.Generic;
24 internal static class FieldDefinitionsExtractor
33 public static IReadOnlyList<ExtractedFieldDefinition> ExtractFieldDefinitions(DefinitionExtractionContext extractionContext, TypeDeclarationSyntax targetTypeDeclaration)
35 List<ExtractedFieldDefinition> propertyDefinitions =
new List<ExtractedFieldDefinition>();
36 ExtractedFieldDefinition fieldDefinition;
37 IReadOnlyList<FieldDeclarationSyntax> serializableFields;
39 serializableFields = GetSerializableFieldDeclarations(extractionContext, targetTypeDeclaration);
40 foreach (FieldDeclarationSyntax fieldDeclaration
in serializableFields)
42 fieldDefinition = FieldDefinitionExtractor.ExtractFieldDefinition(extractionContext, fieldDeclaration);
43 propertyDefinitions.Add(fieldDefinition);
46 return propertyDefinitions;
49 #region Private Helper Methods
57 private static IReadOnlyList<FieldDeclarationSyntax> GetSerializableFieldDeclarations(DefinitionExtractionContext extractionContext, TypeDeclarationSyntax targetTypeDeclaration)
59 List<FieldDeclarationSyntax> serializableFields;
60 List<FieldDeclarationSyntax> optedInSerializableFields;
63 serializableFields = GetPublicNonExcludedFields(extractionContext, targetTypeDeclaration);
66 optedInSerializableFields = GetNonPublicIncludedFields(extractionContext, targetTypeDeclaration);
67 serializableFields.AddRange(optedInSerializableFields);
70 return serializableFields;
79 private static List<FieldDeclarationSyntax> GetPublicNonExcludedFields(DefinitionExtractionContext extractionContext, TypeDeclarationSyntax targetTypeDeclaration)
81 List<FieldDeclarationSyntax> serializableFields;
84 serializableFields = targetTypeDeclaration.Members.Where(
85 m => m is FieldDeclarationSyntax fieldDeclaration &&
86 HasOneOfScopes(extractionContext, fieldDeclaration,
"public") &&
87 !extractionContext.IsFieldDecoratedWithAutoNonSerialized(fieldDeclaration))
88 .Cast<FieldDeclarationSyntax>()
91 return serializableFields;
100 private static List<FieldDeclarationSyntax> GetNonPublicIncludedFields(DefinitionExtractionContext extractionContext, TypeDeclarationSyntax targetTypeDeclaration)
102 List<FieldDeclarationSyntax> serializableFields;
105 serializableFields = targetTypeDeclaration.Members.Where(
106 m => m is FieldDeclarationSyntax fieldDeclaration &&
107 !HasOneOfScopes(extractionContext, fieldDeclaration,
"public") &&
108 extractionContext.IsFieldDecoratedWithAutoSerialized(fieldDeclaration))
109 .Cast<FieldDeclarationSyntax>()
112 return serializableFields;
121 private static List<FieldDeclarationSyntax> GetAllIncludedFields(DefinitionExtractionContext extractionContext, TypeDeclarationSyntax targetTypeDeclaration)
123 List<FieldDeclarationSyntax> serializableFields;
126 serializableFields = targetTypeDeclaration.Members.Where(
127 m => m is FieldDeclarationSyntax fieldDeclaration &&
128 extractionContext.IsFieldDecoratedWithAutoSerialized(fieldDeclaration))
129 .Cast<FieldDeclarationSyntax>()
132 return serializableFields;
142 private static bool HasOneOfScopes(DefinitionExtractionContext context, FieldDeclarationSyntax fieldDeclaration, params
string[] scopes)
144 foreach (
string scope
in scopes)
146 if (fieldDeclaration.Modifiers.Any(m => m.ValueText.Equals(scope, StringComparison.InvariantCultureIgnoreCase)))