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.imports; 21 22 /** 23 * Represents whether a package is allowed to be imported or not. 24 */ 25 class PkgImportRule extends AbstractImportRule { 26 27 /** Package to control access to. */ 28 private final String pkgName; 29 30 /** Indicates if the package name must be an exact match. */ 31 private final boolean exactMatch; 32 33 /** 34 * Constructs an instance. 35 * 36 * @param allow whether to allow access. 37 * @param localOnly whether the rule is to be applied locally only 38 * @param pkgName the package to apply the rule on. 39 * @param exactMatch whether the package name must match exactly. 40 * @param regExp whether the package name is to be interpreted as a regular 41 * expression. 42 */ 43 /* package */ PkgImportRule(final boolean allow, final boolean localOnly, 44 final String pkgName, final boolean exactMatch, final boolean regExp) { 45 super(allow, localOnly, regExp); 46 this.pkgName = pkgName; 47 this.exactMatch = exactMatch; 48 } 49 50 /** 51 * Verifies whether a package name is used. 52 * 53 * @param forImport the import to check. 54 * @return a result {@link AccessResult} indicating whether it can be used. 55 */ 56 @Override 57 public AccessResult verifyImport(final String forImport) { 58 // First check that we actually match the package. 59 // Then check if matched and f we must be an exact match. 60 // In this case, the text after the first "." must not contain 61 // another "." as this indicates that it is not an exact match. 62 63 boolean pkgMatch; 64 65 if (isRegExp()) { 66 pkgMatch = forImport.matches(pkgName + "\\..*"); 67 68 if (pkgMatch && exactMatch) { 69 pkgMatch = !forImport.matches(pkgName + "\\..*\\..*"); 70 } 71 } 72 else { 73 pkgMatch = forImport.startsWith(pkgName + "."); 74 75 if (pkgMatch && exactMatch) { 76 pkgMatch = forImport.indexOf('.', 77 pkgName.length() + 1) == -1; 78 } 79 } 80 81 return calculateResult(pkgMatch); 82 } 83 84 }