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.header; 021 022import java.io.File; 023import java.util.Arrays; 024 025import com.puppycrawl.tools.checkstyle.StatelessCheck; 026import com.puppycrawl.tools.checkstyle.api.FileText; 027 028/** 029 * <p> 030 * Checks that a source file begins with a specified header. 031 * Property {@code headerFile} specifies a file that contains the required header. 032 * Alternatively, the header specification can be set directly in the 033 * {@code header} property without the need for an external file. 034 * </p> 035 * <p> 036 * Property {@code ignoreLines} specifies the line numbers to ignore when matching 037 * lines in a header file. This property is very useful for supporting headers 038 * that contain copyright dates. For example, consider the following header: 039 * </p> 040 * <pre> 041 * line 1: //////////////////////////////////////////////////////////////////// 042 * line 2: // checkstyle: 043 * line 3: // Checks Java source code for adherence to a set of rules. 044 * line 4: // Copyright (C) 2002 Oliver Burn 045 * line 5: //////////////////////////////////////////////////////////////////// 046 * </pre> 047 * <p> 048 * Since the year information will change over time, you can tell Checkstyle 049 * to ignore line 4 by setting property {@code ignoreLines} to {@code 4}. 050 * </p> 051 * <p> 052 * In default configuration, if header is not specified, the default value 053 * of header is set to {@code null} and the check does not rise any violations. 054 * </p> 055 * <ul> 056 * <li> 057 * Property {@code headerFile} - Specify the name of the file containing the required header. 058 * Type is {@code java.net.URI}. 059 * Default value is {@code null}. 060 * </li> 061 * <li> 062 * Property {@code charset} - Specify the character encoding to use when reading the headerFile. 063 * Type is {@code java.lang.String}. 064 * Default value is the charset property of the parent 065 * <a href="https://checkstyle.org/config.html#Checker">Checker</a> module. 066 * </li> 067 * <li> 068 * Property {@code header} - Specify the required header specified inline. 069 * Individual header lines must be separated by the string {@code "\n"} 070 * (even on platforms with a different line separator), see examples below. 071 * Type is {@code java.lang.String}. 072 * Default value is {@code null}. 073 * </li> 074 * <li> 075 * Property {@code ignoreLines} - Specify the line numbers to ignore. 076 * Type is {@code int[]}. 077 * Default value is {@code {}}. 078 * </li> 079 * <li> 080 * Property {@code fileExtensions} - Specify the file type extension of files to process. 081 * Type is {@code java.lang.String[]}. 082 * Default value is {@code all files}. 083 * </li> 084 * </ul> 085 * <p> 086 * In default configuration the check does not rise any violations. 087 * Default values of properties are used. 088 * </p> 089 * <pre> 090 * <module name="Header"/> 091 * </pre> 092 * <p> 093 * To configure the check to use header file {@code "config/java.header"} 094 * and ignore lines {@code 2}, {@code 3}, and {@code 4} and only process Java files: 095 * </p> 096 * <pre> 097 * <module name="Header"> 098 * <property name="headerFile" value="config/java.header"/> 099 * <property name="ignoreLines" value="2, 3, 4"/> 100 * <property name="fileExtensions" value="java"/> 101 * </module> 102 * </pre> 103 * <p> 104 * To configure the check to verify that each file starts with the header 105 * </p> 106 * <pre> 107 * // Copyright (C) 2004 MyCompany 108 * // All rights reserved 109 * </pre> 110 * <p> 111 * without the need for an external header file: 112 * </p> 113 * <pre> 114 * <module name="Header"> 115 * <property name="header" 116 * value="// Copyright (C) 2004 MyCompany\n// All rights reserved"/> 117 * </module> 118 * </pre> 119 * <p> 120 * Parent is {@code com.puppycrawl.tools.checkstyle.Checker} 121 * </p> 122 * <p> 123 * Violation Message Keys: 124 * </p> 125 * <ul> 126 * <li> 127 * {@code header.mismatch} 128 * </li> 129 * <li> 130 * {@code header.missing} 131 * </li> 132 * </ul> 133 * 134 * @since 6.9 135 */ 136@StatelessCheck 137public class HeaderCheck extends AbstractHeaderCheck { 138 139 /** 140 * A key is pointing to the warning message text in "messages.properties" 141 * file. 142 */ 143 public static final String MSG_MISSING = "header.missing"; 144 145 /** 146 * A key is pointing to the warning message text in "messages.properties" 147 * file. 148 */ 149 public static final String MSG_MISMATCH = "header.mismatch"; 150 151 /** Empty array to avoid instantiations. */ 152 private static final int[] EMPTY_INT_ARRAY = new int[0]; 153 154 /** Specify the line numbers to ignore. */ 155 private int[] ignoreLines = EMPTY_INT_ARRAY; 156 157 /** 158 * Returns true if lineNo is header lines or false. 159 * 160 * @param lineNo a line number 161 * @return if {@code lineNo} is one of the ignored header lines. 162 */ 163 private boolean isIgnoreLine(int lineNo) { 164 return Arrays.binarySearch(ignoreLines, lineNo) >= 0; 165 } 166 167 /** 168 * Checks if a code line matches the required header line. 169 * 170 * @param lineNumber the line number to check against the header 171 * @param line the line contents 172 * @return true if and only if the line matches the required header line 173 */ 174 private boolean isMatch(int lineNumber, String line) { 175 // skip lines we are meant to ignore 176 return isIgnoreLine(lineNumber + 1) 177 || getHeaderLines().get(lineNumber).equals(line); 178 } 179 180 /** 181 * Setter to specify the line numbers to ignore. 182 * 183 * @param list comma separated list of line numbers to ignore in header. 184 */ 185 public void setIgnoreLines(int... list) { 186 if (list.length == 0) { 187 ignoreLines = EMPTY_INT_ARRAY; 188 } 189 else { 190 ignoreLines = new int[list.length]; 191 System.arraycopy(list, 0, ignoreLines, 0, list.length); 192 Arrays.sort(ignoreLines); 193 } 194 } 195 196 @Override 197 protected void processFiltered(File file, FileText fileText) { 198 if (getHeaderLines().size() > fileText.size()) { 199 log(1, MSG_MISSING); 200 } 201 else { 202 for (int i = 0; i < getHeaderLines().size(); i++) { 203 if (!isMatch(i, fileText.get(i))) { 204 log(i + 1, MSG_MISMATCH, getHeaderLines().get(i)); 205 break; 206 } 207 } 208 } 209 } 210 211 @Override 212 protected void postProcessHeaderLines() { 213 // no code 214 } 215 216}