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.whitespace;
020
021import com.puppycrawl.tools.checkstyle.api.Check;
022import com.puppycrawl.tools.checkstyle.api.TokenTypes;
023import com.puppycrawl.tools.checkstyle.api.DetailAST;
024
025/**
026 * <p>
027 * Checks that a token is followed by whitespace, with the exception that it
028 * does not check for whitespace after the semicolon of an empty for iterator.
029 * Use Check {@link EmptyForIteratorPadCheck EmptyForIteratorPad} to validate
030 * empty for iterators.
031 * </p>
032 * <p> By default the check will check the following tokens:
033 *  {@link TokenTypes#COMMA COMMA},
034 *  {@link TokenTypes#SEMI SEMI},
035 *  {@link TokenTypes#TYPECAST TYPECAST}.
036 * </p>
037 * <p>
038 * An example of how to configure the check is:
039 * </p>
040 * <pre>
041 * &lt;module name="WhitespaceAfter"/&gt;
042 * </pre>
043 * <p> An example of how to configure the check for whitespace only after
044 * {@link TokenTypes#COMMA COMMA} and {@link TokenTypes#SEMI SEMI} tokens is:
045 * </p>
046 * <pre>
047 * &lt;module name="WhitespaceAfter"&gt;
048 *     &lt;property name="tokens" value="COMMA, SEMI"/&gt;
049 * &lt;/module&gt;
050 * </pre>
051 * @author Oliver Burn
052 * @author Rick Giles
053 * @version 1.0
054 */
055public class WhitespaceAfterCheck
056    extends Check
057{
058    @Override
059    public int[] getDefaultTokens()
060    {
061        return new int[] {
062            TokenTypes.COMMA,
063            TokenTypes.SEMI,
064            TokenTypes.TYPECAST,
065        };
066    }
067
068    @Override
069    public void visitToken(DetailAST ast)
070    {
071        final Object[] message;
072        final DetailAST targetAST;
073        if (ast.getType() == TokenTypes.TYPECAST) {
074            targetAST = ast.findFirstToken(TokenTypes.RPAREN);
075            // TODO: i18n
076            message = new Object[]{"cast"};
077        }
078        else {
079            targetAST = ast;
080            message = new Object[]{ast.getText()};
081        }
082        final String line = getLine(targetAST.getLineNo() - 1);
083        final int after =
084            targetAST.getColumnNo() + targetAST.getText().length();
085
086        if (after < line.length()) {
087
088            final char charAfter = line.charAt(after);
089            if ((targetAST.getType() == TokenTypes.SEMI)
090                && ((charAfter == ';') || (charAfter == ')')))
091            {
092                return;
093            }
094            if (!Character.isWhitespace(charAfter)) {
095                //empty FOR_ITERATOR?
096                if (targetAST.getType() == TokenTypes.SEMI) {
097                    final DetailAST sibling =
098                        targetAST.getNextSibling();
099                    if ((sibling != null)
100                        && (sibling.getType() == TokenTypes.FOR_ITERATOR)
101                        && (sibling.getChildCount() == 0))
102                    {
103                        return;
104                    }
105                }
106                log(targetAST.getLineNo(),
107                    targetAST.getColumnNo() + targetAST.getText().length(),
108                    "ws.notFollowed",
109                    message);
110            }
111        }
112    }
113}