#160 detect main methods (debugging leftovers)

release_3.2
closed
Check (274)
5
2012-10-10
2003-05-04
Lars Kühne
No

Check code, contributed by Michael Yui:

/*
* Detects uncommented main methods. Basically detects
any main method, since
* if it is detectable that means it is uncommented.
/
public class UncommentedMainCheck extends Check {

    public int[] getDefaultTokens() {
            return new int[] { TokenTypes.METHOD_DEF };
    }

    public void visitToken(DetailAST ast) {
            DetailAST identAST =

ast.findFirstToken(TokenTypes.IDENT);
if ("main".equals(identAST.getText())) {
log(ast.getLineNo(),
"uncommented main method found. debugging leftovers
should be removed or commented out.");
}
}

}

Nice idea for a check.

The code above is only a basic sketch:
- i18n and unit tests are missing
- it should check that the method has modifiers
"public" and "static"
- a list of classes that are allowed to have main
methods (entry points for standalone programs) should
be configurable

Discussion

  • Logged In: YES
    user_id=746148

    UncommentedMain check committed in CVS for version 3.2.