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