| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| PackageAnnotationCheck |
|
| 1.5;1.5 |
| 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.annotation; | |
| 20 | ||
| 21 | import com.puppycrawl.tools.checkstyle.api.AnnotationUtility; | |
| 22 | import com.puppycrawl.tools.checkstyle.api.Check; | |
| 23 | import com.puppycrawl.tools.checkstyle.api.DetailAST; | |
| 24 | import com.puppycrawl.tools.checkstyle.api.TokenTypes; | |
| 25 | ||
| 26 | /** | |
| 27 | * This check makes sure that all package annotations are in the | |
| 28 | * package-info.java file. | |
| 29 | * | |
| 30 | * <p> | |
| 31 | * According to the Java JLS 3rd ed. | |
| 32 | * </p> | |
| 33 | * | |
| 34 | * <p> | |
| 35 | * The JLS does not enforce the placement of package annotations. | |
| 36 | * This placement may vary based on implementation. The JLS | |
| 37 | * does highly recommend that all package annotations are | |
| 38 | * placed in the package-info.java file. | |
| 39 | * | |
| 40 | * See <a | |
| 41 | * href="http://java.sun.com/docs/books/jls/third_edition/html/j3TOC.html"> | |
| 42 | * Java Language specification, sections 7.4.1.1</a>. | |
| 43 | * </p> | |
| 44 | * @author Travis Schneeberger | |
| 45 | */ | |
| 46 | 3 | public class PackageAnnotationCheck extends Check |
| 47 | { | |
| 48 | ||
| 49 | /** {@inheritDoc} */ | |
| 50 | @Override | |
| 51 | public int[] getDefaultTokens() | |
| 52 | { | |
| 53 | 3 | return this.getRequiredTokens(); |
| 54 | } | |
| 55 | ||
| 56 | /** {@inheritDoc} */ | |
| 57 | @Override | |
| 58 | public int[] getRequiredTokens() | |
| 59 | { | |
| 60 | 3 | return new int[] { |
| 61 | TokenTypes.PACKAGE_DEF, | |
| 62 | }; | |
| 63 | } | |
| 64 | ||
| 65 | /** {@inheritDoc} */ | |
| 66 | @Override | |
| 67 | public int[] getAcceptableTokens() | |
| 68 | { | |
| 69 | 0 | return this.getRequiredTokens(); |
| 70 | } | |
| 71 | ||
| 72 | /** {@inheritDoc} */ | |
| 73 | @Override | |
| 74 | public void visitToken(final DetailAST aAST) | |
| 75 | { | |
| 76 | 3 | final boolean containsAnnotation = |
| 77 | AnnotationUtility.containsAnnotation(aAST); | |
| 78 | 3 | final boolean inPackageInfo = |
| 79 | this.getFileContents().inPackageInfo(); | |
| 80 | ||
| 81 | 3 | if (containsAnnotation && !inPackageInfo) { |
| 82 | 2 | this.log(aAST.getLine(), "annotation.package.location"); |
| 83 | } | |
| 84 | 3 | } |
| 85 | } |