1 /////////////////////////////////////////////////////////////////////////////////////////////// 2 // checkstyle: Checks Java source code and other text files for adherence to a set of rules. 3 // Copyright (C) 2001-2024 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.naming; 21 22 import java.util.regex.Pattern; 23 24 import com.puppycrawl.tools.checkstyle.StatelessCheck; 25 import com.puppycrawl.tools.checkstyle.api.AbstractCheck; 26 import com.puppycrawl.tools.checkstyle.api.DetailAST; 27 import com.puppycrawl.tools.checkstyle.api.FullIdent; 28 import com.puppycrawl.tools.checkstyle.api.TokenTypes; 29 30 /** 31 * <p> 32 * Checks that package names conform to a specified pattern. 33 * </p> 34 * <p> 35 * The default value of {@code format} for module {@code PackageName} has been chosen to match 36 * the requirements in the 37 * <a href="https://docs.oracle.com/javase/specs/jls/se8/html/jls-6.html#jls-6.5.3"> 38 * Java Language specification</a> 39 * and the Sun coding conventions. However, both underscores and uppercase letters are rather 40 * uncommon, so most configurations should probably assign value 41 * {@code ^[a-z]+(\.[a-z][a-z0-9]*)*$} to {@code format} for module {@code PackageName}. 42 * </p> 43 * <ul> 44 * <li> 45 * Property {@code format} - Control the pattern to match valid identifiers. 46 * Type is {@code java.util.regex.Pattern}. 47 * Default value is {@code "^[a-z]+(\.[a-zA-Z_]\w*)*$"}. 48 * </li> 49 * </ul> 50 * <p> 51 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker} 52 * </p> 53 * <p> 54 * Violation Message Keys: 55 * </p> 56 * <ul> 57 * <li> 58 * {@code name.invalidPattern} 59 * </li> 60 * </ul> 61 * 62 * @since 3.0 63 */ 64 @StatelessCheck 65 public class PackageNameCheck 66 extends AbstractCheck { 67 68 /** 69 * A key is pointing to the warning message text in "messages.properties" 70 * file. 71 */ 72 public static final String MSG_KEY = "name.invalidPattern"; 73 74 /** Control the pattern to match valid identifiers. */ 75 // Uppercase letters seem rather uncommon, but they're allowed in 76 // https://docs.oracle.com/javase/specs/ 77 // second_edition/html/packages.doc.html#40169 78 private Pattern format = Pattern.compile("^[a-z]+(\\.[a-zA-Z_]\\w*)*$"); 79 80 /** 81 * Setter to control the pattern to match valid identifiers. 82 * 83 * @param pattern the new pattern 84 * @since 3.0 85 */ 86 public void setFormat(Pattern pattern) { 87 format = pattern; 88 } 89 90 @Override 91 public int[] getDefaultTokens() { 92 return getRequiredTokens(); 93 } 94 95 @Override 96 public int[] getAcceptableTokens() { 97 return getRequiredTokens(); 98 } 99 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 }