001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2017 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.api.AbstractCheck;
025import com.puppycrawl.tools.checkstyle.api.DetailAST;
026import com.puppycrawl.tools.checkstyle.api.TokenTypes;
027
028/**
029 * <p>
030 * Ensures that the names of abstract classes conforming to some
031 * regular expression and check that {@code abstract} modifier exists.
032 * </p>
033 * <p>
034 * Rationale: Abstract classes are convenience base class
035 * implementations of interfaces, not types as such. As such
036 * they should be named to indicate this. Also if names of classes
037 * starts with 'Abstract' it's very convenient that they will
038 * have abstract modifier.
039 * </p>
040 *
041 * @author <a href="mailto:simon@redhillconsulting.com.au">Simon Harris</a>
042 * @author <a href="mailto:solid.danil@gmail.com">Danil Lopatin</a>
043 */
044public final class AbstractClassNameCheck extends AbstractCheck {
045
046    /**
047     * A key is pointing to the warning message text in "messages.properties"
048     * file.
049     */
050    public static final String MSG_ILLEGAL_ABSTRACT_CLASS_NAME = "illegal.abstract.class.name";
051
052    /**
053     * A key is pointing to the warning message text in "messages.properties"
054     * file.
055     */
056    public static final String MSG_NO_ABSTRACT_CLASS_MODIFIER = "no.abstract.class.modifier";
057
058    /** Whether to ignore checking the modifier. */
059    private boolean ignoreModifier;
060
061    /** Whether to ignore checking the name. */
062    private boolean ignoreName;
063
064    /** The regexp to match against. */
065    private Pattern format = Pattern.compile("^Abstract.+$");
066
067    /**
068     * Whether to ignore checking for the {@code abstract} modifier.
069     * @param value new value
070     */
071    public void setIgnoreModifier(boolean value) {
072        ignoreModifier = value;
073    }
074
075    /**
076     * Whether to ignore checking the name.
077     * @param value new value.
078     */
079    public void setIgnoreName(boolean value) {
080        ignoreName = value;
081    }
082
083    /**
084     * Set the format for the specified regular expression.
085     * @param pattern the new pattern
086     */
087    public void setFormat(Pattern pattern) {
088        format = pattern;
089    }
090
091    @Override
092    public int[] getDefaultTokens() {
093        return new int[] {TokenTypes.CLASS_DEF};
094    }
095
096    @Override
097    public int[] getRequiredTokens() {
098        return new int[] {TokenTypes.CLASS_DEF};
099    }
100
101    @Override
102    public int[] getAcceptableTokens() {
103        return new int[] {TokenTypes.CLASS_DEF};
104    }
105
106    @Override
107    public void visitToken(DetailAST ast) {
108        visitClassDef(ast);
109    }
110
111    /**
112     * Checks class definition.
113     * @param ast class definition for check.
114     */
115    private void visitClassDef(DetailAST ast) {
116        final String className =
117            ast.findFirstToken(TokenTypes.IDENT).getText();
118        if (isAbstract(ast)) {
119            // if class has abstract modifier
120            if (!ignoreName && !isMatchingClassName(className)) {
121                log(ast.getLineNo(), ast.getColumnNo(),
122                    MSG_ILLEGAL_ABSTRACT_CLASS_NAME, className, format.pattern());
123            }
124        }
125        else if (!ignoreModifier && isMatchingClassName(className)) {
126            log(ast.getLineNo(), ast.getColumnNo(),
127                MSG_NO_ABSTRACT_CLASS_MODIFIER, className);
128        }
129    }
130
131    /**
132     * Checks if declared class is abstract or not.
133     * @param ast class definition for check.
134     * @return true if a given class declared as abstract.
135     */
136    private static boolean isAbstract(DetailAST ast) {
137        final DetailAST abstractAST = ast.findFirstToken(TokenTypes.MODIFIERS)
138            .findFirstToken(TokenTypes.ABSTRACT);
139
140        return abstractAST != null;
141    }
142
143    /**
144     * Returns true if class name matches format of abstract class names.
145     * @param className class name for check.
146     * @return true if class name matches format of abstract class names.
147     */
148    private boolean isMatchingClassName(String className) {
149        return format.matcher(className).find();
150    }
151}