001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2019 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 format specified 033 * by the format property. 034 * </p> 035 * <p> 036 * The default value of {@code format} for module {@code PackageName} has been chosen to match 037 * the requirements in the 038 * <a href="https://docs.oracle.com/javase/specs/jls/se8/html/jls-6.html#jls-6.5.3">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} - Specifies valid identifiers. Default value is 046 * {@code "^[a-z]+(\.[a-zA-Z_][a-zA-Z0-9_]*)*$"}. 047 * </li> 048 * </ul> 049 * <p> 050 * An example of how to configure the check is: 051 * </p> 052 * <pre> 053 * <module name="PackageName"/> 054 * </pre> 055 * <p> 056 * An example of how to configure the check to ensure with packages start with a lowercase letter 057 * and only contains lowercase letters or numbers is: 058 * </p> 059 * <pre> 060 * <module name="PackageName"> 061 * <property name="format" 062 * value="^[a-z]+(\.[a-z][a-z0-9]*)*$"/> 063 * </module> 064 * </pre> 065 * 066 * @since 3.0 067 */ 068@StatelessCheck 069public class PackageNameCheck 070 extends AbstractCheck { 071 072 /** 073 * A key is pointing to the warning message text in "messages.properties" 074 * file. 075 */ 076 public static final String MSG_KEY = "name.invalidPattern"; 077 078 /** Specifies valid identifiers. */ 079 // Uppercase letters seem rather uncommon, but they're allowed in 080 // https://docs.oracle.com/javase/specs/ 081 // second_edition/html/packages.doc.html#40169 082 private Pattern format = Pattern.compile("^[a-z]+(\\.[a-zA-Z_][a-zA-Z0-9_]*)*$"); 083 084 /** 085 * Setter to specifies valid identifiers. 086 * @param pattern the new pattern 087 */ 088 public void setFormat(Pattern pattern) { 089 format = pattern; 090 } 091 092 @Override 093 public int[] getDefaultTokens() { 094 return getRequiredTokens(); 095 } 096 097 @Override 098 public int[] getAcceptableTokens() { 099 return getRequiredTokens(); 100 } 101 102 @Override 103 public int[] getRequiredTokens() { 104 return new int[] {TokenTypes.PACKAGE_DEF}; 105 } 106 107 @Override 108 public void visitToken(DetailAST ast) { 109 final DetailAST nameAST = ast.getLastChild().getPreviousSibling(); 110 final FullIdent full = FullIdent.createFullIdent(nameAST); 111 if (!format.matcher(full.getText()).find()) { 112 log(full.getDetailAst(), 113 MSG_KEY, 114 full.getText(), 115 format.pattern()); 116 } 117 } 118 119}