Skip to content

WhitespaceAfter should support LITERAL_TRY token #11489

@turbanoff

Description

@turbanoff

https://checkstyle.org/config_whitespace.html#WhitespaceAfter

I have downloaded the latest cli from: https://checkstyle.org/cmdline.html#Download_and_Run
I have executed the cli and showed it below, as cli describes the problem better than 1,000 words

How it works Now:

/var/tmp $ javac TryStatement.java
/var/tmp $

/var/tmp $ cat config.xml
<!DOCTYPE module PUBLIC
        "-//Puppy Crawl//DTD Check Configuration 1.3//EN"
        "http://www.puppycrawl.com/dtds/configuration_1_3.dtd">
<module name="Checker">
    <module name="TreeWalker">
        <module name="WhitespaceAfter">
            <property name="tokens" value="LITERAL_TRY"/>
        </module>
    </module>
</module>

/var/tmp $ cat TryStatement.java
import java.io.IOException;
import java.io.InputStream;

public class TryStatement {
    public static void main(String[] args) throws IOException {
        try(InputStream ignored = System.in) {

        }
    }
}

/var/tmp $ RUN_LOCALE="-Duser.language=en -Duser.country=US"
/var/tmp $ java $RUN_LOCALE -jar checkstyle-10.1-all.jar -c config.xml TryStatement.java
com.puppycrawl.tools.checkstyle.api.CheckstyleException: cannot initialize module TreeWalker - Token "LITERAL_TRY" was not found in Acceptable tokens list in check com.puppycrawl.tools.checkstyle.checks.whitespace.WhitespaceAfterCheck
        at com.puppycrawl.tools.checkstyle.Checker.setupChild(Checker.java:476)
        at com.puppycrawl.tools.checkstyle.api.AutomaticBean.configure(AutomaticBean.java:201)
        at com.puppycrawl.tools.checkstyle.Main.runCheckstyle(Main.java:403)
        at com.puppycrawl.tools.checkstyle.Main.runCli(Main.java:330)
        at com.puppycrawl.tools.checkstyle.Main.execute(Main.java:189)
        at com.puppycrawl.tools.checkstyle.Main.main(Main.java:126)
Caused by: com.puppycrawl.tools.checkstyle.api.CheckstyleException: Token "LITERAL_TRY" was not found in Acceptable tokens list in check com.puppycrawl.tools.checkstyle.checks.whitespace.WhitespaceAfterCheck
        at com.puppycrawl.tools.checkstyle.TreeWalker.registerCheck(TreeWalker.java:223)
        at com.puppycrawl.tools.checkstyle.TreeWalker.setupChild(TreeWalker.java:133)
        at com.puppycrawl.tools.checkstyle.api.AutomaticBean.configure(AutomaticBean.java:201)
        at com.puppycrawl.tools.checkstyle.Checker.setupChild(Checker.java:471)
        ... 5 more
Checkstyle ends with 1 errors.

Is your feature request related to a problem? Please describe.
It would be nice if checkstyle could detect missing space after try keyword.

Describe the solution you'd like
WhitespaceAfter could support new literal type - LITERAL_TRY

Additional context
Google Java Style Guide requires whitespace separation of ( from any reserved words
See https://google.github.io/styleguide/javaguide.html#s4.6.2-horizontal-whitespace

  1. Separating any reserved word, such as if, for or catch, from an open parenthesis (() that follows it on that line

#8122 seems related. This feature request is part of it.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions