View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2026 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.naming;
21  
22  import com.puppycrawl.tools.checkstyle.api.DetailAST;
23  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
24  import com.puppycrawl.tools.checkstyle.utils.ScopeUtil;
25  
26  /**
27   * Abstract class for checking a class member (field/method)'s name conforms to a specified pattern.
28   *
29   * <p>
30   * This class extends {@link AbstractNameCheck} with support for access level
31   * restrictions. This allows the check to be configured to be applied to one of
32   * the four Java access levels: {@code public}, {@code protected},
33   * {@code "package"}, and {@code private}.
34   * </p>
35   *
36   * <p>Level is configured using the following properties:
37   * <ol>
38   * <li>applyToPublic, default true;</li>
39   * <li>applyToProtected, default true;</li>
40   * <li>applyToPackage, default true;</li>
41   * <li>applyToPrivate, default true;</li>
42   * </ol>
43   *
44   */
45  public abstract class AbstractAccessControlNameCheck extends AbstractNameCheck {
46  
47      /** If true, applies the check be public members. */
48      private boolean applyToPublic = true;
49  
50      /** If true, applies the check be protected members. */
51      private boolean applyToProtected = true;
52  
53      /** If true, applies the check be "package" members. */
54      private boolean applyToPackage = true;
55  
56      /** If true, applies the check be private members. */
57      private boolean applyToPrivate = true;
58  
59      /**
60       * Creates a new {@code AbstractAccessControlNameCheck} instance.
61       *
62       * @param format
63       *                format to check with
64       * @param messageKey
65       *                the key for the message
66       */
67      protected AbstractAccessControlNameCheck(String format, String messageKey) {
68          super(format, messageKey);
69      }
70  
71      @Override
72      protected boolean mustCheckName(DetailAST ast) {
73          return shouldCheckInScope(ast.findFirstToken(TokenTypes.MODIFIERS));
74      }
75  
76      /**
77       * Should we check member with given modifiers.
78       *
79       * @param modifiers
80       *                modifiers of member to check.
81       * @return true if we should check such member.
82       */
83      protected boolean shouldCheckInScope(DetailAST modifiers) {
84          final boolean isProtected = modifiers
85                  .findFirstToken(TokenTypes.LITERAL_PROTECTED) != null;
86          final boolean isPrivate = modifiers
87                  .findFirstToken(TokenTypes.LITERAL_PRIVATE) != null;
88          final boolean isPublic = isPublic(modifiers);
89  
90          final boolean isPackage = !(isPublic || isProtected || isPrivate);
91  
92          return applyToPublic && isPublic
93                  || applyToProtected && isProtected
94                  || applyToPackage && isPackage
95                  || applyToPrivate && isPrivate;
96      }
97  
98      /**
99       * Checks if given modifiers has public access.
100      * There are 2 cases - it is either has explicit modifier, or it is in annotation or interface.
101      *
102      * @param modifiers - modifiers to check
103      * @return true if public
104      */
105     private static boolean isPublic(DetailAST modifiers) {
106         return modifiers.findFirstToken(TokenTypes.LITERAL_PUBLIC) != null
107                 || ScopeUtil.isInAnnotationBlock(modifiers)
108                 || ScopeUtil.isInInterfaceBlock(modifiers)
109                     // interface methods can be private
110                     && modifiers.findFirstToken(TokenTypes.LITERAL_PRIVATE) == null;
111     }
112 
113     /**
114      * Setter to control if check should apply to public members.
115      *
116      * @param applyTo new value of the property.
117      */
118     public void setApplyToPublic(boolean applyTo) {
119         applyToPublic = applyTo;
120     }
121 
122     /**
123      * Setter to control if check should apply to protected members.
124      *
125      * @param applyTo new value of the property.
126      */
127     public void setApplyToProtected(boolean applyTo) {
128         applyToProtected = applyTo;
129     }
130 
131     /**
132      * Setter to control if check should apply to package-private members.
133      *
134      * @param applyTo new value of the property.
135      */
136     public void setApplyToPackage(boolean applyTo) {
137         applyToPackage = applyTo;
138     }
139 
140     /**
141      * Setter to control if check should apply to private members.
142      *
143      * @param applyTo new value of the property.
144      */
145     public void setApplyToPrivate(boolean applyTo) {
146         applyToPrivate = applyTo;
147     }
148 
149 }