Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5961887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:01:44+00:00 2026-05-22T19:01:44+00:00

I use checkstyle to check if my java code respects the guidelines of our

  • 0

I use checkstyle to check if my java code respects the guidelines of our project.

However, we have one guideline that I cannot figure out how to check with this tool. We want to allow simple if (understand if with no else and no other conditional structure in it) to have no brace, like in this example :

// valid
if(condition) callFunction();

// invalid
if(condition) for(int i = 0; i < someValue; i++) callFunction(i);

// valid
if(condition) {
    for(int i = 0; i < someValue; i++) {
        callFunction(i);
    }
}

// invalid
if(condition) callFunction();
else callOtherFunction();

This convention can be discussed, but this is the one we chose. It allows a reduced if syntax for very trivial cases, but ensures we have good indentation and block delimitation for more complex structures.

Any help with that would be really appreciated.

I’m also ready to do some code to perform this check if nothing is available, but really don’t know where to start. In last ressort, some tips about that will be appreciated too.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T19:01:45+00:00Added an answer on May 22, 2026 at 7:01 pm

    At the end, I did implement a custom check for checkstyle. Here is the source code if someone else is interested in it :

    import com.puppycrawl.tools.checkstyle.api.Check;
    import com.puppycrawl.tools.checkstyle.api.DetailAST;
    import com.puppycrawl.tools.checkstyle.api.TokenTypes;
    
    public class IfBracesCheck extends Check {
    
        @Override
        public int[] getDefaultTokens() {
            return new int[] {
                TokenTypes.LITERAL_ELSE,
                TokenTypes.LITERAL_IF,
            };
        }
    
        @Override
        public void visitToken(DetailAST aAST) {
            final DetailAST slistAST = aAST.findFirstToken(TokenTypes.SLIST);
    
            if(aAST.getType() == TokenTypes.LITERAL_ELSE) {
                // If we have an else, it must have braces, except it is an "else if" (then the if must have braces).
                DetailAST ifToken = aAST.findFirstToken(TokenTypes.LITERAL_IF);
    
                if(ifToken == null) {
                    // This is an simple else, it must have brace.
                    if(slistAST == null) {
                        log(aAST.getLineNo(), "ifBracesElse", aAST.getText());
                    }
                } else {
                    // This is an "else if", the if must have braces.
                    if(ifToken.findFirstToken(TokenTypes.SLIST) == null) {
                        log(aAST.getLineNo(), "ifBracesConditional", ifToken.getText(), aAST.getText() + " " + ifToken.getText());
                    }
                }
            } else if(aAST.getType() == TokenTypes.LITERAL_IF) {
                // If the if uses braces, nothing as to be checked.
                if (slistAST != null) {
                    return;
                }
    
                // We have an if, we need to check if it has no conditionnal structure as direct child.
                final int[] conditionals = {
                    TokenTypes.LITERAL_DO,
                    TokenTypes.LITERAL_ELSE,
                    TokenTypes.LITERAL_FOR,
                    TokenTypes.LITERAL_IF,
                    TokenTypes.LITERAL_WHILE,
                    TokenTypes.LITERAL_SWITCH,
                };
    
                for(int conditional : conditionals) {
                    DetailAST conditionalAST = aAST.findFirstToken(conditional);
    
                    if (conditionalAST != null) {
                        log(aAST.getLineNo(), "ifBracesConditional", aAST.getText(), conditionalAST.getText());
    
                        // Let's trigger this only once.
                        return;
                    }
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In one library that is in heavy use in our project there is a
In Java we have a wonderful tool named CheckStyle that reinforce all our corporate
I use checkstyle to analyze my code. A useful rule is PackageHtml, that checks,
I'm trying to use checkstyle for a java project but I can't seem to
Recently we planned to use checkstyle plug-in in our project. As a part of
I have configured my Eclipse to use a remote checkstyle configuration that is located
I have custom checkstyle checks file (called checks.xml), and I'm trying to use that
I'm trying to use Checkstyle ( this check ) to raise a warning when
'''use Jython''' import shutil print dir(shutil) There is no, shutil.move, how does one move
We use a data acquisition card to take readings from a device that increases

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.