001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2025 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.javadoc; 021 022import java.io.File; 023import java.io.IOException; 024import java.nio.file.Files; 025import java.nio.file.Path; 026import java.util.Set; 027import java.util.concurrent.ConcurrentHashMap; 028 029import com.puppycrawl.tools.checkstyle.GlobalStatefulCheck; 030import com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck; 031import com.puppycrawl.tools.checkstyle.api.CheckstyleException; 032import com.puppycrawl.tools.checkstyle.api.FileText; 033 034/** 035 * <div> 036 * Checks that each Java package has a Javadoc file used for commenting. 037 * By default, it only allows a {@code package-info.java} file, 038 * but can be configured to allow a {@code package.html} file. 039 * </div> 040 * 041 * <p> 042 * A violation will be reported if both files exist as this is not allowed by the Javadoc tool. 043 * </p> 044 * <ul> 045 * <li> 046 * Property {@code allowLegacy} - Allow legacy {@code package.html} file to be used. 047 * Type is {@code boolean}. 048 * Default value is {@code false}. 049 * </li> 050 * <li> 051 * Property {@code fileExtensions} - Specify the file extensions of the files to process. 052 * Type is {@code java.lang.String[]}. 053 * Default value is {@code .java}. 054 * </li> 055 * </ul> 056 * 057 * <p> 058 * Parent is {@code com.puppycrawl.tools.checkstyle.Checker} 059 * </p> 060 * 061 * <p> 062 * Violation Message Keys: 063 * </p> 064 * <ul> 065 * <li> 066 * {@code javadoc.legacyPackageHtml} 067 * </li> 068 * <li> 069 * {@code javadoc.packageInfo} 070 * </li> 071 * </ul> 072 * 073 * @since 5.0 074 */ 075@GlobalStatefulCheck 076public class JavadocPackageCheck extends AbstractFileSetCheck { 077 078 /** 079 * A key is pointing to the warning message text in "messages.properties" 080 * file. 081 */ 082 public static final String MSG_LEGACY_PACKAGE_HTML = "javadoc.legacyPackageHtml"; 083 084 /** 085 * A key is pointing to the warning message text in "messages.properties" 086 * file. 087 */ 088 public static final String MSG_PACKAGE_INFO = "javadoc.packageInfo"; 089 090 /** The directories checked. */ 091 private final Set<File> directoriesChecked = ConcurrentHashMap.newKeySet(); 092 093 /** Allow legacy {@code package.html} file to be used. */ 094 private boolean allowLegacy; 095 096 /** 097 * Creates a new instance. 098 */ 099 public JavadocPackageCheck() { 100 // java, not html! 101 // The rule is: Every JAVA file should have a package.html sibling 102 setFileExtensions("java"); 103 } 104 105 @Override 106 protected void processFiltered(File file, FileText fileText) throws CheckstyleException { 107 // Check if already processed directory 108 final File dir; 109 try { 110 dir = file.getCanonicalFile().getParentFile(); 111 } 112 catch (IOException ex) { 113 throw new CheckstyleException( 114 "Exception while getting canonical path to file " + file.getPath(), ex); 115 } 116 final boolean isDirChecked = !directoriesChecked.add(dir); 117 if (!isDirChecked) { 118 // Check for the preferred file. 119 final Path packageInfo = Path.of(dir.getPath(), "package-info.java"); 120 final Path packageHtml = Path.of(dir.getPath(), "package.html"); 121 122 if (Files.exists(packageInfo)) { 123 if (Files.exists(packageHtml)) { 124 log(1, MSG_LEGACY_PACKAGE_HTML); 125 } 126 } 127 else if (!allowLegacy || !Files.exists(packageHtml)) { 128 log(1, MSG_PACKAGE_INFO); 129 } 130 } 131 } 132 133 /** 134 * Setter to allow legacy {@code package.html} file to be used. 135 * 136 * @param allowLegacy whether to allow support. 137 * @since 5.0 138 */ 139 public void setAllowLegacy(boolean allowLegacy) { 140 this.allowLegacy = allowLegacy; 141 } 142 143}