001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2014  Oliver Burn
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////////////////////////////////////////////////////////////////////////////////
019package com.puppycrawl.tools.checkstyle.checks.naming;
020
021import com.puppycrawl.tools.checkstyle.api.DetailAST;
022import com.puppycrawl.tools.checkstyle.api.TokenTypes;
023
024/**
025 * Abstract class for checking a class member (field/method)'s name conforms to
026 * a format specified by the format property.
027 *
028 * <p>
029 * This class extends {@link AbstractNameCheck} with support for access level
030 * restrictions. This allows the check to be configured to be applied to one of
031 * the four Java access levels: {@code public}, {@code protected},
032 * {@code "package"}, and {@code private}.
033 * </p>
034 *
035 * <p>
036 * 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 * @version 1.0
047 */
048public abstract class AbstractAccessControlNameCheck
049    extends AbstractNameCheck
050{
051    /** If true, applies the check be public members. */
052    private boolean applyToPublic = true;
053
054    /** If true, applies the check be protected members. */
055    private boolean applyToProtected = true;
056
057    /** If true, applies the check be "package" members. */
058    private boolean applyToPackage = true;
059
060    /** If true, applies the check be private members. */
061    private boolean applyToPrivate = true;
062
063    /**
064     * Creates a new {@code AbstractAccessControlNameCheck} instance.
065     *
066     * @param format
067     *                format to check with
068     */
069    public AbstractAccessControlNameCheck(String format)
070    {
071        super(format);
072    }
073
074    @Override
075    protected boolean mustCheckName(DetailAST ast)
076    {
077        return shouldCheckInScope(ast);
078    }
079
080    /**
081     * Should we check member with given modifiers.
082     *
083     * @param modifiers
084     *                modifiers of member to check.
085     * @return true if we should check such member.
086     */
087    protected boolean shouldCheckInScope(DetailAST modifiers)
088    {
089        if (modifiers == null) {
090            // if there are no modifiers it is a package-private
091            return applyToPackage;
092        }
093
094        final boolean isPublic = modifiers
095                .branchContains(TokenTypes.LITERAL_PUBLIC);
096        final boolean isProtected = modifiers
097                .branchContains(TokenTypes.LITERAL_PROTECTED);
098        final boolean isPrivate = modifiers
099                .branchContains(TokenTypes.LITERAL_PRIVATE);
100        final boolean isPackage = !(isPublic || isProtected || isPrivate);
101
102        return (applyToPublic && isPublic)
103                || (applyToProtected && isProtected)
104                || (applyToPackage && isPackage)
105                || (applyToPrivate && isPrivate);
106    }
107
108    /**
109     * Sets whether we should apply the check to public members.
110     *
111     * @param applyTo new value of the property.
112     */
113    public void setApplyToPublic(boolean applyTo)
114    {
115        applyToPublic = applyTo;
116    }
117
118    /** @return true if the check should be applied to public members. */
119    public boolean getApplyToPublic()
120    {
121        return applyToPublic;
122    }
123
124    /**
125     * Sets whether we should apply the check to protected members.
126     *
127     * @param applyTo new value of the property.
128     */
129    public void setApplyToProtected(boolean applyTo)
130    {
131        applyToProtected = applyTo;
132    }
133
134    /** @return true if the check should be applied to protected members. */
135    public boolean getApplyToProtected()
136    {
137        return applyToProtected;
138    }
139
140    /**
141     * Sets whether we should apply the check to package-private members.
142     *
143     * @param applyTo new value of the property.
144     */
145    public void setApplyToPackage(boolean applyTo)
146    {
147        applyToPackage = applyTo;
148    }
149
150    /**
151     * @return true if the check should be applied to package-private members.
152     */
153    public boolean getApplyToPackage()
154    {
155        return applyToPackage;
156    }
157
158    /**
159     * Sets whether we should apply the check to private members.
160     *
161     * @param applyTo new value of the property.
162     */
163    public void setApplyToPrivate(boolean applyTo)
164    {
165        applyToPrivate = applyTo;
166    }
167
168    /** @return true if the check should be applied to private members. */
169    public boolean getApplyToPrivate()
170    {
171        return applyToPrivate;
172    }
173}