| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| Guard |
|
| 3.2;3.2 |
| 1 | //////////////////////////////////////////////////////////////////////////////// | |
| 2 | // checkstyle: Checks Java source code for adherence to a set of rules. | |
| 3 | // Copyright (C) 2001-2014 Oliver Burn | |
| 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 | package com.puppycrawl.tools.checkstyle.checks.imports; | |
| 20 | ||
| 21 | /** | |
| 22 | * Represents whether a package is allowed to be used or not. | |
| 23 | * @author Oliver Burn | |
| 24 | */ | |
| 25 | 1 | class Guard |
| 26 | { | |
| 27 | /** Indicates if allow access or not. */ | |
| 28 | private final boolean mAllowed; | |
| 29 | /** Package to control access to. */ | |
| 30 | private final String mPkgName; | |
| 31 | /** Package to control access to. */ | |
| 32 | private final String mClassName; | |
| 33 | ||
| 34 | /** | |
| 35 | * Indicates if must be an exact match. Only valid if guard using a | |
| 36 | * package. | |
| 37 | */ | |
| 38 | private final boolean mExactMatch; | |
| 39 | /** Indicates if the guard only applies to this package. */ | |
| 40 | private final boolean mLocalOnly; | |
| 41 | /** | |
| 42 | * Indicates if the package and the class names are to be interpreted | |
| 43 | * as regular expressions. | |
| 44 | */ | |
| 45 | private final boolean mRegExp; | |
| 46 | ||
| 47 | /** | |
| 48 | * Constructs an instance. | |
| 49 | * @param aAllow whether to allow access. | |
| 50 | * @param aLocalOnly whether guard is to be applied locally only | |
| 51 | * @param aPkgName the package to apply guard on. | |
| 52 | * @param aExactMatch whether the package must match exactly. | |
| 53 | * @param aRegExp whether the package is to be interpreted as regular | |
| 54 | * expression. | |
| 55 | */ | |
| 56 | Guard(final boolean aAllow, final boolean aLocalOnly, | |
| 57 | final String aPkgName, final boolean aExactMatch, final boolean aRegExp) | |
| 58 | 48 | { |
| 59 | 48 | mAllowed = aAllow; |
| 60 | 48 | mLocalOnly = aLocalOnly; |
| 61 | 48 | mPkgName = aPkgName; |
| 62 | 48 | mRegExp = aRegExp; |
| 63 | 48 | mClassName = null; |
| 64 | 48 | mExactMatch = aExactMatch; |
| 65 | 48 | } |
| 66 | ||
| 67 | /** | |
| 68 | * Constructs an instance. | |
| 69 | * @param aAllow whether to allow access. | |
| 70 | * @param aLocalOnly whether guard is to be applied locally only | |
| 71 | * @param aClassName the class to apply guard on. | |
| 72 | * @param aRegExp whether the class is to be interpreted as regular | |
| 73 | * expression. | |
| 74 | */ | |
| 75 | Guard(final boolean aAllow, final boolean aLocalOnly, | |
| 76 | final String aClassName, final boolean aRegExp) | |
| 77 | 18 | { |
| 78 | 18 | mAllowed = aAllow; |
| 79 | 18 | mLocalOnly = aLocalOnly; |
| 80 | 18 | mRegExp = aRegExp; |
| 81 | 18 | mPkgName = null; |
| 82 | 18 | mClassName = aClassName; |
| 83 | 18 | mExactMatch = true; // not used. |
| 84 | 18 | } |
| 85 | ||
| 86 | /** | |
| 87 | * Verifies whether a package name be used. | |
| 88 | * @param aForImport the package to check. | |
| 89 | * @return a result {@link AccessResult} indicating whether it can be used. | |
| 90 | */ | |
| 91 | AccessResult verifyImport(final String aForImport) | |
| 92 | { | |
| 93 | 153 | assert aForImport != null; |
| 94 | 153 | if (mClassName != null) { |
| 95 | 39 | final boolean classMatch = mRegExp |
| 96 | ? aForImport.matches(mClassName) | |
| 97 | : aForImport.equals(mClassName); | |
| 98 | 39 | return calculateResult(classMatch); |
| 99 | } | |
| 100 | ||
| 101 | // Must be checking a package. First check that we actually match | |
| 102 | // the package. Then check if matched and we must be an exact match. | |
| 103 | // In this case, the text after the first "." must not contain | |
| 104 | // another "." as this indicates that it is not an exact match. | |
| 105 | 114 | assert mPkgName != null; |
| 106 | boolean pkgMatch; | |
| 107 | 114 | if (mRegExp) { |
| 108 | 75 | pkgMatch = aForImport.matches(mPkgName + "\\..*"); |
| 109 | 75 | if (pkgMatch && mExactMatch) { |
| 110 | 8 | pkgMatch = !aForImport.matches(mPkgName + "\\..*\\..*"); |
| 111 | } | |
| 112 | } | |
| 113 | else { | |
| 114 | 39 | pkgMatch = aForImport.startsWith(mPkgName + "."); |
| 115 | 39 | if (pkgMatch && mExactMatch) { |
| 116 | 4 | pkgMatch = (aForImport.indexOf('.', |
| 117 | (mPkgName.length() + 1)) == -1); | |
| 118 | } | |
| 119 | } | |
| 120 | 114 | return calculateResult(pkgMatch); |
| 121 | } | |
| 122 | ||
| 123 | /** | |
| 124 | * @return returns whether the guard is to only be applied locally. | |
| 125 | */ | |
| 126 | boolean isLocalOnly() | |
| 127 | { | |
| 128 | 104 | return mLocalOnly; |
| 129 | } | |
| 130 | ||
| 131 | /** | |
| 132 | * Returns the appropriate {@link AccessResult} based on whether there | |
| 133 | * was a match and if the guard is to allow access. | |
| 134 | * @param aMatched indicates whether there was a match. | |
| 135 | * @return An appropriate {@link AccessResult}. | |
| 136 | */ | |
| 137 | private AccessResult calculateResult(final boolean aMatched) | |
| 138 | { | |
| 139 | 153 | if (aMatched) { |
| 140 | 38 | return mAllowed ? AccessResult.ALLOWED : AccessResult.DISALLOWED; |
| 141 | } | |
| 142 | 115 | return AccessResult.UNKNOWN; |
| 143 | } | |
| 144 | } |