001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2018 the original author or authors.
004//
005// This library is free software; you can redistribute it and/or
006// modify it under the terms of the GNU Lesser General Public
007// License as published by the Free Software Foundation; either
008// version 2.1 of the License, or (at your option) any later version.
009//
010// This library is distributed in the hope that it will be useful,
011// but WITHOUT ANY WARRANTY; without even the implied warranty of
012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013// Lesser General Public License for more details.
014//
015// You should have received a copy of the GNU Lesser General Public
016// License along with this library; if not, write to the Free Software
017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
018////////////////////////////////////////////////////////////////////////////////
019
020package com.puppycrawl.tools.checkstyle.checks.naming;
021
022import com.puppycrawl.tools.checkstyle.api.DetailAST;
023import com.puppycrawl.tools.checkstyle.api.TokenTypes;
024
025/**
026 * Abstract class for checking a class member (field/method)'s name conforms to
027 * a format specified by the format property.
028 *
029 * <p>
030 * This class extends {@link AbstractNameCheck} with support for access level
031 * restrictions. This allows the check to be configured to be applied to one of
032 * the four Java access levels: {@code public}, {@code protected},
033 * {@code "package"}, and {@code private}.
034 * </p>
035 *
036 * <p>Level is configured using the following properties:
037 * <ol>
038 * <li>applyToPublic, default true;</li>
039 * <li>applyToProtected, default true;</li>
040 * <li>applyToPackage, default true;</li>
041 * <li>applyToPrivate, default true;</li>
042 * </ol>
043 *
044 *
045 * @author Rick Giles
046 */
047public abstract class AbstractAccessControlNameCheck
048    extends AbstractNameCheck {
049
050    /** If true, applies the check be public members. */
051    private boolean applyToPublic = true;
052
053    /** If true, applies the check be protected members. */
054    private boolean applyToProtected = true;
055
056    /** If true, applies the check be "package" members. */
057    private boolean applyToPackage = true;
058
059    /** If true, applies the check be private members. */
060    private boolean applyToPrivate = true;
061
062    /**
063     * Creates a new {@code AbstractAccessControlNameCheck} instance.
064     *
065     * @param format
066     *                format to check with
067     */
068    protected AbstractAccessControlNameCheck(String format) {
069        super(format);
070    }
071
072    @Override
073    protected boolean mustCheckName(DetailAST ast) {
074        return shouldCheckInScope(ast.findFirstToken(TokenTypes.MODIFIERS));
075    }
076
077    /**
078     * Should we check member with given modifiers.
079     *
080     * @param modifiers
081     *                modifiers of member to check.
082     * @return true if we should check such member.
083     */
084    protected boolean shouldCheckInScope(DetailAST modifiers) {
085        final boolean isPublic = modifiers
086                .findFirstToken(TokenTypes.LITERAL_PUBLIC) != null;
087        final boolean isProtected = modifiers
088                .findFirstToken(TokenTypes.LITERAL_PROTECTED) != null;
089        final boolean isPrivate = modifiers
090                .findFirstToken(TokenTypes.LITERAL_PRIVATE) != null;
091        final boolean isPackage = !(isPublic || isProtected || isPrivate);
092
093        return applyToPublic && isPublic
094                || applyToProtected && isProtected
095                || applyToPackage && isPackage
096                || applyToPrivate && isPrivate;
097    }
098
099    /**
100     * Sets whether we should apply the check to public members.
101     *
102     * @param applyTo new value of the property.
103     */
104    public void setApplyToPublic(boolean applyTo) {
105        applyToPublic = applyTo;
106    }
107
108    /**
109     * Sets whether we should apply the check to protected members.
110     *
111     * @param applyTo new value of the property.
112     */
113    public void setApplyToProtected(boolean applyTo) {
114        applyToProtected = applyTo;
115    }
116
117    /**
118     * Sets whether we should apply the check to package-private members.
119     *
120     * @param applyTo new value of the property.
121     */
122    public void setApplyToPackage(boolean applyTo) {
123        applyToPackage = applyTo;
124    }
125
126    /**
127     * Sets whether we should apply the check to private members.
128     *
129     * @param applyTo new value of the property.
130     */
131    public void setApplyToPrivate(boolean applyTo) {
132        applyToPrivate = applyTo;
133    }
134
135}