All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.puppycrawl.tools.checkstyle.checks.whitespace.WhitespaceAfterCheck Maven / Gradle / Ivy

Go to download

Checkstyle is a development tool to help programmers write Java code that adheres to a coding standard

There is a newer version: 10.18.1
Show newest version
////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code for adherence to a set of rules.
// Copyright (C) 2001-2021 the original author or authors.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
////////////////////////////////////////////////////////////////////////////////

package com.puppycrawl.tools.checkstyle.checks.whitespace;

import com.puppycrawl.tools.checkstyle.StatelessCheck;
import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
import com.puppycrawl.tools.checkstyle.utils.CommonUtil;

/**
 * 

* Checks that a token is followed by whitespace, with the exception that it * does not check for whitespace after the semicolon of an empty for iterator. * Use Check * EmptyForIteratorPad to validate empty for iterators. *

* *

* To configure the check: *

*
 * <module name="WhitespaceAfter"/>
 * 
*

Example:

*
 *  public void myTest() {
 *      if (foo) {              // OK
 *              //...
 *      } else if(bar) {        // violation
 *              //...
 *      }
 *
 *      testMethod(foo, bar);   // OK
 *      testMethod(foo,bar);    // violation
 *
 *      for (;;){}               // OK
 *      for(;;){}                // violation, space after 'for' is required
 *      }
 * 
*

* To configure the check for whitespace only after COMMA and SEMI tokens: *

*
 * <module name="WhitespaceAfter">
 *   <property name="tokens" value="COMMA, SEMI"/>
 * </module>
 * 
*

Example:

*
 *     public void myTest() {
 *         int a; int b;           // OK
 *         int a;int b;            // violation
 *
 *         testMethod(foo, bar);   // OK
 *         testMethod(foo,bar);    // violation
 *
 *         for(;;) {} // OK
 *     }
 * 
*

* Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker} *

*

* Violation Message Keys: *

*
    *
  • * {@code ws.notFollowed} *
  • *
  • * {@code ws.typeCast} *
  • *
* * @since 3.0 */ @StatelessCheck public class WhitespaceAfterCheck extends AbstractCheck { /** * A key is pointing to the warning message text in "messages.properties" * file. */ public static final String MSG_WS_NOT_FOLLOWED = "ws.notFollowed"; /** * A key is pointing to the warning message text in "messages.properties" * file. */ public static final String MSG_WS_TYPECAST = "ws.typeCast"; @Override public int[] getDefaultTokens() { return getAcceptableTokens(); } @Override public int[] getAcceptableTokens() { return new int[] { TokenTypes.COMMA, TokenTypes.SEMI, TokenTypes.TYPECAST, TokenTypes.LITERAL_IF, TokenTypes.LITERAL_ELSE, TokenTypes.LITERAL_WHILE, TokenTypes.LITERAL_DO, TokenTypes.LITERAL_FOR, TokenTypes.DO_WHILE, }; } @Override public int[] getRequiredTokens() { return CommonUtil.EMPTY_INT_ARRAY; } @Override public void visitToken(DetailAST ast) { if (ast.getType() == TokenTypes.TYPECAST) { final DetailAST targetAST = ast.findFirstToken(TokenTypes.RPAREN); final String line = getLine(targetAST.getLineNo() - 1); if (!isFollowedByWhitespace(targetAST, line)) { log(targetAST, MSG_WS_TYPECAST); } } else { final String line = getLine(ast.getLineNo() - 1); if (!isFollowedByWhitespace(ast, line)) { final Object[] message = {ast.getText()}; log(ast, MSG_WS_NOT_FOLLOWED, message); } } } /** * Checks whether token is followed by a whitespace. * * @param targetAST Ast token. * @param line The line associated with the ast token. * @return true if ast token is followed by a whitespace. */ private static boolean isFollowedByWhitespace(DetailAST targetAST, String line) { final int after = targetAST.getColumnNo() + targetAST.getText().length(); boolean followedByWhitespace = true; if (after < line.codePointCount(0, line.length())) { final int[] codePoints = line.codePoints().toArray(); final int codePoint = codePoints[after]; followedByWhitespace = codePoint == ';' || codePoint == ')' || Character.isWhitespace(codePoint); } return followedByWhitespace; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy