1 /////////////////////////////////////////////////////////////////////////////////////////////// 2 // checkstyle: Checks Java source code and other text files for adherence to a set of rules. 3 // Copyright (C) 2001-2025 the original author or authors. 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 20 package com.puppycrawl.tools.checkstyle.checks.javadoc; 21 22 import java.io.File; 23 import java.io.IOException; 24 import java.nio.file.Files; 25 import java.nio.file.Path; 26 import java.util.Set; 27 import java.util.concurrent.ConcurrentHashMap; 28 29 import com.puppycrawl.tools.checkstyle.GlobalStatefulCheck; 30 import com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck; 31 import com.puppycrawl.tools.checkstyle.api.CheckstyleException; 32 import com.puppycrawl.tools.checkstyle.api.FileText; 33 34 /** 35 * <div> 36 * Checks that each Java package has a Javadoc file used for commenting. 37 * By default, it only allows a {@code package-info.java} file, 38 * but can be configured to allow a {@code package.html} file. 39 * </div> 40 * 41 * <p> 42 * A violation will be reported if both files exist as this is not allowed by the Javadoc tool. 43 * </p> 44 * <ul> 45 * <li> 46 * Property {@code allowLegacy} - Allow legacy {@code package.html} file to be used. 47 * Type is {@code boolean}. 48 * Default value is {@code false}. 49 * </li> 50 * <li> 51 * Property {@code fileExtensions} - Specify the file extensions of the files to process. 52 * Type is {@code java.lang.String[]}. 53 * Default value is {@code .java}. 54 * </li> 55 * </ul> 56 * 57 * <p> 58 * Parent is {@code com.puppycrawl.tools.checkstyle.Checker} 59 * </p> 60 * 61 * <p> 62 * Violation Message Keys: 63 * </p> 64 * <ul> 65 * <li> 66 * {@code javadoc.legacyPackageHtml} 67 * </li> 68 * <li> 69 * {@code javadoc.packageInfo} 70 * </li> 71 * </ul> 72 * 73 * @since 5.0 74 */ 75 @GlobalStatefulCheck 76 public class JavadocPackageCheck extends AbstractFileSetCheck { 77 78 /** 79 * A key is pointing to the warning message text in "messages.properties" 80 * file. 81 */ 82 public static final String MSG_LEGACY_PACKAGE_HTML = "javadoc.legacyPackageHtml"; 83 84 /** 85 * A key is pointing to the warning message text in "messages.properties" 86 * file. 87 */ 88 public static final String MSG_PACKAGE_INFO = "javadoc.packageInfo"; 89 90 /** The directories checked. */ 91 private final Set<File> directoriesChecked = ConcurrentHashMap.newKeySet(); 92 93 /** Allow legacy {@code package.html} file to be used. */ 94 private boolean allowLegacy; 95 96 /** 97 * Creates a new instance. 98 */ 99 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 }