Re: Package and class declaration versus the directory and file names

Richard Walker <[email protected]> Wed, 4 Aug 2010 14:25:40 +1000
Newsgroups gmane.comp.java.audit.checkstyle.user
Message-ID <[email protected]>
On 4 August 2010 11:12, Oliver Burn <[email protected]> wrote:
>  All available checks are listed at
> http://checkstyle.sourceforge.net/availablechecks.html

Yes, I did check that list first.

> There are no checks for what you are looking for. Typically the compiler
> or IDE warns about these errors.

Except that neither my IDE (Emacs) nor compiler (Sun, oops, Oracle JDK
javac) does.

So after a few hours, I now have the following two checks working.
I have not done any polishing; I attach in case anyone else is interested.


1.

import com.puppycrawl.tools.checkstyle.api.*;

public class PackageMatchesDirectory extends Check {

    private String dirSearchString = "";
    private int dirSearchStringLength;

    public void setDirSearchString(String s) {
        dirSearchString = s;
        dirSearchStringLength = dirSearchString.length();
    }

    @Override
        public int[] getDefaultTokens()
    {
        return new int[]{TokenTypes.PACKAGE_DEF};
    }

    @Override
        public void visitToken(DetailAST ast)
    {
        DetailAST nameAST = ast.getLastChild().getPreviousSibling();
        FullIdent full = FullIdent.createFullIdent(nameAST);

        String thisFilename = getFileContents().getFilename();

        /* Strip away everything up to and including dirSearchString */
        int srcPosition = thisFilename.indexOf(dirSearchString);
        if (srcPosition == -1) {
            log(ast.getLineNo(),
                "attempt to check source code not in " +
                dirSearchString + " directory");
            return;
        }

        thisFilename = thisFilename.substring(srcPosition +
                                              dirSearchStringLength);
        srcPosition = thisFilename.lastIndexOf("/");
        if (srcPosition == -1) {
            log(ast.getLineNo(),
                "source file at top level has a package declaration");
            return;
        }

        String thisDirname = thisFilename.substring(0,srcPosition);

        if (!(thisDirname.replace('/','.').equals(full.getText()))) {
            log(ast.getLineNo(),
                "package declaration does not match directory path");

        }
    }

}


2.

import com.puppycrawl.tools.checkstyle.api.*;

public class OuterTypeMatchesFilename extends Check {

    private boolean haveSeenFirstToken;

    @Override
        public int[] getDefaultTokens()
    {
        return new int[] {TokenTypes.CLASS_DEF, TokenTypes.INTERFACE_DEF,
            TokenTypes.ENUM_DEF, TokenTypes.ANNOTATION_DEF, };
    }

    @Override
    public void beginTree(DetailAST ast)
    {
        haveSeenFirstToken = false;
    }

    @Override
    public void visitToken(DetailAST ast)
    {
        /* Only check first declaration */
        if (haveSeenFirstToken) {
            return;
        }
        haveSeenFirstToken = true;

        String outerTypeName = ast.findFirstToken(TokenTypes.IDENT).getText();


        String thisFilename = getFileContents().getFilename();

        /* Strip away everything up to and including last slash, and
           .java suffix */
        int srcPosition = thisFilename.lastIndexOf("/");
        thisFilename = thisFilename.substring(srcPosition + 1);

        if (!thisFilename.endsWith(".java")) {
            log(ast.getLineNo(),
                "filename does not end in .java");
            return;
        }

        thisFilename = thisFilename.substring(0,thisFilename.length()-5);

        if (!(thisFilename.equals(outerTypeName))) {
            log(ast.getLineNo(),
                "outer type declaration does not match filename");

        }
    }

}

------------------------------------------------------------------------------
The Palm PDK Hot Apps Program offers developers who use the
Plug-In Development Kit to bring their C/C++ apps to Palm for a share
of $1 Million in cash or HP Products. Visit us here for more details:
http://p.sf.net/sfu/dev2dev-palm