View Javadoc
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 class is allowed to be imported or not.
24   */
25  class ClassImportRule extends AbstractImportRule {
26  
27      /** Package to control access to. */
28      private final String className;
29  
30      /**
31       * Constructs an instance.
32       *
33       * @param allow whether to allow access.
34       * @param localOnly whether the rule is to be applied locally only
35       * @param className the class to apply the rule on.
36       * @param regExp whether the class name is to be interpreted as a regular
37       *        expression.
38       */
39      /* package */  ClassImportRule(final boolean allow, final boolean localOnly,
40          final String className, final boolean regExp) {
41          super(allow, localOnly, regExp);
42          this.className = className;
43      }
44  
45      /**
46       * Verifies whether a class name is used.
47       *
48       * @param forImport the import to check.
49       * @return a result {@link AccessResult} indicating whether it can be used.
50       */
51      @Override
52      public AccessResult verifyImport(final String forImport) {
53          final boolean classMatch;
54  
55          if (isRegExp()) {
56              classMatch = forImport.matches(className);
57          }
58          else {
59              classMatch = forImport.equals(className);
60          }
61  
62          return calculateResult(classMatch);
63      }
64  
65  }