001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2024 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.naming; 021 022import java.util.regex.Pattern; 023 024import com.puppycrawl.tools.checkstyle.StatelessCheck; 025import com.puppycrawl.tools.checkstyle.api.AbstractCheck; 026import com.puppycrawl.tools.checkstyle.api.DetailAST; 027import com.puppycrawl.tools.checkstyle.api.FullIdent; 028import com.puppycrawl.tools.checkstyle.api.TokenTypes; 029 030/** 031 * <p> 032 * Checks that package names conform to a specified pattern. 033 * </p> 034 * <p> 035 * The default value of {@code format} for module {@code PackageName} has been chosen to match 036 * the requirements in the 037 * <a href="https://docs.oracle.com/javase/specs/jls/se8/html/jls-6.html#jls-6.5.3"> 038 * Java Language specification</a> 039 * and the Sun coding conventions. However, both underscores and uppercase letters are rather 040 * uncommon, so most configurations should probably assign value 041 * {@code ^[a-z]+(\.[a-z][a-z0-9]*)*$} to {@code format} for module {@code PackageName}. 042 * </p> 043 * <ul> 044 * <li> 045 * Property {@code format} - Control the pattern to match valid identifiers. 046 * Type is {@code java.util.regex.Pattern}. 047 * Default value is {@code "^[a-z]+(\.[a-zA-Z_]\w*)*$"}. 048 * </li> 049 * </ul> 050 * <p> 051 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker} 052 * </p> 053 * <p> 054 * Violation Message Keys: 055 * </p> 056 * <ul> 057 * <li> 058 * {@code name.invalidPattern} 059 * </li> 060 * </ul> 061 * 062 * @since 3.0 063 */ 064@StatelessCheck 065public class PackageNameCheck 066 extends AbstractCheck { 067 068 /** 069 * A key is pointing to the warning message text in "messages.properties" 070 * file. 071 */ 072 public static final String MSG_KEY = "name.invalidPattern"; 073 074 /** Control the pattern to match valid identifiers. */ 075 // Uppercase letters seem rather uncommon, but they're allowed in 076 // https://docs.oracle.com/javase/specs/ 077 // second_edition/html/packages.doc.html#40169 078 private Pattern format = Pattern.compile("^[a-z]+(\\.[a-zA-Z_]\\w*)*$"); 079 080 /** 081 * Setter to control the pattern to match valid identifiers. 082 * 083 * @param pattern the new pattern 084 * @since 3.0 085 */ 086 public void setFormat(Pattern pattern) { 087 format = pattern; 088 } 089 090 @Override 091 public int[] getDefaultTokens() { 092 return getRequiredTokens(); 093 } 094 095 @Override 096 public int[] getAcceptableTokens() { 097 return getRequiredTokens(); 098 } 099 100 @Override 101 public int[] getRequiredTokens() { 102 return new int[] {TokenTypes.PACKAGE_DEF}; 103 } 104 105 @Override 106 public void visitToken(DetailAST ast) { 107 final DetailAST nameAST = ast.getLastChild().getPreviousSibling(); 108 final FullIdent full = FullIdent.createFullIdent(nameAST); 109 if (!format.matcher(full.getText()).find()) { 110 log(full.getDetailAst(), 111 MSG_KEY, 112 full.getText(), 113 format.pattern()); 114 } 115 } 116 117}