Coverage Report - com.puppycrawl.tools.checkstyle.checks.coding.PackageDeclarationCheck
 
Classes in this File Line Coverage Branch Coverage Complexity
PackageDeclarationCheck
95%
21/22
83%
5/6
1.667
 
 1  
 ////////////////////////////////////////////////////////////////////////////////
 2  
 // checkstyle: Checks Java source code for adherence to a set of rules.
 3  
 // Copyright (C) 2001-2014  Oliver Burn
 4  
 //
 5  
 // This library is free software; you can redistribute it and/or
 6  
 // modify it under the terms of the GNU Lesser General Public
 7  
 // License as published by the Free Software Foundation; either
 8  
 // version 2.1 of the License, or (at your option) any later version.
 9  
 //
 10  
 // This library is distributed in the hope that it will be useful,
 11  
 // but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  
 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 13  
 // Lesser General Public License for more details.
 14  
 //
 15  
 // You should have received a copy of the GNU Lesser General Public
 16  
 // License along with this library; if not, write to the Free Software
 17  
 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 18  
 ////////////////////////////////////////////////////////////////////////////////
 19  
 package com.puppycrawl.tools.checkstyle.checks.coding;
 20  
 
 21  
 import com.puppycrawl.tools.checkstyle.api.Check;
 22  
 import com.puppycrawl.tools.checkstyle.api.DetailAST;
 23  
 import com.puppycrawl.tools.checkstyle.api.FullIdent;
 24  
 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
 25  
 import java.io.File;
 26  
 
 27  
 /**
 28  
  * Ensures there is a package declaration.
 29  
  * Rationale: Classes that live in the null package cannot be
 30  
  * imported. Many novice developers are not aware of this.
 31  
  *
 32  
  * @author <a href="mailto:simon@redhillconsulting.com.au">Simon Harris</a>
 33  
  * @author Oliver Burn
 34  
  */
 35  3
 public final class PackageDeclarationCheck extends Check
 36  
 {
 37  
     /** is package defined. */
 38  
     private boolean mDefined;
 39  
     /** whether to ignore the directory name matches the package. */
 40  
     private boolean mIgnoreDirectoryName;
 41  
 
 42  
     /**
 43  
      * Set whether to ignore checking the directory name.
 44  
      * @param aValue the new value.
 45  
      */
 46  
     public void setIgnoreDirectoryName(boolean aValue)
 47  
     {
 48  1
         mIgnoreDirectoryName = aValue;
 49  1
     }
 50  
 
 51  
     @Override
 52  
     public int[] getDefaultTokens()
 53  
     {
 54  3
         return new int[] {TokenTypes.PACKAGE_DEF};
 55  
     }
 56  
 
 57  
     @Override
 58  
     public int[] getRequiredTokens()
 59  
     {
 60  0
         return getDefaultTokens();
 61  
     }
 62  
 
 63  
     @Override
 64  
     public void beginTree(DetailAST aAST)
 65  
     {
 66  3
         mDefined = false;
 67  3
     }
 68  
 
 69  
     @Override
 70  
     public void finishTree(DetailAST aAST)
 71  
     {
 72  3
         if (!mDefined) {
 73  1
             log(aAST.getLineNo(), "missing.package.declaration");
 74  
         }
 75  3
     }
 76  
 
 77  
     @Override
 78  
     public void visitToken(DetailAST aAST)
 79  
     {
 80  2
         mDefined = true;
 81  2
         if (mIgnoreDirectoryName) {
 82  1
             return;
 83  
         }
 84  
 
 85  
         // Calculate the directory name, but stripping off the last
 86  
         // part.
 87  1
         final String fname = getFileContents().getFilename();
 88  1
         final int lastPos = fname.lastIndexOf(File.separatorChar);
 89  1
         final String dirname = fname.substring(0, lastPos);
 90  
 
 91  
         // Convert the found package name into the expected directory name.
 92  1
         final DetailAST nameAST = aAST.getLastChild().getPreviousSibling();
 93  1
         final FullIdent full = FullIdent.createFullIdent(nameAST);
 94  1
         final String expected = full.getText().replace('.', File.separatorChar);
 95  
 
 96  
         // Finally see that the real directory ends with the expected directory
 97  1
         if (!dirname.endsWith(expected)) {
 98  1
             log(full.getLineNo(),
 99  
                 full.getColumnNo(),
 100  
                 "package.dir.mismatch",
 101  
                 expected);
 102  
         }
 103  1
     }
 104  
 }