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 java.util.regex.Pattern;
023
024import com.puppycrawl.tools.checkstyle.StatelessCheck;
025import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
026import com.puppycrawl.tools.checkstyle.api.DetailAST;
027import com.puppycrawl.tools.checkstyle.api.TokenTypes;
028
029/**
030 * <p>
031 * Ensures that the names of abstract classes conforming to some regular
032 * expression and check that {@code abstract} modifier exists.
033 * </p>
034 * <p>
035 * Rationale: Abstract classes are convenience base class implementations of
036 * interfaces, not types as such. As such they should be named to indicate this.
037 * Also if names of classes starts with 'Abstract' it's very convenient that
038 * they will have abstract modifier.
039 * </p>
040 * <ul>
041 * <li>
042 * Property {@code format} - Specify valid identifiers. Default value is
043 * {@code "^Abstract.+$"}.</li>
044 * <li>
045 * Property {@code ignoreModifier} - Control whether to ignore checking for the
046 * {@code abstract} modifier on classes that match the name. Default value is
047 * {@code false}.</li>
048 * <li>
049 * Property {@code ignoreName} - Control whether to ignore checking the name.
050 * Realistically only useful if using the check to identify that match name and
051 * do not have the {@code abstract} modifier. Default value is
052 * {@code false}.</li>
053 * </ul>
054 * <p>
055 * The following example shows how to configure the {@code AbstractClassName} to
056 * checks names, but ignore missing {@code abstract} modifiers:
057 * </p>
058 *
059 * <pre>
060 * &lt;module name="AbstractClassName"&gt;
061 *   &lt;property name="ignoreModifier" value="true"/&gt;
062 * &lt;/module&gt;
063 * </pre>
064 *
065 * @since 3.2
066 */
067@StatelessCheck
068public final class AbstractClassNameCheck extends AbstractCheck {
069
070    /**
071     * A key is pointing to the warning message text in "messages.properties"
072     * file.
073     */
074    public static final String MSG_ILLEGAL_ABSTRACT_CLASS_NAME = "illegal.abstract.class.name";
075
076    /**
077     * A key is pointing to the warning message text in "messages.properties"
078     * file.
079     */
080    public static final String MSG_NO_ABSTRACT_CLASS_MODIFIER = "no.abstract.class.modifier";
081
082    /**
083     * Control whether to ignore checking for the {@code abstract} modifier on
084     * classes that match the name.
085     */
086    private boolean ignoreModifier;
087
088    /**
089     * Control whether to ignore checking the name. Realistically only useful
090     * if using the check to identify that match name and do not have the
091     * {@code abstract} modifier.
092     */
093    private boolean ignoreName;
094
095    /** Specify valid identifiers. */
096    private Pattern format = Pattern.compile("^Abstract.+$");
097
098    /**
099     * Setter to control whether to ignore checking for the {@code abstract} modifier on
100     * classes that match the name.
101     * @param value new value
102     */
103    public void setIgnoreModifier(boolean value) {
104        ignoreModifier = value;
105    }
106
107    /**
108     * Setter to control whether to ignore checking the name. Realistically only useful if
109     * using the check to identify that match name and do not have the {@code abstract} modifier.
110     * @param value new value.
111     */
112    public void setIgnoreName(boolean value) {
113        ignoreName = value;
114    }
115
116    /**
117     * Setter to specify valid identifiers.
118     * @param pattern the new pattern
119     */
120    public void setFormat(Pattern pattern) {
121        format = pattern;
122    }
123
124    @Override
125    public int[] getDefaultTokens() {
126        return getRequiredTokens();
127    }
128
129    @Override
130    public int[] getRequiredTokens() {
131        return new int[] {TokenTypes.CLASS_DEF};
132    }
133
134    @Override
135    public int[] getAcceptableTokens() {
136        return getRequiredTokens();
137    }
138
139    @Override
140    public void visitToken(DetailAST ast) {
141        visitClassDef(ast);
142    }
143
144    /**
145     * Checks class definition.
146     * @param ast class definition for check.
147     */
148    private void visitClassDef(DetailAST ast) {
149        final String className =
150            ast.findFirstToken(TokenTypes.IDENT).getText();
151        if (isAbstract(ast)) {
152            // if class has abstract modifier
153            if (!ignoreName && !isMatchingClassName(className)) {
154                log(ast, MSG_ILLEGAL_ABSTRACT_CLASS_NAME, className, format.pattern());
155            }
156        }
157        else if (!ignoreModifier && isMatchingClassName(className)) {
158            log(ast, MSG_NO_ABSTRACT_CLASS_MODIFIER, className);
159        }
160    }
161
162    /**
163     * Checks if declared class is abstract or not.
164     * @param ast class definition for check.
165     * @return true if a given class declared as abstract.
166     */
167    private static boolean isAbstract(DetailAST ast) {
168        final DetailAST abstractAST = ast.findFirstToken(TokenTypes.MODIFIERS)
169            .findFirstToken(TokenTypes.ABSTRACT);
170
171        return abstractAST != null;
172    }
173
174    /**
175     * Returns true if class name matches format of abstract class names.
176     * @param className class name for check.
177     * @return true if class name matches format of abstract class names.
178     */
179    private boolean isMatchingClassName(String className) {
180        return format.matcher(className).find();
181    }
182
183}