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