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; 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.utils.CommonUtil; 026 027/** 028 * <p> 029 * Checks whether file contains code. Files which are considered to have no code: 030 * </p> 031 * <ul> 032 * <li> 033 * File with no text 034 * </li> 035 * <li> 036 * File with single line comment(s) 037 * </li> 038 * <li> 039 * File with a multi line comment(s). 040 * </li> 041 * </ul> 042 * <p> 043 * To configure the check: 044 * </p> 045 * <pre> 046 * <module name="NoCodeInFile"/> 047 * </pre> 048 * <p> 049 * Example: 050 * </p> 051 * <p> 052 * Content of the files: 053 * </p> 054 * <pre> 055 * // single line comment // violation 056 * </pre> 057 * <pre> 058 * /* // violation 059 * block comment 060 * */ 061 * </pre> 062 * <p> 063 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker} 064 * </p> 065 * <p> 066 * Violation Message Keys: 067 * </p> 068 * <ul> 069 * <li> 070 * {@code nocode.in.file} 071 * </li> 072 * </ul> 073 * 074 * @since 8.33 075 */ 076@StatelessCheck 077public class NoCodeInFileCheck extends AbstractCheck { 078 079 /** 080 * A key is pointing to the warning message text in "messages.properties" 081 * file. 082 */ 083 public static final String MSG_KEY_NO_CODE = "nocode.in.file"; 084 085 /** Line number used to log violation when no AST nodes are present in file. */ 086 private static final int DEFAULT_LINE_NUMBER = 1; 087 088 @Override 089 public int[] getDefaultTokens() { 090 return getRequiredTokens(); 091 } 092 093 @Override 094 public int[] getAcceptableTokens() { 095 return getRequiredTokens(); 096 } 097 098 @Override 099 public int[] getRequiredTokens() { 100 return CommonUtil.EMPTY_INT_ARRAY; 101 } 102 103 @Override 104 public void finishTree(DetailAST ast) { 105 if (ast == null) { 106 log(DEFAULT_LINE_NUMBER, MSG_KEY_NO_CODE); 107 } 108 } 109}