Coverage Report - com.puppycrawl.tools.checkstyle.checks.annotation.PackageAnnotationCheck
 
Classes in this File Line Coverage Branch Coverage Complexity
PackageAnnotationCheck
100%
11/11
100%
4/4
1.5
 
 1  
 ////////////////////////////////////////////////////////////////////////////////
 2  
 // checkstyle: Checks Java source code for adherence to a set of rules.
 3  
 // Copyright (C) 2001-2017 the original author or authors.
 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  
 
 20  
 package com.puppycrawl.tools.checkstyle.checks.annotation;
 21  
 
 22  
 import com.puppycrawl.tools.checkstyle.StatelessCheck;
 23  
 import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
 24  
 import com.puppycrawl.tools.checkstyle.api.DetailAST;
 25  
 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
 26  
 import com.puppycrawl.tools.checkstyle.utils.AnnotationUtility;
 27  
 
 28  
 /**
 29  
  * This check makes sure that all package annotations are in the
 30  
  * package-info.java file.
 31  
  *
 32  
  * <p>
 33  
  * According to the Java Language Specification.
 34  
  * </p>
 35  
  *
 36  
  * <p>
 37  
  * The JLS does not enforce the placement of package annotations.
 38  
  * This placement may vary based on implementation. The JLS
 39  
  * does highly recommend that all package annotations are
 40  
  * placed in the package-info.java file.
 41  
  *
 42  
  * See <a
 43  
  * href="https://docs.oracle.com/javase/specs/jls/se8/html/jls-7.html#jls-7.4.1">
 44  
  * Java Language Specification, section 7.4.1</a>.
 45  
  * </p>
 46  
  * @author Travis Schneeberger
 47  
  */
 48  
 @StatelessCheck
 49  11
 public class PackageAnnotationCheck extends AbstractCheck {
 50  
 
 51  
     /**
 52  
      * A key is pointing to the warning message text in "messages.properties"
 53  
      * file.
 54  
      */
 55  
     public static final String MSG_KEY = "annotation.package.location";
 56  
 
 57  
     @Override
 58  
     public int[] getDefaultTokens() {
 59  14
         return getRequiredTokens();
 60  
     }
 61  
 
 62  
     @Override
 63  
     public int[] getRequiredTokens() {
 64  33
         return new int[] {
 65  
             TokenTypes.PACKAGE_DEF,
 66  
         };
 67  
     }
 68  
 
 69  
     @Override
 70  
     public int[] getAcceptableTokens() {
 71  5
         return getRequiredTokens();
 72  
     }
 73  
 
 74  
     @Override
 75  
     public void visitToken(final DetailAST ast) {
 76  4
         final boolean containsAnnotation =
 77  4
             AnnotationUtility.containsAnnotation(ast);
 78  4
         final boolean inPackageInfo =
 79  4
             getFileContents().inPackageInfo();
 80  
 
 81  4
         if (containsAnnotation && !inPackageInfo) {
 82  1
             log(ast.getLine(), MSG_KEY);
 83  
         }
 84  4
     }
 85  
 }