PageRenderTime 25ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/Project.AutoFix/Src/AddIns/CSharp/CodeModel/Elements/Field.cs

#
C# | 202 lines | 109 code | 33 blank | 60 comment | 6 complexity | fb0cd5360ff1fc8612d563384a9d4ef6 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. //-----------------------------------------------------------------------
  2. // <copyright file="Field.cs">
  3. // MS-PL
  4. // </copyright>
  5. // <license>
  6. // This source code is subject to terms and conditions of the Microsoft
  7. // Public License. A copy of the license can be found in the License.html
  8. // file at the root of this distribution.
  9. // By using this source code in any fashion, you are agreeing to be bound
  10. // by the terms of the Microsoft Public License. You must not remove this
  11. // notice, or any other, from this software.
  12. // </license>
  13. //-----------------------------------------------------------------------
  14. namespace StyleCop.CSharp.CodeModel
  15. {
  16. using System.Collections.Generic;
  17. using System.Diagnostics.CodeAnalysis;
  18. /// <summary>
  19. /// Describes a field within a class or struct.
  20. /// </summary>
  21. /// <subcategory>element</subcategory>
  22. public sealed partial class Field : Element
  23. {
  24. #region Private Fields
  25. /// <summary>
  26. /// The type of the field.
  27. /// </summary>
  28. private CodeUnitProperty<TypeToken> fieldType;
  29. /// <summary>
  30. /// Indicates whether the item is declared const.
  31. /// </summary>
  32. private CodeUnitProperty<bool> isConst;
  33. /// <summary>
  34. /// Indicates whether the item is declared readonly.
  35. /// </summary>
  36. private CodeUnitProperty<bool> isReadOnly;
  37. /// <summary>
  38. /// The statement within the field.
  39. /// </summary>
  40. private CodeUnitProperty<VariableDeclarationStatement> statement;
  41. #endregion Private Fields
  42. #region Internal Constructors
  43. /// <summary>
  44. /// Initializes a new instance of the Field class.
  45. /// </summary>
  46. /// <param name="proxy">Proxy object for the field.</param>
  47. /// <param name="name">The name of the field.</param>
  48. /// <param name="attributes">The list of attributes attached to this element.</param>
  49. /// <param name="fieldType">The type of the field.</param>
  50. /// <param name="unsafeCode">Indicates whether the element resides within a block of unsafe code.</param>
  51. internal Field(CodeUnitProxy proxy, string name, ICollection<Attribute> attributes, TypeToken fieldType, bool unsafeCode)
  52. : base(proxy, ElementType.Field, name, attributes, unsafeCode)
  53. {
  54. Param.AssertNotNull(proxy, "proxy");
  55. Param.AssertValidString(name, "name");
  56. Param.Ignore(attributes);
  57. Param.AssertNotNull(fieldType, "fieldType");
  58. Param.Ignore(unsafeCode);
  59. this.fieldType.Value = fieldType;
  60. }
  61. #endregion Internal Constructors
  62. #region Public Properties
  63. /// <summary>
  64. /// Gets a value indicating whether the field is declared const.
  65. /// </summary>
  66. public bool Const
  67. {
  68. get
  69. {
  70. this.ValidateEditVersion();
  71. if (!this.isConst.Initialized)
  72. {
  73. this.isConst.Value = this.ContainsModifier(TokenType.Const);
  74. }
  75. return this.isConst.Value;
  76. }
  77. }
  78. /// <summary>
  79. /// Gets a value indicating whether the field is declared readonly.
  80. /// </summary>
  81. [SuppressMessage(
  82. "Microsoft.Naming",
  83. "CA1702:CompoundWordsShouldBeCasedCorrectly",
  84. MessageId = "Readonly",
  85. Justification = "API has already been published and should not be changed.")]
  86. public bool Readonly
  87. {
  88. get
  89. {
  90. this.ValidateEditVersion();
  91. if (!this.isReadOnly.Initialized)
  92. {
  93. this.isReadOnly.Value = this.ContainsModifier(TokenType.Readonly);
  94. }
  95. return this.isReadOnly.Value;
  96. }
  97. }
  98. /// <summary>
  99. /// Gets the type of the field.
  100. /// </summary>
  101. public TypeToken FieldType
  102. {
  103. get
  104. {
  105. this.ValidateEditVersion();
  106. if (!this.fieldType.Initialized)
  107. {
  108. this.fieldType.Value = this.FindFirstChild<TypeToken>();
  109. }
  110. return this.fieldType.Value;
  111. }
  112. }
  113. /// <summary>
  114. /// Gets the variable declaration statement within this field.
  115. /// </summary>
  116. public VariableDeclarationStatement VariableDeclarationStatement
  117. {
  118. get
  119. {
  120. this.ValidateEditVersion();
  121. if (!this.statement.Initialized)
  122. {
  123. this.statement.Value = this.FindNext<VariableDeclarationStatement>();
  124. }
  125. return this.statement.Value;
  126. }
  127. }
  128. #endregion Public Properties
  129. #region Protected Override Properties
  130. /// <summary>
  131. /// Gets the collection of modifiers allowed on this element.
  132. /// </summary>
  133. protected override IEnumerable<string> AllowedModifiers
  134. {
  135. get
  136. {
  137. return CodeParser.FieldModifiers;
  138. }
  139. }
  140. #endregion Protected Override Properties
  141. #region Protected Override Methods
  142. /// <summary>
  143. /// Gets the name of the element.
  144. /// </summary>
  145. /// <returns>The name of the element.</returns>
  146. protected override string GetElementName()
  147. {
  148. // For a field, the name of the first variable declarator is the name of the field.
  149. VariableDeclaratorExpression declarator = this.FindFirstDescendent<VariableDeclaratorExpression>();
  150. if (declarator != null)
  151. {
  152. return declarator.Identifier.Text;
  153. }
  154. throw new SyntaxException(this.Document, this.LineNumber);
  155. }
  156. /// <summary>
  157. /// Resets the contents of the class.
  158. /// </summary>
  159. protected override void Reset()
  160. {
  161. base.Reset();
  162. this.fieldType.Reset();
  163. this.isConst.Reset();
  164. this.isReadOnly.Reset();
  165. this.statement.Reset();
  166. }
  167. #endregion Protected Override Methods
  168. }
  169. }