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.imports; 021 022/** 023 * Represents whether a class is allowed to be imported or not. 024 */ 025class ClassImportRule extends AbstractImportRule { 026 027 /** Package to control access to. */ 028 private final String className; 029 030 /** 031 * Constructs an instance. 032 * 033 * @param allow whether to allow access. 034 * @param localOnly whether the rule is to be applied locally only 035 * @param className the class to apply the rule on. 036 * @param regExp whether the class name is to be interpreted as a regular 037 * expression. 038 */ 039 /* package */ ClassImportRule(final boolean allow, final boolean localOnly, 040 final String className, final boolean regExp) { 041 super(allow, localOnly, regExp); 042 this.className = className; 043 } 044 045 /** 046 * Verifies whether a class name is used. 047 * 048 * @param forImport the import to check. 049 * @return a result {@link AccessResult} indicating whether it can be used. 050 */ 051 @Override 052 public AccessResult verifyImport(final String forImport) { 053 final boolean classMatch; 054 055 if (isRegExp()) { 056 classMatch = forImport.matches(className); 057 } 058 else { 059 classMatch = forImport.equals(className); 060 } 061 062 return calculateResult(classMatch); 063 } 064 065}