Package and class declaration versus the directory and file names

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Package and class declaration versus the directory and file names

Richard Walker-2
I would like to do the following checks:

1. Check that the package declaration matches the directory path,
   e.g. "package a.b.c;" means the source file must be in directory
   a/b/c.
2. Check that the first class defined in a file has a name that matches
   the filename, e.g. "class MyClass" must be defined in MyClass.java.

I didn't find these checks among the Checkstyle built-ins.  Did I miss them?
Or, has anyone else implemented them?

------------------------------------------------------------------------------
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
_______________________________________________
Checkstyle-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/checkstyle-user
Reply | Threaded
Open this post in threaded view
|

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

Oliver Burn
  All available checks are listed at
http://checkstyle.sourceforge.net/availablechecks.html

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

A check that is related is
http://checkstyle.sourceforge.net/config_sizes.html#OuterTypeNumber 
which checks for the number of types declared at the outer  (or root)
level in a file.

Cheers,
Oliver

On 4/08/2010 10:47 AM, Richard Walker wrote:

> I would like to do the following checks:
>
> 1. Check that the package declaration matches the directory path,
>     e.g. "package a.b.c;" means the source file must be in directory
>     a/b/c.
> 2. Check that the first class defined in a file has a name that matches
>     the filename, e.g. "class MyClass" must be defined in MyClass.java.
>
> I didn't find these checks among the Checkstyle built-ins.  Did I miss them?
> Or, has anyone else implemented them?
>
> ------------------------------------------------------------------------------
> 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
> _______________________________________________
> Checkstyle-user mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/checkstyle-user

------------------------------------------------------------------------------
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
_______________________________________________
Checkstyle-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/checkstyle-user
Reply | Threaded
Open this post in threaded view
|

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

Richard Walker-2
On 4 August 2010 11:12, Oliver Burn <[hidden email]> 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
_______________________________________________
Checkstyle-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/checkstyle-user
Reply | Threaded
Open this post in threaded view
|

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

Oliver Burn
Thanks - do you mind if I add these to the next release of Checkstyle?

On Wed, Aug 4, 2010 at 14:25, Richard Walker <[hidden email]> wrote:
On 4 August 2010 11:12, Oliver Burn <[hidden email]> 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
_______________________________________________
Checkstyle-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/checkstyle-user


------------------------------------------------------------------------------
This SF.net email is sponsored by

Make an app they can't live without
Enter the BlackBerry Developer Challenge
http://p.sf.net/sfu/RIM-dev2dev 
_______________________________________________
Checkstyle-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/checkstyle-user
Reply | Threaded
Open this post in threaded view
|

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

Richard Walker-2
In reply to this post by Richard Walker-2
> Thanks - do you mind if I add these to the next release of Checkstyle?

Go ahead, though please apply your "house style" to the code first.
I only did the minimum to make the checks work in my
setup.

------------------------------------------------------------------------------
Sell apps to millions through the Intel(R) Atom(Tm) Developer Program
Be part of this innovative community and reach millions of netbook users
worldwide. Take advantage of special opportunities to increase revenue and
speed time-to-market. Join now, and jumpstart your future.
http://p.sf.net/sfu/intel-atom-d2d
_______________________________________________
Checkstyle-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/checkstyle-user