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 mApplyToPublic = true;
053
054    /** If true, applies the check be protected members. */
055    private boolean mApplyToProtected = true;
056
057    /** If true, applies the check be "package" members. */
058    private boolean mApplyToPackage = true;
059
060    /** If true, applies the check be private members. */
061    private boolean mApplyToPrivate = true;
062
063    /**
064     * Creates a new {@code AbstractAccessControlNameCheck} instance.
065     *
066     * @param aFormat
067     *                format to check with
068     */
069    public AbstractAccessControlNameCheck(String aFormat)
070    {
071        super(aFormat);
072    }
073
074    @Override
075    protected boolean mustCheckName(DetailAST aAST)
076    {
077        return shouldCheckInScope(aAST);
078    }
079
080    /**
081     * Should we check member with given modifiers.
082     *
083     * @param aModifiers
084     *                modifiers of member to check.
085     * @return true if we should check such member.
086     */
087    protected boolean shouldCheckInScope(DetailAST aModifiers)
088    {
089        if (aModifiers == null) {
090            // if there are no modifiers it is a package-private
091            return mApplyToPackage;
092        }
093
094        final boolean isPublic = aModifiers
095                .branchContains(TokenTypes.LITERAL_PUBLIC);
096        final boolean isProtected = aModifiers
097                .branchContains(TokenTypes.LITERAL_PROTECTED);
098        final boolean isPrivate = aModifiers
099                .branchContains(TokenTypes.LITERAL_PRIVATE);
100        final boolean isPackage = !(isPublic || isProtected || isPrivate);
101
102        return (mApplyToPublic && isPublic)
103                || (mApplyToProtected && isProtected)
104                || (mApplyToPackage && isPackage)
105                || (mApplyToPrivate && isPrivate);
106    }
107
108    /**
109     * Sets whether we should apply the check to public members.
110     *
111     * @param aApplyTo new value of the property.
112     */
113    public void setApplyToPublic(boolean aApplyTo)
114    {
115        mApplyToPublic = aApplyTo;
116    }
117
118    /** @return true if the check should be applied to public members. */
119    public boolean getApplyToPublic()
120    {
121        return mApplyToPublic;
122    }
123
124    /**
125     * Sets whether we should apply the check to protected members.
126     *
127     * @param aApplyTo new value of the property.
128     */
129    public void setApplyToProtected(boolean aApplyTo)
130    {
131        mApplyToProtected = aApplyTo;
132    }
133
134    /** @return true if the check should be applied to protected members. */
135    public boolean getApplyToProtected()
136    {
137        return mApplyToProtected;
138    }
139
140    /**
141     * Sets whether we should apply the check to package-private members.
142     *
143     * @param aApplyTo new value of the property.
144     */
145    public void setApplyToPackage(boolean aApplyTo)
146    {
147        mApplyToPackage = aApplyTo;
148    }
149
150    /**
151     * @return true if the check should be applied to package-private members.
152     */
153    public boolean getApplyToPackage()
154    {
155        return mApplyToPackage;
156    }
157
158    /**
159     * Sets whether we should apply the check to private members.
160     *
161     * @param aApplyTo new value of the property.
162     */
163    public void setApplyToPrivate(boolean aApplyTo)
164    {
165        mApplyToPrivate = aApplyTo;
166    }
167
168    /** @return true if the check should be applied to private members. */
169    public boolean getApplyToPrivate()
170    {
171        return mApplyToPrivate;
172    }
173}