View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  ///////////////////////////////////////////////////////////////////////////////////////////////
19  
20  package com.puppycrawl.tools.checkstyle.checks.modifier;
21  
22  import com.puppycrawl.tools.checkstyle.StatelessCheck;
23  import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
24  import com.puppycrawl.tools.checkstyle.api.DetailAST;
25  import com.puppycrawl.tools.checkstyle.api.Scope;
26  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
27  import com.puppycrawl.tools.checkstyle.utils.ScopeUtil;
28  
29  /**
30   * <div>
31   * Checks for implicit modifiers on nested types in classes and records.
32   * </div>
33   *
34   * <p>
35   * This check is effectively the opposite of
36   * <a href="https://checkstyle.org/checks/modifier/redundantmodifier.html#RedundantModifier">
37   * RedundantModifier</a>.
38   * It checks the modifiers on nested types in classes and records, ensuring that certain modifiers
39   * are explicitly specified even though they are actually redundant.
40   * </p>
41   *
42   * <p>
43   * Nested enums, interfaces, and records within a class are always {@code static} and as such the
44   * compiler does not require the {@code static} modifier. This check provides the ability to enforce
45   * that the {@code static} modifier is explicitly coded and not implicitly added by the compiler.
46   * </p>
47   * <pre>
48   * public final class Person {
49   *   enum Age {  // violation
50   *     CHILD, ADULT
51   *   }
52   * }
53   * </pre>
54   *
55   * <p>
56   * Rationale for this check: Nested enums, interfaces, and records are treated differently from
57   * nested classes as they are only allowed to be {@code static}. Developers should not need to
58   * remember this rule, and this check provides the means to enforce that the modifier is coded
59   * explicitly.
60   * </p>
61   * <ul>
62   * <li>
63   * Property {@code violateImpliedStaticOnNestedEnum} - Control whether to enforce that
64   * {@code static} is explicitly coded on nested enums in classes and records.
65   * Type is {@code boolean}.
66   * Default value is {@code true}.
67   * </li>
68   * <li>
69   * Property {@code violateImpliedStaticOnNestedInterface} - Control whether to enforce that
70   * {@code static} is explicitly coded on nested interfaces in classes and records.
71   * Type is {@code boolean}.
72   * Default value is {@code true}.
73   * </li>
74   * <li>
75   * Property {@code violateImpliedStaticOnNestedRecord} - Control whether to enforce that
76   * {@code static} is explicitly coded on nested records in classes and records.
77   * Type is {@code boolean}.
78   * Default value is {@code true}.
79   * </li>
80   * </ul>
81   *
82   * <p>
83   * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
84   * </p>
85   *
86   * <p>
87   * Violation Message Keys:
88   * </p>
89   * <ul>
90   * <li>
91   * {@code class.implied.modifier}
92   * </li>
93   * </ul>
94   *
95   * @since 8.16
96   */
97  @StatelessCheck
98  public class ClassMemberImpliedModifierCheck
99      extends AbstractCheck {
100 
101     /**
102      * A key is pointing to the warning message text in "messages.properties" file.
103      */
104     public static final String MSG_KEY = "class.implied.modifier";
105 
106     /** Name for 'static' keyword. */
107     private static final String STATIC_KEYWORD = "static";
108 
109     /**
110      * Control whether to enforce that {@code static} is explicitly coded
111      * on nested enums in classes and records.
112      */
113     private boolean violateImpliedStaticOnNestedEnum = true;
114 
115     /**
116      * Control whether to enforce that {@code static} is explicitly coded
117      * on nested interfaces in classes and records.
118      */
119     private boolean violateImpliedStaticOnNestedInterface = true;
120 
121     /**
122      * Control whether to enforce that {@code static} is explicitly coded
123      * on nested records in classes and records.
124      */
125     private boolean violateImpliedStaticOnNestedRecord = true;
126 
127     /**
128      * Setter to control whether to enforce that {@code static} is explicitly coded
129      * on nested enums in classes and records.
130      *
131      * @param violateImplied
132      *        True to perform the check, false to turn the check off.
133      * @since 8.16
134      */
135     public void setViolateImpliedStaticOnNestedEnum(boolean violateImplied) {
136         violateImpliedStaticOnNestedEnum = violateImplied;
137     }
138 
139     /**
140      * Setter to control whether to enforce that {@code static} is explicitly coded
141      * on nested interfaces in classes and records.
142      *
143      * @param violateImplied
144      *        True to perform the check, false to turn the check off.
145      * @since 8.16
146      */
147     public void setViolateImpliedStaticOnNestedInterface(boolean violateImplied) {
148         violateImpliedStaticOnNestedInterface = violateImplied;
149     }
150 
151     /**
152      * Setter to control whether to enforce that {@code static} is explicitly coded
153      * on nested records in classes and records.
154      *
155      * @param violateImplied
156      *        True to perform the check, false to turn the check off.
157      * @since 8.36
158      */
159     public void setViolateImpliedStaticOnNestedRecord(boolean violateImplied) {
160         violateImpliedStaticOnNestedRecord = violateImplied;
161     }
162 
163     @Override
164     public int[] getDefaultTokens() {
165         return getAcceptableTokens();
166     }
167 
168     @Override
169     public int[] getRequiredTokens() {
170         return getAcceptableTokens();
171     }
172 
173     @Override
174     public int[] getAcceptableTokens() {
175         return new int[] {
176             TokenTypes.INTERFACE_DEF,
177             TokenTypes.ENUM_DEF,
178             TokenTypes.RECORD_DEF,
179         };
180     }
181 
182     @Override
183     public void visitToken(DetailAST ast) {
184         if (isInTypeBlock(ast)) {
185             final DetailAST modifiers = ast.findFirstToken(TokenTypes.MODIFIERS);
186             switch (ast.getType()) {
187                 case TokenTypes.ENUM_DEF:
188                     if (violateImpliedStaticOnNestedEnum
189                             && modifiers.findFirstToken(TokenTypes.LITERAL_STATIC) == null) {
190                         log(ast, MSG_KEY, STATIC_KEYWORD);
191                     }
192                     break;
193                 case TokenTypes.INTERFACE_DEF:
194                     if (violateImpliedStaticOnNestedInterface
195                             && modifiers.findFirstToken(TokenTypes.LITERAL_STATIC) == null) {
196                         log(ast, MSG_KEY, STATIC_KEYWORD);
197                     }
198                     break;
199                 case TokenTypes.RECORD_DEF:
200                     if (violateImpliedStaticOnNestedRecord
201                             && modifiers.findFirstToken(TokenTypes.LITERAL_STATIC) == null) {
202                         log(ast, MSG_KEY, STATIC_KEYWORD);
203                     }
204                     break;
205                 default:
206                     throw new IllegalStateException(ast.toString());
207             }
208         }
209     }
210 
211     /**
212      * Checks if ast is in a class, enum, anon class or record block.
213      *
214      * @param ast the current ast
215      * @return true if ast is in a class, enum, anon class or record
216      */
217     private static boolean isInTypeBlock(DetailAST ast) {
218         return ScopeUtil.isInScope(ast, Scope.ANONINNER)
219                 || ScopeUtil.isInClassBlock(ast)
220                 || ScopeUtil.isInEnumBlock(ast)
221                 || ScopeUtil.isInRecordBlock(ast);
222     }
223 
224 }