001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2020 the original author or authors. 004// 005// This library is free software; you can redistribute it and/or 006// modify it under the terms of the GNU Lesser General Public 007// License as published by the Free Software Foundation; either 008// version 2.1 of the License, or (at your option) any later version. 009// 010// This library is distributed in the hope that it will be useful, 011// but WITHOUT ANY WARRANTY; without even the implied warranty of 012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013// Lesser General Public License for more details. 014// 015// You should have received a copy of the GNU Lesser General Public 016// License along with this library; if not, write to the Free Software 017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 018//////////////////////////////////////////////////////////////////////////////// 019 020package com.puppycrawl.tools.checkstyle.checks.annotation; 021 022import com.puppycrawl.tools.checkstyle.StatelessCheck; 023import com.puppycrawl.tools.checkstyle.api.AbstractCheck; 024import com.puppycrawl.tools.checkstyle.api.DetailAST; 025import com.puppycrawl.tools.checkstyle.api.TokenTypes; 026import com.puppycrawl.tools.checkstyle.utils.AnnotationUtil; 027 028/** 029 * <p> 030 * Checks that all package annotations are in the package-info.java file. 031 * </p> 032 * <p> 033 * For Java SE8 and above, placement of package annotations in the package-info.java 034 * file is enforced by the compiler and this check is not necessary. 035 * </p> 036 * <p> 037 * For Java SE7 and below, the Java Language Specification highly recommends 038 * but doesn't require that annotations are placed in the package-info.java file, 039 * and this check can help to enforce that placement. 040 * </p> 041 * <p> 042 * See <a href="https://docs.oracle.com/javase/specs/jls/se11/html/jls-7.html#jls-7.4.1"> 043 * Java Language Specification, §7.4.1</a> for more info. 044 * </p> 045 * <p> 046 * To configure the check: 047 * </p> 048 * <pre> 049 * <module name="PackageAnnotation"/> 050 * </pre> 051 * <p>Example of validating MyClass.java:</p> 052 * <pre> 053 * @Deprecated 054 * package com.example.annotations.packageannotation; //violation 055 * </pre> 056 * <p>Example of fixing violation in MyClass.java:</p> 057 * <pre> 058 * package com.example.annotations.packageannotation; //ok 059 * </pre> 060 * <p>Example of validating package-info.java:</p> 061 * <pre> 062 * @Deprecated 063 * package com.example.annotations.packageannotation; //ok 064 * </pre> 065 * <p> 066 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker} 067 * </p> 068 * <p> 069 * Violation Message Keys: 070 * </p> 071 * <ul> 072 * <li> 073 * {@code annotation.package.location} 074 * </li> 075 * </ul> 076 * 077 * @since 5.0 078 */ 079@StatelessCheck 080public class PackageAnnotationCheck extends AbstractCheck { 081 082 /** 083 * A key is pointing to the warning message text in "messages.properties" 084 * file. 085 */ 086 public static final String MSG_KEY = "annotation.package.location"; 087 088 @Override 089 public int[] getDefaultTokens() { 090 return getRequiredTokens(); 091 } 092 093 @Override 094 public int[] getRequiredTokens() { 095 return new int[] { 096 TokenTypes.PACKAGE_DEF, 097 }; 098 } 099 100 @Override 101 public int[] getAcceptableTokens() { 102 return getRequiredTokens(); 103 } 104 105 @Override 106 public void visitToken(final DetailAST ast) { 107 final boolean containsAnnotation = 108 AnnotationUtil.containsAnnotation(ast); 109 final boolean inPackageInfo = 110 getFileContents().inPackageInfo(); 111 112 if (containsAnnotation && !inPackageInfo) { 113 log(ast, MSG_KEY); 114 } 115 } 116 117}