001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2015 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 com.puppycrawl.tools.checkstyle.api.DetailAST; 023import com.puppycrawl.tools.checkstyle.api.FullIdent; 024import com.puppycrawl.tools.checkstyle.api.TokenTypes; 025import com.puppycrawl.tools.checkstyle.checks.AbstractFormatCheck; 026 027/** 028 * <p> 029 * Checks that package names conform to a format specified 030 * by the format property. The format is a 031 * {@link java.util.regex.Pattern regular expression} 032 * and defaults to 033 * <strong>^[a-z]+(\.[a-zA-Z_][a-zA-Z_0-9]*)*$</strong>. 034 * </p> 035 * <p> 036 * The default format has been chosen to match the requirements in the 037 * <a 038 * href="http://docs.oracle.com/javase/specs/jls/se8/html/jls-7.html"> 039 * Java Language specification</a> and the Sun coding conventions. 040 * However both underscores and uppercase letters are rather uncommon, 041 * so most projects should probably use 042 * <strong>^[a-z]+(\.[a-z][a-z0-9]*)*$</strong>. 043 * </p> 044 * <p> 045 * An example of how to configure the check is: 046 * </p> 047 * <pre> 048 * <module name="PackageName"/> 049 * </pre> 050 * <p> 051 * An example of how to configure the check for package names that begin with 052 * {@code com.puppycrawl.tools.checkstyle} is: 053 * </p> 054 * <pre> 055 * <module name="PackageName"> 056 * <property name="format" 057 * value="^com\.puppycrawl\.tools\.checkstyle(\\.[a-zA-Z_][a-zA-Z_0-9]*)*$"/> 058 * </module> 059 * </pre> 060 * 061 * @author Oliver Burn 062 */ 063public class PackageNameCheck 064 extends AbstractFormatCheck { 065 /** 066 * Creates a new {@code PackageNameCheck} instance. 067 */ 068 public PackageNameCheck() { 069 // Uppercase letters seem rather uncommon, but they're allowed in 070 // http://docs.oracle.com/javase/specs/ 071 // second_edition/html/packages.doc.html#40169 072 super("^[a-z]+(\\.[a-zA-Z_][a-zA-Z0-9_]*)*$"); 073 } 074 075 @Override 076 public int[] getDefaultTokens() { 077 return getAcceptableTokens(); 078 } 079 080 @Override 081 public int[] getAcceptableTokens() { 082 return new int[] {TokenTypes.PACKAGE_DEF}; 083 } 084 085 @Override 086 public int[] getRequiredTokens() { 087 return getAcceptableTokens(); 088 } 089 090 @Override 091 public void visitToken(DetailAST ast) { 092 final DetailAST nameAST = ast.getLastChild().getPreviousSibling(); 093 final FullIdent full = FullIdent.createFullIdent(nameAST); 094 if (!getRegexp().matcher(full.getText()).find()) { 095 log(full.getLineNo(), 096 full.getColumnNo(), 097 "name.invalidPattern", 098 full.getText(), 099 getFormat()); 100 } 101 } 102}